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
|
# File 'lib/cli_tester.rb', line 32
def run_command(cmd: nil, args: nil, working_directory: nil)
if cmd
path = "bin/" + cmd
else
path = "bin/" + File.basename(Dir.pwd)
end
if !File.exist?(path)
executable = [cmd]
else
executable = [File.expand_path(path)]
end
if args
executable += args
end
if working_directory
current_working_directory = Dir.pwd
Dir.chdir(working_directory)
end
result = CommandResult.new
result.cmd = executable
begin
o, e = Cheetah.run(executable, stdout: :capture, stderr: :capture )
result.exit_code = 0
result.stdout = o
result.stderr = e
rescue Cheetah::ExecutionFailed => e
result.error = e
result.exit_code = e.status.exitstatus
result.stdout = e.stdout
result.stderr = e.stderr
end
if working_directory
Dir.chdir(current_working_directory)
end
result
end
|