Skip to content

Commit 1fba250

Browse files
authored
Merge pull request #9471 from justinstoller/thats-rich
2 parents 7bd9221 + 8633e94 commit 1fba250

File tree

7 files changed

+44
-11
lines changed

7 files changed

+44
-11
lines changed

lib/puppet.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ def self.base_context(settings)
237237
:ssl_context => proc { Puppet.runtime[:http].default_ssl_context },
238238
:http_session => proc { Puppet.runtime[:http].create_session },
239239
:plugins => proc { Puppet::Plugins::Configuration.load_plugins },
240-
:rich_data => false
240+
:rich_data => Puppet[:rich_data],
241+
# `stringify_rich` controls whether `rich_data` is stringified into a lossy format
242+
# instead of a lossless format. Catalogs should not be stringified, though to_yaml
243+
# and the resource application have uses for a lossy, user friendly format.
244+
:stringify_rich => false
241245
}
242246
end
243247

lib/puppet/application/resource.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ def main
146146
# If the specified environment does not exist locally, fall back to the default (production) environment
147147
env = Puppet.lookup(:environments).get(Puppet[:environment]) || create_default_environment
148148

149-
Puppet.override(:current_environment => env, :loaders => Puppet::Pops::Loaders.new(env)) do
149+
Puppet.override(
150+
current_environment: env,
151+
loaders: Puppet::Pops::Loaders.new(env),
152+
stringify_rich: true
153+
) do
150154
type, name, params = parse_args(command_line.args)
151155

152156
raise _("Editing with Yaml output is not supported") if options[:edit] and options[:to_yaml]

lib/puppet/resource.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def to_data_hash
112112
# To get stringified parameter values the flag :stringify_rich can be set
113113
# in the puppet context.
114114
#
115-
stringify = Puppet.lookup(:stringify_rich) { false }
115+
stringify = Puppet.lookup(:stringify_rich)
116116
converter = stringify ? Puppet::Pops::Serialization::ToStringifiedConverter.new : nil
117117

118118
params = {}

spec/integration/application/apply_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,12 @@ def bogus()
447447
Puppet[:strict] = :warning
448448
end
449449

450+
around :each do |test|
451+
Puppet.override(rich_data: false) do
452+
test.run
453+
end
454+
end
455+
450456
it 'will notify a string that is the result of Regexp#inspect (from Runtime3xConverter)' do
451457
catalog = compile_to_catalog(execute, node)
452458
apply.command_line.args = ['--catalog', file_containing('manifest', catalog.to_json)]

spec/unit/application/resource_spec.rb

-7
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,6 @@ def string
170170
expect { @resource_app.main }.not_to raise_error
171171
end
172172

173-
it "should raise an error when printing yaml by default" do
174-
@resource_app.options[:to_yaml] = true
175-
allow(@resource_app.command_line).to receive(:args).and_return(['stringify', 'hello', 'ensure=present', 'string=asd'])
176-
expect { @resource_app.main }.to raise_error( Puppet::PreformattedError,
177-
/Stringify\[hello\]\['string'\] contains a Puppet::Util::Execution::ProcessOutput value. It will be converted to the String 'test'/)
178-
end
179-
180173
it "should ensure all values to be printed are in the external encoding" do
181174
resources = [
182175
Puppet::Type.type(:user).new(:name => "\u2603".force_encoding(Encoding::UTF_8)).to_resource,

spec/unit/resource/catalog_spec.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -965,10 +965,15 @@ class {'multi_param_class':
965965

966966
context 'and rich_data is disabled' do
967967
before(:each) do
968-
Puppet[:rich_data] = false
969968
Puppet[:strict] = :warning # do not want to stub out behavior in tests
970969
end
971970

971+
around(:each) do |test|
972+
Puppet.override(rich_data: false) do
973+
test.run
974+
end
975+
end
976+
972977
let(:catalog_w_regexp) { compile_to_catalog("notify {'foo': message => /[a-z]+/ }") }
973978

974979
it 'should not generate rich value hash for parameter values that are not Data' do

spec/unit/resource_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,27 @@ def inject_and_set_defaults(resource, scope)
906906
# Note: to_stringified_spec.rb has tests for all other data types
907907
end
908908

909+
describe 'when serializing resources' do
910+
require 'puppet_spec/compiler'
911+
include PuppetSpec::Compiler
912+
913+
it 'serializes rich data' do
914+
resource = compile_to_catalog('notify {"foo": message => Deferred("func", ["a", "b", "c"])}')
915+
916+
# This assume rich_data is true by default
917+
expect(resource.to_data_hash.class).to be(Hash)
918+
end
919+
920+
it 'raises when rich data is disabled' do
921+
resource = compile_to_catalog('notify {"foo": message => Deferred("func", ["a", "b", "c"])}')
922+
expect {
923+
Puppet.override(rich_data: false) do
924+
resource.to_data_hash
925+
end
926+
}.to raise_error(Puppet::PreformattedError)
927+
end
928+
end
929+
909930
describe "when converting from json" do
910931
before do
911932
@data = {

0 commit comments

Comments
 (0)