command_kit 0.4.1 → 0.5.1

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 (131) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +1 -1
  3. data/.rubocop.yml +7 -1
  4. data/ChangeLog.md +11 -0
  5. data/Gemfile +2 -0
  6. data/README.md +3 -0
  7. data/Rakefile +2 -0
  8. data/command_kit.gemspec +8 -6
  9. data/lib/command_kit/arguments/argument.rb +3 -1
  10. data/lib/command_kit/arguments/argument_value.rb +2 -0
  11. data/lib/command_kit/arguments.rb +5 -5
  12. data/lib/command_kit/bug_report.rb +4 -2
  13. data/lib/command_kit/colors.rb +2 -2
  14. data/lib/command_kit/command.rb +13 -11
  15. data/lib/command_kit/command_name.rb +3 -1
  16. data/lib/command_kit/commands/auto_load/subcommand.rb +3 -1
  17. data/lib/command_kit/commands/auto_load.rb +3 -3
  18. data/lib/command_kit/commands/auto_require.rb +5 -3
  19. data/lib/command_kit/commands/command.rb +4 -2
  20. data/lib/command_kit/commands/help.rb +2 -2
  21. data/lib/command_kit/commands/parent_command.rb +2 -0
  22. data/lib/command_kit/commands/subcommand.rb +2 -0
  23. data/lib/command_kit/commands.rb +8 -8
  24. data/lib/command_kit/completion/install.rb +277 -0
  25. data/lib/command_kit/description.rb +3 -1
  26. data/lib/command_kit/edit.rb +1 -1
  27. data/lib/command_kit/env/home.rb +1 -1
  28. data/lib/command_kit/env/path.rb +1 -1
  29. data/lib/command_kit/env/prefix.rb +41 -0
  30. data/lib/command_kit/env/shell.rb +58 -0
  31. data/lib/command_kit/env.rb +2 -0
  32. data/lib/command_kit/examples.rb +4 -2
  33. data/lib/command_kit/exception_handler.rb +4 -2
  34. data/lib/command_kit/file_utils.rb +2 -0
  35. data/lib/command_kit/help/man.rb +4 -4
  36. data/lib/command_kit/help.rb +2 -0
  37. data/lib/command_kit/interactive.rb +1 -1
  38. data/lib/command_kit/main.rb +2 -0
  39. data/lib/command_kit/open_app.rb +4 -2
  40. data/lib/command_kit/options/option.rb +3 -1
  41. data/lib/command_kit/options/option_value.rb +2 -2
  42. data/lib/command_kit/options/parser.rb +3 -3
  43. data/lib/command_kit/options/quiet.rb +3 -1
  44. data/lib/command_kit/options/verbose.rb +3 -1
  45. data/lib/command_kit/options/version.rb +3 -1
  46. data/lib/command_kit/options.rb +5 -3
  47. data/lib/command_kit/os/linux.rb +3 -1
  48. data/lib/command_kit/os.rb +2 -0
  49. data/lib/command_kit/package_manager.rb +6 -4
  50. data/lib/command_kit/pager.rb +4 -4
  51. data/lib/command_kit/printing/fields.rb +3 -1
  52. data/lib/command_kit/printing/indent.rb +2 -0
  53. data/lib/command_kit/printing/lists.rb +1 -1
  54. data/lib/command_kit/printing/tables/border_style.rb +2 -0
  55. data/lib/command_kit/printing/tables/row_builder.rb +3 -1
  56. data/lib/command_kit/printing/tables/style.rb +1 -1
  57. data/lib/command_kit/printing/tables/table_builder.rb +3 -1
  58. data/lib/command_kit/printing/tables.rb +4 -4
  59. data/lib/command_kit/printing.rb +1 -1
  60. data/lib/command_kit/program_name.rb +2 -0
  61. data/lib/command_kit/stdio.rb +2 -0
  62. data/lib/command_kit/sudo.rb +1 -1
  63. data/lib/command_kit/terminal.rb +4 -2
  64. data/lib/command_kit/usage.rb +4 -2
  65. data/lib/command_kit/version.rb +3 -1
  66. data/lib/command_kit/xdg.rb +4 -2
  67. data/lib/command_kit.rb +4 -2
  68. metadata +5 -65
  69. data/spec/arguments/argument_spec.rb +0 -133
  70. data/spec/arguments/argument_value_spec.rb +0 -66
  71. data/spec/arguments_spec.rb +0 -279
  72. data/spec/bug_report_spec.rb +0 -266
  73. data/spec/colors_spec.rb +0 -771
  74. data/spec/command_kit_spec.rb +0 -8
  75. data/spec/command_name_spec.rb +0 -130
  76. data/spec/command_spec.rb +0 -123
  77. data/spec/commands/auto_load/subcommand_spec.rb +0 -82
  78. data/spec/commands/auto_load_spec.rb +0 -159
  79. data/spec/commands/auto_require_spec.rb +0 -142
  80. data/spec/commands/fixtures/test_auto_load/cli/commands/test1.rb +0 -10
  81. data/spec/commands/fixtures/test_auto_load/cli/commands/test2.rb +0 -10
  82. data/spec/commands/fixtures/test_auto_require/lib/test_auto_require/cli/commands/test1.rb +0 -10
  83. data/spec/commands/help_spec.rb +0 -66
  84. data/spec/commands/parent_command_spec.rb +0 -40
  85. data/spec/commands/subcommand_spec.rb +0 -99
  86. data/spec/commands_spec.rb +0 -865
  87. data/spec/description_spec.rb +0 -179
  88. data/spec/edit_spec.rb +0 -72
  89. data/spec/env/home_spec.rb +0 -46
  90. data/spec/env/path_spec.rb +0 -84
  91. data/spec/env_spec.rb +0 -123
  92. data/spec/examples_spec.rb +0 -211
  93. data/spec/exception_handler_spec.rb +0 -103
  94. data/spec/file_utils_spec.rb +0 -59
  95. data/spec/fixtures/template.erb +0 -5
  96. data/spec/help/man_spec.rb +0 -345
  97. data/spec/help_spec.rb +0 -94
  98. data/spec/inflector_spec.rb +0 -166
  99. data/spec/interactive_spec.rb +0 -415
  100. data/spec/main_spec.rb +0 -179
  101. data/spec/man_spec.rb +0 -46
  102. data/spec/open_app_spec.rb +0 -85
  103. data/spec/options/option_spec.rb +0 -343
  104. data/spec/options/option_value_spec.rb +0 -171
  105. data/spec/options/parser_spec.rb +0 -274
  106. data/spec/options/quiet_spec.rb +0 -51
  107. data/spec/options/verbose_spec.rb +0 -51
  108. data/spec/options/version_spec.rb +0 -146
  109. data/spec/options_spec.rb +0 -465
  110. data/spec/os/linux_spec.rb +0 -164
  111. data/spec/os_spec.rb +0 -233
  112. data/spec/package_manager_spec.rb +0 -806
  113. data/spec/pager_spec.rb +0 -217
  114. data/spec/printing/fields_spec.rb +0 -167
  115. data/spec/printing/indent_spec.rb +0 -132
  116. data/spec/printing/lists_spec.rb +0 -99
  117. data/spec/printing/tables/border_style.rb +0 -43
  118. data/spec/printing/tables/cell_builer_spec.rb +0 -135
  119. data/spec/printing/tables/row_builder_spec.rb +0 -165
  120. data/spec/printing/tables/style_spec.rb +0 -377
  121. data/spec/printing/tables/table_builder_spec.rb +0 -252
  122. data/spec/printing/tables/table_formatter_spec.rb +0 -1190
  123. data/spec/printing/tables_spec.rb +0 -1069
  124. data/spec/printing_spec.rb +0 -106
  125. data/spec/program_name_spec.rb +0 -70
  126. data/spec/spec_helper.rb +0 -3
  127. data/spec/stdio_spec.rb +0 -264
  128. data/spec/sudo_spec.rb +0 -51
  129. data/spec/terminal_spec.rb +0 -231
  130. data/spec/usage_spec.rb +0 -237
  131. data/spec/xdg_spec.rb +0 -191
@@ -1,106 +0,0 @@
1
- require 'spec_helper'
2
- require 'command_kit/printing'
3
- require 'command_kit/command_name'
4
-
5
- require 'stringio'
6
-
7
- describe CommandKit::Printing do
8
- module TestPrinting
9
- class TestCmd
10
-
11
- include CommandKit::Printing
12
-
13
- end
14
- end
15
-
16
- let(:command_class) { TestPrinting::TestCmd }
17
- subject { command_class.new }
18
-
19
- describe "EOL" do
20
- subject { described_class::EOL }
21
-
22
- it "must equal $/" do
23
- expect(subject).to eq($/)
24
- end
25
- end
26
-
27
- describe ".included" do
28
- subject { command_class }
29
-
30
- it { expect(command_class).to include(CommandKit::Stdio) }
31
- end
32
-
33
- let(:nl) { $/ }
34
-
35
- describe "#print_error" do
36
- let(:message) { "oh no!" }
37
-
38
- it "must print the error message to stderr" do
39
- expect {
40
- subject.print_error(message)
41
- }.to output("#{message}#{nl}").to_stderr
42
- end
43
-
44
- context "and when CommandKit::CommandName is included" do
45
- module TestPrinting
46
- class TestCmdWithCommandName
47
-
48
- include CommandKit::CommandName
49
- include CommandKit::Printing
50
-
51
- command_name 'foo'
52
-
53
- end
54
- end
55
-
56
- let(:command_class) { TestPrinting::TestCmdWithCommandName }
57
-
58
- it "must print the command_name and the error message" do
59
- expect {
60
- subject.print_error(message)
61
- }.to output("#{subject.command_name}: #{message}#{nl}").to_stderr
62
- end
63
- end
64
- end
65
-
66
- describe "#print_exception" do
67
- let(:message) { "error!" }
68
- let(:backtrace) do
69
- [
70
- "/path/to/test1.rb:1 in `test1'",
71
- "/path/to/test2.rb:2 in `test2'",
72
- "/path/to/test3.rb:3 in `test3'",
73
- "/path/to/test4.rb:4 in `test4'"
74
- ]
75
- end
76
- let(:exception) do
77
- error = RuntimeError.new(message)
78
- error.set_backtrace(backtrace)
79
- error
80
- end
81
-
82
- subject { command_class.new(stderr: StringIO.new) }
83
-
84
- context "when stderr is a TTY" do
85
- before { expect(subject.stderr).to receive(:tty?).and_return(true) }
86
-
87
- it "must print a highlighted exception" do
88
- subject.print_exception(exception)
89
-
90
- expect(subject.stderr.string).to eq(
91
- exception.full_message(highlight: true)
92
- )
93
- end
94
- end
95
-
96
- context "when stderr is not a TTY" do
97
- it "must not highlight the exception" do
98
- subject.print_exception(exception)
99
-
100
- expect(subject.stderr.string).to eq(
101
- exception.full_message(highlight: false)
102
- )
103
- end
104
- end
105
- end
106
- end
@@ -1,70 +0,0 @@
1
- require 'spec_helper'
2
- require 'command_kit/program_name'
3
-
4
- describe CommandKit::ProgramName do
5
- module TestProgramName
6
- class TestCmd
7
- include CommandKit::ProgramName
8
- end
9
- end
10
-
11
- let(:command_class) { TestProgramName::TestCmd }
12
- let(:program_name) { 'foo' }
13
-
14
- before do
15
- @original_program_name = $PROGRAM_NAME
16
- $PROGRAM_NAME = program_name
17
- end
18
-
19
- describe ".program_name" do
20
- subject { command_class }
21
-
22
- it "must return $PROGRAM_NAME" do
23
- expect(subject.program_name).to eq(program_name)
24
- end
25
-
26
- context "when $PROGRAM_NAME is '-e'" do
27
- let(:program_name) { '-e' }
28
-
29
- it "must return nil" do
30
- expect(subject.program_name).to be(nil)
31
- end
32
- end
33
-
34
- context "when $PROGRAM_NAME is 'irb'" do
35
- let(:program_name) { 'irb' }
36
-
37
- it "must return nil" do
38
- expect(subject.program_name).to be(nil)
39
- end
40
- end
41
-
42
- context "when $PROGRAM_NAME is 'rspec'" do
43
- let(:program_name) { 'rspec' }
44
-
45
- it "must return nil" do
46
- expect(subject.program_name).to be(nil)
47
- end
48
- end
49
- end
50
-
51
- describe "#program_name" do
52
- subject { command_class.new }
53
-
54
- it "should be the same as .program_name" do
55
- expect(subject.program_name).to eq(command_class.program_name)
56
- end
57
- end
58
-
59
- describe "#command_name" do
60
- subject { command_class.new }
61
-
62
- it "should be the same as #program_name" do
63
- expect(subject.command_name).to eq(subject.program_name)
64
- end
65
- end
66
-
67
- after do
68
- $PROGRAM_NAME = @original_program_name
69
- end
70
- end
data/spec/spec_helper.rb DELETED
@@ -1,3 +0,0 @@
1
- require 'rspec'
2
- require 'simplecov'
3
- SimpleCov.start
data/spec/stdio_spec.rb DELETED
@@ -1,264 +0,0 @@
1
- require 'spec_helper'
2
- require 'command_kit/stdio'
3
-
4
- describe CommandKit::Stdio do
5
- module TestStdio
6
- class TestCommand
7
- include CommandKit::Stdio
8
- end
9
- end
10
-
11
- let(:command_class) { TestStdio::TestCommand }
12
-
13
- describe "#initialize" do
14
- module TestStdio
15
- class TestCommandWithInitialize
16
- include CommandKit::Stdio
17
-
18
- attr_reader :var
19
-
20
- def initialize(**kwargs)
21
- super(**kwargs)
22
-
23
- @var = 'foo'
24
- end
25
- end
26
-
27
- class TestCommandWithInitializeAndKeywordArgs
28
- include CommandKit::Stdio
29
-
30
- attr_reader :var
31
-
32
- def initialize(var: "foo", **kwargs)
33
- super(**kwargs)
34
-
35
- @var = var
36
- end
37
- end
38
- end
39
-
40
- context "when given no arguments" do
41
- subject { command_class.new() }
42
-
43
- it "must initialize #stdin to $stdin" do
44
- expect(subject.stdin).to be($stdin)
45
- end
46
-
47
- it "must initialize #stdout to $stdout" do
48
- expect(subject.stdout).to be($stdout)
49
- end
50
-
51
- it "must initialize #stderr to $stderr" do
52
- expect(subject.stderr).to be($stderr)
53
- end
54
-
55
- context "and the including class defines it's own #initialize method" do
56
- let(:command_class) { TestStdio::TestCommandWithInitialize }
57
-
58
- it "must initialize #stdin to $stdin" do
59
- expect(subject.stdin).to be($stdin)
60
- end
61
-
62
- it "must initialize #stdout to $stdout" do
63
- expect(subject.stdout).to be($stdout)
64
- end
65
-
66
- it "must initialize #stderr to $stderr" do
67
- expect(subject.stderr).to be($stderr)
68
- end
69
-
70
- it "must also call the class'es #initialize method" do
71
- expect(subject.var).to eq('foo')
72
- end
73
-
74
- context "and it accepts keyword arguments" do
75
- let(:command_class) { TestStdio::TestCommandWithInitializeAndKeywordArgs }
76
- let(:var) { 'custom value' }
77
-
78
- subject { command_class.new(var: var) }
79
-
80
- it "must initialize #stdin to $stdin" do
81
- expect(subject.stdin).to be($stdin)
82
- end
83
-
84
- it "must initialize #stdout to $stdout" do
85
- expect(subject.stdout).to be($stdout)
86
- end
87
-
88
- it "must initialize #stderr to $stderr" do
89
- expect(subject.stderr).to be($stderr)
90
- end
91
-
92
- it "must also call the class'es #initialize method with any additional keyword arguments" do
93
- expect(subject.var).to eq(var)
94
- end
95
- end
96
- end
97
- end
98
-
99
- context "when given a custom env: value" do
100
- let(:custom_stdin) { StringIO.new }
101
- let(:custom_stdout) { StringIO.new }
102
- let(:custom_stderr) { StringIO.new }
103
-
104
- subject do
105
- command_class.new(
106
- stdin: custom_stdin,
107
- stdout: custom_stdout,
108
- stderr: custom_stderr
109
- )
110
- end
111
-
112
- it "must initialize #stdin to the stdin: value" do
113
- expect(subject.stdin).to eq(custom_stdin)
114
- end
115
-
116
- it "must initialize #stdout to the stdout: value" do
117
- expect(subject.stdout).to eq(custom_stdout)
118
- end
119
-
120
- it "must initialize #stderr to the stderr: value" do
121
- expect(subject.stderr).to eq(custom_stderr)
122
- end
123
-
124
- context "and the including class defines it's own #initialize method" do
125
- let(:command_class) { TestStdio::TestCommandWithInitialize }
126
-
127
- it "must initialize #stdin to the stdin: value" do
128
- expect(subject.stdin).to eq(custom_stdin)
129
- end
130
-
131
- it "must initialize #stdout to the stdout: value" do
132
- expect(subject.stdout).to eq(custom_stdout)
133
- end
134
-
135
- it "must initialize #stderr to the stderr: value" do
136
- expect(subject.stderr).to eq(custom_stderr)
137
- end
138
-
139
- it "must also call the class'es #initialize method" do
140
- expect(subject.var).to eq('foo')
141
- end
142
-
143
- context "and it accepts keyword arguments" do
144
- let(:command_class) { TestStdio::TestCommandWithInitializeAndKeywordArgs }
145
- let(:var) { 'custom value' }
146
-
147
- subject do
148
- command_class.new(
149
- stdin: custom_stdin,
150
- stdout: custom_stdout,
151
- stderr: custom_stderr,
152
- var: var
153
- )
154
- end
155
-
156
- it "must initialize #stdin to the stdin: value" do
157
- expect(subject.stdin).to eq(custom_stdin)
158
- end
159
-
160
- it "must initialize #stdout to the stdout: value" do
161
- expect(subject.stdout).to eq(custom_stdout)
162
- end
163
-
164
- it "must initialize #stderr to the stderr: value" do
165
- expect(subject.stderr).to eq(custom_stderr)
166
- end
167
-
168
- it "must also call the class'es #initialize method with any additional keyword arguments" do
169
- expect(subject.var).to eq(var)
170
- end
171
- end
172
- end
173
- end
174
- end
175
-
176
- describe "#stdin" do
177
- let(:command_class) { TestStdio::TestCommand }
178
- let(:stdin) { StringIO.new }
179
-
180
- subject { command_class.new(stdin: stdin) }
181
-
182
- it "must return the initialized stdin: value" do
183
- expect(subject.stdin).to eq(stdin)
184
- end
185
- end
186
-
187
- describe "#stdout" do
188
- let(:command_class) { TestStdio::TestCommand }
189
- let(:stdout) { StringIO.new }
190
-
191
- subject { command_class.new(stdout: stdout) }
192
-
193
- it "must return the initialized stdout: value" do
194
- expect(subject.stdout).to eq(stdout)
195
- end
196
- end
197
-
198
- describe "#stderr" do
199
- let(:command_class) { TestStdio::TestCommand }
200
- let(:stderr) { StringIO.new }
201
-
202
- subject { command_class.new(stderr: stderr) }
203
-
204
- it "must return the initialized stderr: value" do
205
- expect(subject.stderr).to eq(stderr)
206
- end
207
- end
208
-
209
- [:gets, :readline, :readlines].each do |method|
210
- describe "##{method}" do
211
- let(:stdin) { StringIO.new }
212
- let(:args) { [double(:arg1), double(:arg2)] }
213
- let(:ret) { double(:return_value) }
214
-
215
- subject { command_class.new(stdin: stdin) }
216
-
217
- it "must pass all arguments ot stdin.#{method}" do
218
- expect(stdin).to receive(method).with(*args).and_return(ret)
219
-
220
- expect(subject.send(method,*args)).to be(ret)
221
- end
222
- end
223
- end
224
-
225
- [:putc, :puts, :print, :printf].each do |method|
226
- describe "##{method}" do
227
- let(:stdout) { StringIO.new }
228
- let(:args) { [double(:arg1), double(:arg2)] }
229
- let(:ret) { double(:return_value) }
230
-
231
- subject { command_class.new(stdout: stdout) }
232
-
233
- it "must pass all arguments ot stdout.#{method}" do
234
- expect(stdout).to receive(method).with(*args).and_return(ret)
235
-
236
- expect(subject.send(method,*args)).to be(ret)
237
- end
238
- end
239
- end
240
-
241
- describe "#abort" do
242
- let(:stderr) { StringIO.new }
243
- subject { command_class.new(stderr: stderr) }
244
-
245
- context "when given an abort message" do
246
- let(:message) { 'fail' }
247
-
248
- it "must print the message to stderr and exit with 1" do
249
- expect(subject.stderr).to receive(:puts).with(message)
250
- expect(subject).to receive(:exit).with(1)
251
-
252
- subject.abort(message)
253
- end
254
- end
255
-
256
- context "when called without any arguments" do
257
- it "must exit with 1" do
258
- expect(subject).to receive(:exit).with(1)
259
-
260
- subject.abort
261
- end
262
- end
263
- end
264
- end
data/spec/sudo_spec.rb DELETED
@@ -1,51 +0,0 @@
1
- require 'spec_helper'
2
- require 'command_kit/sudo'
3
-
4
- describe CommandKit::Sudo do
5
- module TestSudo
6
- class TestCommand
7
- include CommandKit::Sudo
8
- end
9
- end
10
-
11
- let(:command_class) { TestSudo::TestCommand }
12
- subject { command_class.new }
13
-
14
- describe "#sudo" do
15
- let(:command) { "ls" }
16
- let(:arguments) { ["-la", "~root"] }
17
- let(:status) { double(:status) }
18
-
19
- context "on UNIX" do
20
- context "when the UID is 0" do
21
- before { allow(Process).to receive(:uid).and_return(0) }
22
-
23
- it "must execute the command without sudo" do
24
- expect(subject).to receive(:system).with(command,*arguments).and_return(status)
25
-
26
- expect(subject.sudo(command,*arguments)).to be(status)
27
- end
28
- end
29
-
30
- context "when the UID is not 0" do
31
- before { allow(Process).to receive(:uid).and_return(1000) }
32
-
33
- it "must execute the command with 'sudo ...'" do
34
- expect(subject).to receive(:system).with('sudo',command,*arguments).and_return(status)
35
-
36
- expect(subject.sudo(command,*arguments)).to be(status)
37
- end
38
- end
39
- end
40
-
41
- context "on Windows" do
42
- subject { command_class.new(os: :windows) }
43
-
44
- it "must execute the command with 'runas /user:administrator ...'" do
45
- expect(subject).to receive(:system).with('runas','/user:administrator',command,*arguments).and_return(status)
46
-
47
- expect(subject.sudo(command,*arguments)).to be(status)
48
- end
49
- end
50
- end
51
- end