{"id":16185311,"url":"https://github.com/jasonkarns/bats-mock","last_synced_at":"2025-05-16T08:04:37.259Z","repository":{"id":74734038,"uuid":"45399924","full_name":"jasonkarns/bats-mock","owner":"jasonkarns","description":"Mocking/stubbing library for BATS (Bash Automated Testing System)","archived":false,"fork":false,"pushed_at":"2024-10-29T16:42:21.000Z","size":128,"stargazers_count":89,"open_issues_count":18,"forks_count":38,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-13T15:57:30.361Z","etag":null,"topics":["bash","bats","mock","shell","stub","testing"],"latest_commit_sha":null,"homepage":"","language":"Shell","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/jasonkarns.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"docs/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-02T14:37:01.000Z","updated_at":"2025-04-15T14:12:30.000Z","dependencies_parsed_at":"2024-09-19T12:29:13.153Z","dependency_job_id":"74931d6e-01ed-4652-b95d-1ce79f0477f4","html_url":"https://github.com/jasonkarns/bats-mock","commit_stats":{"total_commits":87,"total_committers":7,"mean_commits":"12.428571428571429","dds":0.2183908045977011,"last_synced_commit":"93e0128b8787db05a2632c70501cd667dd35d253"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonkarns%2Fbats-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonkarns%2Fbats-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonkarns%2Fbats-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonkarns%2Fbats-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jasonkarns","download_url":"https://codeload.github.com/jasonkarns/bats-mock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253979202,"owners_count":21994037,"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","bats","mock","shell","stub","testing"],"created_at":"2024-10-10T07:13:59.677Z","updated_at":"2025-05-16T08:04:32.241Z","avatar_url":"https://github.com/jasonkarns.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bats-mock\n\nMocking/stubbing library for [BATS (Bash Automated Testing System)][bats-core]\n\n## Installation\n\n### git\n\nThe recommended installation is with git-subtree.\nAssuming your project's bats tests are in `test/`:\n\n```console\n# v1 can be any valid ref (tag, branch, commit) of the bats-mock repo\ngit subtree add --prefix test/helpers/bats-mock https://github.com/jasonkarns/bats-mock v1 --squash\n\n# pull a future bats-mock release (say, v2) with:\n# git subtree pull --prefix test/helpers/bats-mock https://github.com/jasonkarns/bats-mock v2 --squash\n\n# bats-mock keeps a \"major version\" tag pinned to the highest corresponding tag\n```\n\nthen in `test/test_helper.bash`:\n\n```bats\nload helpers/bats-mock/stub\n```\n\n### npm\n\nAlso available as an [npm module](https://www.npmjs.com/package/bats-mock)\nif you're into that sort of thing.\n\n```console\nnpm install --save-dev bats-mock\n```\n\nthen in `test/test_helper.bash`:\n\n```bats\nload ../node_modules/bats-mock/stub\n```\n\n## Usage\n\nAfter loading `bats-mock/stub` you have two new functions defined:\n\n- `stub`: for creating new stubs,\n  along with a plan with expected args and the results to return when called.\n- `unstub`: for cleaning up, and also verifying that the plan was fullfilled.\n\n### Stubbing\n\nThe `stub` function takes a program name as its first argument;\nany remaining arguments go into the stub plan.\n\nEach plan line represents an expected invocation:\na list of expected arguments followed by the command to execute,\nseparated with a colon.\n\n```text\narg1 arg2 ... : only_run if args matched\n```\n\nThe expected args (and the colon) is optional.\n\nSo, in order to stub `date`,\nwe could use something like this in a test case\n(where `format_date` is the function under test,\nrelying on data from the `date` command):\n\n```bats\nload helper\n\n# this is the \"code under test\"\n# it would normally be in another file\nformat_date() {\n  date -r 222\n}\n\nsetup() {\n  _DATE_ARGS='-r 222'\n  stub date \\\n      \"${_DATE_ARGS} : echo 'I am stubbed!'\" \\\n      \"${_DATE_ARGS} : echo 'Wed Dec 31 18:03:42 CST 1969'\"\n}\n\nteardown() {\n  unstub date\n}\n\n@test \"date format util formats date with expected arguments\" {\n  result=\"$(format_date)\"\n  [ \"$result\" == 'I am stubbed!' ]\n\n  result=\"$(format_date)\"\n  [ \"$result\" == 'Wed Dec 31 18:03:42 CST 1969' ]\n}\n```\n\nThis verifies that `format_date` indeed called `date`\nusing the args defined in `${_DATE_ARGS}`\n(which can not be declared in the test-case with local),\nand made proper use of the output of it.\n\nThe plan is verified, one by one, as the calls come in.\nFinally, when the stub is removed with `unstub`,\nthere is a final check to ensure there are no remaining un-met plans\n(which would indicated an expected invocation that did not occur).\n\n### Unstubbing\n\nOnce the test case is done,\nyou should call `unstub \u003cprogram\u003e` in order to clean up the temporary files,\nand make a final check that all the plans have been met for the stub.\n\n## How it works\n\nUnder the covers, `bats-mock` uses three scripts to manage the stubbed programs/functions.\n\nFirst, it is the command (or program) itself,\nwhich when the stub is created is placed in\n(or rather, the `binstub` script is sym-linked to)\n`${BATS_MOCK_BINDIR}/${program}`\n(which is added to your `PATH` when loading the stub library).\nSecondly, it creates a stub plan,\nbased on the arguments passed when creating the stub.\nAnd finally, during execution, the command invocations are\ntracked in a stub run file which is checked once the command is `unstub`'ed.\nThe `${program}-stub-[plan|run]` files are both in `${BATS_MOCK_TMPDIR}`.\n\n### Caveat\n\nIf you stub functions, make sure to unset them,\nor the stub script wan't be called,\nas the function will shadow the binstub script on the `PATH`.\n\n## Credits\n\nExtracted from the [ruby-build][] test suite.\nMany thanks to its author and contributors:\n[Sam Stephenson][sstephenson] and [Mislav Marohnić][mislav].\n\n[ruby-build]: https://github.com/sstephenson/ruby-build\n[sstephenson]: https://github.com/sstephenson\n[mislav]: https://github.com/mislav\n[bats-core]: https://github.com/bats-core\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonkarns%2Fbats-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjasonkarns%2Fbats-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonkarns%2Fbats-mock/lists"}