Skip to content

Commit 3ced4f2

Browse files
authored
Add some specs for improved branch-level coverage (#214)
1 parent 953ff27 commit 3ced4f2

File tree

13 files changed

+125
-3
lines changed

13 files changed

+125
-3
lines changed

lib/bashly/script/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def args
4040

4141
# Returns a string suitable to be a headline
4242
def caption_string
43-
help ? "#{full_name} - #{summary}" : full_name
43+
help.empty? ? full_name : "#{full_name} - #{summary}"
4444
end
4545

4646
def catch_all

spec/approvals/cli/generate/nested

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
creating user files in spec/tmp/src
2+
created spec/tmp/src/initialize.sh
3+
created spec/tmp/src/download_command.sh
4+
created spec/tmp/src/upload_ftp_command.sh
5+
created spec/tmp/src/upload_ssh_command.sh
6+
created spec/tmp/cli
7+
run spec/tmp/cli --help to test your bash script

spec/bashly/commands/generate_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@
6161
expect(File).to exist(cli_script)
6262
end
6363
end
64+
65+
context "with 2 levels of nested commands" do
66+
let(:file) { "#{target_dir}/src/upload_command.sh" }
67+
68+
before do
69+
system "cp spec/fixtures/nested.yml #{source_dir}/bashly.yml"
70+
end
71+
72+
it "does not generate the file for the middle command" do
73+
expect { subject.run %w[generate] }.to output_approval('cli/generate/nested')
74+
expect(File).to_not exist(file)
75+
end
76+
end
6477
end
6578

6679
context "with --quiet" do

spec/bashly/extensions/array_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@
66
it "prepends each element with spaces" do
77
expect(subject.indent 2).to eq [" root:", " indented:"]
88
end
9+
10+
context "when offset is 0" do
11+
it "returns the array as is" do
12+
expect(subject.indent 0).to eq subject
13+
end
14+
end
915
end
1016
end

spec/bashly/extensions/string_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,37 @@
33
describe String do
44
describe '#sanitize_for_print' do
55
subject { %Q[this is\na "new line"] }
6+
67
it "escapes newlines and quotes" do
78
expect(subject.sanitize_for_print).to eq "this is\\na \\\"new line\\\""
89
end
910
end
1011

1112
describe '#indent' do
1213
subject { "hello" }
14+
1315
it "prepends the string with spaces" do
1416
expect(subject.indent 3).to eq " hello"
1517
end
18+
19+
context "when offset is 0" do
20+
it "returns the string as is" do
21+
expect(subject.indent 0).to eq subject
22+
end
23+
end
1624
end
1725

1826
describe '#wrap' do
1927
subject { "a long line of text that is going to be wrapped, fingers crossed!" }
28+
2029
it "wraps the string to the specified length" do
2130
expect(subject.wrap 30).to eq "a long line of text that is\ngoing to be wrapped, fingers\ncrossed!"
2231
end
2332

2433
# GH-79
2534
context "with an uninterrupted string" do
2635
subject { "a long line of text with a nice uninterrupted-string-like-a-url-for-example" }
36+
2737
it "does not break the uninterrupted portion" do
2838
expect(subject.wrap 30).to eq "a long line of text with a\nnice\nuninterrupted-string-like-a-url-for-example"
2939
end
@@ -84,6 +94,7 @@
8494

8595
context "with a string that contains front matter" do
8696
subject { "#{front_matter}\n---\n#{rest}" }
97+
8798
let(:front_matter) { "this is the front matter" }
8899
let(:rest) { "this is\nthe script" }
89100

@@ -94,6 +105,7 @@
94105

95106
context "with a string that contains front matter with a leading separator" do
96107
subject { "---\n#{front_matter}\n---\n#{rest}" }
108+
97109
let(:front_matter) { "this is the front matter" }
98110
let(:rest) { "this is\nthe script" }
99111

spec/bashly/script/argument_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,18 @@
2121
end
2222
end
2323
end
24+
25+
describe '#label' do
26+
it "returns a string suitable to be used as a compact usage pattern" do
27+
expect(subject.label).to eq "FILE"
28+
end
29+
30+
context "with a repeatable arg" do
31+
let(:fixture) { :repeatable }
32+
33+
it "adds a ... suffix" do
34+
expect(subject.label).to eq "FILE..."
35+
end
36+
end
37+
end
2438
end

spec/bashly/script/base_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@
1010
end
1111
end
1212

13+
describe '#initialize' do
14+
context "with invalid options" do
15+
let(:options) { "not-a-hash" }
16+
17+
it "raises an error" do
18+
expect { subject }.to raise_error(Bashly::Error, /Invalid options/)
19+
end
20+
end
21+
end
22+
23+
describe '#method_missing' do
24+
context "when the method is not a valid option key" do
25+
it "raises an error" do
26+
expect { subject.no_such_option }.to raise_error(NoMethodError)
27+
end
28+
end
29+
end
30+
1331
describe '#optional' do
1432
context "when required is false" do
1533
it "returns true" do

spec/bashly/script/command_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@
5959
it "returns a string containing the name and summary" do
6060
expect(subject.caption_string).to eq "get - get something from somewhere"
6161
end
62+
63+
context "when help is not defined" do
64+
let(:fixture) { :helpless }
65+
66+
it "returns the full name only" do
67+
expect(subject.caption_string).to eq "helpless"
68+
end
69+
end
6270
end
6371

6472
describe '#command_names' do
@@ -320,6 +328,14 @@
320328
expect(subject.usage_string).to eq "docker [command]"
321329
end
322330
end
331+
332+
context "when catch_all is enabled" do
333+
let(:fixture) { :catch_all }
334+
335+
it "includes the catch_all usage string" do
336+
expect(subject.usage_string).to eq "get [...]"
337+
end
338+
end
323339
end
324340

325341
describe '#whitelisted_args' do

spec/bashly/script/flag_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,21 @@
6868
end
6969
end
7070

71-
context "when the flag is required" do
71+
context "when the flag is also required" do
7272
let(:fixture) { :required }
7373

7474
it "appends (required) to the usage string" do
7575
expect(subject.usage_string extended: true).to eq "#{subject.usage_string} (required)"
7676
end
7777
end
78+
79+
context "when the flag is also repeatable" do
80+
let(:fixture) { :repeatable }
81+
82+
it "appends (repeatable) to the usage string" do
83+
expect(subject.usage_string extended: true).to eq "#{subject.usage_string} (repeatable)"
84+
end
85+
end
7886

7987
end
8088
end

spec/fixtures/nested.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
commands:
6+
- name: download
7+
short: d
8+
help: Download a file
9+
10+
- name: upload
11+
short: u
12+
help: Upload a file
13+
commands:
14+
- name: ftp
15+
- name: ssh

0 commit comments

Comments
 (0)