Skip to content

Commit d798621

Browse files
authored
Merge pull request #7083 from nicklewis/PUP-9135-wrong-error-when-module-not-found
(PUP-9135) Treat task metadata referring to missing module as invalid
2 parents e8d7933 + e9fed0a commit d798621

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/puppet/module/task.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ def self.is_tasks_filename?(path)
6363
end
6464

6565
def self.get_file_details(path, mod)
66-
unless File.absolute_path(path) == File.path(path)
67-
msg = _("File pathnames cannot include relative paths")
68-
raise InvalidMetadata.new(msg, 'puppet.tasks/invalid-metadata')
69-
end
70-
7166
# This gets the path from the starting point onward
7267
# For files this should be the file subpath from the metadata
7368
# For directories it should be the directory subpath plus whatever we globbed
@@ -90,17 +85,23 @@ def self.find_files(files, mod)
9085

9186
pup_module = Puppet::Module.find(module_name, env)
9287
if pup_module.nil?
93-
raise Puppet::Module::MissingModule, _("Module %{module_name} not found in environment %{environment_name}.") %
94-
{module_name: pup_module.name, environment_name: env}
88+
msg = _("Could not find module %{module_name} containing task file %{filename}" %
89+
{module_name: module_name, filename: endpath})
90+
raise InvalidMetadata.new(msg, 'puppet.tasks/invalid-metadata')
9591
end
9692

9793
unless MOUNTS.include? mount
98-
msg = _("Files must be saved in module directories that Puppet makes available via mount points: %{mounts}" %
94+
msg = _("Files must be saved in module directories that Puppet makes available via mount points: %{mounts}" %
9995
{mounts: MOUNTS.join(', ')})
10096
raise InvalidMetadata.new(msg, 'puppet.tasks/invalid-metadata')
10197
end
10298

10399
path = File.join(pup_module.path, mount, endpath)
100+
unless File.absolute_path(path) == File.path(path).chomp('/')
101+
msg = _("File pathnames cannot include relative paths")
102+
raise InvalidMetadata.new(msg, 'puppet.tasks/invalid-metadata')
103+
end
104+
104105
unless File.exist?(path)
105106
msg = _("Could not find %{path} on disk" % { path: path })
106107
raise InvalidFile.new(msg)

spec/unit/task_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@
9292
expect(tasks.map{|t| t.files.map{ |f| f["path"] } }).to eq([["#{tasks_path}/task1.sh"] + long_files])
9393
end
9494

95+
it "fails to load a task if its metadata specifies a non-existent file" do
96+
og_files = %w{task1.sh task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
97+
Dir.stubs(:glob).with(tasks_glob).returns(og_files)
98+
File.stubs(:exist?).with(any_parameters).returns(true)
99+
100+
Puppet::Module.expects(:find).with(othermod.name, "production").returns(nil).at_least(1)
101+
tasks = Puppet::Module::Task.tasks_in_module(mymod)
102+
Puppet::Module::Task.any_instance.stubs(:metadata).returns({'files' => ["#{othermod.name}/files/test"]})
103+
104+
expect { tasks.first.files }.to raise_error(Puppet::Module::Task::InvalidMetadata, /Could not find module #{othermod.name} containing task file test/)
105+
end
106+
95107
it "finds files whose names (besides extensions) are valid task names" do
96108
Dir.expects(:glob).with(tasks_glob).returns(%w{task task_1 xx_t_a_s_k_2_xx})
97109
tasks = Puppet::Module::Task.tasks_in_module(mymod)

0 commit comments

Comments
 (0)