stub_shell 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ coverage
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use "ruby-1.9.3-p0@betamax" --create
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - rbx-2.0
6
+ - jruby
7
+ - ruby-head
8
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in stub_shell.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) Stack Builders inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,34 @@
1
+ # StubShell [![Build Status](https://secure.travis-ci.org/stackbuilders/stub_shell.png)](http://travis-ci.org/stackbuilders/stub_shell)
2
+
3
+ ## Installation
4
+
5
+ gem install stub_shell
6
+
7
+ ## Configuration
8
+
9
+ require 'stub_shell'
10
+
11
+ RSpec.configure do |config|
12
+ config.include StubShell::TestHelpers
13
+ end
14
+
15
+ ## Usage
16
+
17
+ it ... do
18
+ stub_shell do
19
+ command "ls /tmp/foobar" do
20
+ stdout "hey there"
21
+ end
22
+ end
23
+ end
24
+
25
+ ## Contribute
26
+
27
+ 1. [Fork](http://help.github.com/forking/) stub_shell
28
+ 2. Create a topic branch - `git checkout -b my_branch`
29
+ 3. Push to your branch - `git push origin my_branch`
30
+ 4. Create a [Pull Request](http://help.github.com/pull-requests/) from your branch
31
+
32
+ ## License
33
+
34
+ See LICENSE
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+
3
+ exit $1
@@ -0,0 +1,44 @@
1
+ module StubShell
2
+ class Command
3
+ attr_reader :context, :result, :match
4
+
5
+ def initialize match, context, &block
6
+ @match = match
7
+ @context = context
8
+ @result = Result.new
9
+
10
+ instance_eval &block
11
+ end
12
+
13
+ def stub_shell &block
14
+ raise ArgumentError, "stub_shell has already been defined for #{self}" if @stub_shell
15
+ @stub_shell = Shell.new(self.context, &block)
16
+ end
17
+
18
+ def matches? input_string
19
+ case match
20
+ when String
21
+ input_string == match
22
+ when Regexp
23
+ !!(match.match input_string)
24
+ end
25
+ end
26
+
27
+ def current_context
28
+ @stub_shell
29
+ end
30
+
31
+ def stdout output
32
+ result.stdout = output
33
+ end
34
+
35
+ def stderr output
36
+ result.stderr = output
37
+ end
38
+
39
+ def exitstatus code
40
+ result.exitstatus = code
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,10 @@
1
+ module StubShell
2
+ class Result
3
+ attr_accessor :stdout, :stderr, :exitstatus
4
+
5
+ def initialize
6
+ @exitstatus = 0
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,39 @@
1
+ module StubShell
2
+ class Shell
3
+ attr_reader :parent_context
4
+
5
+ def initialize parent_context = nil, &block
6
+ @parent_context = parent_context
7
+ @commands = [ ]
8
+
9
+ instance_eval &block
10
+ end
11
+
12
+ def command name, &block
13
+ @commands << Command.new(name, self, &block)
14
+ end
15
+
16
+ # Resolves the requested command and returns the command definition and
17
+ # the context from which to execute the next command.
18
+ def execute command_string
19
+ resolved_command = resolve(command_string)
20
+ [resolved_command, resolved_command.current_context || self]
21
+ end
22
+
23
+ protected
24
+
25
+ # Look in current context and recursively through any available parent contexts to
26
+ # find definition of command. An Exception is raised if no implementation of command
27
+ # is found.
28
+ def resolve command_string
29
+ if detected_command = @commands.detect{|cmd| cmd.matches? command_string }
30
+ detected_command
31
+ elsif parent_context
32
+ parent_context.resolve(command_string)
33
+ else
34
+ raise "Command #{command_string} could not be resolved from the current context."
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,7 @@
1
+ module StubShell
2
+ module TestHelpers
3
+ def stub_shell &block
4
+ StubShell.current_context = StubShell::Shell.new &block
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module StubShell
2
+ VERSION = "0.0.1"
3
+ end
data/lib/stub_shell.rb ADDED
@@ -0,0 +1,26 @@
1
+ module StubShell
2
+ class << self
3
+ attr_accessor :current_context
4
+ end
5
+
6
+ self.current_context = nil
7
+ end
8
+
9
+ require 'stub_shell/result'
10
+ require 'stub_shell/command'
11
+ require 'stub_shell/shell'
12
+ require 'stub_shell/test_helpers'
13
+
14
+ def run_command cmd
15
+ command, StubShell.current_context = StubShell.current_context.execute(cmd)
16
+ Kernel.send(:`, "#{File.join(File.dirname(__FILE__), '..', 'bin', 'fake_process.sh')} '#{command.result.exitstatus}'")
17
+ command
18
+ end
19
+
20
+ def `(cmd)
21
+ run_command(cmd).result.stdout
22
+ end
23
+
24
+ def system(cmd)
25
+ run_command(cmd).result.exitstatus == 0
26
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe StubShell do
4
+ describe "stubbing a command with backquote" do
5
+ it "should set the correct value for STDOUT" do
6
+ stub_shell do
7
+ command "ls /tmp/foobar" do
8
+ stdout "hey there"
9
+ end
10
+ end
11
+
12
+ `ls /tmp/foobar`.should == 'hey there'
13
+ end
14
+
15
+ it "should have an exitstatus of 0 by default" do
16
+ stub_shell do
17
+ command "ls /tmp/foobar" do
18
+ stdout "hey there"
19
+ end
20
+ end
21
+
22
+ `ls /tmp/foobar`
23
+ $?.exitstatus.should == 0
24
+ end
25
+
26
+ it "should allow the user to set a non-zero exit status" do
27
+ stub_shell do
28
+ command "ls /tmp/foobar" do
29
+ stdout "hey there"
30
+ exitstatus 1
31
+ end
32
+ end
33
+
34
+ `ls /tmp/foobar`
35
+ $?.exitstatus.should == 1
36
+ end
37
+ end
38
+
39
+ describe "using a shell context after the shell state has been mutated" do
40
+ it "should use the return value from the nested context" do
41
+ stub_shell do
42
+ command 'ls /tmp/foobar' do
43
+ stdout 'yes, foobar exists'
44
+ end
45
+
46
+ command "rm /tmp/foobar" do
47
+ stderr 1
48
+
49
+ stub_shell do
50
+ command 'ls /tmp/foobar' do
51
+ stdout 'the file no longer exists'
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ `ls /tmp/foobar`.should == 'yes, foobar exists'
58
+ `rm /tmp/foobar`
59
+ `ls /tmp/foobar`.should == 'the file no longer exists'
60
+ end
61
+
62
+ it "should find commands defined in a more general context even when some state has been mutated" do
63
+ stub_shell do
64
+ command 'ls /tmp/myfile' do
65
+ stdout 'yes, your file exists'
66
+ end
67
+
68
+ command "rm /tmp/foobar" do
69
+ stderr 1
70
+
71
+ stub_shell do
72
+ command 'ls /tmp/foobar' do
73
+ stdout 'the file no longer exists'
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ `rm /tmp/foobar`
80
+ `ls /tmp/myfile`.should == 'yes, your file exists'
81
+ end
82
+ end
83
+
84
+ describe "with Kernel#system" do
85
+ it "should return true if the command succeeds (ie., has an exitstatus of 0)" do
86
+ stub_shell { command('ls /tmp/stuff') { exitstatus 0 } }
87
+ system('ls /tmp/stuff').should be_true
88
+ end
89
+
90
+ it "should return true if the command succeeds (ie., has a non-zero exit status)" do
91
+ stub_shell { command('ls /tmp/stuff') { exitstatus 1 } }
92
+ system('ls /tmp/stuff').should be_false
93
+ end
94
+ end
95
+
96
+ describe "with a command having a regular expression" do
97
+ it "should match a command matching the regular expression" do
98
+ stub_shell do
99
+ command /ls .*/ do
100
+ stdout 'yes, your file exists'
101
+ end
102
+ end
103
+
104
+ `ls /tmp/heythere`.should == 'yes, your file exists'
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,10 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+
6
+ require 'stub_shell'
7
+
8
+ RSpec.configure do |config|
9
+ config.include StubShell::TestHelpers
10
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "stub_shell/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "stub_shell"
7
+ s.version = StubShell::VERSION
8
+ s.authors = ["Justin Leitgeb", "itsmeduncan"]
9
+ s.email = ["justin@stackbuilders.com", "itsmeduncan@gmail.com"]
10
+ s.homepage = "http://github.com/stackbuilders/stub_shell"
11
+ s.summary = %q{Stub results of shell commands for test purposes}
12
+ s.description = %q{Stubs a set of shell commands and their return values using the backquote operator.}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- spec/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency 'rake', '~> 0.9.2.2'
20
+ s.add_development_dependency 'rspec', '~> 2.8.0'
21
+ s.add_development_dependency 'simplecov', '~> 0.5.4'
22
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stub_shell
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin Leitgeb
9
+ - itsmeduncan
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-02-04 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: &70158647692840 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.2.2
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *70158647692840
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &70158647692280 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.8.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70158647692280
37
+ - !ruby/object:Gem::Dependency
38
+ name: simplecov
39
+ requirement: &70158647691760 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 0.5.4
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70158647691760
48
+ description: Stubs a set of shell commands and their return values using the backquote
49
+ operator.
50
+ email:
51
+ - justin@stackbuilders.com
52
+ - itsmeduncan@gmail.com
53
+ executables:
54
+ - fake_process.sh
55
+ extensions: []
56
+ extra_rdoc_files: []
57
+ files:
58
+ - .gitignore
59
+ - .rspec
60
+ - .rvmrc
61
+ - .travis.yml
62
+ - Gemfile
63
+ - LICENSE
64
+ - README.markdown
65
+ - Rakefile
66
+ - bin/fake_process.sh
67
+ - lib/stub_shell.rb
68
+ - lib/stub_shell/command.rb
69
+ - lib/stub_shell/result.rb
70
+ - lib/stub_shell/shell.rb
71
+ - lib/stub_shell/test_helpers.rb
72
+ - lib/stub_shell/version.rb
73
+ - spec/acceptance/stub_shell_spec.rb
74
+ - spec/spec_helper.rb
75
+ - stub_shell.gemspec
76
+ homepage: http://github.com/stackbuilders/stub_shell
77
+ licenses: []
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.10
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Stub results of shell commands for test purposes
100
+ test_files:
101
+ - spec/acceptance/stub_shell_spec.rb
102
+ - spec/spec_helper.rb