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
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require "bundler/gem_tasks"
4
-
5
- FileList['tasks/**/*.rake'].each(&method(:import))
6
-
7
- task default: :spec
8
-
9
- desc 'Run all specs'
10
- task ci: %w[ spec ]
@@ -1,6 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
4
- require 'tty-command'
5
- require 'irb'
6
- IRB.start
data/bin/setup DELETED
@@ -1,6 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cmd = TTY::Command.new
6
-
7
- f = 'file'
8
- if cmd.test("[ -f #{f} ]")
9
- puts "#{f} already exists!"
10
- else
11
- cmd.run :touch, f
12
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cmd = TTY::Command.new
6
-
7
- out, err = cmd.run(:echo, 'hello world!')
8
-
9
- puts "Result: #{out}"
@@ -1,4 +0,0 @@
1
- require_relative "../lib/tty-command"
2
-
3
- cmd = TTY::Command.new(pty: true)
4
- cmd.run("rubocop")
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cmd = TTY::Command.new
6
-
7
- out, _ = cmd.run("env | grep FOO", env: { 'FOO' =>'hello'})
8
-
9
- puts "Result: #{out}"
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'logger'
4
- require_relative '../lib/tty-command'
5
-
6
- logger = Logger.new('dev.log')
7
-
8
- cmd = TTY::Command.new(output: logger, color: false)
9
-
10
- cmd.run(:ls)
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cmd = TTY::Command.new
6
- cmd.run("i=0; while true; do i=$[$i+1]; echo 'hello '$i; sleep 1; done") do |out, err|
7
- if out =~ /.*5.*/
8
- raise ArgumentError, 'BOOM'
9
- end
10
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cmd = TTY::Command.new
6
-
7
- path = File.expand_path("../spec/fixtures/color", __dir__)
8
-
9
- cmd.run(path, pty: true)
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cmd = TTY::Command.new
6
-
7
- out, err = cmd.run("echo 'hello'", :out => :err)
8
-
9
- puts "out: #{out}"
10
- puts "err: #{err}"
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cli = File.expand_path('cli', __dir__)
6
- cmd = TTY::Command.new
7
-
8
- stdin = StringIO.new
9
- stdin.puts "hello"
10
- stdin.puts "world"
11
- stdin.rewind
12
-
13
- out, _ = cmd.run(cli, :in => stdin)
14
-
15
- puts "#{out}"
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cmd = TTY::Command.new
6
-
7
- out, err = cmd.run(:ls, :out => 'ls.log')
8
-
9
- puts "OUT>> #{out}"
10
- puts "ERR>> #{err}"
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pathname'
4
- require_relative '../lib/tty-command'
5
-
6
- cmd = TTY::Command.new
7
- cli = Pathname.new('examples/cli')
8
- out, _ = cmd.run(cli, input: "Piotr\n")
9
-
10
- puts "#{out}"
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cmd = TTY::Command.new
6
-
7
- threads = []
8
- 3.times do |i|
9
- th = Thread.new do
10
- 10.times { cmd.run("echo th#{i}; sleep 0.1") }
11
- end
12
- threads << th
13
- end
14
- threads.each(&:join)
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cmd = TTY::Command.new
6
-
7
- begin
8
- cmd.run("while test 1; do echo 'hello'; sleep 1; done", timeout: 5, signal: :KILL)
9
- rescue TTY::Command::TimeoutExceeded
10
- puts 'BOOM!'
11
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../lib/tty-command'
4
-
5
- cmd = TTY::Command.new
6
-
7
- path = File.expand_path("../spec/fixtures/infinite_input", __dir__)
8
-
9
- range = 1..Float::INFINITY
10
- infinite_input = range.lazy.map { |x| x }.first(10_000).join("\n")
11
-
12
- cmd.run(path, input: infinite_input, timeout: 2)
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'logger'
4
- require_relative '../lib/tty-command'
5
-
6
- logger = Logger.new('dev.log')
7
- cmd = TTY::Command.new
8
-
9
- Thread.new do
10
- 10.times do |i|
11
- sleep 1
12
- if i == 5
13
- logger << "error\n"
14
- else
15
- logger << "hello #{i}\n"
16
- end
17
- end
18
- end
19
-
20
-
21
- cmd.wait('tail -f dev.log', /error/)
@@ -1,78 +0,0 @@
1
- if RUBY_VERSION > '1.9' and (ENV['COVERAGE'] || ENV['TRAVIS'])
2
- require 'simplecov'
3
- require 'coveralls'
4
-
5
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
6
- SimpleCov::Formatter::HTMLFormatter,
7
- Coveralls::SimpleCov::Formatter
8
- ]
9
-
10
- SimpleCov.start do
11
- command_name 'spec'
12
- add_filter 'spec'
13
- end
14
- end
15
-
16
- require 'tty-command'
17
-
18
- module TestHelpers
19
- module Paths
20
- def gem_root
21
- File.expand_path(File.join(File.dirname(__FILE__), ".."))
22
- end
23
-
24
- def dir_path(*args)
25
- path = File.join(gem_root, *args)
26
- FileUtils.mkdir_p(path) unless ::File.exist?(path)
27
- File.realpath(path)
28
- end
29
-
30
- def tmp_path(*args)
31
- File.expand_path(File.join(dir_path('tmp'), *args))
32
- end
33
-
34
- def fixtures_path(*args)
35
- File.expand_path(File.join(dir_path('spec/fixtures'), *args))
36
- end
37
- end
38
-
39
- module Platform
40
- def jruby?
41
- RUBY_PLATFORM == "java"
42
- end
43
- end
44
- end
45
-
46
- RSpec.configure do |config|
47
- config.include(TestHelpers::Paths)
48
- config.include(TestHelpers::Platform)
49
-
50
- config.after(:each, type: :cli) do
51
- FileUtils.rm_rf(tmp_path)
52
- end
53
-
54
- config.expect_with :rspec do |expectations|
55
- expectations.include_chain_clauses_in_custom_matcher_descriptions = true
56
- end
57
-
58
- config.mock_with :rspec do |mocks|
59
- mocks.verify_partial_doubles = true
60
- end
61
-
62
- # Limits the available syntax to the non-monkey patched syntax that is recommended.
63
- config.disable_monkey_patching!
64
-
65
- # This setting enables warnings. It's recommended, but in some cases may
66
- # be too noisy due to issues in dependencies.
67
- config.warnings = true
68
-
69
- if config.files_to_run.one?
70
- config.default_formatter = 'doc'
71
- end
72
-
73
- config.profile_examples = 2
74
-
75
- config.order = :random
76
-
77
- Kernel.srand config.seed
78
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Command, '#run' do
4
- it "encodes output as unicode by default" do
5
- output = StringIO.new
6
- cmd = TTY::Command.new(output: output)
7
- out, _ = cmd.run("echo '昨夜のコンサートは'")
8
-
9
- expect(out.encoding).to eq(Encoding::UTF_8)
10
- # expect(out.chomp).to eq("昨夜のコンサートは")
11
- end
12
-
13
- it "encodes output as binary" do
14
- output = StringIO.new
15
- cmd = TTY::Command.new(output: output)
16
- out, _ = cmd.run("echo '昨夜のコンサートは'", binmode: true)
17
-
18
- expect(out.encoding).to eq(Encoding::BINARY)
19
- #expect(out.chomp).to eq("\xE6\x98\xA8\xE5\xA4\x9C\xE3\x81\xAE\xE3\x82\xB3\xE3\x83\xB3\xE3\x82\xB5\xE3\x83\xBC\xE3\x83\x88\xE3\x81\xAF".force_encoding(Encoding::BINARY))
20
- end
21
-
22
- it "encodes all commands output as binary" do
23
- output = StringIO.new
24
- cmd = TTY::Command.new(output: output, binmode: true)
25
- out, _ = cmd.run("echo 'hello'")
26
-
27
- expect(out.encoding).to eq(Encoding::BINARY)
28
- end
29
- end
@@ -1,152 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Command::Cmd, '::new' do
4
- it "requires at least command argument" do
5
- expect {
6
- TTY::Command::Cmd.new({})
7
- }.to raise_error(ArgumentError, /Cmd requires command argument/)
8
- end
9
-
10
- it "requires non empty command argument" do
11
- expect {
12
- TTY::Command::Cmd.new(nil)
13
- }.to raise_error(ArgumentError, /No command provided/)
14
- end
15
-
16
- it "accepts a command" do
17
- cmd = TTY::Command::Cmd.new(:echo)
18
- expect(cmd.command).to eq('echo')
19
- expect(cmd.argv).to eq([])
20
- expect(cmd.options).to eq({})
21
- expect(cmd.to_command).to eq('echo')
22
- end
23
-
24
- it "accepts a command as heredoc" do
25
- cmd = TTY::Command::Cmd.new <<-EOHEREDOC
26
- if [[ $? -eq 0]]; then
27
- echo "Bash it!"
28
- fi
29
- EOHEREDOC
30
- expect(cmd.argv).to eq([])
31
- expect(cmd.options).to eq({})
32
- expect(cmd.to_command).to eq([
33
- " if [[ $? -eq 0]]; then",
34
- " echo \"Bash it!\"",
35
- " fi\n"
36
- ].join("\n"))
37
- end
38
-
39
- it "accepts command as [cmdname, arg1, ...]" do
40
- cmd = TTY::Command::Cmd.new(:echo, '-n', 'hello')
41
- expect(cmd.command).to eq('echo')
42
- expect(cmd.argv).to eq(['-n', 'hello'])
43
- expect(cmd.to_command).to eq('echo -n hello')
44
- end
45
-
46
- it "accepts command as [[cmdname, argv0], arg1, ...]" do
47
- cmd = TTY::Command::Cmd.new([:echo, '-n'], 'hello')
48
- expect(cmd.command).to eq('echo')
49
- expect(cmd.argv).to eq(['-n', 'hello'])
50
- expect(cmd.to_command).to eq('echo -n hello')
51
- end
52
-
53
- it "accepts command with environment as [cmdname, arg1, ..., opts]" do
54
- cmd = TTY::Command::Cmd.new(:echo, 'hello', env: {foo: 'bar'})
55
- expect(cmd.to_command).to eq(%{( export FOO=\"bar\" ; echo hello )})
56
- end
57
-
58
- it "accepts command with multiple environment keys" do
59
- cmd = TTY::Command::Cmd.new(:echo, 'hello', env: {foo: 'a', bar: 'b'})
60
- expect(cmd.to_command).to eq(%{( export FOO=\"a\" BAR=\"b\" ; echo hello )})
61
- end
62
-
63
- it "accepts command with environemnt string keys" do
64
- cmd = TTY::Command::Cmd.new(:echo, 'hello', env: {'FOO_bar' => 'a', bar: 'b'})
65
- expect(cmd.to_command).to eq(%{( export FOO_bar=\"a\" BAR=\"b\" ; echo hello )})
66
- end
67
-
68
- it "escapes environment values" do
69
- cmd = TTY::Command::Cmd.new(:echo, 'hello', env: {foo: 'abc"def'})
70
- expect(cmd.to_command).to eq(%{( export FOO=\"abc\\\"def\" ; echo hello )})
71
- end
72
-
73
- it "accepts environment as first argument" do
74
- cmd = TTY::Command::Cmd.new({'FOO' => true, 'BAR' => 1}, :echo, 'hello')
75
- expect(cmd.to_command).to eq(%{( export FOO=\"true\" BAR=\"1\" ; echo hello )})
76
- end
77
-
78
- it "runs command in specified directory" do
79
- cmd = TTY::Command::Cmd.new(:echo, 'hello', chdir: '/tmp')
80
- expect(cmd.to_command).to eq("cd /tmp && echo hello")
81
- end
82
-
83
- it "runs command in specified directory with environment" do
84
- cmd = TTY::Command::Cmd.new(:echo, 'hello', chdir: '/tmp', env: {foo: 'bar'})
85
- expect(cmd.to_command).to eq(%{cd /tmp && ( export FOO=\"bar\" ; echo hello )})
86
- end
87
-
88
- it "runs command as a user" do
89
- cmd = TTY::Command::Cmd.new(:echo, 'hello', user: 'piotrmurach')
90
- expect(cmd.to_command).to eq("sudo -u piotrmurach -- sh -c 'echo hello'")
91
- end
92
-
93
- it "runs command as a group" do
94
- cmd = TTY::Command::Cmd.new(:echo, 'hello', group: 'devs')
95
- expect(cmd.to_command).to eq("sg devs -c \\\"echo hello\\\"")
96
- end
97
-
98
- it "runs command as a user in a group" do
99
- cmd = TTY::Command::Cmd.new(:echo, 'hello', user: 'piotrmurach', group: 'devs')
100
- expect(cmd.to_command).to eq("sudo -u piotrmurach -- sh -c 'sg devs -c \\\"echo hello\\\"'")
101
- end
102
-
103
- it "runs command with umask" do
104
- cmd = TTY::Command::Cmd.new(:echo, 'hello', umask: '077')
105
- expect(cmd.to_command).to eq("umask 077 && echo hello")
106
- end
107
-
108
- it "runs command with umask, chdir" do
109
- cmd = TTY::Command::Cmd.new(:echo, 'hello', umask: '077', chdir: '/tmp')
110
- expect(cmd.to_command).to eq("cd /tmp && umask 077 && echo hello")
111
- end
112
-
113
- it "runs command with umask, chdir & user" do
114
- cmd = TTY::Command::Cmd.new(:echo, 'hello', umask: '077', chdir: '/tmp', user: 'piotrmurach')
115
- expect(cmd.to_command).to eq("cd /tmp && umask 077 && sudo -u piotrmurach -- sh -c 'echo hello'")
116
- end
117
-
118
- it "runs command with umask, user, chdir and env" do
119
- cmd = TTY::Command::Cmd.new(:echo, 'hello', umask: '077', chdir: '/tmp', user: 'piotrmurach', env: {foo: 'bar'})
120
- expect(cmd.to_command).to eq(%{cd /tmp && umask 077 && ( export FOO=\"bar\" ; sudo -u piotrmurach FOO=\"bar\" -- sh -c 'echo hello' )})
121
- end
122
-
123
- it "provides unique identifier" do
124
- cmd = TTY::Command::Cmd.new(:echo, 'hello')
125
- expect(cmd.uuid).to match(/^\w{8}$/)
126
- end
127
-
128
- it "converts command to hash" do
129
- cmd = TTY::Command::Cmd.new(:echo, 'hello')
130
- expect(cmd.to_hash).to include({
131
- command: 'echo',
132
- argv: ['hello']
133
- })
134
- end
135
-
136
- it "escapes arguments that need escaping" do
137
- cmd = TTY::Command::Cmd.new(:echo, 'hello world')
138
- expect(cmd.to_hash).to include({
139
- command: 'echo',
140
- argv: ["hello\\ world"]
141
- })
142
- end
143
-
144
- it "escapes special characters in split arguments" do
145
- args = %w(git for-each-ref --format='%(refname)' refs/heads/)
146
- cmd = TTY::Command::Cmd.new(*args)
147
- expect(cmd.to_hash).to include({
148
- command: 'git',
149
- argv: ["for-each-ref", "--format\\=\\'\\%\\(refname\\)\\'", "refs/heads/"]
150
- })
151
- end
152
- end