Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
Merge 010ad22 into 950dff8
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanIrimie committed May 11, 2020
2 parents 950dff8 + 010ad22 commit 3cb42d7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/custom_facts/util/normalization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class NormalizationError < StandardError; end
# @return [void]
def normalize(value)
case value
when Integer, Float, TrueClass, FalseClass, NilClass, Symbol
when Integer, Float, TrueClass, FalseClass, NilClass, Symbol, Date
value
when String
normalize_string(value)
Expand Down
12 changes: 10 additions & 2 deletions lib/custom_facts/util/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# facts such as scripts, text, json and yaml files.
#
# Parsers must subclass this class and provide their own #results method.
require 'yaml'

module LegacyFacter
module Util
Expand Down Expand Up @@ -93,9 +92,18 @@ def self.parse(output)
end
end

# This regex was taken from Psych and adapted
# https://github.com/ruby/psych/blob/d2deaa9adfc88fc0b870df022a434d6431277d08/lib/psych/scalar_scanner.rb#L9
# It is used to detect Time in YAML, but we use it to wrap time objects in quotes to be treated as strings.
TIME =
/(\d{4}-\d{1,2}-\d{1,2}(?:[Tt]|\s+)\d{1,2}:\d\d:\d\d(?:\.\d*)?(?:\s*(?:Z|[-+]\d{1,2}:?(?:\d\d)?))?\s*$)/.freeze

class YamlParser < Base
def parse_results
YAML.safe_load(content)
# Add quotes to Yaml time
cont = content.gsub(TIME, '"\1"')

YAML.safe_load(cont, [Date])
end
end

Expand Down
38 changes: 38 additions & 0 deletions spec/custom_facts/util/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,42 @@ def expects_to_parse_powershell(cmd, result)
expect(LegacyFacter::Util::Parser.parser_for('this.is.not.valid').results).to be nil
end
end

describe LegacyFacter::Util::Parser::YamlParser do
let(:yaml_parser) { LegacyFacter::Util::Parser::YamlParser.new(nil, yaml_content) }

describe '#parse_results' do
context 'when yaml contains Time formatted fields' do
context 'when time zone is present' do
let(:yaml_content) { load_fixture('external_fact_yaml').read }

it 'treats it as a string' do
expected_result = { 'testsfact' => { 'time' => '2020-04-28 01:44:08.148119000 +01:01' } }

expect(yaml_parser.parse_results).to eq(expected_result)
end
end

context 'when time zone is missing' do
let(:yaml_content) { load_fixture('external_fact_yaml_no_zone').read }

it 'is interpreted as a string' do
expected_result = { 'testsfact' => { 'time' => '2020-04-28 01:44:08.148119000' } }

expect(yaml_parser.parse_results).to eq(expected_result)
end
end
end

context 'when yaml contains Date formatted fields' do
let(:yaml_content) { load_fixture('external_fact_yaml_date').read }

it 'loads date' do
expected_result = { 'testsfact' => { 'date' => Date.parse('2020-04-28') } }

expect(yaml_parser.parse_results).to eq(expected_result)
end
end
end
end
end
3 changes: 3 additions & 0 deletions spec/fixtures/external_fact_yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
testsfact:
time: 2020-04-28 01:44:08.148119000 +01:01
3 changes: 3 additions & 0 deletions spec/fixtures/external_fact_yaml_date
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
testsfact:
date: 2020-04-28
3 changes: 3 additions & 0 deletions spec/fixtures/external_fact_yaml_no_zone
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
testsfact:
time: 2020-04-28 01:44:08.148119000

0 comments on commit 3cb42d7

Please sign in to comment.