diff --git a/lib/facter/framework/formatters/legacy_fact_formatter.rb b/lib/facter/framework/formatters/legacy_fact_formatter.rb index c75a14099f..37c9725515 100644 --- a/lib/facter/framework/formatters/legacy_fact_formatter.rb +++ b/lib/facter/framework/formatters/legacy_fact_formatter.rb @@ -26,7 +26,8 @@ def format_for_no_query(resolved_facts) pretty_json = hash_to_facter_format(fact_collection) pretty_json = remove_enclosing_accolades(pretty_json) - remove_comma_and_quotation(pretty_json) + pretty_json = remove_comma_and_quotation(pretty_json) + handle_newlines(pretty_json) end def format_for_multiple_user_queries(user_queries, resolved_facts) @@ -38,6 +39,7 @@ def format_for_multiple_user_queries(user_queries, resolved_facts) pretty_json = hash_to_facter_format(facts_to_display) pretty_json = remove_enclosing_accolades(pretty_json) pretty_json = remove_comma_and_quotation(pretty_json) + pretty_json = handle_newlines(pretty_json) @log.debug('Remove quotes from value if value is a string') pretty_json.gsub(/^(\S*) => \"(.*)\"/, '\1 => \2') @@ -84,6 +86,11 @@ def remove_enclosing_accolades(pretty_fact_json) pretty_fact_json.gsub(/^},/, '}') end + def handle_newlines(pretty_fact_json) + @log.debug('Convert newline characters to actual newlines') + pretty_fact_json.gsub('\n', "\n") + end + def remove_comma_and_quotation(output) # quotation marks that come after \ are not removed @log.debug('Remove unnecessary comma and quotation marks on root facts') diff --git a/spec/framework/formatters/legacy_fact_formatter_spec.rb b/spec/framework/formatters/legacy_fact_formatter_spec.rb index 23a3cba2c9..9effa6ce41 100644 --- a/spec/framework/formatters/legacy_fact_formatter_spec.rb +++ b/spec/framework/formatters/legacy_fact_formatter_spec.rb @@ -248,4 +248,16 @@ .to eq('C:\\Program Files\\App => bin_dir') end end + + context 'when fact value contains newline' do + let(:resolved_fact) do + instance_spy(Facter::ResolvedFact, name: 'custom_fact', value: 'value1 \n value2', + user_query: '', filter_tokens: [], type: :core) + end + + it 'formats the fact correctly' do + expect(legacy_formatter.format([resolved_fact])) + .to eq("custom_fact => value1 \n value2") + end + end end