Skip to content

Commit

Permalink
Merge pull request #31 from jasonkarns/linting
Browse files Browse the repository at this point in the history
Lint fixes
  • Loading branch information
jasonkarns authored May 14, 2024
2 parents 0c8f32e + 0c4d768 commit ff3e1fc
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 41 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ name: Release
on:
push: { tags: 'v[0-9]+.[0-9]+.[0-9]+*' }

permissions: { contents: read }

jobs:
github:
runs-on: ubuntu-latest
permissions: { contents: write }
steps:
- uses: actions/checkout@v4
- run: gh release create ${tag/*-*/$tag --prerelease} --generate-notes
- run: |
# shellcheck disable=SC2086
gh release create ${tag/*-*/$tag --prerelease} --generate-notes
env:
GH_TOKEN: ${{ github.token }}
tag: ${{ github.ref_name }}
Expand Down
11 changes: 9 additions & 2 deletions .github/workflows/sync-main-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ on:
push: { branches: main }
workflow_dispatch:

permissions: { contents: read }

# One-time commands for users to switch-over:
# ```sh
# git branch -m master main; git fetch origin; git branch -u origin/main main; git remote set-head origin -a
#
# ```console
# git branch -m master main
# git fetch origin
# git branch -u origin/main main
# git remote set-head origin -a
# ```

jobs:
sync:
permissions: { contents: write }
uses: nodenv/.github/.github/workflows/sync-main-master.yml@v2
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Test
on: [push, pull_request]

permissions: { contents: read }

jobs:
test:
runs-on: ubuntu-latest
Expand All @@ -10,8 +12,13 @@ jobs:

lint:
runs-on: ubuntu-latest
if: github.ref_name != 'main'
permissions: { statuses: write }
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 } # super-linter needs full git history
- uses: github/super-linter/slim@v6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASH_EXEC_IGNORE_LIBRARIES: true
FILTER_REGEX_EXCLUDE: '.*stub.*'
86 changes: 58 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,73 @@
# bats-mock

Mocking/stubbing library for BATS (Bash Automated Testing System)

## bats-core

There are great things happening in the `bats` ecosystem! Anyone actively using it should be installing from [bats-core][].
Mocking/stubbing library for [BATS (Bash Automated Testing System)][bats-core]

## Installation

Recommended installation is via git submodule. Assuming your project's bats
tests are in `test`:
### git

``` sh
The recommended installation is via git submodule.
Assuming your project's bats tests are in `test`:

```console
git submodule add https://github.com/jasonkarns/bats-mock test/helpers/mocks
git commit -am 'added bats-mock module'
```

then in `test/test_helper.bash`:

``` bash
```bats
load helpers/mocks/stub
```

(Optionally configure sparse-checkout if you're concerned with all the non-essential files being in your repo)
> [!note]
> Consider using git's [sparse-checkout](https://git-scm.com/docs/git-sparse-checkout)
> if you're concerned with the non-essential files cluttering your repository.
### npm

Also available as an [npm module](https://www.npmjs.com/package/bats-mock) if you're into that sort of thing.
Also available as an [npm module](https://www.npmjs.com/package/bats-mock)
if you're into that sort of thing.

``` sh
```console
npm install --save-dev bats-mock
```

then in `test/test_helper.bash`:

``` bash
```bats
load ../node_modules/bats-mock/stub
```

## Usage

After loading `bats-mock/stub` you have two new functions defined:

- `stub`: for creating new stubs, along with a plan with expected args and the results to return when called.

- `stub`: for creating new stubs,
along with a plan with expected args and the results to return when called.
- `unstub`: for cleaning up, and also verifying that the plan was fullfilled.

### Stubbing

The `stub` function takes a program name as its first argument, and any remaining arguments goes into the stub plan, one line per arg.
The `stub` function takes a program name as its first argument;
any remaining arguments go into the stub plan.

Each plan line represents an expected invocation, with a list of expected arguments followed by a command to execute in case the arguments matched, separated with a colon:
Each plan line represents an expected invocation:
a list of expected arguments followed by the command to execute,
separated with a colon.

arg1 arg2 ... : only_run if args matched
```text
arg1 arg2 ... : only_run if args matched
```

The expected args (and the colon) is optional.

So, in order to stub `date`, we could use something like this in a test case (where `format_date` is the function under test, relying on data from the `date` command):
So, in order to stub `date`,
we could use something like this in a test case
(where `format_date` is the function under test,
relying on data from the `date` command):

```bash
```bats
load helper

# this is the "code under test"
Expand Down Expand Up @@ -85,29 +96,48 @@ teardown() {
}
```

This verifies that `format_date` indeed called `date` using the args defined in `${_DATE_ARGS}` (which can not be declared in the test-case with local), and made proper use of the output of it.
This verifies that `format_date` indeed called `date`
using the args defined in `${_DATE_ARGS}`
(which can not be declared in the test-case with local),
and made proper use of the output of it.

The plan is verified, one by one, as the calls come in, but the final check that there are no remaining un-met plans at the end is left until the stub is removed with `unstub`.
The plan is verified, one by one, as the calls come in.
Finally, when the stub is removed with `unstub`,
there is a final check to ensure there are no remaining un-met plans
(which would indicated an expected invocation that did not occur).

### Unstubbing

Once the test case is done, you should call `unstub <program>` in order to clean up the temporary files, and make a final check that all the plans have been met for the stub.
Once the test case is done,
you should call `unstub <program>` in order to clean up the temporary files,
and make a final check that all the plans have been met for the stub.

## How it works

(You may want to know this, if you get weird results there may be stray files lingering about messing with your state.)

Under the covers, `bats-mock` uses three scripts to manage the stubbed programs/functions.

First, it is the command (or program) itself, which when the stub is created is placed in (or rather, the `binstub` script is sym-linked to) `${BATS_MOCK_BINDIR}/${program}` (which is added to your `PATH` when loading the stub library). Secondly, it creates a stub plan, based on the arguments passed when creating the stub, and finally, during execution, the command invocations are tracked in a stub run file which is checked once the command is `unstub`'ed. The `${program}-stub-[plan|run]` files are both in `${BATS_MOCK_TMPDIR}`.
First, it is the command (or program) itself,
which when the stub is created is placed in
(or rather, the `binstub` script is sym-linked to)
`${BATS_MOCK_BINDIR}/${program}`
(which is added to your `PATH` when loading the stub library).
Secondly, it creates a stub plan,
based on the arguments passed when creating the stub.
And finally, during execution, the command invocations are
tracked in a stub run file which is checked once the command is `unstub`'ed.
The `${program}-stub-[plan|run]` files are both in `${BATS_MOCK_TMPDIR}`.

### Caveat

If you stub functions, make sure to unset them, or the stub script wan't be called, as the function will shadow the binstub script on the `PATH`.
If you stub functions, make sure to unset them,
or the stub script wan't be called,
as the function will shadow the binstub script on the `PATH`.

## Credits

Extracted from the [ruby-build][] test suite. Many thanks to its author and contributors: [Sam Stephenson][sstephenson] and [Mislav Marohnić][mislav].
Extracted from the [ruby-build][] test suite.
Many thanks to its author and contributors:
[Sam Stephenson][sstephenson] and [Mislav Marohnić][mislav].

[ruby-build]: https://github.com/sstephenson/ruby-build
[sstephenson]: https://github.com/sstephenson
Expand Down
16 changes: 8 additions & 8 deletions docs/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ decisions when appropriate.

This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
Examples of representing our community include using an official email address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the bats-core community leaders responsible for enforcement at
https://github.com/orgs/bats-core/people.
reported to the
[bats-core community leaders](https://github.com/orgs/bats-core/people).

All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
Expand Down Expand Up @@ -115,14 +116,13 @@ the community.
## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
version [2.0](https://www.contributor-covenant.org/version/2/0/code_of_conduct.html).

Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.
For answers to common questions about this code of conduct,
[see the FAQ](https://www.contributor-covenant.org/faq).
[Translations are available](https://www.contributor-covenant.org/translations).
8 changes: 6 additions & 2 deletions stub.bash
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# shellcheck shell=bash

BATS_MOCK_TMPDIR="${BATS_TMPDIR}"
BATS_MOCK_BINDIR="${BATS_MOCK_TMPDIR}/bin"

PATH="$BATS_MOCK_BINDIR:$PATH"

stub() {
local program="$1"
local prefix="$(echo "$program" | tr a-z- A-Z_)"
local prefix
prefix="$(echo "$program" | tr a-z- A-Z_)"
shift

export "${prefix}_STUB_PLAN"="${BATS_MOCK_TMPDIR}/${program}-stub-plan"
Expand All @@ -21,7 +24,8 @@ stub() {

unstub() {
local program="$1"
local prefix="$(echo "$program" | tr a-z- A-Z_)"
local prefix
prefix="$(echo "$program" | tr a-z- A-Z_)"
local path="${BATS_MOCK_BINDIR}/${program}"

export "${prefix}_STUB_END"=1
Expand Down

0 comments on commit ff3e1fc

Please sign in to comment.