It’s webmock for shell commands. It’s pretty simple. You can do things like this:
require 'shell_mock/rspec'
RSpec.describe "shelling out to run 'ls'" do
before { ShellMock.enable } # (1)
after { ShellMock.disable }
let(:stub) { ShellMock.stub_command('ls') } # (2)
it "works"
system('ls') # (3)
expect(stub).to have_run # (4)
end
end
-
enables ShellMock’s monkey patches during the test
-
creates a command stub that will match the command
"ls"
(by default it will exit0
and have no output) -
shells out to run
"ls"
(in this case usingKernel#system
) -
correctly expects that our command stub for
"ls"
will have recorded an invocation
Match env vars as well as the command: ShellMock.stub_command('ls').with_env({'FOO' ⇒ 'bar'})
Provide a more complete invocation: ShellMock.stub_command('ls $HOME')
Shelling out to run "ls"
won’t match this command stub, but shelling out to run "ls $HOME"
will.
ℹ️
|
ShellMock always matches as strictly as possible, so if you stubbed both "ls" and "ls $HOME" , invocations of "ls $HOME' will only ever match against the "ls $HOME" stub and never the "ls" stub.
|
Have the mock command invocation write to stdout: ShellMock.stub_command('ls').to_output("\n")
Set the mock command invocation’s exit status: ShellMock.stub_command('ls').to_exit(2)
Set the mock command invocation’s exit status to 0
: ShellMock.stub_command('ls').to_succeed
Set the mock command invocation’s exit status to 1
: ShellMock.stub_command('ls').to_fail
If you want to both write to stdout and set the exit code (a common pair), ShellMock.stub_command('ls').to_return("\n")
will both have the command invocation write the passed string to stdout, and will set the mock command invocation’s exit status to 0
.
Called exactly once: expect(stub).to have_run.once
Not called: expect(stub).to have_run.never
Not called (using RSpec expectation negation): expect(stub).to_not have_run
Called exactly n
times: expect(stub).to have_run.times(n)
Called more than n
times: expect(stub).to have_run.more_than(n)
Called fewer than n
times: expect(stub).to have_run.fewer_than(n)
less_than
can be used as an alias for fewer_than
Currently, only exact string matches of the stubbed command string are supported. Basic regex support or more complex matching for arguments and flags may be added later.
ShellMock supports stubbing these ways of shelling out in Ruby:
-
Kernel#`
(aka "backticks") -
%x
command literal (which delegates to backticks) -
the
Open3
module (since all its methods usespawn
)
ShellMock currently DOES NOT support stubbing these ways of shelling out in Ruby (but will):
Add this line to your application’s Gemfile:
gem 'shell_mock'
And then execute:
$ bundle
Or install it yourself as:
$ gem install shell_mock
Bug reports and pull requests are welcome on GitHub at https://github.com/yarmiganosca/shell_mock
❗
|
Code of Conduct
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Convenant code of conduct. |
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
.
Pull requests should be well-scoped and include tests appropriate to the changes.
When submitting a pull request that changes user-facing behavior, add release note lines to the commit message body like this. You can preview your release lines by running
$ bundle exec rake changelog:preview
Releasing a new version is a 2-step process.
First, run
$ bundle exec rake changelog:compile
This will add a new release section before the other release sections. It will contain all the release notes in the commit messages since the last release, and will be prepopulated with the minimum possible version given those changes. Proof-read it and reorder the notes if you think doing so would be necessary or clearer. Feel free to increase the version if necessary (to force a major release, for example).
Once you’re satisfied, run
$ bundle exec rake changelog:release
This will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
The gem is available as open source under the terms of the MIT License.