Skip to content

Commit c11abe4

Browse files
authored
Merge pull request #8500 from joshcooper/dont_quote_values_10861
(PUP-10861) Don't JSON.dump single fact values
2 parents 01310f1 + 0de8cfc commit c11abe4

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

lib/puppet/face/facts.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,23 @@
151151
options[:resolve_options] = true
152152
result = Puppet::Node::Facts.indirection.find(Puppet.settings[:certname], options)
153153

154-
facts = result.values
155-
156154
if options[:value_only]
157-
facts.values.first
155+
result.values.values.first
158156
else
159-
facts
157+
result.values
160158
end
161159
end
162160

163161
when_rendering :console do |result|
164-
Puppet::Util::Json.dump(result, :pretty => true)
162+
# VALID_TYPES = [Integer, Float, TrueClass, FalseClass, NilClass, Symbol, String, Array, Hash].freeze
163+
# from https://github.com/puppetlabs/facter/blob/4.0.49/lib/facter/custom_facts/util/normalization.rb#L8
164+
165+
case result
166+
when Array, Hash
167+
Puppet::Util::Json.dump(result, :pretty => true)
168+
else # one of VALID_TYPES above
169+
result
170+
end
165171
end
166172
end
167173
end

spec/unit/application/facts_spec.rb

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@
5252
end
5353

5454
context 'when show action is called' do
55-
let(:expected) { "{\n \"filesystems\": \"apfs,autofs,devfs\",\n \"macaddress\": \"64:52:11:22:03:25\"\n}\n" }
55+
let(:expected) { <<~END }
56+
{
57+
"filesystems": "apfs,autofs,devfs",
58+
"macaddress": "64:52:11:22:03:25"
59+
}
60+
END
5661

5762
before :each do
5863
Puppet::Node::Facts.indirection.terminus_class = :facter
@@ -64,12 +69,58 @@
6469
expect {
6570
app.run
6671
}.to exit_with(0)
67-
.and output(expected).to_stdout
72+
.and output(expected).to_stdout
73+
end
74+
75+
it 'displays a single fact value' do
76+
app.command_line.args << 'filesystems' << '--value-only'
77+
expect {
78+
app.run
79+
}.to exit_with(0)
80+
.and output("apfs,autofs,devfs\n").to_stdout
81+
end
82+
83+
it "warns and ignores value-only when multiple fact names are specified" do
84+
app.command_line.args << 'filesystems' << 'macaddress' << '--value-only'
85+
expect {
86+
app.run
87+
}.to exit_with(0)
88+
.and output(expected).to_stdout
89+
.and output(/it can only be used when querying for a single fact/).to_stderr
90+
end
91+
92+
{
93+
"type_hash" => [{'a' => 2}, "{\n \"a\": 2\n}"],
94+
"type_array" => [[], "[\n\n]"],
95+
"type_string" => ["str", "str"],
96+
"type_int" => [1, "1"],
97+
"type_float" => [1.0, "1.0"],
98+
"type_true" => [true, "true"],
99+
"type_false" => [false, "false"],
100+
"type_nil" => [nil, ""],
101+
"type_sym" => [:sym, "sym"]
102+
}.each_pair do |name, values|
103+
it "renders '#{name}' as '#{values.last}'" do
104+
fact_value = values.first
105+
fact_output = values.last
106+
107+
allow(Facter).to receive(:resolve).and_return({name => fact_value})
108+
109+
app.command_line.args << name << '--value-only'
110+
expect {
111+
app.run
112+
}.to exit_with(0)
113+
.and output("#{fact_output}\n").to_stdout
114+
end
68115
end
69116
end
70117

71118
context 'when default action is called' do
72-
let(:expected) { "---\nfilesystems: apfs,autofs,devfs\nmacaddress: 64:52:11:22:03:25\n" }
119+
let(:expected) { <<~END }
120+
---
121+
filesystems: apfs,autofs,devfs
122+
macaddress: 64:52:11:22:03:25
123+
END
73124

74125
before :each do
75126
Puppet::Node::Facts.indirection.terminus_class = :facter
@@ -81,7 +132,7 @@
81132
expect {
82133
app.run
83134
}.to exit_with(0)
84-
.and output(expected).to_stdout
135+
.and output(expected).to_stdout
85136
expect(app.action.name).to eq(:show)
86137
end
87138
end

0 commit comments

Comments
 (0)