Skip to content

Commit 7156126

Browse files
authored
Merge pull request #8910 from smortex/ignore-task-directories
(PUP-11539) Ignore directories when listing tasks
2 parents 363f22b + b27c752 commit 7156126

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

lib/puppet/module/task.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def self.is_task_name?(name)
5252
return false
5353
end
5454

55+
def self.is_tasks_file?(path)
56+
File.file?(path) && is_tasks_filename?(path)
57+
end
58+
5559
# Determine whether a file has a legal name for either a task's executable or metadata file.
5660
def self.is_tasks_filename?(path)
5761
name_less_extension = File.basename(path, '.*')
@@ -200,7 +204,7 @@ def self.is_tasks_executable_filename?(name)
200204

201205
def self.tasks_in_module(pup_module)
202206
task_files = Dir.glob(File.join(pup_module.tasks_directory, '*'))
203-
.keep_if { |f| is_tasks_filename?(f) }
207+
.keep_if { |f| is_tasks_file?(f) }
204208

205209
module_executables = task_files.reject(&method(:is_tasks_metadata_filename?)).map.to_a
206210

spec/unit/task_spec.rb

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
it "constructs tasks as expected when every task has a metadata file with the same name (besides extension)" do
2525
task_files = %w{task1.json task1 task2.json task2.exe task3.json task3.sh}.map { |bn| "#{tasks_path}/#{bn}" }
2626
expect(Dir).to receive(:glob).with(tasks_glob).and_return(task_files)
27+
task_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
2728
tasks = Puppet::Module::Task.tasks_in_module(mymod)
2829
allow_any_instance_of(Puppet::Module::Task).to receive(:metadata).and_return({})
2930

@@ -52,6 +53,7 @@
5253
it "constructs tasks as expected when some tasks don't have a metadata file" do
5354
task_files = %w{task1 task2.exe task3.json task3.sh}.map { |bn| "#{tasks_path}/#{bn}" }
5455
expect(Dir).to receive(:glob).with(tasks_glob).and_return(task_files)
56+
task_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
5557
allow_any_instance_of(Puppet::Module::Task).to receive(:metadata).and_return({})
5658
tasks = Puppet::Module::Task.tasks_in_module(mymod)
5759

@@ -66,6 +68,7 @@
6668
it "constructs a task as expected when a task has implementations" do
6769
task_files = %w{task1.elf task1.sh task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
6870
expect(Dir).to receive(:glob).with(tasks_glob).and_return(task_files)
71+
task_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
6972
tasks = Puppet::Module::Task.tasks_in_module(mymod)
7073
allow_any_instance_of(Puppet::Module::Task).to receive(:metadata).and_return({'implementations' => [{"name" => "task1.sh"}]})
7174

@@ -78,6 +81,7 @@
7881
it "constructs a task as expected when task metadata declares additional files" do
7982
task_files = %w{task1.sh task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
8083
expect(Dir).to receive(:glob).with(tasks_glob).and_return(task_files)
84+
task_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
8185
expect(Puppet::Module::Task).to receive(:find_extra_files).and_return([{'name' => 'mymod/lib/file0.elf', 'path' => "/path/to/file0.elf"}])
8286
tasks = Puppet::Module::Task.tasks_in_module(mymod)
8387
allow_any_instance_of(Puppet::Module::Task).to receive(:metadata).and_return({'files' => ["mymod/lib/file0.elf"]})
@@ -91,6 +95,7 @@
9195
it "constructs a task as expected when a task implementation declares additional files" do
9296
task_files = %w{task1.sh task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
9397
expect(Dir).to receive(:glob).with(tasks_glob).and_return(task_files)
98+
task_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
9499
expect(Puppet::Module::Task).to receive(:find_extra_files).and_return([{'name' => 'mymod/lib/file0.elf', 'path' => "/path/to/file0.elf"}])
95100
tasks = Puppet::Module::Task.tasks_in_module(mymod)
96101
allow_any_instance_of(Puppet::Module::Task).to receive(:metadata).and_return({'implementations' => [{"name" => "task1.sh", "files" => ["mymod/lib/file0.elf"]}]})
@@ -104,6 +109,7 @@
104109
it "constructs a task as expected when task metadata and a task implementation both declare additional files" do
105110
task_files = %w{task1.sh task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
106111
expect(Dir).to receive(:glob).with(tasks_glob).and_return(task_files)
112+
task_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
107113
expect(Puppet::Module::Task).to receive(:find_extra_files).and_return([
108114
{'name' => 'mymod/lib/file0.elf', 'path' => "/path/to/file0.elf"},
109115
{'name' => 'yourmod/files/file1.txt', 'path' => "/other/path/to/file1.txt"}
@@ -124,6 +130,7 @@
124130
it "constructs a task as expected when a task has files" do
125131
og_files = %w{task1.sh task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
126132
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
133+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
127134
expect(File).to receive(:exist?).with(any_args).and_return(true).at_least(:once)
128135

129136
expect(Puppet::Module).to receive(:find).with(othermod.name, "production").and_return(othermod).at_least(:once)
@@ -139,6 +146,7 @@
139146
it "fails to load a task if its metadata specifies a non-existent file" do
140147
og_files = %w{task1.sh task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
141148
allow(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
149+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
142150
allow(File).to receive(:exist?).with(any_args).and_return(true)
143151

144152
expect(Puppet::Module).to receive(:find).with(othermod.name, "production").and_return(nil).at_least(:once)
@@ -149,37 +157,54 @@
149157
end
150158

151159
it "finds files whose names (besides extensions) are valid task names" do
152-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task task_1 xx_t_a_s_k_2_xx})
160+
og_files = %w{task task_1 xx_t_a_s_k_2_xx}.map { |bn| "#{tasks_path}/#{bn}" }
161+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
162+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
153163
tasks = Puppet::Module::Task.tasks_in_module(mymod)
154164

155165
expect(tasks.count).to eq(3)
156166
expect(tasks.map{|t| t.name}).to eq(%w{mymod::task mymod::task_1 mymod::xx_t_a_s_k_2_xx})
157167
end
158168

159169
it "ignores files that have names (besides extensions) that are not valid task names" do
160-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{.nottask.exe .wat !runme _task 2task2furious def_a_task_PSYCH Fake_task not-a-task realtask})
170+
og_files = %w{.nottask.exe .wat !runme _task 2task2furious def_a_task_PSYCH Fake_task not-a-task realtask}.map { |bn| "#{tasks_path}/#{bn}" }
171+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
172+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
161173
tasks = Puppet::Module::Task.tasks_in_module(mymod)
162174

163175
expect(tasks.count).to eq(1)
164176
expect(tasks.map{|t| t.name}).to eq(%w{mymod::realtask})
165177
end
166178

167179
it "ignores files that have names ending in .conf and .md" do
168-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{ginuwine_task task.conf readme.md other_task.md})
180+
og_files = %w{ginuwine_task task.conf readme.md other_task.md}.map { |bn| "#{tasks_path}/#{bn}" }
181+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
182+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
169183
tasks = Puppet::Module::Task.tasks_in_module(mymod)
170184

171185
expect(tasks.count).to eq(1)
172186
expect(tasks.map{|t| t.name}).to eq(%w{mymod::ginuwine_task})
173187
end
174188

189+
it "ignores files which are not regular files" do
190+
og_files = %w{foo}.map { |bn| "#{tasks_path}/#{bn}" }
191+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
192+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(false) }
193+
tasks = Puppet::Module::Task.tasks_in_module(mymod)
194+
195+
expect(tasks.count).to eq(0)
196+
end
197+
175198
it "gives the 'init' task a name that is just the module's name" do
176199
expect(Puppet::Module::Task.new(mymod, 'init', ["#{tasks_path}/init.sh"]).name).to eq('mymod')
177200
end
178201

179202
describe :metadata do
180203
it "loads metadata for a task" do
181204
metadata = {'desciption': 'some info'}
182-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task1.exe task1.json})
205+
og_files = %w{task1.exe task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
206+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
207+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
183208
allow(Puppet::Module::Task).to receive(:read_metadata).and_return(metadata)
184209

185210
tasks = Puppet::Module::Task.tasks_in_module(mymod)
@@ -189,7 +214,9 @@
189214
end
190215

191216
it 'returns nil for metadata if no file is present' do
192-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task1.exe})
217+
og_files = %w{task1.exe}.map { |bn| "#{tasks_path}/#{bn}" }
218+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
219+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
193220
tasks = Puppet::Module::Task.tasks_in_module(mymod)
194221

195222
expect(tasks.count).to eq(1)
@@ -212,7 +239,9 @@
212239

213240
describe :validate do
214241
it "validates when there is no metadata" do
215-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task1.exe})
242+
og_files = %w{task1.exe}.map { |bn| "#{tasks_path}/#{bn}" }
243+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
244+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
216245

217246
tasks = Puppet::Module::Task.tasks_in_module(mymod)
218247

@@ -223,7 +252,9 @@
223252
it "validates when an implementation isn't used" do
224253
metadata = {'desciption' => 'some info',
225254
'implementations' => [ {"name" => "task1.exe"}, ] }
226-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task1.exe task1.sh task1.json})
255+
og_files = %w{task1.exe task1.sh task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
256+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
257+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
227258
allow(Puppet::Module::Task).to receive(:read_metadata).and_return(metadata)
228259

229260
tasks = Puppet::Module::Task.tasks_in_module(mymod)
@@ -235,7 +266,9 @@
235266
it "validates when an implementation is another task" do
236267
metadata = {'desciption' => 'some info',
237268
'implementations' => [ {"name" => "task2.sh"}, ] }
238-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task1.exe task2.sh task1.json})
269+
og_files = %w{task1.exe task2.sh task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
270+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
271+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
239272
allow(Puppet::Module::Task).to receive(:read_metadata).and_return(metadata)
240273

241274
tasks = Puppet::Module::Task.tasks_in_module(mymod)
@@ -245,7 +278,9 @@
245278
end
246279

247280
it "fails validation when there is no metadata and multiple task files" do
248-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task1.elf task1.exe task1.json task2.ps1 task2.sh})
281+
og_files = %w{task1.elf task1.exe task1.json task2.ps1 task2.sh}.map { |bn| "#{tasks_path}/#{bn}" }
282+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
283+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
249284
tasks = Puppet::Module::Task.tasks_in_module(mymod)
250285
allow_any_instance_of(Puppet::Module::Task).to receive(:metadata).and_return({})
251286

@@ -255,7 +290,9 @@
255290
end
256291

257292
it "fails validation when an implementation references a non-existant file" do
258-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task1.elf task1.exe task1.json})
293+
og_files = %w{task1.elf task1.exe task1.json}.map { |bn| "#{tasks_path}/#{bn}" }
294+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
295+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
259296
tasks = Puppet::Module::Task.tasks_in_module(mymod)
260297
allow_any_instance_of(Puppet::Module::Task).to receive(:metadata).and_return({'implementations' => [ { 'name' => 'task1.sh' } ] })
261298

@@ -265,23 +302,29 @@
265302
end
266303

267304
it 'fails validation when there is metadata but no executable' do
268-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task1.json task2.sh})
305+
og_files = %w{task1.json task2.sh}.map { |bn| "#{tasks_path}/#{bn}" }
306+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
307+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
269308
tasks = Puppet::Module::Task.tasks_in_module(mymod)
270309
allow_any_instance_of(Puppet::Module::Task).to receive(:metadata).and_return({})
271310

272311
expect { tasks.find { |t| t.name == 'mymod::task1' }.validate }.to raise_error(Puppet::Module::Task::InvalidTask)
273312
end
274313

275314
it 'fails validation when the implementations are not an array' do
276-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task1.json task2.sh})
315+
og_files = %w{task1.json task2.sh}.map { |bn| "#{tasks_path}/#{bn}" }
316+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
317+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
277318
tasks = Puppet::Module::Task.tasks_in_module(mymod)
278319
allow_any_instance_of(Puppet::Module::Task).to receive(:metadata).and_return({"implemenations" => {}})
279320

280321
expect { tasks.find { |t| t.name == 'mymod::task1' }.validate }.to raise_error(Puppet::Module::Task::InvalidTask)
281322
end
282323

283324
it 'fails validation when the implementation is json' do
284-
expect(Dir).to receive(:glob).with(tasks_glob).and_return(%w{task1.json task1.sh})
325+
og_files = %w{task1.json task1.sh}.map { |bn| "#{tasks_path}/#{bn}" }
326+
expect(Dir).to receive(:glob).with(tasks_glob).and_return(og_files)
327+
og_files.each { |f| expect(File).to receive(:file?).with(f).and_return(true) }
285328
tasks = Puppet::Module::Task.tasks_in_module(mymod)
286329
allow_any_instance_of(Puppet::Module::Task).to receive(:metadata).and_return({'implementations' => [ { 'name' => 'task1.json' } ] })
287330

0 commit comments

Comments
 (0)