= ShellMock
:ext-relative: .adoc
:source-highlighter: coderay
:sectanchors:
:linkattrs:
:icons: font
:toc: macro
:toc-title:
:toclevels: 3
ifdef::env-github[]
:tip-caption: :bulb:
:note-caption: :information_source:
:important-caption: :heavy_exclamation_mark:
:caution-caption: :fire:
:warning-caption: :warning:
endif::[]
image:https://badge.fury.io/rb/shell_mock.svg["Gem Version", link="https://badge.fury.io/rb/shell_mock"]
image:https://travis-ci.org/yarmiganosca/shell_mock.svg?branch=master["Build Status", link="https://travis-ci.org/yarmiganosca/shell_mock"]
image:https://coveralls.io/repos/github/yarmiganosca/shell_mock/badge.svg?branch=master["Test Coverage", link="https://coveralls.io/github/yarmiganosca/shell_mock?branch=master"]
toc::[]
== What is ShellMock?
It's http://github.com/bblimke/webmock[webmock, target="_blank"] for shell commands. It's pretty simple. You can do things like this:
[source,ruby]
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
<1> enables ShellMock's monkey patches during the test
<2> creates a command stub that will match the command "ls"
(by default it will exit 0
and have no output)
<3> shells out to run "ls"
(in this case using Kernel#system
)
<4> correctly expects that our command stub for "ls"
will have recorded an invocation
== Using ShellMock
=== You can narrow what invocations are matched to your command stub:
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.
NOTE: 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.
=== Setting the behavior of the command invocation:
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
.
=== Specifying the expected number of command invocations:
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
== Limitations
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:
ShellMock currently DOES NOT support stubbing these ways of shelling out in Ruby (but will):
== Installation
Add this line to your application's Gemfile:
[source,ruby]
gem 'shell_mock'
And then execute:
....
$ bundle
....
Or install it yourself as:
....
$ gem install shell_mock
....
== Development
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
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to https://rubygems.org[rubygems.org].
== Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/yarmiganosca/shell_mock. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the http://contributor-covenant.org[Contributor Covenant] code of conduct.
== License
The gem is available as open source under the terms of the http://opensource.org/licenses/MIT[MIT License].