-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_command_runner_create.rb
102 lines (78 loc) · 2.87 KB
/
test_command_runner_create.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
require 'test/unit'
require 'command_runner'
class TestCommandRunner < Test::Unit::TestCase
def test_ls_no_args
ls = CommandRunner.create(['ls'])
result = ls.run
assert_equal 0, result[:status].exitstatus
assert result[:out].include? "LICENSE.txt"
end
def test_ls_with_args
ls = CommandRunner.create(['ls', '-lort'], allowed_sub_commands: ['/tmp', 'test'])
# Without boxing args
result = ls.run('test')
assert_equal 0, result[:status].exitstatus
assert result[:out].include? "test_command_runner_create.rb"
# With boxed arg
result = ls.run(['test'])
assert_equal 0, result[:status].exitstatus
assert result[:out].include? "test_command_runner_create.rb"
result = ls.run('/tmp')
assert_equal 0, result[:status].exitstatus
begin
result = ls.run('lib')
raise "Listing lib should not be allowed"
rescue => e
# good. lib is not in allowed_sub_commands
end
end
def test_ls_with_symbl_sub_cmd
ls = CommandRunner.create(['ls', '-lort'], allowed_sub_commands: [:test, :lib])
result = ls.run(:test)
assert_equal 0, result[:status].exitstatus
assert result[:out].include? "test_command_runner_create.rb"
result = ls.run(:lib)
assert_equal 0, result[:status].exitstatus
begin
result = ls.run('test')
raise "Listing 'test' should should require symbol, not string"
rescue => e
# good. 'test' string not allowed, only :test
end
end
def test_with_defaults
slep = CommandRunner.create(['sleep'], timeout: 3)
start_time = Time.now
result = slep.run(10)
assert (Time.now - start_time) < 4 && (Time.now - start_time) > 2
assert_equal 9, result[:status].termsig
start_time = Time.now
result = slep.run(10, timeout: 1)
assert (Time.now - start_time) < 2
assert_equal 9, result[:status].termsig
end
def test_env
bash = CommandRunner.create(['ruby', '-e'], environment: {'TEHVAL' => 'world'})
result = bash.run('puts "hello " + ENV["TEHVAL"]')
assert_equal "hello world\n", result[:out]
assert_equal 0, result[:status].exitstatus
result = bash.run('puts "hello " + ENV["TEHVAL"]', environment: {'TEHVAL' => 'mundo'})
assert_equal 0, result[:status].exitstatus
assert_equal "hello mundo\n", result[:out]
end
def test_debug_log
rd, wr = IO.pipe
ls = CommandRunner.create(['ls'], debug_log: wr)
result = ls.run('test')
wr.close
assert_equal "CommandRunnerNG spawn: args=[[\"ls\", \"test\"]], timeout=, encoding=, options: {:err=>[:child, :out]}, PID: #{result[:pid]}\nCommandRunnerNG exit: PID: #{result[:pid]}, code: 0\n", rd.read
ensure
rd.close
end
def test_split_stderr
rb = CommandRunner.create(['ruby', '-e'], split_stderr: true)
result = rb.run('puts "OUT"; $stderr.puts "ERR"')
assert_equal "OUT\n", result[:out]
assert_equal "ERR\n", result[:err]
end
end