rspec-shell-expectations 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -1
- data/bin/stub +21 -2
- data/features/developer/step_definitions/general_steps.rb +2 -2
- data/lib/rspec/shell/expectations/stubbed_env.rb +10 -1
- data/lib/rspec/shell/expectations/version.rb +1 -1
- data/rspec-shell-expectations.gemspec +2 -2
- data/spec/stub_output_spec.rb +17 -0
- data/spec/stubbed_env_spec.rb +33 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21c937d11afe4208b5dcc9f5a50ede733e65bbf5
|
4
|
+
data.tar.gz: 42d77825f030757dae04172a2fa6c7e561d78c74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8753dc31edea97051a92de8cda82189bd8098742cb1f60f39ac3da352d9ba082e62c33bfd694bb684fee3014fdb288e51eda72f391356f0bed5aacbda93db6d1
|
7
|
+
data.tar.gz: 99861b40a7daefa40221c32d412edc90c4bd21494815193731758813221e0942ff2d24524d1063fc3a83bbc0d7180068dd6e8ddb01491f5c17c2edf79faf14f7
|
data/README.md
CHANGED
@@ -6,6 +6,19 @@
|
|
6
6
|
Run your shell script in a mocked environment to test its behaviour
|
7
7
|
using RSpec.
|
8
8
|
|
9
|
+
## Features
|
10
|
+
|
11
|
+
- Setup a fake shell environment that has preference in exucing commands
|
12
|
+
(highest path value)
|
13
|
+
- Stub shell commands in this environment
|
14
|
+
- Control multiple outputs (through STDOUT, STDERR or files)
|
15
|
+
- Control exit status codes
|
16
|
+
- Set different configurations based on command line arguments
|
17
|
+
- Verify if command is called
|
18
|
+
- Verify arguments command is called with
|
19
|
+
- Verify STDIN
|
20
|
+
|
21
|
+
|
9
22
|
## Installation
|
10
23
|
|
11
24
|
Add this line to your application's Gemfile:
|
@@ -92,6 +105,8 @@ see specs in `spec/` folder:
|
|
92
105
|
rake_stub.outputs('informative message', to: :stdout)
|
93
106
|
.outputs('error message', to: :stderr)
|
94
107
|
.outputs('log contents', to: 'logfile.log')
|
108
|
+
.outputs('converted result', to: ['prefix-', :arg2, '.txt'])
|
109
|
+
# last one creates 'prefix-foo.txt' when called as 'rake convert foo'
|
95
110
|
end
|
96
111
|
```
|
97
112
|
|
@@ -124,7 +139,7 @@ see specs in `spec/` folder:
|
|
124
139
|
|
125
140
|
## More examples
|
126
141
|
|
127
|
-
see the *features* folder
|
142
|
+
see the *features* and *spec* folder
|
128
143
|
|
129
144
|
## Supported ruby versions
|
130
145
|
|
data/bin/stub
CHANGED
@@ -12,6 +12,23 @@ call_logs.open('a') do |f|
|
|
12
12
|
f.puts " stdin: #{$stdin.read.inspect}" unless STDIN.tty?
|
13
13
|
end
|
14
14
|
|
15
|
+
def interpolate_filename(elements)
|
16
|
+
return elements if elements.is_a? String
|
17
|
+
return nil unless elements.is_a? Array
|
18
|
+
|
19
|
+
elements.map do |element|
|
20
|
+
case element
|
21
|
+
when String then element
|
22
|
+
when Symbol then interpolate_argument(element)
|
23
|
+
end
|
24
|
+
end.join
|
25
|
+
end
|
26
|
+
|
27
|
+
def interpolate_argument(name)
|
28
|
+
return unless (data = /^arg(\d+)$/.match(name.to_s))
|
29
|
+
ARGV[data[1].to_i - 1]
|
30
|
+
end
|
31
|
+
|
15
32
|
call_configurations = Pathname.new(folder).join("#{command}_stub.yml")
|
16
33
|
if call_configurations.exist?
|
17
34
|
config = YAML.load call_configurations.read
|
@@ -32,8 +49,10 @@ if call_configurations.exist?
|
|
32
49
|
(call_config[:outputs] || []).each do |data|
|
33
50
|
$stdout.print data[:content] if data[:target] == :stdout
|
34
51
|
$stderr.print data[:content] if data[:target] == :stderr
|
35
|
-
|
36
|
-
|
52
|
+
|
53
|
+
output_filename = interpolate_filename(data[:target])
|
54
|
+
next unless output_filename
|
55
|
+
Pathname.new(output_filename).open('w') do |f|
|
37
56
|
f.print data[:content]
|
38
57
|
end
|
39
58
|
end
|
@@ -14,7 +14,7 @@ Given(/^I have stubbed "(.*?)" with args as "(.*)":$/) do |command, call, table|
|
|
14
14
|
# table is a Cucumber::Ast::Table
|
15
15
|
args = table.hashes.map { |d| d['args'] }
|
16
16
|
@stubbed_command = simulated_environment.stub_command(command)
|
17
|
-
|
17
|
+
.with_args(*args)
|
18
18
|
@remembered_commands ||= {}
|
19
19
|
@remembered_commands[call] = @stubbed_command
|
20
20
|
end
|
@@ -23,7 +23,7 @@ Given(/^I have stubbed "(.*?)" with args:$/) do |command, table|
|
|
23
23
|
# table is a Cucumber::Ast::Table
|
24
24
|
args = table.hashes.map { |d| d['args'] }
|
25
25
|
@stubbed_command = simulated_environment.stub_command(command)
|
26
|
-
|
26
|
+
.with_args(*args)
|
27
27
|
end
|
28
28
|
|
29
29
|
sc = /^the stubbed command/
|
@@ -13,9 +13,18 @@ module Rspec
|
|
13
13
|
# A shell environment that can manipulate behaviour
|
14
14
|
# of executables
|
15
15
|
class StubbedEnv
|
16
|
+
attr_reader :dir
|
17
|
+
|
16
18
|
def initialize
|
17
19
|
@dir = Dir.mktmpdir
|
18
|
-
|
20
|
+
ENV['PATH'] = "#{@dir}:#{ENV['PATH']}"
|
21
|
+
at_exit { cleanup }
|
22
|
+
end
|
23
|
+
|
24
|
+
def cleanup
|
25
|
+
paths = (ENV['PATH'].split ':') - [@dir]
|
26
|
+
ENV['PATH'] = paths.join ':'
|
27
|
+
FileUtils.remove_entry_secure @dir if Pathname.new(@dir).exist?
|
19
28
|
end
|
20
29
|
|
21
30
|
def stub_command(command)
|
@@ -17,8 +17,8 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.license = 'MIT'
|
18
18
|
|
19
19
|
spec.files = `git ls-files -z`.split("\x0")
|
20
|
-
spec.executables = spec.files.grep(
|
21
|
-
spec.test_files = spec.files.grep(
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
22
|
spec.require_paths = ['lib']
|
23
23
|
|
24
24
|
spec.add_development_dependency 'bundler', '>= 1.6'
|
data/spec/stub_output_spec.rb
CHANGED
@@ -65,5 +65,22 @@ describe 'Stub command output' do
|
|
65
65
|
expect(o).to be_empty
|
66
66
|
expect(Pathname.new(filename).read).to eql 'world'
|
67
67
|
end
|
68
|
+
|
69
|
+
describe 'using passed argument as filename' do
|
70
|
+
let(:script) do
|
71
|
+
<<-SCRIPT
|
72
|
+
command1 input output
|
73
|
+
SCRIPT
|
74
|
+
end
|
75
|
+
|
76
|
+
let(:passed_filename) { ['hello-', :arg2, '.foo'] }
|
77
|
+
let(:filename) { 'hello-output.foo' }
|
78
|
+
|
79
|
+
it 'writes data to a interpolated filename' do
|
80
|
+
command1_stub.outputs('world', to: passed_filename)
|
81
|
+
subject
|
82
|
+
expect(Pathname.new(filename).read).to eql 'world'
|
83
|
+
end
|
84
|
+
end
|
68
85
|
end
|
69
86
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'English'
|
2
|
+
require 'rspec/shell/expectations'
|
3
|
+
|
4
|
+
describe 'StubbedEnv' do
|
5
|
+
include Rspec::Shell::Expectations
|
6
|
+
|
7
|
+
describe 'creating a stubbed env' do
|
8
|
+
it 'extends the PATH with the stubbed folder first' do
|
9
|
+
expect { create_stubbed_env }.to change { ENV['PATH'] }
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'creates a folder to place the stubbed commands in' do
|
13
|
+
env = create_stubbed_env
|
14
|
+
expect(Pathname.new(env.dir)).to exist
|
15
|
+
expect(Pathname.new(env.dir)).to be_directory
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#cleanup' do
|
20
|
+
it 'restores the environment variable PATH' do
|
21
|
+
original_path = ENV['PATH']
|
22
|
+
env = create_stubbed_env
|
23
|
+
|
24
|
+
expect { env.cleanup }.to change { ENV['PATH'] }.to original_path
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'removes the folder with stubbed commands' do
|
28
|
+
env = create_stubbed_env
|
29
|
+
env.cleanup
|
30
|
+
expect(Pathname.new(env.dir)).not_to exist
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-shell-expectations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthijs Groen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- spec/provide_env_vars_spec.rb
|
86
86
|
- spec/replace_shell_commands_spec.rb
|
87
87
|
- spec/stub_output_spec.rb
|
88
|
+
- spec/stubbed_env_spec.rb
|
88
89
|
homepage: ''
|
89
90
|
licenses:
|
90
91
|
- MIT
|
@@ -129,3 +130,4 @@ test_files:
|
|
129
130
|
- spec/provide_env_vars_spec.rb
|
130
131
|
- spec/replace_shell_commands_spec.rb
|
131
132
|
- spec/stub_output_spec.rb
|
133
|
+
- spec/stubbed_env_spec.rb
|