Skip to content

Commit d3b48a2

Browse files
authored
Merge pull request #1977 from gimmyxd/maint/fix_yaml_load
(maint) handle Time and Symbol in executable facts
2 parents 26c64dd + e1835ff commit d3b48a2

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

.rubocop.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ require:
1313
Layout/LineLength:
1414
Max: 120
1515

16-
Lint/RescueException:
17-
Exclude:
18-
- 'lib/facter/custom_facts/util/parser.rb'
19-
2016
Lint/RaiseException:
2117
Enabled: true
2218

lib/facter/custom_facts/util/parser.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def content
5656
# wrapper.
5757
def results
5858
parse_results
59-
rescue Exception => e
59+
rescue StandardError => e
6060
Facter.log_exception(e, "Failed to handle #{filename} as #{self.class} facts: #{e.message}")
6161
nil
6262
end
@@ -68,8 +68,8 @@ def parse_results
6868
def parse_executable_output(output)
6969
res = nil
7070
begin
71-
res = YAML.safe_load output
72-
rescue Exception => e
71+
res = YAML.safe_load(output, [Symbol, Time])
72+
rescue StandardError => e
7373
Facter.debug("Could not parse executable fact output as YAML or JSON (#{e.message})")
7474
end
7575
res = KeyValuePairOutputFormat.parse output unless res.is_a?(Hash)

spec/custom_facts/util/parser_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ def expects_parser_to_return_nil_for_directory(path)
162162
expects_script_to_return(cmd, yaml_data, data)
163163
end
164164

165+
it 'handles Symbol correctly' do
166+
yaml_data = "---\n:one: :two\nthree: four\n"
167+
exptected_data = { :one => :two, 'three' => 'four' }
168+
expects_script_to_return(cmd, yaml_data, exptected_data)
169+
end
170+
171+
it 'handles Time correctly' do
172+
yaml_data = "---\nfirst: 2020-07-15 05:38:12.427678398 +00:00\n"
173+
allow(Facter::Core::Execution).to receive(:exec).with(cmd).and_return(yaml_data)
174+
allow(File).to receive(:executable?).with(cmd).and_return(true)
175+
allow(FileTest).to receive(:file?).with(cmd).and_return(true)
176+
177+
expect(LegacyFacter::Util::Parser.parser_for(cmd).results['first']).to be_a(Time)
178+
end
179+
165180
it 'returns an empty hash when the script returns nil' do
166181
expects_script_to_return(cmd, nil, {})
167182
end

0 commit comments

Comments
 (0)