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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/LICENSE.txt +1 -1
- data/README.md +44 -44
- data/lib/tty-command.rb +1 -1
- data/lib/tty/command.rb +18 -18
- data/lib/tty/command/child_process.rb +9 -9
- data/lib/tty/command/cmd.rb +11 -11
- data/lib/tty/command/dry_runner.rb +3 -3
- data/lib/tty/command/exit_error.rb +1 -1
- data/lib/tty/command/printers/abstract.rb +8 -10
- data/lib/tty/command/printers/null.rb +1 -4
- data/lib/tty/command/printers/pretty.rb +10 -15
- data/lib/tty/command/printers/progress.rb +3 -8
- data/lib/tty/command/printers/quiet.rb +3 -7
- data/lib/tty/command/process_runner.rb +25 -17
- data/lib/tty/command/truncator.rb +3 -3
- data/lib/tty/command/version.rb +1 -1
- metadata +20 -68
- data/Rakefile +0 -10
- data/bin/console +0 -6
- data/bin/setup +0 -6
- data/examples/bash.rb +0 -12
- data/examples/basic.rb +0 -9
- data/examples/buffer.rb +0 -4
- data/examples/env.rb +0 -9
- data/examples/logger.rb +0 -10
- data/examples/output.rb +0 -10
- data/examples/pty.rb +0 -9
- data/examples/redirect_stderr.rb +0 -10
- data/examples/redirect_stdin.rb +0 -15
- data/examples/redirect_stdout.rb +0 -10
- data/examples/stdin_input.rb +0 -10
- data/examples/threaded.rb +0 -14
- data/examples/timeout.rb +0 -11
- data/examples/timeout_input.rb +0 -12
- data/examples/wait.rb +0 -21
- data/spec/spec_helper.rb +0 -78
- data/spec/unit/binmode_spec.rb +0 -29
- data/spec/unit/cmd_spec.rb +0 -152
- data/spec/unit/dry_run_spec.rb +0 -42
- data/spec/unit/exit_error_spec.rb +0 -25
- data/spec/unit/input_spec.rb +0 -13
- data/spec/unit/output_spec.rb +0 -25
- data/spec/unit/printer_spec.rb +0 -50
- data/spec/unit/printers/custom_spec.rb +0 -48
- data/spec/unit/printers/null_spec.rb +0 -36
- data/spec/unit/printers/pretty_spec.rb +0 -172
- data/spec/unit/printers/progress_spec.rb +0 -45
- data/spec/unit/printers/quiet_spec.rb +0 -71
- data/spec/unit/pty_spec.rb +0 -62
- data/spec/unit/redirect_spec.rb +0 -103
- data/spec/unit/result_spec.rb +0 -64
- data/spec/unit/ruby_spec.rb +0 -20
- data/spec/unit/run_spec.rb +0 -159
- data/spec/unit/test_spec.rb +0 -11
- data/spec/unit/timeout_spec.rb +0 -36
- data/spec/unit/truncator_spec.rb +0 -73
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-command.gemspec +0 -30
data/Rakefile
DELETED
data/bin/console
DELETED
data/bin/setup
DELETED
data/examples/bash.rb
DELETED
data/examples/basic.rb
DELETED
data/examples/buffer.rb
DELETED
data/examples/env.rb
DELETED
data/examples/logger.rb
DELETED
data/examples/output.rb
DELETED
data/examples/pty.rb
DELETED
data/examples/redirect_stderr.rb
DELETED
data/examples/redirect_stdin.rb
DELETED
@@ -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}"
|
data/examples/redirect_stdout.rb
DELETED
data/examples/stdin_input.rb
DELETED
data/examples/threaded.rb
DELETED
@@ -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)
|
data/examples/timeout.rb
DELETED
data/examples/timeout_input.rb
DELETED
@@ -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)
|
data/examples/wait.rb
DELETED
@@ -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/)
|
data/spec/spec_helper.rb
DELETED
@@ -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
|
data/spec/unit/binmode_spec.rb
DELETED
@@ -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
|
data/spec/unit/cmd_spec.rb
DELETED
@@ -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
|