shell_mock 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 160dfa02d4b23e67d61886aecffb53bd916f2f72
4
+ data.tar.gz: de591f0065dcae5483cd75d1527b9564a1d22a8a
5
+ SHA512:
6
+ metadata.gz: 5e9d2edb53f6dc2f931706bf11f3ffe1e9526642e4b8d06b6e9d2355a0de8abc3860e10c3fa53746d4806943b4538529e28dcc411b4f21be4be345369538273c
7
+ data.tar.gz: d0d12149d6d206641407d64fc2a6dfd645f669694f7837cacac9803e320d38c98e3945bd5a36550e5a0c1a679c0ede56cb8e9977960a30a1affd31c3c5e4490d
data/.env ADDED
@@ -0,0 +1 @@
1
+ rvm use 2.3.0
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /spec/examples.txt
11
+ tags
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require shell_mock_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at choffman@optoro.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shell_mock.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Chris Hoffman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # ShellMock
2
+
3
+ It's [webmock](http://github.com/bblimke/webmock) for shell commands. It's pretty simple. You can do things like this:
4
+
5
+ ```ruby
6
+ require 'shell_mock/rspec'
7
+
8
+ describe 'something' do
9
+ stub = ShellMock.stub_command('ls')
10
+
11
+ expect(system('ls')).to eq true
12
+
13
+ expect(stub).to have_been_called
14
+ end
15
+ ```
16
+
17
+ ### More complex expectations are also supported.
18
+
19
+ Called exactly once: `expect(stub).to have_been_called.once`
20
+
21
+ Not called: `expect(stub).to_not have_been_called` or `expect(stub).to have_been_called.never`
22
+
23
+ Called exactly `n` times: `expect(stub).to have_been_called.times(n)`
24
+
25
+ Called more than `n` times: `expect(stub).to have_been_called.more_than(n)`
26
+
27
+ Called fewer than `n` times: `expect(stub).to have_been_called.fewer_than(n)`
28
+
29
+ `less_than` is also an alias for `fewer_than`.
30
+
31
+ Right now, only exact command string matches are supported.
32
+
33
+ ### You can also set the output of the command invocation:
34
+
35
+ ```ruby
36
+ require 'shell_mock/rspec'
37
+
38
+ describe 'something' do
39
+ stub = ShellMock.stub_commmand('ls').and_return("\n")
40
+
41
+ expect(`ls`).to eq "\n"
42
+
43
+ expect(stub).to have_been_called.once
44
+ end
45
+ ```
46
+
47
+ ## Installation
48
+
49
+ Add this line to your application's Gemfile:
50
+
51
+ ```ruby
52
+ gem 'shell_mock'
53
+ ```
54
+
55
+ And then execute:
56
+
57
+ $ bundle
58
+
59
+ Or install it yourself as:
60
+
61
+ $ gem install shell_mock
62
+
63
+ ## Development
64
+
65
+ 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.
66
+
67
+ 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 [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ 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 [Contributor Covenant](http://contributor-covenant.org) code of conduct.
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
76
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "shell_mock"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,56 @@
1
+ module ShellMock
2
+ class CallVerifier
3
+ def initialize
4
+ match_calls_when { |calls| calls.any? }
5
+ end
6
+
7
+ def matches?(actual)
8
+ @actual = actual
9
+
10
+ condition.call(actual.calls)
11
+ end
12
+
13
+ def failure_message
14
+ "#{actual.command} was expected."
15
+ end
16
+
17
+ def failure_message_when_negated
18
+ "#{actual.command} was not expected."
19
+ end
20
+
21
+ def once
22
+ times(1)
23
+ end
24
+
25
+ def never
26
+ times(0)
27
+ end
28
+
29
+ def times(n)
30
+ match_calls_when { |calls| calls.size == n }
31
+
32
+ self
33
+ end
34
+
35
+ def fewer_than(n)
36
+ match_calls_when { |calls| calls.size < n }
37
+
38
+ self
39
+ end
40
+ alias less_than fewer_than
41
+
42
+ def more_than(n)
43
+ match_calls_when { |calls| calls.size > n }
44
+
45
+ self
46
+ end
47
+
48
+ private
49
+
50
+ attr_reader :actual, :condition
51
+
52
+ def match_calls_when(&blk)
53
+ @condition = blk
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,50 @@
1
+ require 'shell_mock/call_verifier'
2
+
3
+ module ShellMock
4
+ class CommandStub
5
+ attr_reader :command, :expected_output, :return_code
6
+
7
+ def initialize(command)
8
+ @command = command
9
+ end
10
+
11
+ def with_env(env)
12
+ @env = env
13
+
14
+ self
15
+ end
16
+
17
+ def env
18
+ @env || {}
19
+ end
20
+
21
+ def with_option(option)
22
+ @options = options
23
+
24
+ self
25
+ end
26
+
27
+ def options
28
+ @options || {}
29
+ end
30
+
31
+ def and_return(expected_output, return_code: 0)
32
+ @expected_output = expected_output
33
+ @return_code = return_code
34
+
35
+ self
36
+ end
37
+
38
+ def side_effect
39
+ @side_effect || proc {}
40
+ end
41
+
42
+ def calls
43
+ @calls ||= []
44
+ end
45
+
46
+ def called_with(env, command, options)
47
+ calls << [env, command, options]
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,41 @@
1
+ require 'shell_mock/no_stub_specified'
2
+
3
+ module Kernel
4
+ def __shell_mocked_system(env, command = nil, **options)
5
+ env, command = {}, env if command.nil?
6
+
7
+ # other arg manipulation
8
+
9
+ stub = ShellMock::StubRegistry.stub_matching(env, command, options)
10
+
11
+ if stub
12
+ stub.side_effect.call
13
+ stub.called_with(env, command, options)
14
+
15
+ return stub.return_code == 0
16
+ else
17
+ if ShellMock.let_commands_run?
18
+ __un_shell_mocked_system(env, command, **options)
19
+ else
20
+ raise ShellMock::NoStubSpecified.new(env, command, options)
21
+ end
22
+ end
23
+ end
24
+
25
+ def __shell_mocked_backtick(command)
26
+ stub = ShellMock::StubRegistry.stub_matching({}, command, {})
27
+
28
+ if stub
29
+ stub.side_effect.call
30
+ stub.called_with({}, command, {})
31
+
32
+ return stub.expected_output
33
+ else
34
+ if ShellMock.let_commands_run?
35
+ __un_shell_mocked_backtick(command)
36
+ else
37
+ raise ShellMock::NoStubSpecified.new({}, command, {})
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ module ShellMock
2
+ class NoStubSpecified < StandardError
3
+ def initialize(env, command, **options)
4
+ super("no stub specified for #{command}")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require 'shell_mock/call_verifier'
2
+
3
+ module ShellMock
4
+ module Matchers
5
+ def have_been_called
6
+ ShellMock::CallVerifier.new
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ require 'shell_mock'
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/expectations'
5
+
6
+ require 'shell_mock/rspec/matchers'
7
+
8
+ ShellMock.enable
9
+ ShellMock.dont_let_commands_run
10
+
11
+ RSpec.configure do |config|
12
+ config.include ShellMock::Matchers
13
+ end
@@ -0,0 +1,30 @@
1
+ module ShellMock
2
+ StubRegistry = Object.new
3
+
4
+ StubRegistry.instance_exec do
5
+ def register_command_stub(command_stub)
6
+ command_stubs << command_stub
7
+ command_stub
8
+ end
9
+
10
+ def stub_matching(env, command, options)
11
+ matching_stubs = command_stubs.select do |command_stub|
12
+ command_stub.env <= env &&
13
+ command_stub.options <= options &&
14
+ command_stub.command == command
15
+ end
16
+
17
+ matching_stubs.max_by do |command_stub|
18
+ [env.size, options.size]
19
+ end
20
+ end
21
+
22
+ def command_stubs
23
+ @command_stubs ||= []
24
+ end
25
+
26
+ def clear
27
+ @command_stubs = []
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module ShellMock
2
+ VERSION = "0.1.0"
3
+ end
data/lib/shell_mock.rb ADDED
@@ -0,0 +1,52 @@
1
+ require "shell_mock/version"
2
+ require 'shell_mock/stub_registry'
3
+ require 'shell_mock/command_stub'
4
+ require 'shell_mock/core_ext/kernel'
5
+
6
+ module ShellMock
7
+ def self.stub_command(command)
8
+ command_stub = CommandStub.new(command)
9
+
10
+ StubRegistry.register_command_stub(command_stub)
11
+ end
12
+
13
+ def self.let_commands_run
14
+ @let_commands_run = true
15
+ end
16
+
17
+ def self.dont_let_commands_run
18
+ @let_commands_run = false
19
+ end
20
+
21
+ def self.let_commands_run?
22
+ @let_commands_run = true if @let_commands_run.nil?
23
+ @let_commands_run
24
+ end
25
+
26
+ def self.dont_let_commands_run?
27
+ !let_commands_run?
28
+ end
29
+
30
+ def self.enable
31
+ Kernel.module_exec do
32
+ if Kernel.respond_to?(:__shell_mocked_system)
33
+ define_method(:__un_shell_mocked_system, &method(:system).to_proc)
34
+ define_method(:system, &method(:__shell_mocked_system).to_proc)
35
+ end
36
+
37
+ if Kernel.respond_to?(:__shell_mocked_backtick)
38
+ define_method(:__un_shell_mocked_backtick, &method(:`).to_proc)
39
+ define_method(:`, &method(:__shell_mocked_backtick).to_proc)
40
+ end
41
+ end
42
+ end
43
+
44
+ def self.disable
45
+ Kernel.module_exec do
46
+ define_method(:system, &method(:__un_shell_mocked_system).to_proc) if Kernel.respond_to?(:__un_shell_mocked_system)
47
+ define_method(:`, &method(:__un_shell_mocked_backtick).to_proc) if Kernel.respond_to?(:__un_shell_mocked_backtick)
48
+ end
49
+
50
+ StubRegistry.clear
51
+ end
52
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shell_mock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "shell_mock"
8
+ spec.version = ShellMock::VERSION
9
+ spec.authors = ["Chris Hoffman"]
10
+ spec.email = ["yarmiganosca@gmail.com"]
11
+
12
+ spec.summary = %q{WebMock for shell commands}
13
+ spec.description = spec.summary
14
+ spec.homepage = "http://www.github.com/yarmiganosca/shell_mock"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency 'simplecov'
26
+ spec.add_development_dependency 'pry-byebug'
27
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shell_mock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Hoffman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: WebMock for shell commands
84
+ email:
85
+ - yarmiganosca@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".env"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/shell_mock.rb
102
+ - lib/shell_mock/call_verifier.rb
103
+ - lib/shell_mock/command_stub.rb
104
+ - lib/shell_mock/core_ext/kernel.rb
105
+ - lib/shell_mock/no_stub_specified.rb
106
+ - lib/shell_mock/rspec.rb
107
+ - lib/shell_mock/rspec/matchers.rb
108
+ - lib/shell_mock/stub_registry.rb
109
+ - lib/shell_mock/version.rb
110
+ - shell_mock.gemspec
111
+ homepage: http://www.github.com/yarmiganosca/shell_mock
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.5.1
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: WebMock for shell commands
135
+ test_files: []