Skip to content

Commit 42cb3a3

Browse files
authored
Merge pull request #8557 from luchihoratiu/PUP-10973
2 parents 61908fc + 07cbf88 commit 42cb3a3

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

lib/puppet/face/facts.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@
117117
$ puppet facts diff
118118
EOT
119119

120-
render_as :json
121-
122120
when_invoked do |*args|
123121
Puppet.settings.preferred_run_mode = :agent
124122
Puppet::Node::Facts.indirection.terminus_class = :facter
@@ -142,6 +140,15 @@
142140
exit 0
143141
end
144142
end
143+
144+
when_rendering :console do |result|
145+
case result
146+
when Array, Hash
147+
Puppet::Util::Json.dump(result, :pretty => true)
148+
else
149+
result
150+
end
151+
end
145152
end
146153

147154
action(:show) do

spec/unit/application/facts_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,73 @@
115115
end
116116
end
117117

118+
context 'when diff action is called' do
119+
let(:facter3_facts) { <<~END }
120+
{
121+
"macaddress": "64:52:11:22:03:2e",
122+
"filesystems": "apfs,autofs"
123+
}
124+
END
125+
126+
let(:facter4_facts) { <<~END }
127+
{
128+
"macaddress": "64:52:11:22:03:2e",
129+
"filesystems": "apfs,autofs,devfs"
130+
}
131+
END
132+
133+
before :each do
134+
Puppet::Node::Facts.indirection.terminus_class = :facter
135+
app.command_line.args = %w{diff}
136+
137+
allow(Facter).to receive(:value).with('facterversion').and_return('3.99.0')
138+
allow(Puppet::Util::Execution).to receive(:execute).with(/puppet facts show --no-facterng/).and_return(facter3_facts)
139+
allow(Puppet::Util::Execution).to receive(:execute).with(/puppet facts show --facterng/).and_return(facter4_facts)
140+
end
141+
142+
shared_examples_for 'correctly rendering output' do |render_format|
143+
it 'correctly displays output' do
144+
app.command_line.args << '--render-as' << render_format if render_format
145+
expect {
146+
app.run
147+
}.to exit_with(0)
148+
.and output(expected_output).to_stdout
149+
end
150+
end
151+
152+
context 'when formatting is set to default' do
153+
let(:expected_output) { <<~END }
154+
{
155+
"filesystems": {
156+
"new_value": "apfs,autofs,devfs",
157+
"old_value": "apfs,autofs"
158+
}
159+
}
160+
END
161+
162+
it_behaves_like 'correctly rendering output'
163+
end
164+
165+
context 'when formatting is set to yaml' do
166+
let(:expected_output) { <<~END }
167+
---
168+
filesystems:
169+
:new_value: apfs,autofs,devfs
170+
:old_value: apfs,autofs
171+
END
172+
173+
it_behaves_like 'correctly rendering output', 'yaml'
174+
end
175+
176+
context 'when formatting is set to json' do
177+
let(:expected_output) { <<~END }
178+
{"filesystems":{"new_value":"apfs,autofs,devfs","old_value":"apfs,autofs"}}
179+
END
180+
181+
it_behaves_like 'correctly rendering output', 'json'
182+
end
183+
end
184+
118185
context 'when default action is called' do
119186
before :each do
120187
Puppet::Node::Facts.indirection.terminus_class = :memory

0 commit comments

Comments
 (0)