command_kit 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog.md +6 -0
  3. data/README.md +3 -0
  4. data/command_kit.gemspec +7 -2
  5. data/lib/command_kit/completion/install.rb +276 -0
  6. data/lib/command_kit/env/prefix.rb +41 -0
  7. data/lib/command_kit/env/shell.rb +58 -0
  8. data/lib/command_kit/version.rb +1 -1
  9. metadata +4 -64
  10. data/spec/arguments/argument_spec.rb +0 -133
  11. data/spec/arguments/argument_value_spec.rb +0 -66
  12. data/spec/arguments_spec.rb +0 -279
  13. data/spec/bug_report_spec.rb +0 -266
  14. data/spec/colors_spec.rb +0 -771
  15. data/spec/command_kit_spec.rb +0 -8
  16. data/spec/command_name_spec.rb +0 -130
  17. data/spec/command_spec.rb +0 -123
  18. data/spec/commands/auto_load/subcommand_spec.rb +0 -82
  19. data/spec/commands/auto_load_spec.rb +0 -159
  20. data/spec/commands/auto_require_spec.rb +0 -142
  21. data/spec/commands/fixtures/test_auto_load/cli/commands/test1.rb +0 -10
  22. data/spec/commands/fixtures/test_auto_load/cli/commands/test2.rb +0 -10
  23. data/spec/commands/fixtures/test_auto_require/lib/test_auto_require/cli/commands/test1.rb +0 -10
  24. data/spec/commands/help_spec.rb +0 -66
  25. data/spec/commands/parent_command_spec.rb +0 -40
  26. data/spec/commands/subcommand_spec.rb +0 -99
  27. data/spec/commands_spec.rb +0 -865
  28. data/spec/description_spec.rb +0 -179
  29. data/spec/edit_spec.rb +0 -72
  30. data/spec/env/home_spec.rb +0 -46
  31. data/spec/env/path_spec.rb +0 -84
  32. data/spec/env_spec.rb +0 -123
  33. data/spec/examples_spec.rb +0 -211
  34. data/spec/exception_handler_spec.rb +0 -103
  35. data/spec/file_utils_spec.rb +0 -59
  36. data/spec/fixtures/template.erb +0 -5
  37. data/spec/help/man_spec.rb +0 -345
  38. data/spec/help_spec.rb +0 -94
  39. data/spec/inflector_spec.rb +0 -166
  40. data/spec/interactive_spec.rb +0 -415
  41. data/spec/main_spec.rb +0 -179
  42. data/spec/man_spec.rb +0 -46
  43. data/spec/open_app_spec.rb +0 -85
  44. data/spec/options/option_spec.rb +0 -343
  45. data/spec/options/option_value_spec.rb +0 -171
  46. data/spec/options/parser_spec.rb +0 -274
  47. data/spec/options/quiet_spec.rb +0 -51
  48. data/spec/options/verbose_spec.rb +0 -51
  49. data/spec/options/version_spec.rb +0 -146
  50. data/spec/options_spec.rb +0 -465
  51. data/spec/os/linux_spec.rb +0 -164
  52. data/spec/os_spec.rb +0 -233
  53. data/spec/package_manager_spec.rb +0 -806
  54. data/spec/pager_spec.rb +0 -217
  55. data/spec/printing/fields_spec.rb +0 -167
  56. data/spec/printing/indent_spec.rb +0 -132
  57. data/spec/printing/lists_spec.rb +0 -99
  58. data/spec/printing/tables/border_style.rb +0 -43
  59. data/spec/printing/tables/cell_builer_spec.rb +0 -135
  60. data/spec/printing/tables/row_builder_spec.rb +0 -165
  61. data/spec/printing/tables/style_spec.rb +0 -377
  62. data/spec/printing/tables/table_builder_spec.rb +0 -252
  63. data/spec/printing/tables/table_formatter_spec.rb +0 -1190
  64. data/spec/printing/tables_spec.rb +0 -1069
  65. data/spec/printing_spec.rb +0 -106
  66. data/spec/program_name_spec.rb +0 -70
  67. data/spec/spec_helper.rb +0 -3
  68. data/spec/stdio_spec.rb +0 -264
  69. data/spec/sudo_spec.rb +0 -51
  70. data/spec/terminal_spec.rb +0 -231
  71. data/spec/usage_spec.rb +0 -237
  72. data/spec/xdg_spec.rb +0 -191
@@ -1,103 +0,0 @@
1
- require 'spec_helper'
2
- require 'command_kit/exception_handler'
3
-
4
- describe CommandKit::ExceptionHandler do
5
- module TestExceptionHandler
6
- class TestIncludedCmd
7
-
8
- include CommandKit::Main
9
- include CommandKit::ExceptionHandler
10
-
11
- end
12
- end
13
-
14
- let(:command_class) { TestExceptionHandler::TestIncludedCmd }
15
- subject { command_class.new }
16
-
17
- describe ".included" do
18
- it "must include CommandKit::Printing" do
19
- expect(command_class).to be_include(CommandKit::Printing)
20
- end
21
- end
22
-
23
- describe "#main" do
24
- context "when the #main method rescues an Interrupt" do
25
- module TestExceptionHandler
26
- class RaisesInterruptCmd
27
-
28
- include CommandKit::Main
29
- include CommandKit::ExceptionHandler
30
-
31
- def run
32
- raise(Interrupt.new)
33
- end
34
-
35
- end
36
- end
37
-
38
- let(:command_class) { TestExceptionHandler::RaisesInterruptCmd }
39
- subject { command_class.new }
40
-
41
- it "must re-raise the Interrupt exception" do
42
- expect { subject.main }.to raise_error(Interrupt)
43
- end
44
- end
45
-
46
- context "when the #main method raises an Errno::EPIPE" do
47
- module TestExceptionHandler
48
- class RaisesErrnoEPIPECmd
49
-
50
- include CommandKit::Main
51
- include CommandKit::ExceptionHandler
52
-
53
- def run
54
- raise(Errno::EPIPE.new)
55
- end
56
-
57
- end
58
- end
59
-
60
- let(:command_class) { TestExceptionHandler::RaisesErrnoEPIPECmd }
61
- subject { command_class.new }
62
-
63
- it "must re-raise the Errno::EPIPE exception" do
64
- expect { subject.main }.to raise_error(Errno::EPIPE)
65
- end
66
- end
67
-
68
- context "when the #main method raises another Exception" do
69
- module TestExceptionHandler
70
- class RaisesExceptionCmd
71
-
72
- include CommandKit::Main
73
- include CommandKit::ExceptionHandler
74
-
75
- def run
76
- raise("error")
77
- end
78
-
79
- end
80
- end
81
-
82
- let(:command_class) { TestExceptionHandler::RaisesExceptionCmd }
83
- subject { command_class.new }
84
-
85
- it "must rescue the exception and call #on_exception" do
86
- expect(subject).to receive(:on_exception).with(RuntimeError)
87
-
88
- subject.main
89
- end
90
- end
91
- end
92
-
93
- describe "#on_exception" do
94
- let(:error) { RuntimeError.new('error') }
95
-
96
- it "must call #print_exception and exit with 1" do
97
- expect(subject).to receive(:print_exception).with(error)
98
- expect(subject).to receive(:exit).with(1)
99
-
100
- subject.on_exception(error)
101
- end
102
- end
103
- end
@@ -1,59 +0,0 @@
1
- require 'spec_helper'
2
- require 'command_kit/file_utils'
3
- require 'tempfile'
4
-
5
- describe CommandKit::FileUtils do
6
- module TestFileUtils
7
- class Command
8
- include CommandKit::FileUtils
9
-
10
- attr_reader :var
11
-
12
- def initialize(var: nil)
13
- super()
14
-
15
- @var = var
16
- end
17
-
18
- def test_method
19
- 'some method'
20
- end
21
- end
22
- end
23
-
24
- let(:var) { 42 }
25
-
26
- let(:command_class) { TestFileUtils::Command }
27
- subject { command_class.new(var: var) }
28
-
29
- let(:fixtures_dir) { File.expand_path(File.join(__dir__,'fixtures')) }
30
- let(:template_path) { File.join(fixtures_dir,'template.erb') }
31
-
32
- describe "#erb" do
33
- let(:expected_result) do
34
- [
35
- "Raw text",
36
- '',
37
- "variable = #{var}",
38
- '',
39
- "method = #{subject.test_method}"
40
- ].join($/)
41
- end
42
-
43
- context "when only given a source file argument" do
44
- it "must render the erb template and return the result" do
45
- expect(subject.erb(template_path)).to eq(expected_result)
46
- end
47
- end
48
-
49
- context "when given an additional destination file argument" do
50
- let(:dest_path) { Tempfile.new.path }
51
-
52
- before { subject.erb(template_path,dest_path) }
53
-
54
- it "must render the erb template and write the result to the destination" do
55
- expect(File.read(dest_path)).to eq(expected_result)
56
- end
57
- end
58
- end
59
- end
@@ -1,5 +0,0 @@
1
- Raw text
2
-
3
- variable = <%= @var %>
4
-
5
- method = <%= test_method -%>
@@ -1,345 +0,0 @@
1
- require 'spec_helper'
2
- require 'command_kit/help/man'
3
-
4
- describe CommandKit::Help::Man do
5
- module TestHelpMan
6
- class TestCommand
7
- include CommandKit::Help::Man
8
-
9
- man_dir "#{__dir__}/fixtures/man"
10
- end
11
-
12
- class TestCommandWithManPage
13
- include CommandKit::Help::Man
14
-
15
- man_dir "#{__dir__}/fixtures/man"
16
- man_page 'foo.1'
17
- end
18
-
19
- class TestCommandWithRelativeManDir
20
- include CommandKit::Help::Man
21
-
22
- man_dir File.join(__dir__,'..','..','..','man')
23
- end
24
-
25
- class TestCommandWithManDirNotSet
26
- include CommandKit::Help::Man
27
- end
28
- end
29
-
30
- let(:command_class) { TestHelpMan::TestCommand }
31
-
32
- describe ".included" do
33
- subject { command_class }
34
-
35
- it "must include CommandName" do
36
- expect(subject).to include(CommandKit::CommandName)
37
- end
38
-
39
- it "must include Help" do
40
- expect(subject).to include(CommandKit::Help)
41
- end
42
-
43
- it "must include Stdio" do
44
- expect(subject).to include(CommandKit::Stdio)
45
- end
46
- end
47
-
48
- describe ".man_dir" do
49
- context "when no man_dir have been set" do
50
- subject { TestHelpMan::TestCommandWithManDirNotSet }
51
-
52
- it "should default to nil" do
53
- expect(subject.man_dir).to be_nil
54
- end
55
- end
56
-
57
- context "when a man_dir is explicitly set" do
58
- subject { TestHelpMan::TestCommand }
59
-
60
- it "must return the explicitly set man_dir" do
61
- expect(subject.man_dir).to eq(File.join(__dir__,'fixtures','man'))
62
- end
63
-
64
- context "but it's a relative path" do
65
- subject { TestHelpMan::TestCommandWithRelativeManDir }
66
-
67
- it "must expand the given man_dir path" do
68
- expect(subject.man_dir).to eq(
69
- File.expand_path(File.join(__dir__,'..','..','..','man'))
70
- )
71
- end
72
- end
73
- end
74
-
75
- context "when the command class inherites from another class" do
76
- context "but no man_dir is set" do
77
- module TestHelpMan
78
- class BaseCmd
79
- include CommandKit::Help::Man
80
- end
81
-
82
- class InheritedCmd < BaseCmd
83
- end
84
- end
85
-
86
- subject { TestHelpMan::InheritedCmd }
87
-
88
- it "must search each class then return nil "do
89
- expect(subject.man_dir).to be_nil
90
- end
91
- end
92
-
93
- module TestHelpMan
94
- class ExplicitBaseCmd
95
- include CommandKit::Help::Man
96
-
97
- man_dir 'set/in/baseclass'
98
- end
99
- end
100
-
101
- context "when the superclass defines an explicit man_dir" do
102
- module TestHelpMan
103
- class ImplicitInheritedCmd < ExplicitBaseCmd
104
- end
105
- end
106
-
107
- let(:super_subject) { TestHelpMan::ExplicitBaseCmd }
108
- subject { TestHelpMan::ImplicitInheritedCmd }
109
-
110
- it "must inherit the superclass'es man_dir" do
111
- expect(subject.man_dir).to eq(super_subject.man_dir)
112
- end
113
-
114
- it "must not change the superclass'es man_dir" do
115
- expect(super_subject.man_dir).to eq(File.expand_path('set/in/baseclass'))
116
- end
117
- end
118
-
119
- context "when the subclass defines an explicit man_dir" do
120
- module TestHelpMan
121
- class ImplicitBaseCmd
122
- include CommandKit::Help::Man
123
- end
124
-
125
- class ExplicitInheritedCmd < ImplicitBaseCmd
126
- man_dir 'set/in/subclass'
127
- end
128
- end
129
-
130
- let(:super_subject) { TestHelpMan::ImplicitBaseCmd }
131
- subject { TestHelpMan::ExplicitInheritedCmd }
132
-
133
- it "must return the subclass'es man_dir" do
134
- expect(subject.man_dir).to eq(File.expand_path('set/in/subclass'))
135
- end
136
-
137
- it "must not change the superclass'es man_dir" do
138
- expect(super_subject.man_dir).to be_nil
139
- end
140
- end
141
-
142
- context "when both the subclass overrides the superclass's man_dirs" do
143
- module TestHelpMan
144
- class ExplicitOverridingInheritedCmd < ExplicitBaseCmd
145
- man_dir 'set/in/subclass'
146
- end
147
- end
148
-
149
- let(:super_subject) { TestHelpMan::ExplicitBaseCmd }
150
- subject { TestHelpMan::ExplicitOverridingInheritedCmd }
151
-
152
- it "must return the subclass'es man_dir" do
153
- expect(subject.man_dir).to eq(File.expand_path('set/in/subclass'))
154
- end
155
-
156
- it "must not change the superclass'es man_dir" do
157
- expect(super_subject.man_dir).to eq(File.expand_path('set/in/baseclass'))
158
- end
159
- end
160
- end
161
- end
162
-
163
- describe ".man_page" do
164
- context "when no man_page has been set" do
165
- subject { TestHelpMan::TestCommand }
166
-
167
- it "should default to \"\#{command_name}.1\"" do
168
- expect(subject.man_page).to eq("#{subject.command_name}.1")
169
- end
170
- end
171
-
172
- context "when a man_page is explicitly set" do
173
- module TestHelpMan
174
- class ExplicitCmd
175
- include CommandKit::Help::Man
176
- man_page 'explicit.1'
177
- end
178
- end
179
-
180
- subject { TestHelpMan::ExplicitCmd }
181
-
182
- it "must return the explicitly set man_page" do
183
- expect(subject.man_page).to eq('explicit.1')
184
- end
185
- end
186
-
187
- context "when the command class inherites from another class" do
188
- module TestHelpMan
189
- class BaseCmd
190
- include CommandKit::Help::Man
191
- end
192
-
193
- class InheritedCmd < BaseCmd
194
- end
195
- end
196
-
197
- subject { TestHelpMan::InheritedCmd }
198
-
199
- it "should underscore the class'es name" do
200
- expect(subject.man_page).to eq('inherited_cmd.1')
201
- end
202
-
203
- context "when the superclass defines an explicit man_page" do
204
- module TestHelpMan
205
- class ExplicitBaseCmd
206
- include CommandKit::Help::Man
207
- man_page 'explicit.1'
208
- end
209
-
210
- class ImplicitInheritedCmd < ExplicitBaseCmd
211
- end
212
- end
213
-
214
- let(:super_subject) { TestHelpMan::ExplicitBaseCmd }
215
- subject { TestHelpMan::ImplicitInheritedCmd }
216
-
217
- it "must return the subclass'es man_page, not the superclass'es" do
218
- expect(subject.man_page).to eq('implicit_inherited_cmd.1')
219
- end
220
-
221
- it "must not change the superclass'es man_page" do
222
- expect(super_subject.man_page).to eq('explicit.1')
223
- end
224
- end
225
-
226
- context "when the subclass defines an explicit man_page" do
227
- module TestHelpMan
228
- class ImplicitBaseCmd
229
- include CommandKit::Help::Man
230
- end
231
-
232
- class ExplicitInheritedCmd < ImplicitBaseCmd
233
- man_page 'explicit.1'
234
- end
235
- end
236
-
237
- let(:super_subject) { TestHelpMan::ImplicitBaseCmd }
238
- subject { TestHelpMan::ExplicitInheritedCmd }
239
-
240
- it "must return the subclass'es man_page, not the superclass'es" do
241
- expect(subject.man_page).to eq('explicit.1')
242
- end
243
-
244
- it "must not change the superclass'es man_page" do
245
- expect(super_subject.man_page).to eq('implicit_base_cmd.1')
246
- end
247
- end
248
- end
249
- end
250
-
251
- subject { command_class.new }
252
-
253
- describe "#help_man" do
254
- let(:man_page_path) do
255
- File.join(subject.class.man_dir,subject.class.man_page)
256
- end
257
-
258
- it "must open the .man_page within the .man_dir" do
259
- expect(subject).to receive(:system).with('man',man_page_path)
260
-
261
- subject.help_man
262
- end
263
-
264
- context "when given a custom man page" do
265
- let(:man_page) { 'bar.1' }
266
- let(:man_page_path) { File.join(subject.class.man_dir,man_page) }
267
-
268
- it "must open the custom man-page within the .man_dir" do
269
- expect(subject).to receive(:system).with('man',man_page_path)
270
-
271
- subject.help_man(man_page)
272
- end
273
- end
274
-
275
- context "but the man_dir is not set" do
276
- let(:command_class) { TestHelpMan::TestCommandWithManDirNotSet }
277
-
278
- it "must call the super help() mehtod" do
279
- expect {
280
- subject.help_man
281
- }.to raise_error(NotImplementedError,"man_dir was not set in #{command_class}")
282
- end
283
- end
284
- end
285
-
286
- describe "#help" do
287
- let(:normal_help_output) do
288
- stdout = StringIO.new
289
-
290
- command_class.new(stdout: stdout).tap do |command|
291
- command.method(:help).super_method.call
292
- end
293
-
294
- stdout.string
295
- end
296
-
297
- let(:stdout) { StringIO.new }
298
-
299
- subject { command_class.new(stdout: stdout) }
300
-
301
- context "when stdout is a TTY" do
302
- before do
303
- expect(subject.stdout).to receive(:tty?).and_return(true)
304
- end
305
-
306
- context "and man_dir is set" do
307
- it "must open the command's man-page" do
308
- expect(subject).to receive(:help_man).and_return(true)
309
-
310
- subject.help
311
- end
312
-
313
- context "but when the man command is not installed" do
314
- before do
315
- expect(subject).to receive(:help_man).and_return(nil)
316
- end
317
-
318
- it "must call the super help() method" do
319
- subject.help
320
-
321
- expect(subject.stdout.string).to eq(normal_help_output)
322
- end
323
- end
324
- end
325
-
326
- context "but the man_dir is not set" do
327
- let(:command_class) { TestHelpMan::TestCommandWithManDirNotSet }
328
-
329
- it "must call the super help() mehtod" do
330
- subject.help
331
-
332
- expect(stdout.string).to eq(normal_help_output)
333
- end
334
- end
335
- end
336
-
337
- context "when stdout is not a TTY" do
338
- it "must call the super help() method" do
339
- subject.help
340
-
341
- expect(stdout.string).to eq(normal_help_output)
342
- end
343
- end
344
- end
345
- end
data/spec/help_spec.rb DELETED
@@ -1,94 +0,0 @@
1
- require 'spec_helper'
2
- require 'command_kit/help'
3
-
4
- describe CommandKit::Help do
5
- module TestHelp
6
- class DefaultCmd
7
- include CommandKit::Help
8
- end
9
-
10
- class TestCommandWithInitialize
11
- include CommandKit::Help
12
-
13
- attr_reader :foo
14
-
15
- attr_reader :bar
16
-
17
- def initialize(foo: 'foo', bar: 'bar', **kwargs)
18
- super(**kwargs)
19
-
20
- @foo = foo
21
- @bar = bar
22
- end
23
- end
24
- end
25
-
26
- let(:command_class) { TestHelp::TestCmd }
27
-
28
- describe ".help" do
29
- subject { command_class }
30
-
31
- context "when no #initialize method is defined" do
32
- let(:command_class) { TestHelp::DefaultCmd }
33
- let(:instance) { command_class.new }
34
-
35
- it "must initialize the command object and call #help" do
36
- expect(subject).to receive(:new).with(no_args).and_return(instance)
37
- expect(instance).to receive(:help).with(no_args)
38
-
39
- subject.help()
40
- end
41
- end
42
-
43
- context "when an #initialize method is defined" do
44
- let(:command_class) { TestHelp::TestCommandWithInitialize }
45
- let(:instance) { command_class.new }
46
-
47
- context "and given no arguments" do
48
- it "must initialize the command object and call #help" do
49
- expect(subject).to receive(:new).with(no_args).and_return(instance)
50
- expect(instance).to receive(:help).with(no_args)
51
-
52
- subject.help()
53
- end
54
- end
55
-
56
- context "and given keyword arguments" do
57
- let(:kwargs) { {foo: 'custom foo', bar: 'custom bar'} }
58
- let(:instance) { command_class.new(**kwargs) }
59
-
60
- it "must pass the keyword arguments .new then call #help" do
61
- expect(subject).to receive(:new).with(**kwargs).and_return(instance)
62
- expect(instance).to receive(:help).with(no_args)
63
-
64
- subject.help(**kwargs)
65
- end
66
- end
67
- end
68
- end
69
-
70
- subject { command_class.new }
71
-
72
- describe "#help" do
73
- context "when there is no superclass #help method" do
74
- module TestHelp
75
- class SuperclassWithoutHelp
76
- end
77
-
78
- class SubclassOfSuperclassWithoutHelp < SuperclassWithoutHelp
79
- include CommandKit::Help
80
- end
81
- end
82
-
83
- let(:command_superclass) { TestHelp::SuperclassWithoutHelp }
84
- let(:command_class) { TestHelp::SubclassOfSuperclassWithoutHelp }
85
-
86
- it "must not attempt to call the superclass'es #help" do
87
- allow_any_instance_of(command_class).to receive(:help)
88
- expect_any_instance_of(command_superclass).to_not receive(:help)
89
-
90
- subject.help()
91
- end
92
- end
93
- end
94
- end