Skip to content

Commit

Permalink
Patch release 0.9.3
Browse files Browse the repository at this point in the history
Signed-off-by: Torsten Long <[email protected]>
  • Loading branch information
razziel89 committed Nov 8, 2024
1 parent fd6f030 commit 8e97ee9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ It is implemented as a shell function with the following sub-commands:
Log past calls to mocks and suggest mock configs to reproduce.
- `delete` / `unmock`:
Remove all mocks for an executable.
- `is-mock`:
Determine whether an executable has been mocked by shellmock.
- `help`:
Provide a help text.

Expand Down Expand Up @@ -904,3 +906,25 @@ shellmock delete git
This will remove the mock executable for `git`.
It will also undo all mock configurations issued via `shellmock config git`.
After unmocking, new mocks can be created for the very same executable.

### is-mock

<!-- shellmock-helptext-start -->

Syntax:
`shellmock is-mock <name>`

The `is-mock` command determines whether a command is mocked by `shellmock`.
An exit code of `0` means that it is, while a non-zero one means it is not.

<!-- shellmock-helptext-end -->

The command should usually be used as part if an `if` condition:

```bash
if shellmock is-mock git; then
echo "Performing actions in case git has been mocked."
else
echo "Otherwise."
fi
```
12 changes: 12 additions & 0 deletions lib/mock_management.bash
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ __shellmock__new() {
PATH="${__SHELLMOCK_ORGPATH}" chmod +x "${__SHELLMOCK_MOCKBIN}/${cmd}"
}

# Check whether a command has been mocked by shellmock.
__shellmock__is-mock() {
__shellmock_internal_pathcheck
__shellmock_internal_trapcheck

local cmd="$1"

local location
location=$(command -v "${cmd}" 2> /dev/null || :)
[[ ${location} == "${__SHELLMOCK_MOCKBIN}/${cmd}" ]]
}

# An alias for the "new" command.
__shellmock__mock() {
__shellmock__new "$@"
Expand Down
17 changes: 17 additions & 0 deletions tests/misc.bats
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,20 @@ setup() {
run ! my_exe asdf
export PATH=${orgpath}
}

@test "determining whether an executable is a mock" {
# An executable is no mock by default.
run ! shellmock is-mock ls
[[ -z ${output} ]]
# Create mock.
shellmock new ls
# An executable is a mock after creating one.
run -0 shellmock is-mock ls
[[ -z ${output} ]]
}

@test "whether something is a mock works for non-existent executables" {
run ! command -v some_non_existent_exe
run ! shellmock is-mock some_non_existent_exe
[[ -z ${output} ]]
}

0 comments on commit 8e97ee9

Please sign in to comment.