Skip to content

Commit 6986942

Browse files
committed
complete test coverage
1 parent 17fd839 commit 6986942

File tree

23 files changed

+461
-2
lines changed

23 files changed

+461
-2
lines changed

lib/bashly/extensions/file.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ def self.deep_write(file, content)
66
FileUtils.mkdir_p dir unless Dir.exist? dir
77
File.write file, content
88
end
9+
10+
def self.append(path, content)
11+
File.open(path, "a") { |f| f << content }
12+
end
913
end
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+
skipped spec/tmp/src/initialize.sh (exists)
3+
skipped spec/tmp/src/download_command.sh (exists)
4+
skipped spec/tmp/src/upload_command.sh (exists)
5+
updated spec/tmp/src/lib/yaml.sh
6+
created spec/tmp/cli
7+
run spec/tmp/cli --help to test your bash script

spec/approvals/cli/generate/upgrade

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
creating user files in spec/tmp/src
2+
skipped spec/tmp/src/initialize.sh (exists)
3+
skipped spec/tmp/src/download_command.sh (exists)
4+
skipped spec/tmp/src/upload_command.sh (exists)
5+
updated spec/tmp/src/lib/colors.sh
6+
updated spec/tmp/src/lib/config.sh
7+
updated spec/tmp/src/lib/send_completions.sh
8+
updated spec/tmp/src/lib/validations/validate_dir_exists.sh
9+
updated spec/tmp/src/lib/validations/validate_file_exists.sh
10+
updated spec/tmp/src/lib/validations/validate_integer.sh
11+
updated spec/tmp/src/lib/validations/validate_not_empty.sh
12+
updated spec/tmp/src/lib/yaml.sh
13+
created spec/tmp/cli
14+
run spec/tmp/cli --help to test your bash script
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
creating user files in spec/tmp/src
2+
skipped spec/tmp/src/initialize.sh (exists)
3+
skipped spec/tmp/src/download_command.sh (exists)
4+
skipped spec/tmp/src/upload_command.sh (exists)
5+
warning not upgrading spec/tmp/src/lib/colors-b.sh, path mismatch
6+
updated spec/tmp/src/lib/config.sh
7+
updated spec/tmp/src/lib/send_completions.sh
8+
updated spec/tmp/src/lib/validations/validate_dir_exists.sh
9+
updated spec/tmp/src/lib/validations/validate_file_exists.sh
10+
updated spec/tmp/src/lib/validations/validate_integer.sh
11+
updated spec/tmp/src/lib/validations/validate_not_empty.sh
12+
updated spec/tmp/src/lib/yaml.sh
13+
created spec/tmp/cli
14+
run spec/tmp/cli --help to test your bash script

spec/approvals/fixtures/lib-upgrade

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
+ bundle exec bashly add colors --force
2+
created src/lib/colors.sh
3+
+ bundle exec bashly add yaml --force
4+
created src/lib/yaml.sh
5+
+ bundle exec bashly add config --force
6+
created src/lib/config.sh
7+
+ bundle exec bashly add validations --force
8+
created src/lib/validations/validate_dir_exists.sh
9+
created src/lib/validations/validate_file_exists.sh
10+
created src/lib/validations/validate_integer.sh
11+
created src/lib/validations/validate_not_empty.sh
12+
+ bundle exec bashly add comp function --force
13+
created src/lib/send_completions.sh
14+
15+
In order to enable completions in your script, create a command or a flag (for example: cli completions or cli --completions) that calls the send_completions function.
16+
17+
Your users can then run something like this to enable completions:
18+
19+
$ eval "$(cli completions)"
20+
21+
+ bundle exec bashly generate --upgrade
22+
creating user files in src
23+
created src/initialize.sh
24+
created src/download_command.sh
25+
created src/upload_command.sh
26+
updated src/lib/colors.sh
27+
updated src/lib/config.sh
28+
updated src/lib/send_completions.sh
29+
updated src/lib/validations/validate_dir_exists.sh
30+
updated src/lib/validations/validate_file_exists.sh
31+
updated src/lib/validations/validate_integer.sh
32+
updated src/lib/validations/validate_not_empty.sh
33+
updated src/lib/yaml.sh
34+
created ./cli
35+
run ./cli --help to test your bash script

spec/bashly/commands/generate_spec.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,68 @@
8282
expect(lines[0..10].join).to match_approval('cli/generate/wrap-script')
8383
end
8484
end
85+
86+
context "with --upgrade" do
87+
let(:lib_files) { Dir["spec/tmp/src/lib/**/*.sh"].sort }
88+
let(:outdated_text) { "OUTDATED TEXT" }
8589

90+
before do
91+
reset_tmp_dir copy_from: 'spec/fixtures/workspaces/lib-upgrade'
92+
end
93+
94+
it "claims to upgrade all upgradable libraries" do
95+
expect { subject.run %w[generate -u] }.to output_approval('cli/generate/upgrade')
96+
end
97+
98+
it "actually upgrades all upgradable libraries" do
99+
lib_files.each do |file|
100+
File.append file, outdated_text
101+
end
102+
103+
expect { subject.run %w[generate -u] }.to output_approval('cli/generate/upgrade')
104+
105+
lib_files.each do |file|
106+
expect(File.read file).to_not include(outdated_text),
107+
"Expected to not find #{outdated_text} in #{file}, but found it"
108+
end
109+
end
110+
111+
context "when the magic comment does not exist in the target file" do
112+
let(:selective_lib_files) { lib_files[0..-2] }
113+
114+
before do
115+
selective_lib_files.each do |file|
116+
File.write file, File.read(file).gsub("[@bashly-upgrade", "[@please-dont")
117+
end
118+
end
119+
120+
it "avoids upgrading it" do
121+
expect { subject.run %w[generate -u] }.to output_approval('cli/generate/dont-upgrade')
122+
123+
selective_lib_files.each do |file|
124+
expect(File.read file).to include("@please-dont"),
125+
"Expected to find @please-dont in #{file}, but didn't"
126+
end
127+
end
128+
end
129+
130+
context "when the upgrade candidate has a different path than the library's" do
131+
let(:file) { lib_files[0] }
132+
let(:custom_text) { "CUSTOM TEXT" }
133+
let(:alt_filename) { file.gsub /([a-z])\.sh/, '\1-b.sh' }
134+
135+
before do
136+
File.append file, custom_text
137+
expect(system "mv #{file} #{alt_filename}").to be true
138+
end
139+
140+
it "avoids upgrading it and shows a warning" do
141+
expect { subject.run %w[generate -u] }.to output_approval('cli/generate/upgrade-path-mismatch')
142+
143+
expect(File.read alt_filename).to include(custom_text),
144+
"Expected to find #{custom_text} in #{file}, but didn't"
145+
end
146+
end
147+
148+
end
86149
end

spec/bashly/integration/examples_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
examples = Dir["examples/*"].select { |f| File.directory? f }
1111

1212
# ...as well as internal examples, not suitable for public view
13-
fixtures = Dir["spec/fixtures/workspaces/*"].select { |f| File.directory? f }
13+
fixtures = Dir["spec/fixtures/workspaces/*"].select do |f|
14+
File.directory? f and File.exist? "#{f}/test.sh"
15+
end
1416

1517
test_cases = fixtures + examples
1618

spec/fixtures/workspaces/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
These folders each contain a bashly workspace intended for testing a particular
2+
aspect that is not covered by other unit tests, and is not suitable for being
3+
a user-facing example.
4+
5+
- Workspaces that contain a `test.sh` will be tested as part of the examples
6+
test.
7+
- Other workspaces are used explicitely by other tests.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This fixture workspace is used for testing `bashly g --upgrade`

0 commit comments

Comments
 (0)