tty-command 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +44 -44
  5. data/lib/tty-command.rb +1 -1
  6. data/lib/tty/command.rb +18 -18
  7. data/lib/tty/command/child_process.rb +9 -9
  8. data/lib/tty/command/cmd.rb +11 -11
  9. data/lib/tty/command/dry_runner.rb +3 -3
  10. data/lib/tty/command/exit_error.rb +1 -1
  11. data/lib/tty/command/printers/abstract.rb +8 -10
  12. data/lib/tty/command/printers/null.rb +1 -4
  13. data/lib/tty/command/printers/pretty.rb +10 -15
  14. data/lib/tty/command/printers/progress.rb +3 -8
  15. data/lib/tty/command/printers/quiet.rb +3 -7
  16. data/lib/tty/command/process_runner.rb +25 -17
  17. data/lib/tty/command/truncator.rb +3 -3
  18. data/lib/tty/command/version.rb +1 -1
  19. metadata +20 -68
  20. data/Rakefile +0 -10
  21. data/bin/console +0 -6
  22. data/bin/setup +0 -6
  23. data/examples/bash.rb +0 -12
  24. data/examples/basic.rb +0 -9
  25. data/examples/buffer.rb +0 -4
  26. data/examples/env.rb +0 -9
  27. data/examples/logger.rb +0 -10
  28. data/examples/output.rb +0 -10
  29. data/examples/pty.rb +0 -9
  30. data/examples/redirect_stderr.rb +0 -10
  31. data/examples/redirect_stdin.rb +0 -15
  32. data/examples/redirect_stdout.rb +0 -10
  33. data/examples/stdin_input.rb +0 -10
  34. data/examples/threaded.rb +0 -14
  35. data/examples/timeout.rb +0 -11
  36. data/examples/timeout_input.rb +0 -12
  37. data/examples/wait.rb +0 -21
  38. data/spec/spec_helper.rb +0 -78
  39. data/spec/unit/binmode_spec.rb +0 -29
  40. data/spec/unit/cmd_spec.rb +0 -152
  41. data/spec/unit/dry_run_spec.rb +0 -42
  42. data/spec/unit/exit_error_spec.rb +0 -25
  43. data/spec/unit/input_spec.rb +0 -13
  44. data/spec/unit/output_spec.rb +0 -25
  45. data/spec/unit/printer_spec.rb +0 -50
  46. data/spec/unit/printers/custom_spec.rb +0 -48
  47. data/spec/unit/printers/null_spec.rb +0 -36
  48. data/spec/unit/printers/pretty_spec.rb +0 -172
  49. data/spec/unit/printers/progress_spec.rb +0 -45
  50. data/spec/unit/printers/quiet_spec.rb +0 -71
  51. data/spec/unit/pty_spec.rb +0 -62
  52. data/spec/unit/redirect_spec.rb +0 -103
  53. data/spec/unit/result_spec.rb +0 -64
  54. data/spec/unit/ruby_spec.rb +0 -20
  55. data/spec/unit/run_spec.rb +0 -159
  56. data/spec/unit/test_spec.rb +0 -11
  57. data/spec/unit/timeout_spec.rb +0 -36
  58. data/spec/unit/truncator_spec.rb +0 -73
  59. data/tasks/console.rake +0 -11
  60. data/tasks/coverage.rake +0 -11
  61. data/tasks/spec.rake +0 -29
  62. data/tty-command.gemspec +0 -30
@@ -1,71 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.describe TTY::Command::Printers::Quiet do
4
- let(:output) { StringIO.new }
5
-
6
- it "doesn't print command start or exit" do
7
- printer = TTY::Command::Printers::Quiet.new(output)
8
- cmd = TTY::Command::Cmd.new("echo hello")
9
-
10
- printer.print_command_start(cmd)
11
- printer.print_command_exit(cmd, 0)
12
- output.rewind
13
-
14
- expect(output.string).to be_empty
15
- end
16
-
17
- it "prints command stdout data" do
18
- printer = TTY::Command::Printers::Quiet.new(output)
19
- cmd = TTY::Command::Cmd.new("echo hello")
20
-
21
- printer.print_command_out_data(cmd, 'hello', 'world')
22
- output.rewind
23
-
24
- expect(output.string).to eq("hello world")
25
- end
26
-
27
- it "prints command stderr data" do
28
- printer = TTY::Command::Printers::Quiet.new(output)
29
- cmd = TTY::Command::Cmd.new("echo hello")
30
-
31
- printer.print_command_err_data(cmd, 'hello', 'world')
32
- output.rewind
33
-
34
- expect(output.string).to eq("hello world")
35
- end
36
-
37
- it "doesn't print output on success when only_output_on_error is true" do
38
- zero_exit = fixtures_path('zero_exit')
39
- printer = TTY::Command::Printers::Quiet
40
- cmd = TTY::Command.new(output: output, printer: printer)
41
-
42
- cmd.run!(:ruby, zero_exit, only_output_on_error: true)
43
- cmd.run!(:ruby, zero_exit)
44
-
45
- output.rewind
46
-
47
- lines = output.readlines.map(&:chomp)
48
-
49
- expect(lines).to eq([
50
- "yess"
51
- ])
52
- end
53
-
54
- it "prints output on error when only_output_on_error is true" do
55
- non_zero_exit = fixtures_path('non_zero_exit')
56
- printer = TTY::Command::Printers::Quiet
57
- cmd = TTY::Command.new(output: output, printer: printer)
58
-
59
- cmd.run!(:ruby, non_zero_exit, only_output_on_error: true)
60
- cmd.run!(:ruby, non_zero_exit)
61
-
62
- output.rewind
63
-
64
- lines = output.readlines.map(&:chomp)
65
-
66
- expect(lines).to eq([
67
- "nooo",
68
- "nooo"
69
- ])
70
- end
71
- end
@@ -1,62 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Command, ':pty' do
4
- it "executes command in pseudo terminal mode as global option",
5
- unless: RSpec::Support::OS.windows? do
6
-
7
- color_cli = fixtures_path('color')
8
- output = StringIO.new
9
- cmd = TTY::Command.new(output: output, pty: true)
10
-
11
- out, err = cmd.run(color_cli)
12
-
13
- expect(err).to eq('')
14
- expect(out).to eq("\e[32mcolored\e[0m\n")
15
- end
16
-
17
- it "executes command in pseudo terminal mode as command option",
18
- unless: RSpec::Support::OS.windows? do
19
-
20
- color_cli = fixtures_path('color')
21
- output = StringIO.new
22
- cmd = TTY::Command.new(output: output)
23
-
24
- out, err = cmd.run(color_cli, pty: true)
25
-
26
- expect(err).to eq('')
27
- expect(out).to eq("\e[32mcolored\e[0m\n")
28
- end
29
-
30
- it "logs phased output in pseudo terminal mode",
31
- unless: RSpec::Support::OS.windows? do
32
-
33
- phased_output = fixtures_path('phased_output')
34
- uuid= 'xxxx'
35
- allow(SecureRandom).to receive(:uuid).and_return(uuid)
36
- output = StringIO.new
37
- cmd = TTY::Command.new(output: output)
38
-
39
- out, err = cmd.run("ruby #{phased_output}", pty: true)
40
-
41
- expect(out).to eq('.' * 10)
42
- expect(err).to eq('')
43
-
44
- output.rewind
45
- lines = output.readlines
46
- lines.last.gsub!(/\d+\.\d+/, 'x')
47
- expect(lines).to eq([
48
- "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{phased_output}\e[0m\n",
49
- "[\e[32m#{uuid}\e[0m] \t.\n",
50
- "[\e[32m#{uuid}\e[0m] \t.\n",
51
- "[\e[32m#{uuid}\e[0m] \t.\n",
52
- "[\e[32m#{uuid}\e[0m] \t.\n",
53
- "[\e[32m#{uuid}\e[0m] \t.\n",
54
- "[\e[32m#{uuid}\e[0m] \t.\n",
55
- "[\e[32m#{uuid}\e[0m] \t.\n",
56
- "[\e[32m#{uuid}\e[0m] \t.\n",
57
- "[\e[32m#{uuid}\e[0m] \t.\n",
58
- "[\e[32m#{uuid}\e[0m] \t.\n",
59
- "[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
60
- ])
61
- end
62
- end
@@ -1,103 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Command, 'redirect' do
4
- it "accepts standard shell redirects" do
5
- output = StringIO.new
6
- command = TTY::Command.new(output: output)
7
-
8
- out, err = command.run("echo hello 1>& 2")
9
-
10
- expect(out).to eq("")
11
- expect(err).to match(%r{hello\s*\n})
12
- end
13
-
14
- it "redirects :out -> :err" do
15
- output = StringIO.new
16
- cmd = TTY::Command.new(output: output)
17
-
18
- out, err = cmd.run("echo hello", :out => :err)
19
-
20
- expect(out).to be_empty
21
- expect(err.chomp).to eq("hello")
22
- end
23
-
24
- it "redirects :stdout -> :stderr" do
25
- output = StringIO.new
26
- cmd = TTY::Command.new(output: output)
27
-
28
- out, err = cmd.run("echo hello", :stdout => :stderr)
29
-
30
- expect(out).to be_empty
31
- expect(err.chomp).to eq("hello")
32
- end
33
-
34
- it "redirects 1 -> 2" do
35
- output = StringIO.new
36
- cmd = TTY::Command.new(output: output)
37
-
38
- out, err = cmd.run("echo hello", 1 => 2)
39
-
40
- expect(out).to be_empty
41
- expect(err.chomp).to eq("hello")
42
- end
43
-
44
- it "redirects STDOUT -> :err" do
45
- output = StringIO.new
46
- cmd = TTY::Command.new(output: output)
47
-
48
- out, err = cmd.run("echo hello", STDOUT => :err)
49
-
50
- expect(out).to be_empty
51
- expect(err.chomp).to eq("hello")
52
- end
53
-
54
- it "redirects STDOUT -> '/dev/null" do
55
- output = StringIO.new
56
- cmd = TTY::Command.new(output: output)
57
-
58
- out, _ = cmd.run('echo hello', :out => IO::NULL)
59
-
60
- expect(out).to eq("")
61
- end
62
-
63
- it "redirects to a file", type: :cli do
64
- file = tmp_path('log')
65
- output = StringIO.new
66
- cmd = TTY::Command.new(output: output)
67
-
68
- out, err = cmd.run('echo hello', :out => file)
69
-
70
- expect(out).to be_empty
71
- expect(err).to be_empty
72
- expect(File.read(file)).to eq("hello\n")
73
- end
74
-
75
- it "redirects to a file as an array value", type: :cli do
76
- file = tmp_path('log')
77
- output = StringIO.new
78
- cmd = TTY::Command.new(output: output)
79
-
80
- expect(File.writable?(file)).to eq(false)
81
- out, err = cmd.run('echo hello', :out => [file, 'w', 0600])
82
-
83
- expect(out).to be_empty
84
- expect(err).to be_empty
85
- expect(File.read(file)).to eq("hello\n")
86
- expect(File.writable?(file)).to eq(true)
87
- unless RSpec::Support::OS.windows?
88
- expect(File.stat(file).mode.to_s(8)[2..5]).to eq('0600')
89
- end
90
- end
91
-
92
- it "redirects multiple fds to a file", type: :cli do
93
- file = tmp_path('log')
94
- output = StringIO.new
95
- cmd = TTY::Command.new(output: output)
96
-
97
- out, err = cmd.run('echo hello', [:out, :err] => file)
98
-
99
- expect(out).to be_empty
100
- expect(err).to be_empty
101
- expect(File.read(file)).to eq("hello\n")
102
- end
103
- end
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Command::Result do
4
- it "exits successfully" do
5
- result = TTY::Command::Result.new(0, '', '')
6
- expect(result.exited?).to eq(true)
7
- expect(result.success?).to eq(true)
8
- end
9
-
10
- it "exist with non-zero code" do
11
- result = TTY::Command::Result.new(127, '', '')
12
- expect(result.exited?).to eq(true)
13
- expect(result.success?).to eq(false)
14
- end
15
-
16
- it "accesses exit code" do
17
- result = TTY::Command::Result.new(127, '', '')
18
- expect(result.to_i).to eq(127)
19
- expect(result.to_s).to eq('127')
20
- end
21
-
22
- it "provides runtime" do
23
- result = TTY::Command::Result.new(0, '', '', 5.4)
24
- expect(result.runtime).to eq(5.4)
25
- end
26
-
27
- it "doesn't exit" do
28
- result = TTY::Command::Result.new(nil, '', '')
29
- expect(result.exited?).to eq(false)
30
- expect(result.success?).to eq(false)
31
- end
32
-
33
- it "reads stdout" do
34
- result = TTY::Command::Result.new(0, 'foo', '')
35
- expect(result.out).to eq('foo')
36
- end
37
-
38
- it "isn't equivalent with another object" do
39
- result = TTY::Command::Result.new(0, '', '')
40
- expect(result).to_not eq(:other)
41
- end
42
-
43
- it "is the same with equivalent object" do
44
- result_foo = TTY::Command::Result.new(0, 'foo', 'bar')
45
- result_bar = TTY::Command::Result.new(0, 'foo', 'bar')
46
- expect(result_foo).to eq(result_bar)
47
- end
48
-
49
- it "iterates over output with default delimiter" do
50
- result = TTY::Command::Result.new(0, "line1\nline2\nline3", '')
51
- expect(result.to_a).to eq(['line1', 'line2', 'line3'])
52
- end
53
-
54
- it "iterates over output with global delimiter" do
55
- allow(TTY::Command).to receive(:record_separator).and_return("\t")
56
- result = TTY::Command::Result.new(0, "line1\tline2\tline3", '')
57
- expect(result.each.to_a).to eq(['line1', 'line2', 'line3'])
58
- end
59
-
60
- it "iterates over output with argument delimiter" do
61
- result = TTY::Command::Result.new(0, "line1\tline2\tline3", '')
62
- expect(result.each("\t").to_a).to eq(['line1', 'line2', 'line3'])
63
- end
64
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Command, '#ruby' do
4
- it "runs ruby with a single string argument" do
5
- output = StringIO.new
6
- cmd = TTY::Command.new(output: output)
7
- out, err = cmd.ruby %q{-e "puts 'Hello World'"}
8
- expect(out.chomp).to eq("Hello World")
9
- expect(err).to be_empty unless jruby?
10
- end
11
-
12
- it "runs ruby with multiple arguments" do
13
- output = StringIO.new
14
- cmd = TTY::Command.new(output: output)
15
- result = double(:success? => true)
16
- allow(cmd).to receive(:run).with(TTY::Command::RUBY,
17
- 'script.rb', 'foo', 'bar', {}).and_return(result)
18
- expect(cmd.ruby('script.rb', 'foo', 'bar')).to eq(result)
19
- end
20
- end
@@ -1,159 +0,0 @@
1
- RSpec.describe TTY::Command, '#run' do
2
- it 'runs command and prints to stdout' do
3
- output = StringIO.new
4
- command = TTY::Command.new(output: output)
5
-
6
- out, err = command.run(:echo, 'hello')
7
-
8
- expect(out.chomp).to eq("hello")
9
- expect(err).to eq("")
10
- end
11
-
12
- it 'runs command successfully with logging' do
13
- output = StringIO.new
14
- uuid= 'xxxx'
15
- allow(SecureRandom).to receive(:uuid).and_return(uuid)
16
- command = TTY::Command.new(output: output)
17
-
18
- command.run(:echo, 'hello')
19
-
20
- output.rewind
21
- lines = output.readlines
22
- lines.last.gsub!(/\d+\.\d+/, 'x')
23
- expect(lines).to eq([
24
- "[\e[32m#{uuid}\e[0m] Running \e[33;1mecho hello\e[0m\n",
25
- "[\e[32m#{uuid}\e[0m] \thello\n",
26
- "[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
27
- ])
28
- end
29
-
30
- it 'runs command successfully with logging without color' do
31
- output = StringIO.new
32
- uuid= 'xxxx'
33
- allow(SecureRandom).to receive(:uuid).and_return(uuid)
34
- command = TTY::Command.new(output: output, color: false)
35
-
36
- command.run(:echo, 'hello')
37
-
38
- output.rewind
39
- lines = output.readlines
40
- lines.last.gsub!(/\d+\.\d+/, 'x')
41
- expect(lines).to eq([
42
- "[#{uuid}] Running echo hello\n",
43
- "[#{uuid}] \thello\n",
44
- "[#{uuid}] Finished in x seconds with exit status 0 (successful)\n"
45
- ])
46
- end
47
-
48
- it 'runs command successfully with logging without uuid set globally' do
49
- output = StringIO.new
50
- command = TTY::Command.new(output: output, uuid: false)
51
-
52
- command.run(:echo, 'hello')
53
- output.rewind
54
-
55
- lines = output.readlines
56
- lines.last.gsub!(/\d+\.\d+/, 'x')
57
- expect(lines).to eq([
58
- "Running \e[33;1mecho hello\e[0m\n",
59
- "\thello\n",
60
- "Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
61
- ])
62
- end
63
-
64
- it 'runs command successfully with logging without uuid set locally' do
65
- output = StringIO.new
66
- command = TTY::Command.new(output: output)
67
-
68
- command.run(:echo, 'hello', uuid: false)
69
- output.rewind
70
-
71
- lines = output.readlines
72
- lines.last.gsub!(/\d+\.\d+/, 'x')
73
- expect(lines).to eq([
74
- "Running \e[33;1mecho hello\e[0m\n",
75
- "\thello\n",
76
- "Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
77
- ])
78
- end
79
-
80
- it "runs command and fails with logging" do
81
- non_zero_exit = fixtures_path('non_zero_exit')
82
- output = StringIO.new
83
- uuid= 'xxxx'
84
- allow(SecureRandom).to receive(:uuid).and_return(uuid)
85
- command = TTY::Command.new(output: output)
86
-
87
- command.run!("ruby #{non_zero_exit}")
88
-
89
- output.rewind
90
- lines = output.readlines
91
- lines.last.gsub!(/\d+\.\d+/, 'x')
92
- expect(lines).to eq([
93
- "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{non_zero_exit}\e[0m\n",
94
- "[\e[32m#{uuid}\e[0m] \tnooo\n",
95
- "[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 1 (\e[31;1mfailed\e[0m)\n"
96
- ])
97
- end
98
-
99
- it "raises ExitError on command failure" do
100
- non_zero_exit = fixtures_path('non_zero_exit')
101
- output = StringIO.new
102
- command = TTY::Command.new(output: output)
103
-
104
- expect {
105
- command.run("ruby #{non_zero_exit}")
106
- }.to raise_error(TTY::Command::ExitError,
107
- ["Running `ruby #{non_zero_exit}` failed with",
108
- " exit status: 1",
109
- " stdout: nooo",
110
- " stderr: Nothing written\n"].join("\n")
111
- )
112
- end
113
-
114
- it "streams output data" do
115
- stream = fixtures_path('stream')
116
- out_stream = StringIO.new
117
- command = TTY::Command.new(output: out_stream)
118
- output = ''
119
- error = ''
120
- command.run("ruby #{stream}") do |out, err|
121
- output << out if out
122
- error << err if err
123
- end
124
- expect(output.gsub(/\r\n|\n/,'')).to eq("hello 1hello 2hello 3")
125
- expect(error).to eq('')
126
- end
127
-
128
- it "preserves ANSI codes" do
129
- output = StringIO.new
130
- command = TTY::Command.new(output: output, printer: :quiet)
131
-
132
- out, _ = command.run("echo \e[35mhello\e[0m")
133
-
134
- expect(out.chomp).to eq("\e[35mhello\e[0m")
135
- expect(output.string.chomp).to eq("\e[35mhello\e[0m")
136
- end
137
-
138
- it "logs phased output in one line" do
139
- phased_output = fixtures_path('phased_output')
140
- uuid= 'xxxx'
141
- allow(SecureRandom).to receive(:uuid).and_return(uuid)
142
- output = StringIO.new
143
- cmd = TTY::Command.new(output: output)
144
-
145
- out, err = cmd.run("ruby #{phased_output}")
146
-
147
- expect(out).to eq('.' * 10)
148
- expect(err).to eq('')
149
-
150
- output.rewind
151
- lines = output.readlines
152
- lines.last.gsub!(/\d+\.\d+/, 'x')
153
- expect(lines).to eq([
154
- "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{phased_output}\e[0m\n",
155
- "[\e[32m#{uuid}\e[0m] \t..........\n",
156
- "[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
157
- ])
158
- end
159
- end