Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(maint) handle Time and Symbol in executable facts #1977

Merged
merged 2 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ require:
Layout/LineLength:
Max: 120

Lint/RescueException:
Exclude:
- 'lib/facter/custom_facts/util/parser.rb'

Lint/RaiseException:
Enabled: true

Expand Down
6 changes: 3 additions & 3 deletions lib/facter/custom_facts/util/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def content
# wrapper.
def results
parse_results
rescue Exception => e
rescue StandardError => e
Facter.log_exception(e, "Failed to handle #{filename} as #{self.class} facts: #{e.message}")
nil
end
Expand All @@ -68,8 +68,8 @@ def parse_results
def parse_executable_output(output)
res = nil
begin
res = YAML.safe_load output
rescue Exception => e
res = YAML.safe_load(output, [Symbol, Time])
rescue StandardError => e
Facter.debug("Could not parse executable fact output as YAML or JSON (#{e.message})")
end
res = KeyValuePairOutputFormat.parse output unless res.is_a?(Hash)
Expand Down
15 changes: 15 additions & 0 deletions spec/custom_facts/util/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ def expects_parser_to_return_nil_for_directory(path)
expects_script_to_return(cmd, yaml_data, data)
end

it 'handles Symbol correctly' do
yaml_data = "---\n:one: :two\nthree: four\n"
exptected_data = { :one => :two, 'three' => 'four' }
expects_script_to_return(cmd, yaml_data, exptected_data)
end

it 'handles Time correctly' do
yaml_data = "---\nfirst: 2020-07-15 05:38:12.427678398 +00:00\n"
allow(Facter::Core::Execution).to receive(:exec).with(cmd).and_return(yaml_data)
allow(File).to receive(:executable?).with(cmd).and_return(true)
allow(FileTest).to receive(:file?).with(cmd).and_return(true)

expect(LegacyFacter::Util::Parser.parser_for(cmd).results['first']).to be_a(Time)
end

it 'returns an empty hash when the script returns nil' do
expects_script_to_return(cmd, nil, {})
end
Expand Down