Skip to content

Commit 4f608de

Browse files
committed
full test coverage
1 parent e0f4dab commit 4f608de

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

spec/approvals/library/base/dir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
- :path: spec/tmp/src/lib/validations/validate_dir_exists.sh
3+
:content: |
4+
validate_dir_exists() {
5+
[[ -d "$1" ]] || echo "must be an existing directory"
6+
}
7+
- :path: spec/tmp/src/lib/validations/validate_file_exists.sh
8+
:content: |
9+
validate_file_exists() {
10+
[[ -f "$1" ]] || echo "must be an existing file"
11+
}
12+
- :path: spec/tmp/src/lib/validations/validate_integer.sh
13+
:content: |-
14+
validate_integer() {
15+
[[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
16+
}
17+
- :path: spec/tmp/src/lib/validations/validate_not_empty.sh
18+
:content: |
19+
validate_not_empty() {
20+
[[ -z "$1" ]] && echo "must not be empty"
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'spec_helper'
2+
3+
describe AssetHelper do
4+
subject { Class.new { include AssetHelper }.new }
5+
let(:path) { "version.rb" }
6+
7+
describe '#asset' do
8+
it "returns a path to any file in the source tree" do
9+
expect(subject.asset path).to end_with "lib/bashly/#{path}"
10+
end
11+
end
12+
13+
describe '#asset_content' do
14+
it "returns the contents of any file in the source tree" do
15+
expect(subject.asset_content path).to eq File.read(subject.asset path)
16+
end
17+
end
18+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe File do
4+
describe '::deep_write' do
5+
it "creates parent directories and writes a file" do
6+
expect(FileUtils).to receive(:mkdir_p).with('some/path')
7+
expect(File).to receive(:write).with('some/path/file.txt', 'text')
8+
File.deep_write 'some/path/file.txt', 'text'
9+
end
10+
end
11+
end

spec/bashly/library/base_spec.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
require 'spec_helper'
2+
3+
describe Library::Base do
4+
let(:lib_dir) { 'lib/bashly/templates/lib' }
5+
6+
describe '#content' do
7+
it "raises a NotImplementedError" do
8+
expect { subject.content }.to raise_error(NotImplementedError)
9+
end
10+
end
11+
12+
describe '#files' do
13+
it "raises a NotImplementedError" do
14+
expect { subject.files }.to raise_error(NotImplementedError)
15+
end
16+
17+
context "when #content is a string representing a lib/file" do
18+
subject do
19+
Class.new(described_class) { def content = "colors.sh" }.new
20+
end
21+
22+
it "returns an array of hashes" do
23+
expect(subject.files).to be_an Array
24+
expect(subject.files.first).to be_a Hash
25+
end
26+
27+
it "returns the content and target paths of the library files" do
28+
matter = subject.files.first
29+
expect(matter.keys).to eq [:path, :content]
30+
expect(matter[:path]).to eq "#{Settings.target_dir}/src/lib/#{subject.content}"
31+
expect(matter[:content]).to eq File.read("#{lib_dir}/#{subject.content}")
32+
end
33+
end
34+
35+
context "when #content is a string representing a lib/dir" do
36+
subject do
37+
Class.new(described_class) { def content = "validations" }.new
38+
end
39+
40+
it "returns an array of hashes" do
41+
expect(subject.files).to be_an Array
42+
expect(subject.files.first).to be_a Hash
43+
end
44+
45+
it "returns an array as big as the library folder" do
46+
expect(subject.files.count).to be > 1
47+
expect(subject.files.count).to eq Dir["#{lib_dir}/#{subject.content}/*.sh"].count
48+
end
49+
50+
it "returns the content and target paths of the library files" do
51+
expect(subject.files.to_yaml).to match_approval 'library/base/dir'
52+
end
53+
end
54+
55+
context "when #content is a hash" do
56+
subject do
57+
Class.new(described_class) { def content = { some: 'hash' } }.new
58+
end
59+
60+
it "returns an array with the hash as its first and only element" do
61+
expect(subject.files).to be_an Array
62+
expect(subject.files.count).to eq 1
63+
expect(subject.files.first).to eq subject.content
64+
end
65+
end
66+
67+
context "when #content is something else" do
68+
subject do
69+
Class.new(described_class) { def content = ["something else"] }.new
70+
end
71+
72+
it "returns it as is" do
73+
expect(subject.files).to eq subject.content
74+
end
75+
end
76+
end
77+
78+
describe '#post_install_message' do
79+
it "returns nil" do
80+
expect(subject.post_install_message).to be_nil
81+
end
82+
end
83+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'spec_helper'
2+
3+
describe Library::Completions do
4+
describe '#file_content' do
5+
it "raises a NotImplementedError" do
6+
expect { subject.content }.to raise_error(NotImplementedError)
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)