Skip to content

Commit

Permalink
(FACT-2457) Display newlines in values
Browse files Browse the repository at this point in the history
  • Loading branch information
florindragos committed Sep 2, 2020
1 parent ab19001 commit 8555336
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/facter/framework/formatters/legacy_fact_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
12 changes: 12 additions & 0 deletions spec/framework/formatters/legacy_fact_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8555336

Please sign in to comment.