Skip to content

Commit 87c9c1f

Browse files
author
Alexander Salmin
committed
Fix merge issue
2 parents fedc008 + 7f93f18 commit 87c9c1f

37 files changed

+78
-245
lines changed

lib/puppet/functions/to_json_pretty.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,32 @@
77
# content => to_json_pretty($myhash),
88
# }
99
#
10+
# @example how to output pretty JSON skipping over keys with undef values
11+
# # output pretty JSON to a file skipping over undef values
12+
# file { '/tmp/my.json':
13+
# ensure => file,
14+
# content => to_json_pretty({
15+
# param_one => 'value',
16+
# param_two => undef,
17+
# }),
18+
# }
1019
#
1120
require 'json'
1221

1322
Puppet::Functions.create_function(:to_json_pretty) do
1423
dispatch :to_json_pretty do
1524
param 'Variant[Hash, Array]', :data
25+
optional_param 'Boolean', :skip_undef
1626
end
1727

18-
def to_json_pretty(data)
19-
JSON.pretty_generate(data)
28+
def to_json_pretty(data, skip_undef = false)
29+
if skip_undef
30+
if data.is_a? Array
31+
data = data.reject { |value| value.nil? }
32+
elsif data.is_a? Hash
33+
data = data.reject { |_, value| value.nil? }
34+
end
35+
end
36+
JSON.pretty_generate(data) << "\n"
2037
end
2138
end

spec/fixtures/test/manifests/absolute_path.pp

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/fixtures/test/manifests/absolutepath.pp

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/fixtures/test/manifests/array.pp

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/fixtures/test/manifests/bool.pp

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Class to test deftype
2-
define test::deftype($param = 'foo') {
2+
define test::deftype( $param = 'foo' ) {
33
notify { "deftype: ${title}": }
44
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# A helper class to test the ensure_resources function
2-
class test::ensure_resources($resource_type, $title_hash, $attributes_hash) {
2+
class test::ensure_resources( $resource_type, $title_hash, $attributes_hash ) {
33
ensure_resources($resource_type, $title_hash, $attributes_hash)
44
}

spec/fixtures/test/manifests/filemode.pp

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/fixtures/test/manifests/float.pp

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/fixtures/test/manifests/hash.pp

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/fixtures/test/manifests/httpsurl.pp

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/fixtures/test/manifests/httpurl.pp

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/fixtures/test/manifests/integer.pp

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/fixtures/test/manifests/ip_address.pp

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/fixtures/test/manifests/ipv4.pp

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/fixtures/test/manifests/ipv6.pp

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/fixtures/test/manifests/numeric.pp

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/fixtures/test/manifests/string.pp

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/fixtures/test/manifests/unixpath.pp

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/fixtures/test/manifests/windowspath.pp

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/functions/to_json_pretty_spec.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
describe 'to_json_pretty' do
44
it { is_expected.not_to eq(nil) }
5-
it { is_expected.to run.with_params([]).and_return("[\n\n]") }
6-
it { is_expected.to run.with_params(['one']).and_return("[\n \"one\"\n]") }
7-
it { is_expected.to run.with_params(%w[one two]).and_return("[\n \"one\",\n \"two\"\n]") }
8-
it { is_expected.to run.with_params({}).and_return("{\n}") }
9-
it { is_expected.to run.with_params('key' => 'value').and_return("{\n \"key\": \"value\"\n}") }
5+
it { is_expected.to run.with_params([]).and_return("[\n\n]\n") }
6+
it { is_expected.to run.with_params(['one']).and_return("[\n \"one\"\n]\n") }
7+
it { is_expected.to run.with_params(%w[one two]).and_return("[\n \"one\",\n \"two\"\n]\n") }
8+
it { is_expected.to run.with_params({}).and_return("{\n}\n") }
9+
it { is_expected.to run.with_params('key' => 'value').and_return("{\n \"key\": \"value\"\n}\n") }
1010
it {
1111
is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => %w[twoA twoB])
12-
.and_return("{\n \"one\": {\n \"oneA\": \"A\",\n \"oneB\": {\n \"oneB1\": \"1\",\n \"oneB2\": \"2\"\n }\n },\n \"two\": [\n \"twoA\",\n \"twoB\"\n ]\n}") # rubocop:disable Metrics/LineLength : Unable to reduce line to required length
12+
.and_return("{\n \"one\": {\n \"oneA\": \"A\",\n \"oneB\": {\n \"oneB1\": \"1\",\n \"oneB2\": \"2\"\n }\n },\n \"two\": [\n \"twoA\",\n \"twoB\"\n ]\n}\n") # rubocop:disable Metrics/LineLength : Unable to reduce line to required length
1313
}
14+
it { is_expected.to run.with_params({ 'one' => '1', 'two' => nil }, true).and_return("{\n \"one\": \"1\"\n}\n") }
15+
it { is_expected.to run.with_params(['one', 'two', nil, 'three'], true).and_return("[\n \"one\",\n \"two\",\n \"three\"\n]\n") }
1416
end

spec/aliases/absolute_path_spec.rb renamed to spec/type_aliases/absolute_path_spec.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4-
describe 'test::absolute_path', type: :class do
4+
describe 'Stdlib::Compat::Absolute_path' do
55
describe 'valid paths handling' do
66
%w[
77
C:/
@@ -20,9 +20,7 @@
2020
/var/ネット
2121
].each do |value|
2222
describe value.inspect do
23-
let(:params) { { value: value } }
24-
25-
it { is_expected.to compile }
23+
it { is_expected.to allow_value(value) }
2624
end
2725
end
2826
end
@@ -38,9 +36,7 @@
3836
'',
3937
].each do |value|
4038
describe value.inspect do
41-
let(:params) { { value: value } }
42-
43-
it { is_expected.to compile.and_raise_error(%r{parameter 'value' expects a match for Stdlib::Compat::Absolute_path}) }
39+
it { is_expected.not_to allow_value(value) }
4440
end
4541
end
4642
end
@@ -59,9 +55,7 @@
5955
\var\ネット
6056
].each do |value|
6157
describe value.inspect do
62-
let(:params) { { value: value } }
63-
64-
it { is_expected.to compile.and_raise_error(%r{parameter 'value' expects a match for Stdlib::Compat::Absolute_path}) }
58+
it { is_expected.not_to allow_value(value) }
6559
end
6660
end
6761
end

spec/aliases/array_spec.rb renamed to spec/type_aliases/array_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4-
describe 'test::array', type: :class do
4+
describe 'Stdlib::Compat::Array' do
55
describe 'accepts arrays' do
66
[
77
[],
@@ -11,9 +11,7 @@
1111
[[]],
1212
].each do |value|
1313
describe value.inspect do
14-
let(:params) { { value: value } }
15-
16-
it { is_expected.to compile }
14+
it { is_expected.to allow_value(value) }
1715
end
1816
end
1917
end
@@ -26,9 +24,7 @@
2624
{},
2725
].each do |value|
2826
describe value.inspect do
29-
let(:params) { { value: value } }
30-
31-
it { is_expected.to compile.and_raise_error(%r{parameter 'value' expects a Stdlib::Compat::Array}) }
27+
it { is_expected.not_to allow_value(value) }
3228
end
3329
end
3430
end

spec/aliases/bool_spec.rb renamed to spec/type_aliases/bool_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
require 'spec_helper'
22

33
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4-
describe 'test::bool', type: :class do
4+
describe 'Stdlib::Compat::Bool' do
55
describe 'accepts booleans' do
66
[
77
true,
88
false,
99
].each do |value|
1010
describe value.inspect do
11-
let(:params) { { value: value } }
12-
13-
it { is_expected.to compile }
11+
it { is_expected.to allow_value(value) }
1412
end
1513
end
1614
end
@@ -24,9 +22,7 @@
2422
'false',
2523
].each do |value|
2624
describe value.inspect do
27-
let(:params) { { value: value } }
28-
29-
it { is_expected.to compile.and_raise_error(%r{parameter 'value' expects a Stdlib::Compat::Bool}) }
25+
it { is_expected.not_to allow_value(value) }
3026
end
3127
end
3228
end

spec/aliases/filemode_spec.rb renamed to spec/type_aliases/filemode_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4-
describe 'test::filemode', type: :class do
4+
describe 'Stdlib::Filemode' do
55
describe 'valid modes' do
66
%w[
77
0644
@@ -12,9 +12,7 @@
1212
0777
1313
].each do |value|
1414
describe value.inspect do
15-
let(:params) { { value: value } }
16-
17-
it { is_expected.to compile }
15+
it { is_expected.to allow_value(value) }
1816
end
1917
end
2018
end
@@ -39,9 +37,7 @@
3937
'0649',
4038
].each do |value|
4139
describe value.inspect do
42-
let(:params) { { value: value } }
43-
44-
it { is_expected.to compile.and_raise_error(%r{parameter 'value' expects a match for Stdlib::Filemode}) }
40+
it { is_expected.not_to allow_value(value) }
4541
end
4642
end
4743
end

spec/aliases/float_spec.rb renamed to spec/type_aliases/float_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4-
describe 'test::float', type: :class do
4+
describe 'Stdlib::Compat::Float' do
55
describe 'accepts floats' do
66
[
77
3.7,
@@ -10,19 +10,15 @@
1010
'-342.2315e-12',
1111
].each do |value|
1212
describe value.inspect do
13-
let(:params) { { value: value } }
14-
15-
it { is_expected.to compile }
13+
it { is_expected.to allow_value(value) }
1614
end
1715
end
1816
end
1917

2018
describe 'rejects other values' do
2119
[true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1 => 2 }, '', :undef, 'x', 3, '3', -3, '-3'].each do |value|
2220
describe value.inspect do
23-
let(:params) { { value: value } }
24-
25-
it { is_expected.to compile.and_raise_error(%r{parameter 'value' expects.*Float.*Pattern}) }
21+
it { is_expected.not_to allow_value(value) }
2622
end
2723
end
2824
end

spec/aliases/hash_spec.rb renamed to spec/type_aliases/hash_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4-
describe 'test::hash', type: :class do
4+
describe 'Stdlib::Compat::Hash' do
55
describe 'accepts hashes' do
66
[
77
{},
@@ -10,9 +10,7 @@
1010
{ '001' => 'helly' },
1111
].each do |value|
1212
describe value.inspect do
13-
let(:params) { { value: value } }
14-
15-
it { is_expected.to compile }
13+
it { is_expected.to allow_value(value) }
1614
end
1715
end
1816
end
@@ -24,9 +22,7 @@
2422
[],
2523
].each do |value|
2624
describe value.inspect do
27-
let(:params) { { value: value } }
28-
29-
it { is_expected.to compile.and_raise_error(%r{parameter 'value' expects a Stdlib::Compat::Hash}) }
25+
it { is_expected.not_to allow_value(value) }
3026
end
3127
end
3228
end

0 commit comments

Comments
 (0)