{"id":19049408,"url":"https://github.com/jhvhs/gob-mock","last_synced_at":"2025-07-21T05:03:54.333Z","repository":{"id":57517822,"uuid":"89568028","full_name":"jhvhs/gob-mock","owner":"jhvhs","description":"Mocks for testing bash scripts with go-basher","archived":false,"fork":false,"pushed_at":"2018-03-21T20:09:27.000Z","size":2504,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-24T01:11:35.476Z","etag":null,"topics":["bash","ginkgo","go-basher","tdd","testing"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jhvhs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-27T07:21:44.000Z","updated_at":"2023-06-18T05:47:41.000Z","dependencies_parsed_at":"2022-09-26T18:01:32.178Z","dependency_job_id":null,"html_url":"https://github.com/jhvhs/gob-mock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jhvhs/gob-mock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhvhs%2Fgob-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhvhs%2Fgob-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhvhs%2Fgob-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhvhs%2Fgob-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jhvhs","download_url":"https://codeload.github.com/jhvhs/gob-mock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhvhs%2Fgob-mock/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266242072,"owners_count":23898102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","ginkgo","go-basher","tdd","testing"],"created_at":"2024-11-08T23:10:48.534Z","updated_at":"2025-07-21T05:03:54.301Z","avatar_url":"https://github.com/jhvhs.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gob-mock\nA simple mocking helper library for bash testing with [golang](https://golang.org/) via \n[go-basher](https://github.com/progrium/go-basher/). It was originally created to help with testing\nthe shell scripts in [kubo-deployment](https://github.com/pivotal-cf-experimental/kubo-deployment/).\n\n## Quick intro\n\n**gob-mock** provides three types of test doubles for stubbing executables or bash functions:\n\n### Stubs\nStub is the most simple version of a test double. It will silently drop the call to the original \nexecutable. Additionally, it would silently discard any data that was piped through it, and would \nnot cause any problems when the `pipefail` option is enabled.\n\n```go\n  bash := basher.NewContext(\"/path/to/bash\", false)\n  mocks := []Gob{Stub(\"wget\")}\n  ApplyMocks(bash, mocks)\n  status, _ := bash.Run(\"wget\", []string{\"zaa://qwee.dooo\"})\n  Expect(status).To(Equal(0))\n```\n\n### Spies\nSpy does everything a stub does, but in addition, it will print the function name, the arguments used \nin a call to STDERR. If any data was piped in, it will also be reported in the same way.\n\n```go\n  bash := basher.NewContext(\"/path/to/bash\", false)\n  bash.StdErr = gbytes.NewBuffer()\n  mocks := []Gob{Spy(\"wget\")}\n  ApplyMocks(bash, mocks)\n  status, _ := bash.Run(\"wget\", []string{\"zaa://qwee.dooo\", \"fus\", \"ro\", \"dah\"})\n  Expect(status).To(Equal(0))\n  Expect(bash.StdErr).To(gbytes.Say(\"\u003c1\u003e wget zaa://qwee.dooo fus ro dah\"))\n```\n\nA spy is also able to invoke the underlying executable, if needed\n```go\n  bash := basher.NewContext(\"/path/to/bash\", false)\n  bash.StdErr = gbytes.NewBuffer()\n  bash.StdOut = gbytes.NewBuffer()\n  mocks := []Gob{SpyAndCallThrough(\"ls\")}\n  ApplyMocks(bash, mocks)\n  \n  status, _ := bash.Run(\"ls\", []string{\"/\"})\n  Expect(status).To(Equal(0))\n  Expect(bash.StdErr).To(gbytes.Say(\"\u003c1\u003e ls /\"))\n  Expect(bash.StdOut).To(gbytes.Say(\"etc\"))\n```\n\nThe invocation of the underlying executable could be conditional\n\n```go\n  bash := basher.NewContext(\"/path/to/bash\", false)\n  bash.StdErr = gbytes.NewBuffer()\n  bash.StdOut = gbytes.NewBuffer()\n  mocks := []Gob{SpyAndConditionallyCallThrough(\"printf\", `[[ \"$1\" =~ at ]]`)}\n  ApplyMocks(bash, mocks)\n  \n  status, _ := bash.Run(\"printf\", []string{\"Catz\"})\n  Expect(status).To(Equal(0))\n  Expect(bash.StdErr).To(gbytes.Say(\"\u003c1\u003e printf Catz\"))\n  Expect(bash.StdOut).To(gbytes.Say(\"Catz\"))\n  \n  status, _ := bash.Run(\"printf\", []string{\"Doggos\"})\n  Expect(status).To(Equal(0))\n  Expect(bash.StdErr).To(gbytes.Say(\"\u003c1\u003e printf Doggos\"))\n  Expect(bash.StdOut).NotTo(gbytes.Say(\"Doggos\"))\n```\n\nAll types of spies also have a variation with a `Shallow` prefix. A shallow spy would not\nbe exported. This is useful for preventing contamination of other scripts called from within\nthe script under test. For more examples, see the [integration tests](./gob_test.go).\n\n### Mocks\nA mock does everything that a spy does, but also provides an entry point into the mocking function. \n\nThe mock may produce output which depends on the supplied arguments:\n```go\n  bash := basher.NewContext(\"/path/to/bash\", false)\n  bash.StdErr = gbytes.NewBuffer()\n  bash.StdOut = gbytes.NewBuffer()\n  mocks := []Gob{Mock(\"wget\", \"if [[ $1 == 'quux' ]]; then echo 'Yes'; else echo 'No'; fi\")}\n  ApplyMocks(bash, mocks)\n  status, _ := bash.Run(\"wget\", []string{\"quux\", \"fus\", \"ro\", \"dah\"})\n  Expect(status).To(Equal(0))\n  Expect(bash.StdErr).To(gbytes.Say(\"\u003c1\u003e wget quux fus ro dah\"))\n  Expect(bash.StdOut).To(gbytes.Say(\"Yes\"))\n```\n\nAn exit code can be simulated by using the `return` keyword with the appropriate number:\n\n```go\n  bash := basher.NewContext(\"/path/to/bash\", false)\n  mocks := []Gob{Mock(\"wget\", \"return 12\")}\n  ApplyMocks(bash, mocks)\n  status, _ := bash.Run(\"wget\", []string{\"https://dangerous.net\"})\n  Expect(status).To(Equal(12))\n```\n\nA mock is also able to call through to an executable. In order to do so, in needs a condition\nto determine when to use the mock behaviour, and when to call through. In both cases, the invocation\nwill be recorded.\n```go\n  bash := basher.NewContext(\"/path/to/bash\", false)\n  bash.StdErr = gbytes.NewBuffer()\n  bash.StdOut = gbytes.NewBuffer()\n  mocks := []Gob{MockOrCallThrough(\"curl\", \"echo 'Here is some contents'\", `[[ \"$1\" =~ \"google\" ]]`)}\n  ApplyMocks(bash, mocks)\n  status, _ := bash.Run(\"curl\", []string{\"https://www.google.ie/\"})\n  Expect(status).To(Equal(0))\n  Expect(bash.StdErr).To(gbytes.Say(\"\u003c1\u003e curl https://www.google.ie/\"))\n  \n  status, _ = bash.Run(\"curl\", []string{\"https://www.aeiou.ea/\"})\n  Expect(status).To(Equal(0))\n  Expect(bash.StdErr).To(gbytes.Say(\"\u003c1\u003e curl https://www.aeiou.ea/\"))\n  Expect(bash.StdOut).To(gbytes.Say(\"Here is some contents\"))\n```\n\nMore examples can be found in the [integration tests](./gob_test.go).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhvhs%2Fgob-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjhvhs%2Fgob-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhvhs%2Fgob-mock/lists"}