command_kit 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +1 -0
- data/.rubocop.yml +1 -1
- data/ChangeLog.md +64 -0
- data/lib/command_kit/arguments/argument.rb +2 -2
- data/lib/command_kit/arguments.rb +27 -2
- data/lib/command_kit/colors.rb +484 -11
- data/lib/command_kit/command.rb +1 -2
- data/lib/command_kit/file_utils.rb +46 -0
- data/lib/command_kit/options/option.rb +40 -21
- data/lib/command_kit/options/parser.rb +0 -3
- data/lib/command_kit/options.rb +89 -14
- data/lib/command_kit/program_name.rb +9 -0
- data/lib/command_kit/version.rb +1 -1
- data/spec/arguments_spec.rb +33 -0
- data/spec/colors_spec.rb +226 -195
- data/spec/command_spec.rb +2 -2
- data/spec/file_utils_spec.rb +59 -0
- data/spec/fixtures/template.erb +5 -0
- data/spec/options/option_spec.rb +36 -0
- data/spec/options/parser_spec.rb +0 -10
- data/spec/options_spec.rb +46 -0
- data/spec/program_name_spec.rb +8 -0
- metadata +4 -1
@@ -0,0 +1,59 @@
|
|
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
|
data/spec/options/option_spec.rb
CHANGED
@@ -145,6 +145,20 @@ describe CommandKit::Options::Option do
|
|
145
145
|
end
|
146
146
|
end
|
147
147
|
|
148
|
+
it "must default #category to nil" do
|
149
|
+
expect(subject.category).to be(nil)
|
150
|
+
end
|
151
|
+
|
152
|
+
context "when the category: keyword is given" do
|
153
|
+
let(:category) { 'Other Options' }
|
154
|
+
|
155
|
+
subject { described_class.new(name, desc: desc, category: category) }
|
156
|
+
|
157
|
+
it "must set #category" do
|
158
|
+
expect(subject.category).to eq(category)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
148
162
|
context "when a block is given" do
|
149
163
|
subject { described_class.new(name, desc: desc, &block) }
|
150
164
|
|
@@ -293,5 +307,27 @@ describe CommandKit::Options::Option do
|
|
293
307
|
expect(subject.desc).to eq("#{desc} (Default: #{default})")
|
294
308
|
end
|
295
309
|
end
|
310
|
+
|
311
|
+
context "when #desc was initialized with an Array" do
|
312
|
+
let(:desc) do
|
313
|
+
[
|
314
|
+
'Line 1',
|
315
|
+
'Line 2'
|
316
|
+
]
|
317
|
+
end
|
318
|
+
|
319
|
+
it "must return the desc: value" do
|
320
|
+
expect(subject.desc).to eq(desc)
|
321
|
+
end
|
322
|
+
|
323
|
+
context "when #value has been initialized with a default value" do
|
324
|
+
let(:default) { "foo" }
|
325
|
+
let(:value) { {default: default} }
|
326
|
+
|
327
|
+
it "should append '(Default: ...)' to the desc Array" do
|
328
|
+
expect(subject.desc).to eq([*desc, "(Default: #{default})"])
|
329
|
+
end
|
330
|
+
end
|
331
|
+
end
|
296
332
|
end
|
297
333
|
end
|
data/spec/options/parser_spec.rb
CHANGED
@@ -34,16 +34,6 @@ describe CommandKit::Options::Parser do
|
|
34
34
|
expect(subject.option_parser.banner).to eq("Usage: #{subject.usage}")
|
35
35
|
end
|
36
36
|
|
37
|
-
it "must include a 'Options:' separator" do
|
38
|
-
expect(subject.option_parser.to_s).to include(
|
39
|
-
[
|
40
|
-
'',
|
41
|
-
'Options:',
|
42
|
-
''
|
43
|
-
].join($/)
|
44
|
-
)
|
45
|
-
end
|
46
|
-
|
47
37
|
it "must define a default --help option" do
|
48
38
|
expect(subject.option_parser.to_s).to include(
|
49
39
|
[
|
data/spec/options_spec.rb
CHANGED
@@ -415,5 +415,51 @@ describe CommandKit::Options do
|
|
415
415
|
].join($/)
|
416
416
|
).to_stdout
|
417
417
|
end
|
418
|
+
|
419
|
+
context "but when the options are have categories" do
|
420
|
+
module TestOptions
|
421
|
+
class TestCommandWithOptionsAndCategories
|
422
|
+
|
423
|
+
include CommandKit::Options
|
424
|
+
|
425
|
+
option :opt1, short: '-a',
|
426
|
+
desc: "Option 1"
|
427
|
+
option :opt2, short: '-b',
|
428
|
+
desc: "Option 2"
|
429
|
+
|
430
|
+
option :opt3, short: '-c',
|
431
|
+
desc: "Option 3",
|
432
|
+
category: 'Other Options'
|
433
|
+
option :opt4, short: '-d',
|
434
|
+
desc: "Option 4",
|
435
|
+
category: 'Other Options'
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
439
|
+
let(:command_class) { TestOptions::TestCommandWithOptionsAndCategories }
|
440
|
+
|
441
|
+
let(:option1) { command_class.options[:opt1] }
|
442
|
+
let(:option2) { command_class.options[:opt2] }
|
443
|
+
let(:option3) { command_class.options[:opt3] }
|
444
|
+
let(:option4) { command_class.options[:opt4] }
|
445
|
+
|
446
|
+
it "must group the options by category" do
|
447
|
+
expect { subject.help }.to output(
|
448
|
+
[
|
449
|
+
"Usage: #{subject.usage}",
|
450
|
+
'',
|
451
|
+
'Other Options:',
|
452
|
+
" #{option3.usage.join(', ').ljust(33 - 1)} #{option3.desc}",
|
453
|
+
" #{option4.usage.join(', ').ljust(33 - 1)} #{option4.desc}",
|
454
|
+
'',
|
455
|
+
'Options:',
|
456
|
+
" #{option1.usage.join(', ').ljust(33 - 1)} #{option1.desc}",
|
457
|
+
" #{option2.usage.join(', ').ljust(33 - 1)} #{option2.desc}",
|
458
|
+
' -h, --help Print help information',
|
459
|
+
''
|
460
|
+
].join($/)
|
461
|
+
).to_stdout
|
462
|
+
end
|
463
|
+
end
|
418
464
|
end
|
419
465
|
end
|
data/spec/program_name_spec.rb
CHANGED
@@ -56,6 +56,14 @@ describe CommandKit::ProgramName do
|
|
56
56
|
end
|
57
57
|
end
|
58
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
|
+
|
59
67
|
after do
|
60
68
|
$PROGRAM_NAME = @original_program_name
|
61
69
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: command_kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Postmodern
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/command_kit/env/path.rb
|
72
72
|
- lib/command_kit/examples.rb
|
73
73
|
- lib/command_kit/exception_handler.rb
|
74
|
+
- lib/command_kit/file_utils.rb
|
74
75
|
- lib/command_kit/help.rb
|
75
76
|
- lib/command_kit/help/man.rb
|
76
77
|
- lib/command_kit/inflector.rb
|
@@ -121,6 +122,8 @@ files:
|
|
121
122
|
- spec/env_spec.rb
|
122
123
|
- spec/examples_spec.rb
|
123
124
|
- spec/exception_handler_spec.rb
|
125
|
+
- spec/file_utils_spec.rb
|
126
|
+
- spec/fixtures/template.erb
|
124
127
|
- spec/help/man_spec.rb
|
125
128
|
- spec/help_spec.rb
|
126
129
|
- spec/inflector_spec.rb
|