command_test 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+
3
+ describe CommandTest::Parser do
4
+ describe "#parse" do
5
+ before do
6
+ @parser = CommandTest::Parser.new
7
+ end
8
+
9
+ def parse(*args)
10
+ @parser.parse(*args)
11
+ end
12
+
13
+ it "should split the given string into shell words" do
14
+ parse('one two three').should == ['one', 'two', 'three']
15
+ end
16
+
17
+ it "should support single quoting" do
18
+ parse("'a b' 'c d'").should == ['a b', 'c d']
19
+ end
20
+
21
+ it "should not allow escaping single quotes within single quotes" do
22
+ parse("'a\\'b' '").should == ["a\\b "]
23
+ end
24
+
25
+ it "should support double quoting" do
26
+ parse('"a b" "c d"').should == ['a b', 'c d']
27
+ end
28
+
29
+ it "should allow escaping double quotes within double quotes" do
30
+ parse('"a\\"b" "c\\"d"').should == ['a"b', 'c"d']
31
+ end
32
+
33
+ it "should support backslash escapes" do
34
+ parse('a\\ b c\\ d').should == ['a b', 'c d']
35
+ end
36
+
37
+ it "should strip input redirects" do
38
+ parse("command 0< file arg").should == ['command', 'arg']
39
+ end
40
+
41
+ it "should support omitting the stream number from an input redirect" do
42
+ parse("command <file arg").should == ['command', 'arg']
43
+ end
44
+
45
+ it "should allow space after the input operator" do
46
+ parse("command 0< file arg").should == ['command', 'arg']
47
+ end
48
+
49
+ it "should strip output redirects" do
50
+ parse("command 1> file arg").should == ['command', 'arg']
51
+ end
52
+
53
+ it "should support omitting the stream number from an output redirect" do
54
+ parse("command 1> file arg").should == ['command', 'arg']
55
+ end
56
+
57
+ it "should stream append output redirects" do
58
+ parse("command 1>> file arg").should == ['command', 'arg']
59
+ end
60
+
61
+ it "should support omitting the stream number from an append output redirect" do
62
+ parse("command >> file arg").should == ['command', 'arg']
63
+ end
64
+
65
+ it "should handle multiple redirects" do
66
+ result = parse('command > out 2>> error one < input two ')
67
+ result.should == ['command', 'one', 'two']
68
+ end
69
+
70
+ it "should strip stream merges" do
71
+ parse('command 2>&1 arg').should == ['command', 'arg']
72
+ end
73
+
74
+ it "should concatenate quoted regions in the same word" do
75
+ parse(%['a'\\ "b" x]).should == ['a b', 'x']
76
+ end
77
+
78
+ it "should not concatenate across input redirects" do
79
+ parse('command arg<input').should == ['command', 'arg']
80
+ end
81
+
82
+ it "should not concatenate across output redirects" do
83
+ parse('command arg>output').should == ['command', 'arg']
84
+ end
85
+
86
+ it "should not concatenate across append redirects" do
87
+ parse('command arg>>output').should == ['command', 'arg']
88
+ end
89
+
90
+ it "should concatenate across quoted redirection characters" do
91
+ parse("command 'arg<input'").should == ['command', 'arg<input']
92
+ end
93
+
94
+ it "should consider the stream number part of the preceding word if there's no space before an output redirect" do
95
+ parse("command arg2>output").should == ['command', 'arg2']
96
+ end
97
+
98
+ it "should ignore trailing whitespace" do
99
+ parse('command arg ').should == ['command', 'arg']
100
+ end
101
+
102
+ it "should ignore trailing whitespace after a redirection" do
103
+ parse('command arg > out').should == ['command', 'arg']
104
+ end
105
+
106
+ it "should raise an ArgumentError if there is an unmatched single quote" do
107
+ lambda{parse("a'b")}.should raise_error(ArgumentError, /unmatched/i)
108
+ end
109
+
110
+ it "should raise an ArgumentError if there is an unmatched double quote" do
111
+ lambda{parse('a"b')}.should raise_error(ArgumentError, /unmatched/i)
112
+ end
113
+
114
+ it "should ignore a trailing backslash" do
115
+ parse('a\\').should == ['a']
116
+ end
117
+
118
+ it "should not ignore a trailing backslash-escaped backslash" do
119
+ parse('a\\\\').should == ['a\\']
120
+ end
121
+
122
+ it "should raise an ArgumentError if there is a missing input redirection source" do
123
+ lambda{parse('command <')}.should raise_error(ArgumentError, /missing.*source/i)
124
+ end
125
+
126
+ it "should raise an ArgumentError if there is a missing output redirection target" do
127
+ lambda{parse('command >')}.should raise_error(ArgumentError, /missing.*target/i)
128
+ end
129
+
130
+ it "should raise an ArgumentError if there is a missing append redirection source" do
131
+ lambda{parse('command >>')}.should raise_error(ArgumentError, /missing.*target/i)
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe CommandTest::Tests::RunsCommand do
4
+ describe "when expected matches actual" do
5
+ before do
6
+ @test = CommandTest::Tests::RunsCommand.new(['sh', '-c', 'true']) do
7
+ system 'sh', '-c', 'true'
8
+ system 'sh', '-c', 'false'
9
+ end
10
+ end
11
+
12
+ describe "#matches?" do
13
+ it "should return true" do
14
+ @test.matches?.should be_true
15
+ end
16
+ end
17
+
18
+ describe "#negative_failure_message" do
19
+ before do
20
+ @test.matches?.should be_true
21
+ end
22
+
23
+ it "should return something useful" do
24
+ @test.negative_failure_message.should == <<-EOS.gsub(/^ *\|/, '')
25
+ |This command should not have been run, but was:
26
+ | "sh" "-c" "true"
27
+ |These were the commands run:
28
+ | "sh" "-c" "true"
29
+ | "sh" "-c" "false"
30
+ EOS
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "when expected does not match actual" do
36
+ before do
37
+ @test = CommandTest::Tests::RunsCommand.new(['sh', '-c', '']) do
38
+ system 'sh', '-c', 'true'
39
+ system 'sh', '-c', 'false'
40
+ end
41
+ end
42
+
43
+ describe "#matches?" do
44
+ it "should return false" do
45
+ @test.matches?.should be_false
46
+ end
47
+ end
48
+
49
+ describe "#positive_failure_message" do
50
+ before do
51
+ @test.matches?.should be_false
52
+ end
53
+
54
+ it "should return something useful" do
55
+ @test.positive_failure_message.should == <<-EOS.gsub(/^ *\|/, '')
56
+ |This command should have been run, but was not:
57
+ | "sh" "-c" ""
58
+ |These were the commands run:
59
+ | "sh" "-c" "true"
60
+ | "sh" "-c" "false"
61
+ EOS
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ describe CommandTest do
4
+ describe ".record" do
5
+ def self.it_should_record_interpreted(method, options={}, &block)
6
+ describe "when #{method} is called during the block" do
7
+ define_method(:run){|*args| block.call(*args)}
8
+
9
+ it "should record the command run" do
10
+ commands = CommandTest.record do
11
+ run "sh -c true"
12
+ end
13
+ commands.should == [['sh', '-c', 'true']]
14
+ end
15
+
16
+ it "should parse the command like a shell" do
17
+ commands = CommandTest.record do
18
+ run "sh -c echo 'a b' > /dev/null"
19
+ end
20
+ commands.should == [['sh', '-c', 'echo', 'a b']]
21
+ end
22
+ end
23
+ end
24
+
25
+ def self.it_should_record(method, options={}, &block)
26
+ describe "when #{method} is called during the block" do
27
+ define_method(:run){|*args| block.call(*args)}
28
+
29
+ it "should record the command run" do
30
+ commands = CommandTest.record do
31
+ run 'sh', '-c', 'true'
32
+ end
33
+ commands.should == [['sh', '-c', 'true']]
34
+ end
35
+
36
+ it "should expand the first and only the first argument" do
37
+ commands = CommandTest.record do
38
+ run 'sh -c echo', 'a b'
39
+ end
40
+ commands.should == [['sh', '-c', 'echo', 'a b']]
41
+ end
42
+ end
43
+ end
44
+
45
+ it_should_record("system") do |*args|
46
+ system(*args)
47
+ end
48
+
49
+ it_should_record("Kernel.system") do |*args|
50
+ Kernel.system(*args)
51
+ end
52
+
53
+ it_should_record_interpreted("`") do |command|
54
+ `#{command}`
55
+ end
56
+
57
+ it_should_record_interpreted("Kernel.`") do |command|
58
+ Kernel.send('`', command)
59
+ end
60
+
61
+ it_should_record_interpreted("open") do |command|
62
+ open("|#{command}"){}
63
+ end
64
+
65
+ it_should_record_interpreted("Kernel.open") do |command|
66
+ Kernel.open("|#{command}"){}
67
+ end
68
+
69
+ it_should_record_interpreted("IO.popen") do |command|
70
+ IO.popen(command){}
71
+ end
72
+
73
+ it_should_record("popen3") do |*args|
74
+ Class.new{include Open3}.new.instance_eval do
75
+ popen3(*args){}
76
+ end
77
+ end
78
+
79
+ it_should_record("Open3.popen3") do |*args|
80
+ Open3.popen3(*args){}
81
+ end
82
+
83
+ describe "when multiple commands are run during the block" do
84
+ it "should return all the commands run" do
85
+ commands = CommandTest.record do
86
+ system 'sh', '-c', 'true'
87
+ system 'sh', '-c', 'false'
88
+ end
89
+ commands.should == [['sh', '-c', 'true'], ['sh', '-c', 'false']]
90
+ end
91
+ end
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command_test
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - George Ogata
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-25 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 3
30
+ - 0
31
+ version: 1.3.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: cucumber
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 8
44
+ - 5
45
+ version: 0.8.5
46
+ type: :development
47
+ version_requirements: *id002
48
+ description:
49
+ email:
50
+ - george.ogata@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ - README.markdown
58
+ files:
59
+ - lib/command_test/adapters/rspec.rb
60
+ - lib/command_test/adapters/test_unit.rb
61
+ - lib/command_test/adapters.rb
62
+ - lib/command_test/core_extensions.rb
63
+ - lib/command_test/matcher.rb
64
+ - lib/command_test/parser.rb
65
+ - lib/command_test/tests.rb
66
+ - lib/command_test/version.rb
67
+ - lib/command_test.rb
68
+ - CHANGELOG
69
+ - LICENSE
70
+ - README.markdown
71
+ - Rakefile
72
+ has_rdoc: true
73
+ homepage: http://github.com/oggy/command_test
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options:
78
+ - --charset=UTF-8
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 1
94
+ - 3
95
+ - 6
96
+ version: 1.3.6
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.6
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Test your ruby programs run commands exactly the way you expect.
104
+ test_files:
105
+ - features/rspec_integration.feature
106
+ - features/step_definitions/test_steps.rb
107
+ - features/support/env.rb
108
+ - features/test_unit_integration.feature
109
+ - spec/spec_helper.rb
110
+ - spec/support/temporary_directory.rb
111
+ - spec/unit/command_test/core_extensions_spec.rb
112
+ - spec/unit/command_test/matcher_spec.rb
113
+ - spec/unit/command_test/parser_spec.rb
114
+ - spec/unit/command_test/tests_spec.rb
115
+ - spec/unit/command_test_spec.rb