Skip to content

Commit 7bcd01a

Browse files
author
willmeek
authored
Merge pull request #184 from DavidS/backports
(maint) backport minor fixes from master to 1.6.x
2 parents bda45be + c6d1b3a commit 7bcd01a

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ matrix:
2020
cache:
2121
bundler: true
2222
directories: ~/.rvm
23-
before_install: rvm use jruby-1.7.26 --install --binary --fuzzy
23+
before_install: rvm use jruby-1.7.26 --install --binary --fuzzy && gem install bundler -v '~> 1.7.0'
2424
# disable coverage on jruby9k as this confuses codecov
2525
- env: RVM="jruby-9.1.9.0" PUPPET_GEM_VERSION='~> 5' JRUBY_OPTS="--debug"
2626
before_cache: pushd ~/.rvm && rm -rf archives rubies/ruby-2.2.7 rubies/ruby-2.3.4 && popd

Gemfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ gemspec
77

88
group :tests do
99
gem 'codecov'
10-
# license_finder does not install on windows using older versions of rubygems.
11-
# ruby 2.4 is confirmed working on appveyor.
12-
gem 'license_finder' if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.0')
1310
gem 'rake', '~> 10.0'
1411
gem 'rspec', '~> 3.0'
1512
# rubocop 0.58 throws when testing against ruby 2.1, so pin to the latest,
1613
# unless we're dealing with jruby...
1714
if RUBY_PLATFORM == 'java'
18-
# load any rubocop version that works on java for the Rakefile
19-
gem 'rubocop'
15+
# load a rubocop version that works on java for the Rakefile
16+
gem 'parser', '2.3.3.1'
17+
gem 'rubocop', '0.41.2'
2018
elsif Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.2.0')
2119
gem 'rubocop', '0.57.2'
2220
# the last version of parallel to support ruby 2.1
@@ -27,6 +25,9 @@ group :tests do
2725
# This needs to be removed once we drop puppet4 support.
2826
gem 'rubocop', '~> 0.57.0'
2927
gem 'rubocop-rspec'
28+
# license_finder does not install on windows using older versions of rubygems.
29+
# ruby 2.4 is confirmed working on appveyor.
30+
gem 'license_finder' if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.0')
3031
end
3132
gem 'simplecov-console'
3233
# the test gems required for module testing

lib/puppet/resource_api/glue.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def to_hash
4545
values
4646
end
4747

48+
def to_json(*)
49+
attrs = filtered_keys.map { |k| [k.to_s, values[k]] unless values[k].nil? }
50+
attributes = Hash[*attrs.compact.flatten]
51+
resource = { title => attributes }
52+
resource.to_json
53+
end
54+
4855
# attribute names that are not title or namevars
4956
def filtered_keys
5057
values.keys.reject { |k| k == :title || !attr_def[k] || (attr_def[k][:behaviour] == :namevar && @namevars.size == 1) }

spec/integration/resource_api_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ def get(_context)
223223
describe 'its title' do
224224
it { expect(instance.to_resource.title).to eq 'somename' }
225225
end
226+
227+
it 'can serialize to json' do
228+
expect({ 'resources' => [instance.to_resource] }.to_json).to eq '{"resources":[{"somename":{"ensure":"absent","boolean_param":false,"integer_param":99,"float_param":3.21,"ensure_param":"present","variant_pattern_param":"1234ABCD","url_param":"http://www.puppet.com","string_array_param":"d","e":"f","string_param":"default value"}}]}' # rubocop:disable Metrics/LineLength
229+
end
226230
end
227231

228232
it('ensure is reported as a symbol') { expect(instance[:ensure]).to be_a Symbol }

spec/puppet/resource_api/glue_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@
4747
end
4848
end
4949

50+
describe '.to_json' do
51+
it { expect(instance.to_json).to eq '{"title":{"attr":"value","attr_ro":"fixed"}}' }
52+
53+
context 'with nil values' do
54+
subject(:instance) do
55+
described_class.new({ namevarname: title, attr: nil, attr_ro: 'fixed' }, 'typename', [:namevarname],
56+
namevarname: { type: 'String', behaviour: :namevar, desc: 'the title' },
57+
attr: { type: 'String', desc: 'a string parameter' },
58+
attr_ro: { type: 'String', desc: 'a string readonly', behaviour: :read_only })
59+
end
60+
61+
it 'doesn\'t output them' do
62+
expect(instance.to_json).to eq '{"title":{"attr_ro":"fixed"}}'
63+
end
64+
end
65+
end
66+
5067
describe '.to_hierayaml' do
5168
it { expect(instance.to_hierayaml).to eq " title:\n attr: value\n attr_ro: fixed\n" }
5269

0 commit comments

Comments
 (0)