Skip to content

Commit 6a424e4

Browse files
committed
(maint) Fix expect_upload for module relative paths outside files
The expect_upload() BoltSpec expectation for upload_file calls in plans handled two cases: Given a /my/modules/amodule with /my/modules/amodule/files/foo /my/modules/amodule/resources/baz /my/modules/amodule/plans/bar expect_upload('/some/thing') would correctly match against an upload_file('/some/thing') in plan bar, because it is not relative to the modulepath at all, and against an upload_file('amodule/foo'), because it is relative to amodule, and foo is relative to files, which expect_upload elides when the MockExecutor runs the source_path through BotlSpec::MockExecutor#module_file_id. The edge case that was peculiar was a call to upload_file('amodule/files/../resources/baz'), which expect_upload would end up expecting as 'amodule/baz'. The hidden behavior here is that Bolt::Util.find_file* functionality will take 'foo/bar' and look for /modulepath/foo/files/bar, which is mirroring Puppety behavior to find module files relative to the files/ dir.
1 parent 6ff6505 commit 6a424e4

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

lib/bolt_spec/plans/mock_executor.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ def module_file_id(file)
4949
path = Pathname.new(file)
5050
relative = path.relative_path_from(Pathname.new(modpath.first))
5151
segments = relative.to_path.split('/')
52-
([segments[0]] + segments[2..-1]).join('/')
52+
segments[1] == 'files' ?
53+
([segments[0]] + segments[2..-1]).join('/') :
54+
segments.join('/')
5355
end
5456

5557
def run_command(targets, command, options = {}, _position = [])

spec/bolt_spec/plan_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def expect_action
207207
with_tempfile_containing('prep', '') do |file|
208208
@source = file.path
209209
allow_upload(@source).with_targets(targets)
210+
expect_upload('plans/resources/bar').with_destination('/o')
210211
example.run
211212
end
212213
end

spec/bolt_spec/plans/mock_executor_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,20 @@
1111

1212
expect(missing_methods.empty?).to be(true), message
1313
end
14+
15+
context '#module_file_id' do
16+
let(:executor) { BoltSpec::Plans::MockExecutor.new('/some/path/to/modules') }
17+
18+
it 'returns nil if path is outside of modulepath' do
19+
expect(executor.module_file_id('/some/other/path')).to be_nil
20+
end
21+
22+
it 'handles module relative paths relative to module/files returning module/path excluding the files dir' do
23+
expect(executor.module_file_id('/some/path/to/modules/amodule/files/dingo')).to eq('amodule/dingo')
24+
end
25+
26+
it 'handles module relative paths outside of module/files' do
27+
expect(executor.module_file_id('/some/path/to/modules/amodule/files/../other/dingo')).to eq('amodule/other/dingo')
28+
end
29+
end
1430
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
plan plans::upload(TargetSpec $nodes, String $source) {
22
upload_file($source, '/b', $nodes)
3+
upload_file('plans/files/../resources/bar', '/o', $nodes)
34
return upload_file('plans/script', '/d', $nodes)
45
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Some other file outside of files/.

0 commit comments

Comments
 (0)