forked from thoughtworks/cruisecontrol.rb
/
command_line_test.rb
136 lines (116 loc) · 4.97 KB
/
command_line_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require File.dirname(__FILE__) + '/../test_helper'
class CommandLineTest < Test::Unit::TestCase
include FileSandbox
def test_should_write_to_both_files_when_both_files_specified_and_no_block
in_total_sandbox do
CommandLine.execute("echo hello", {:dir => @dir, :stdout => @stdout, :stderr => @stderr})
assert_match(/.* echo hello *\n.?hello ?/n, File.read(@stdout))
assert_match(/.* echo hello/n, File.read(@stderr))
end
end
def test_should_not_write_to_stdout_file_when_no_stdout_specified
in_total_sandbox do
with_redirected_stdout do
CommandLine.execute("echo hello", {:dir => @dir, :stderr => @stderr})
end
assert_equal("hello", File.read(@stdout).strip)
# assert_equal("#{@prompt} echo hello\nhello", File.read(@stdout).strip)
assert_equal("#{@prompt} echo hello", File.read(@stderr).strip)
end
end
def test_should_only_write_command_to_stdout_when_block_specified
in_total_sandbox do
CommandLine.execute("echo hello", {:dir => @dir, :stdout => @stdout, :stderr => @stderr}) do |io|
assert_equal("hello", io.read.strip)
end
assert_match(/.* echo hello\s*\[output captured and therefore not logged\]/n, File.read(@stdout).strip)
assert_equal("#{@prompt} echo hello", File.read(@stderr).strip)
end
end
def test_should_raise_on_bad_command
in_total_sandbox do
assert_raise(CommandLine::ExecutionError) do
CommandLine.execute("xaswedf", {:dir => @dir, :stdout => @stdout, :stderr => @stderr})
end
end
end
def test_should_raise_on_bad_command_with_block
in_total_sandbox do
assert_raise(CommandLine::ExecutionError) do
CommandLine.execute("xaswedf", {:dir => @dir, :stdout => @stdout, :stderr => @stderr}) do |io|
io.each_line do |line|
end
end
end
end
end
def test_should_return_block_result
in_total_sandbox do
result = CommandLine.execute("echo hello", {:dir => @dir, :stdout => @stdout, :stderr => @stderr}) do |io|
io.read
end
assert_equal "hello", result.strip
end
end
def test_execute_should_raise_when_return_code_is_not_zero
in_total_sandbox do
with_redirected_stdout do
assert_raise(CommandLine::ExecutionError) do
CommandLine.execute "ruby -e 'exit(-1)'"
end
end
end
end
def test_escape_and_concatenate
Platform.stubs(:family).returns('linux')
assert_equal 'foo', CommandLine.escape_and_concatenate(['foo'])
assert_equal 'foo bar', CommandLine.escape_and_concatenate(['foo', 'bar'])
assert_equal 'foo b\\"ar', CommandLine.escape_and_concatenate(['foo', 'b"ar'])
assert_equal 'foo b\\ \\ \\ ar', CommandLine.escape_and_concatenate(['foo', 'b ar'])
assert_equal "foo b\\'\\&\\<\\>\\\\\\|\\;ar", CommandLine.escape_and_concatenate(['foo', "b'&<>\\|;ar"])
end
def test_escape_and_concatenate_on_windows
Platform.stubs(:family).returns('mswin32')
assert_equal 'foo "bar ^\\ ^& ^| ^> ^< ^^ baz"', CommandLine.escape_and_concatenate(['foo', "bar \\ & | > < ^ baz"])
end
def test_escape_and_concatenate_should_not_escape_variable_references_and_wildcards
Platform.stubs(:family).returns('linux')
assert_equal "foo $*?{}[]", CommandLine.escape_and_concatenate(['foo', "$*?{}[]"])
end
def test_escape_and_concatenate_should_put_double_quotes_around_arguments_with_spaces_on_windows
Platform.stubs(:family).returns('mswin32')
assert_equal '"foo bar^\\tom"', CommandLine.escape_and_concatenate(['foo bar\\tom'])
Platform.stubs(:family).returns('linux')
assert_equal 'foo\ bar\\\\tom', CommandLine.escape_and_concatenate(['foo bar\\tom'])
end
def test_full_cmd_should_not_escape_command_if_it_is_a_string
Platform.stubs(:family).returns('linux')
assert_equal 'foo bar\ baz ', CommandLine.full_cmd('foo bar\ baz', {})
end
def test_full_cmd_should_escape_command_if_it_is_an_array
Platform.stubs(:family).returns('linux')
assert_equal 'foo bar baz\\ \\> ', CommandLine.full_cmd(['foo', 'bar', 'baz >'], {})
end
def test_escape_and_concatenate_accepts_non_strings
assert_equal 'foo 10', CommandLine.escape_and_concatenate(['foo', 10])
end
def test_full_cmd_should_escape_wierd_characters_for_echoing
Platform.stubs(:family).returns('linux')
Platform.stubs(:prompt).returns('prompt>')
assert_equal "echo prompt> cd\\ some/path\\;\\ ant\\ cruise >> build.log && " +
"cd some/path; ant cruise >> build.log ",
CommandLine.full_cmd("cd some/path; ant cruise", :stdout => "build.log")
assert_equal "echo prompt> cd\\ some/path\\ \\&\\&\\ ant\\ cruise >> build.log && " +
"cd some/path && ant cruise >> build.log ",
CommandLine.full_cmd("cd some/path && ant cruise", :stdout => "build.log")
end
def with_redirected_stdout
orgout = STDOUT.dup
STDOUT.reopen(@stdout)
begin
yield
ensure
STDOUT.reopen(orgout) rescue nil
end
end
end