File tree Expand file tree Collapse file tree 4 files changed +30
-3
lines changed
approvals/refinements/compose Expand file tree Collapse file tree 4 files changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ def compose(keyword = 'import')
6
6
result = { }
7
7
each do |k , v |
8
8
if k . to_s == keyword
9
- sub = YAML . load_file ( v ) . compose keyword
9
+ sub = safe_load_yaml ( v ) . compose keyword
10
10
if sub . is_a? Array
11
11
result = sub
12
12
else
@@ -20,6 +20,15 @@ def compose(keyword = 'import')
20
20
end
21
21
result
22
22
end
23
+
24
+ def safe_load_yaml ( path )
25
+ loaded = YAML . load_file path
26
+ return loaded if loaded . is_a? Array or loaded . is_a? Hash
27
+ raise Bashly ::ConfigurationError , "Cannot find a valid YAML in '#{ path } '"
28
+
29
+ rescue Errno ::ENOENT
30
+ raise Bashly ::ConfigurationError , "Cannot find import file '#{ path } '"
31
+ end
23
32
end
24
33
25
34
refine Array do
Original file line number Diff line number Diff line change
1
+ #<Bashly::ConfigurationError: Cannot find a valid YAML in 'examples/minimal/src/root_command.sh'>
Original file line number Diff line number Diff line change
1
+ #<Bashly::ConfigurationError: Cannot find import file 'no-such-file.yml'>
Original file line number Diff line number Diff line change 1
1
require 'spec_helper'
2
2
3
- describe ComposeRefinements , :focus do
3
+ describe ComposeRefinements do
4
4
using ComposeRefinements
5
5
6
6
subject do
19
19
describe '#compose' do
20
20
it "calls #compose on all elements that respond to #compose" do
21
21
expect ( subject . compose . to_yaml ) . to match_approval "refinements/compose/array"
22
- end
22
+ end
23
+
24
+ context "when the import path does not exist" do
25
+ subject { { import : "no-such-file.yml" } }
26
+
27
+ it "raises ConfigurationError" do
28
+ expect { subject . compose } . to raise_approval ( "refinements/compose/error-not-found" )
29
+ end
30
+ end
31
+
32
+ context "when the import path does not contain a valid YAML front matter" do
33
+ subject { { import : "examples/minimal/src/root_command.sh" } }
34
+
35
+ it "raises ConfigurationError" do
36
+ expect { subject . compose } . to raise_approval ( "refinements/compose/error-invalid" )
37
+ end
38
+ end
23
39
end
24
40
end
You can’t perform that action at this time.
0 commit comments