Skip to content

Commit a12dad0

Browse files
authored
Merge pull request #8937 from mhashizume/PUP-11629/main/json_error
(PUP-11629) Accept empty string with MultiJSON
2 parents 948a4be + e40c490 commit a12dad0

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/puppet/module/task.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,12 @@ def initialize(pup_module, task_name, module_executables, metadata_file = nil)
232232
end
233233

234234
def self.read_metadata(file)
235-
Puppet::Util::Json.load(Puppet::FileSystem.read(file, :encoding => 'utf-8')) if file
235+
# MultiJSON has a bug that improperly errors when loading an empty string
236+
# so we handle it here for now. See: PUP-11629
237+
if file
238+
content = Puppet::FileSystem.read(file, :encoding => 'utf-8')
239+
content.empty? ? {} : Puppet::Util::Json.load(content)
240+
end
236241
rescue SystemCallError, IOError => err
237242
msg = _("Error reading metadata: %{message}" % {message: err.message})
238243
raise InvalidMetadata.new(msg, 'puppet.tasks/unreadable-metadata')

spec/unit/task_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@
235235
tasks[0].metadata
236236
}.to raise_error(Puppet::Module::Task::InvalidMetadata, /whoops/)
237237
end
238+
239+
it 'returns empty hash for metadata when json metadata file is empty' do
240+
FileUtils.mkdir_p(tasks_path)
241+
FileUtils.touch(File.join(tasks_path, 'task.json'))
242+
FileUtils.touch(File.join(tasks_path, 'task'))
243+
244+
tasks = Puppet::Module::Task.tasks_in_module(mymod)
245+
246+
expect(tasks.count).to eq(1)
247+
expect(tasks[0].metadata).to eq({})
248+
end
238249
end
239250

240251
describe :validate do

0 commit comments

Comments
 (0)