From 2d3166f28ef3dc2cea11979ae058b977d4ab45b0 Mon Sep 17 00:00:00 2001 From: David Swan Date: Mon, 4 Jun 2018 13:56:36 +0100 Subject: [PATCH] PDK Convert 1.5.0 --- .gitignore | 38 +- .rubocop.yml | 15 +- .travis.yml | 85 ++-- Gemfile | 128 +++--- Rakefile | 35 +- lib/puppet/feature/hocon.rb | 5 +- lib/puppet/provider/hocon_setting/ruby.rb | 62 +-- lib/puppet/type/hocon_setting.rb | 115 +++--- metadata.json | 7 +- spec/acceptance/conf_setting_spec.rb | 82 ++-- spec/spec_helper.rb | 43 ++- spec/spec_helper_acceptance.rb | 8 +- .../puppet/provider/conf_setting/ruby_spec.rb | 365 ++++++++++-------- spec/unit/puppet/type/hocon_setting_spec.rb | 62 +-- 14 files changed, 575 insertions(+), 475 deletions(-) diff --git a/.gitignore b/.gitignore index a102277..49bc2a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,23 @@ -# This file is generated by ModuleSync, do not edit. -*.iml .*.sw[op] -.DS_Store -.bundle/ -.idea/ .metadata -.vagrant/ .yardoc .yardwarns -Gemfile.local -Gemfile.lock -bin/ -coverage/ -doc/ -junit/ -log/ -pkg/ -spec/fixtures/manifests/ -spec/fixtures/modules/ -tmp/ -vendor/ - +*.iml +/.bundle/ +/.idea/ +/.vagrant/ +/coverage/ +/bin/ +/doc/ +/Gemfile.local +/Gemfile.lock +/junit/ +/log/ +/pkg/ +/spec/fixtures/manifests/ +/spec/fixtures/modules/ +/tmp/ +/vendor/ +/convert_report.txt +/update_report.txt +.DS_Store diff --git a/.rubocop.yml b/.rubocop.yml index 515ef63..40a58e0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,7 @@ --- -require: - - rubocop-rspec +require: rubocop-rspec AllCops: + DisplayCopNames: true TargetRubyVersion: '2.1' Include: - "./**/*.rb" @@ -13,7 +13,6 @@ AllCops: - pkg/**/* - spec/fixtures/**/* - vendor/**/* -inherit_from: .rubocop_todo.yml Metrics/LineLength: Description: People have wide screens, use them. Max: 200 @@ -64,12 +63,16 @@ Style/TrailingCommaInLiteral: Style/SymbolArray: Description: Using percent style obscures symbolic intent of array's contents. EnforcedStyle: brackets +RSpec/MessageSpies: + EnforcedStyle: receive Style/CollectionMethods: Enabled: true Style/MethodCalledOnDoEndBlock: Enabled: true Style/StringMethods: Enabled: true +Layout/EndOfLine: + Enabled: false Metrics/AbcSize: Enabled: false Metrics/BlockLength: @@ -88,8 +91,14 @@ Metrics/PerceivedComplexity: Enabled: false RSpec/DescribeClass: Enabled: false +RSpec/ExampleLength: + Enabled: false RSpec/MessageExpectation: Enabled: false +RSpec/MultipleExpectations: + Enabled: false +RSpec/NestedGroups: + Enabled: false Style/AsciiComments: Enabled: false Style/IfUnlessModifier: diff --git a/.travis.yml b/.travis.yml index 160a724..1428837 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,36 +1,67 @@ -#This file is generated by ModuleSync, do not edit. --- sudo: false +dist: trusty language: ruby cache: bundler -script: "bundle exec rake release_checks" +before_install: + - bundle -v + - rm -f Gemfile.lock + - gem update --system + - gem update bundler + - gem --version + - bundle -v +script: + - 'bundle exec rake $CHECK' +bundler_args: --without system_tests +rvm: + - 2.4.1 +env: + - PUPPET_GEM_VERSION="~> 5.0" CHECK=spec matrix: fast_finish: true include: - - rvm: 2.3.1 - dist: trusty - env: PUPPET_INSTALL_TYPE=agent BEAKER_debug=true BEAKER_set=docker/ubuntu-14.04 - script: bundle exec rake beaker - services: docker - sudo: required - - rvm: 2.3.1 - dist: trusty - env: PUPPET_INSTALL_TYPE=agent BEAKER_debug=true BEAKER_set=docker/centos-7 - script: bundle exec rake beaker - services: docker - sudo: required - - rvm: 2.4.1 - bundler_args: --without system_tests - env: PUPPET_GEM_VERSION="~> 5.0" - - rvm: 2.1.9 - bundler_args: --without system_tests - env: PUPPET_GEM_VERSION="~> 4.0" + - + bundler_args: + dist: trusty + env: PUPPET_INSTALL_TYPE=agent BEAKER_debug=true BEAKER_set=docker/centos-7 + rvm: 2.4.1 + script: bundle exec rake beaker + services: docker + sudo: required + - + bundler_args: + dist: trusty + env: PUPPET_INSTALL_TYPE=agent BEAKER_debug=true BEAKER_set=docker/ubuntu-14.04 + rvm: 2.4.1 + script: bundle exec rake beaker + services: docker + sudo: required + - + env: CHECK=rubocop + - + env: CHECK="syntax lint" + - + env: CHECK=metadata_lint + - + env: CHECK=release_checks + - + env: CHECK=spec + - + env: PUPPET_GEM_VERSION="~> 4.0" CHECK=spec + rvm: 2.1.9 +branches: + only: + - master + - /^v\d/ + - release notifications: email: false - hipchat: - rooms: - secure: ZnXjXpfn2I11z1/x4DS+o2YnTW5Gll8xMOXvF68HyTQTtJCgIXVzmyq6w1oNAKDBEOm6jWJ6kQA2fD2GSktk+Py0hDkNs232BGDf1nvQ7naBXZMfpkdwVG4hYNR20iirJW5FN2BaqoU8KusForv/Sp6veQkVo7Tw0BjfgFiJKP8= - template: - - ! '%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}' - - ! 'Change view: %{compare_url}' - - ! 'Build details: %{build_url}' +deploy: + provider: puppetforge + user: puppet + password: + secure: "" + on: + tags: true + all_branches: true + condition: "$DEPLOY_TO_FORGE = yes" diff --git a/Gemfile b/Gemfile index 4b3f4d5..9cc8e34 100644 --- a/Gemfile +++ b/Gemfile @@ -1,85 +1,89 @@ -#This file is generated by ModuleSync, do not edit. +source ENV['GEM_SOURCE'] || 'https://rubygems.org' -source ENV['GEM_SOURCE'] || "https://rubygems.org" +def location_for(place_or_version, fake_version = nil) + if place_or_version =~ %r{\A(git[:@][^#]*)#(.*)} + [fake_version, { git: Regexp.last_match(1), branch: Regexp.last_match(2), require: false }].compact + elsif place_or_version =~ %r{\Afile:\/\/(.*)} + ['>= 0', { path: File.expand_path(Regexp.last_match(1)), require: false }] + else + [place_or_version, { require: false }] + end +end -# Determines what type of gem is requested based on place_or_version. def gem_type(place_or_version) - if place_or_version =~ /^git:/ + if place_or_version =~ %r{\Agit[:@]} :git - elsif place_or_version =~ /^file:/ + elsif !place_or_version.nil? && place_or_version.start_with?('file:') :file else :gem end end -# Find a location or specific version for a gem. place_or_version can be a -# version, which is most often used. It can also be git, which is specified as -# `git://somewhere.git#branch`. You can also use a file source location, which -# is specified as `file://some/location/on/disk`. -def location_for(place_or_version, fake_version = nil) - if place_or_version =~ /^(git[:@][^#]*)#(.*)/ - [fake_version, { :git => $1, :branch => $2, :require => false }].compact - elsif place_or_version =~ /^file:\/\/(.*)/ - ['>= 0', { :path => File.expand_path($1), :require => false }] - else - [place_or_version, { :require => false }] - end -end - -# Used for gem conditionals ruby_version_segments = Gem::Version.new(RUBY_VERSION.dup).segments -minor_version = "#{ruby_version_segments[0]}.#{ruby_version_segments[1]}" - -# The following gems are not included by default as they require DevKit on Windows. -# You should probably include them in a Gemfile.local or a ~/.gemfile -#gem 'pry' #this may already be included in the gemfile -#gem 'pry-stack_explorer', :require => false -#if RUBY_VERSION =~ /^2/ -# gem 'pry-byebug' -#else -# gem 'pry-debugger' -#end +minor_version = ruby_version_segments[0..1].join('.') group :development do - gem "puppet-module-posix-default-r#{minor_version}", :require => false, :platforms => "ruby" - gem "puppet-module-win-default-r#{minor_version}", :require => false, :platforms => ["mswin", "mingw", "x64_mingw"] - gem "puppet-module-posix-dev-r#{minor_version}", :require => false, :platforms => "ruby" - gem "puppet-module-win-dev-r#{minor_version}", '0.0.7', :require => false, :platforms => ["mswin", "mingw", "x64_mingw"] - gem "json_pure", '<= 2.0.1', :require => false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.0.0') - gem "fast_gettext", '1.1.0', :require => false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.1.0') - gem "fast_gettext", :require => false if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1.0') - gem "hocon", '~> 1.1', :require => false + gem "fast_gettext", '1.1.0', require: false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.1.0') + gem "fast_gettext", require: false if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1.0') + gem "json_pure", '<= 2.0.1', require: false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.0.0') + gem "json", '= 1.8.1', require: false if Gem::Version.new(RUBY_VERSION.dup) == Gem::Version.new('2.1.9') + gem "json", '<= 2.0.4', require: false if Gem::Version.new(RUBY_VERSION.dup) == Gem::Version.new('2.4.4') + gem "puppet-module-posix-default-r#{minor_version}", require: false, platforms: [:ruby] + gem "puppet-module-posix-dev-r#{minor_version}", require: false, platforms: [:ruby] + gem "puppet-module-win-default-r#{minor_version}", require: false, platforms: [:mswin, :mingw, :x64_mingw] + gem "puppet-module-win-dev-r#{minor_version}", require: false, platforms: [:mswin, :mingw, :x64_mingw] + gem "puppet-blacksmith", '~> 3.4', require: false, platforms: [:ruby] + gem "hocon", '~> 1.1', require: false end - group :system_tests do - gem "puppet-module-posix-system-r#{minor_version}", :require => false, :platforms => "ruby" - gem "puppet-module-win-system-r#{minor_version}", :require => false, :platforms => ["mswin", "mingw", "x64_mingw"] - gem "beaker", *location_for(ENV['BEAKER_VERSION'] || '>= 3') - gem "beaker-pe", :require => false - gem "beaker-rspec", *location_for(ENV['BEAKER_RSPEC_VERSION']) - gem "beaker-hostgenerator", *location_for(ENV['BEAKER_HOSTGENERATOR_VERSION']) - gem "beaker-abs", *location_for(ENV['BEAKER_ABS_VERSION'] || '~> 0.1') - gem "puppet-blacksmith", '~> 3.4', :require => false + gem "puppet-module-posix-system-r#{minor_version}", require: false, platforms: [:ruby] + gem "puppet-module-win-system-r#{minor_version}", require: false, platforms: [:mswin, :mingw, :x64_mingw] + gem "beaker", *location_for(ENV['BEAKER_VERSION'] || '~> 3.13') + gem "beaker-abs", *location_for(ENV['BEAKER_ABS_VERSION'] || '~> 0.1') + gem "beaker-pe", require: false + gem "beaker-hostgenerator" + gem "beaker-rspec" end -gem 'puppet', *location_for(ENV['PUPPET_GEM_VERSION']) +puppet_version = ENV['PUPPET_GEM_VERSION'] +puppet_type = gem_type(puppet_version) +facter_version = ENV['FACTER_GEM_VERSION'] +hiera_version = ENV['HIERA_GEM_VERSION'] + +gems = {} + +gems['puppet'] = location_for(puppet_version) -# Only explicitly specify Facter/Hiera if a version has been specified. -# Otherwise it can lead to strange bundler behavior. If you are seeing weird -# gem resolution behavior, try setting `DEBUG_RESOLVER` environment variable -# to `1` and then run bundle install. -gem 'facter', *location_for(ENV['FACTER_GEM_VERSION']) if ENV['FACTER_GEM_VERSION'] -gem 'hiera', *location_for(ENV['HIERA_GEM_VERSION']) if ENV['HIERA_GEM_VERSION'] +# If facter or hiera versions have been specified via the environment +# variables -# Evaluate Gemfile.local if it exists -if File.exists? "#{__FILE__}.local" - eval(File.read("#{__FILE__}.local"), binding) +gems['facter'] = location_for(facter_version) if facter_version +gems['hiera'] = location_for(hiera_version) if hiera_version + +if Gem.win_platform? && puppet_version =~ %r{^(file:///|git://)} + # If we're using a Puppet gem on Windows which handles its own win32-xxx gem + # dependencies (>= 3.5.0), set the maximum versions (see PUP-6445). + gems['win32-dir'] = ['<= 0.4.9', require: false] + gems['win32-eventlog'] = ['<= 0.6.5', require: false] + gems['win32-process'] = ['<= 0.7.5', require: false] + gems['win32-security'] = ['<= 0.2.5', require: false] + gems['win32-service'] = ['0.8.8', require: false] end -# Evaluate ~/.gemfile if it exists -if File.exists?(File.join(Dir.home, '.gemfile')) - eval(File.read(File.join(Dir.home, '.gemfile')), binding) +gems.each do |gem_name, gem_params| + gem gem_name, *gem_params end -# vim:ft=ruby +# Evaluate Gemfile.local and ~/.gemfile if they exist +extra_gemfiles = [ + "#{__FILE__}.local", + File.join(Dir.home, '.gemfile'), +] + +extra_gemfiles.each do |gemfile| + if File.file?(gemfile) && File.readable?(gemfile) + eval(File.read(gemfile), binding) + end +end +# vim: syntax=ruby diff --git a/Rakefile b/Rakefile index d12d854..1c39287 100644 --- a/Rakefile +++ b/Rakefile @@ -1,37 +1,6 @@ require 'puppetlabs_spec_helper/rake_tasks' -require 'puppet-lint/tasks/puppet-lint' +require 'puppet-syntax/tasks/puppet-syntax' require 'puppet_blacksmith/rake_tasks' if Bundler.rubygems.find_name('puppet-blacksmith').any? +require 'puppet-lint/tasks/puppet-lint' -PuppetLint.configuration.fail_on_warnings = true PuppetLint.configuration.send('relative') - -desc 'Generate pooler nodesets' -task :gen_nodeset do - require 'beaker-hostgenerator' - require 'securerandom' - require 'fileutils' - - agent_target = ENV['TEST_TARGET'] - if ! agent_target - STDERR.puts 'TEST_TARGET environment variable is not set' - STDERR.puts 'setting to default value of "redhat-64default."' - agent_target = 'redhat-64default.' - end - - master_target = ENV['MASTER_TEST_TARGET'] - if ! master_target - STDERR.puts 'MASTER_TEST_TARGET environment variable is not set' - STDERR.puts 'setting to default value of "redhat7-64mdcl"' - master_target = 'redhat7-64mdcl' - end - - targets = "#{master_target}-#{agent_target}" - cli = BeakerHostGenerator::CLI.new([targets]) - nodeset_dir = "tmp/nodesets" - nodeset = "#{nodeset_dir}/#{targets}-#{SecureRandom.uuid}.yaml" - FileUtils.mkdir_p(nodeset_dir) - File.open(nodeset, 'w') do |fh| - fh.print(cli.execute) - end - puts nodeset -end diff --git a/lib/puppet/feature/hocon.rb b/lib/puppet/feature/hocon.rb index 624292d..10c8da4 100644 --- a/lib/puppet/feature/hocon.rb +++ b/lib/puppet/feature/hocon.rb @@ -3,7 +3,6 @@ libs = ['hocon', 'hocon/config_factory', 'hocon/config_value_factory', - 'hocon/parser/config_document_factory' - ] + 'hocon/parser/config_document_factory'] -Puppet.features.add(:hocon, :libs => libs) +Puppet.features.add(:hocon, libs: libs) diff --git a/lib/puppet/provider/hocon_setting/ruby.rb b/lib/puppet/provider/hocon_setting/ruby.rb index 44c44c5..d9d85ed 100644 --- a/lib/puppet/provider/hocon_setting/ruby.rb +++ b/lib/puppet/provider/hocon_setting/ruby.rb @@ -1,12 +1,12 @@ Puppet::Type.type(:hocon_setting).provide(:ruby) do - confine :feature => :hocon + confine feature: :hocon - def self.namevar(section_name, setting) - "#{setting}" + def self.namevar(_section_name, setting) + setting.to_s end def exists? - unless conf_file.has_value?(setting) + unless conf_file.has_value?(setting) # rubocop:disable Style/PreferredHashMethods return false end @@ -19,7 +19,7 @@ def exists? end end - if type == nil && + if type.nil? && conf_value.is_a?(Array) && conf_value.length == 1 @@ -30,7 +30,7 @@ def exists? return Array(@resource[:value]).any? { |v| value.flatten.include?(v) } end - return true + true end def create @@ -54,22 +54,21 @@ def type=(value) end def value - val = conf_file.has_value?(setting) ? - conf_object.get_value(setting).unwrapped : [] + val = conf_file.has_value?(setting) ? conf_object.get_value(setting).unwrapped : [] # rubocop:disable Style/PreferredHashMethods unless val.is_a?(Array) - if resource[:type] == 'array_element' - # If the current value of the target setting is not an array, - # present the current value as an empty array so that an - # element is added to an empty array (as opposed to converting - # the current value into the first element in an array and - # adding the value to set as a second element in the array). - val = [] - else - # This is required because of :array_matching => :all. - # Without this, Puppet will almost always register changes - # to a hocon_setting even when it shouldn't. - val = [val] - end + val = if resource[:type] == 'array_element' + # If the current value of the target setting is not an array, + # present the current value as an empty array so that an + # element is added to an empty array (as opposed to converting + # the current value into the first element in an array and + # adding the value to set as a second element in the array). + [] + else + # This is required because of :array_matching => :all. + # Without this, Puppet will almost always register changes + # to a hocon_setting even when it shouldn't. + [val] + end end val end @@ -89,9 +88,10 @@ def file_path end private + def conf_file - if @conf_file.nil? && (not File.exist?(file_path)) - File.new(file_path, "w") + if @conf_file.nil? && !File.exist?(file_path) + File.new(file_path, 'w') end @conf_file ||= Hocon::Parser::ConfigDocumentFactory.parse_file(file_path) end @@ -104,8 +104,8 @@ def write_conf(conf) end def conf_object - if @conf_file.nil? && (not File.exist?(file_path)) - File.new(file_path, "w") + if @conf_file.nil? && !File.exist?(file_path) + File.new(file_path, 'w') end Hocon::ConfigFactory.parse_file(file_path) end @@ -128,7 +128,7 @@ def remove_value(value_to_remove) conf_file_modified end - def set_value(value_to_set) + def set_value(value_to_set) # rubocop:disable Style/AccessorMethodName if resource[:type] == 'array_element' tmp_val = [] val = value @@ -150,11 +150,11 @@ def set_value(value_to_set) new_value = Hocon::ConfigValueFactory.from_any_ref(value_to_set[0], nil) end - if resource[:type] == 'text' - conf_file_modified = conf_file.set_value(setting, new_value) - else - conf_file_modified = conf_file.set_config_value(setting, new_value) - end + conf_file_modified = if resource[:type] == 'text' + conf_file.set_value(setting, new_value) + else + conf_file.set_config_value(setting, new_value) + end conf_file_modified end end diff --git a/lib/puppet/type/hocon_setting.rb b/lib/puppet/type/hocon_setting.rb index 9f28490..a0b82e7 100644 --- a/lib/puppet/type/hocon_setting.rb +++ b/lib/puppet/type/hocon_setting.rb @@ -1,88 +1,99 @@ Puppet::Type.newtype(:hocon_setting) do - ensurable do defaultvalues defaultto :present end - newparam(:setting, :namevar => true) do + newparam(:setting, namevar: true) do desc 'The name of the setting to be defined.' end - newparam(:path, :namevar => true) do + newparam(:path, namevar: true) do desc 'The file Puppet will ensure contains the specified setting.' validate do |value| - unless (Puppet.features.posix? and value =~ /^\//) or (Puppet.features.microsoft_windows? and (value =~ /^.:\// or value =~ /^\/\/[^\/]+\/[^\/]+/)) + unless (Puppet.features.posix? && value =~ %r{^\/}) || (Puppet.features.microsoft_windows? && (value =~ %r{^.:\/} || value =~ %r{^\/\/[^\/]+\/[^\/]+})) raise(Puppet::Error, "File paths must be fully qualified, not '#{value}'") end end end newproperty(:type) do - desc "The value type" + desc 'The value type' # This property has no default. If it is not supplied, the validation of the "value" # property will set one automatically. end - newproperty(:value, :array_matching => :all) do + newproperty(:value, array_matching: :all) do desc 'The value of the setting to be defined.' - validate do |val| + validate do |_val| # Grab the value we are going to validate - value = @shouldorig.is_a?(Array) && (@shouldorig.size > 1 || @resource[:type] == 'array') ? @shouldorig : @shouldorig[0] + value = (@shouldorig.is_a?(Array) && (@shouldorig.size > 1 || @resource[:type] == 'array')) ? @shouldorig : @shouldorig[0] case @resource[:type] - when 'boolean' - if value != true && value != false - raise "Type specified as 'boolean' but was #{value.class}" - end - when 'string', 'text' - unless value.is_a?(String) - raise "Type specified as #{@resource[:type]} but was #{value.class}" - end - when 'number' - # Puppet stringifies numerics in versions of Puppet < 4.0.0 - # Account for this by first attempting to cast to an Integer. - # Failing that, attempt to cast to a Float or return false - numeric_as_string = Integer(value) rescue false - numeric_as_string = numeric_as_string ? numeric_as_string : Float(value) rescue false + when 'boolean' + if value != true && value != false + raise "Type specified as 'boolean' but was #{value.class}" + end + when 'string', 'text' + unless value.is_a?(String) + raise "Type specified as #{@resource[:type]} but was #{value.class}" + end + when 'number' + # Puppet stringifies numerics in versions of Puppet < 4.0.0 + # Account for this by first attempting to cast to an Integer. + # Failing that, attempt to cast to a Float or return false + numeric_as_string = begin + Integer(value) + rescue + false + end + numeric_as_string = begin + numeric_as_string ? numeric_as_string : Float(value) + rescue + false + end - unless (value.is_a?(Numeric) or numeric_as_string) - raise "Type specified as 'number' but was #{value.class}" - end - when 'array' - unless value.is_a?(Array) - raise "Type specified as 'array' but was #{value.class}" - end - when 'hash' - unless value.is_a?(Hash) - raise "Type specified as 'hash' but was #{value.class}" - end - when 'array_element', nil - # Do nothing, we'll figure it out on our own - else - raise "Type was specified as #{@resource[:type]}, but should have been one of 'boolean', 'string', 'text', 'number', 'array', or 'hash'" + unless value.is_a?(Numeric) || numeric_as_string + raise "Type specified as 'number' but was #{value.class}" + end + when 'array' + unless value.is_a?(Array) + raise "Type specified as 'array' but was #{value.class}" + end + when 'hash' + unless value.is_a?(Hash) + raise "Type specified as 'hash' but was #{value.class}" + end + when 'array_element', nil # rubocop:disable Lint/EmptyWhen + # Do nothing, we'll figure it out on our own + else + raise "Type was specified as #{@resource[:type]}, but should have been one of 'boolean', 'string', 'text', 'number', 'array', or 'hash'" end end munge do |value| - if value.is_a?(String) and @resource[:type] == 'number' - munged_value = Integer(value) rescue false + if value.is_a?(String) && @resource[:type] == 'number' + munged_value = begin + Integer(value) + rescue + false + end value = munged_value ? munged_value : Float(value) end value end def insync?(is) - # TODO this doesn't appear to get called, and according to Puppet's source + # TODO: this doesn't appear to get called, and according to Puppet's source # it may be deprecated. if @resource[:type] == 'array_element' # make sure all passed values are in the file Array(@resource[:value]).each do |v| - if not provider.value.flatten.include?(v) + unless provider.value.flatten.include?(v) return false end end - return true + true else super end @@ -95,7 +106,7 @@ def change_to_s(current, new) real_new << new real_new.flatten! real_new.uniq! - "value changed [#{Array(current).flatten.join(", ")}] to [#{real_new.join(", ")}]" + "value changed [#{Array(current).flatten.join(', ')}] to [#{real_new.join(', ')}]" else super end @@ -107,21 +118,21 @@ def self.title_patterns # set the title to :setting instead of :name. This is also hard-wired to # ONLY set :setting and nothing else, and this will be overridden if # the :setting parameter is set manually. - [ [ /(.*)/m, [ [:setting] ] ] ] + [[%r{(.*)}m, [[:setting]]]] end validate do - message = "" - if self.original_parameters[:path].nil? - message += "path is a required parameter. " + message = '' + if original_parameters[:path].nil? + message += 'path is a required parameter. ' end - if self.original_parameters[:setting].nil? - message += "setting is a required parameter. " + if original_parameters[:setting].nil? + message += 'setting is a required parameter. ' end - if self.original_parameters[:value].nil? && self[:ensure] != :absent - message += "value is a required parameter unless ensuring a setting is absent." + if original_parameters[:value].nil? && self[:ensure] != :absent + message += 'value is a required parameter unless ensuring a setting is absent.' end - if message != "" + if message != '' raise(Puppet::Error, message) end end diff --git a/metadata.json b/metadata.json index da5cc36..4077036 100644 --- a/metadata.json +++ b/metadata.json @@ -8,7 +8,7 @@ "project_page": "https://github.com/puppetlabs/puppetlabs-hocon", "issues_url": "https://github.com/puppetlabs/puppetlabs-hocon/issues", "dependencies": [ - + ], "operatingsystem_support": [ { @@ -36,5 +36,8 @@ "name": "ruby", "version_requirement": ">=1.9.0" } - ] + ], + "pdk-version": "1.5.0", + "template-url": "file:///opt/puppetlabs/pdk/share/cache/pdk-templates.git", + "template-ref": "1.5.0-0-gd1b3eca" } diff --git a/spec/acceptance/conf_setting_spec.rb b/spec/acceptance/conf_setting_spec.rb index 6b38c85..1c496f8 100644 --- a/spec/acceptance/conf_setting_spec.rb +++ b/spec/acceptance/conf_setting_spec.rb @@ -4,47 +4,47 @@ describe 'hocon_setting resource' do after :all do - shell("rm #{tmpdir}/*.conf", :acceptable_exit_codes => [0,1,2]) + shell("rm #{tmpdir}/*.conf", acceptable_exit_codes: [0, 1, 2]) end - shared_examples 'has_content' do |path,pp,content| + shared_examples 'has_content' do |path, pp, content| before :all do - shell("rm #{path}", :acceptable_exit_codes => [0,1,2]) + shell("rm #{path}", acceptable_exit_codes: [0, 1, 2]) end after :all do - shell("cat #{path}", :acceptable_exit_codes => [0,1,2]) - shell("rm #{path}", :acceptable_exit_codes => [0,1,2]) + shell("cat #{path}", acceptable_exit_codes: [0, 1, 2]) + shell("rm #{path}", acceptable_exit_codes: [0, 1, 2]) end it 'applies the manifest twice' do - apply_manifest(pp, :catch_failures => true) - apply_manifest(pp, :catch_changes => true) + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) end describe file(path) do - it { should be_file } - #XXX Solaris 10 doesn't support multi-line grep - it("should contain #{content}", :unless => fact('osfamily') == 'Solaris') { - should contain(content) + it { is_expected.to be_file } + # XXX Solaris 10 doesn't support multi-line grep + it("should contain #{content}", unless: fact('osfamily') == 'Solaris') { + is_expected.to contain(content) } end end - shared_examples 'has_error' do |path,pp,error| + shared_examples 'has_error' do |path, pp, error| before :all do - shell("rm #{path}", :acceptable_exit_codes => [0,1,2]) + shell("rm #{path}", acceptable_exit_codes: [0, 1, 2]) end after :all do - shell("cat #{path}", :acceptable_exit_codes => [0,1,2]) - shell("rm #{path}", :acceptable_exit_codes => [0,1,2]) + shell("cat #{path}", acceptable_exit_codes: [0, 1, 2]) + shell("rm #{path}", acceptable_exit_codes: [0, 1, 2]) end it 'applies the manifest and gets a failure message' do - expect(apply_manifest(pp, :expect_failures => true).stderr).to match(error) + expect(apply_manifest(pp, expect_failures: true).stderr).to match(error) end describe file(path) do - it { should_not be_file } + it { is_expected.not_to be_file } end end @@ -66,15 +66,15 @@ EOS it 'applies the manifest twice' do - apply_manifest(pp, :catch_failures => true) - apply_manifest(pp, :catch_changes => true) + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) end describe file("#{tmpdir}/hocon_setting.conf") do - it { should be_file } - #XXX Solaris 10 doesn't support multi-line grep - it("should contain one {\n two=three\n}\nfour=five", :unless => fact('osfamily') == 'Solaris') { - should contain("one {\n two=three\n}\nfour=five") + it { is_expected.to be_file } + # XXX Solaris 10 doesn't support multi-line grep + it("contains one {\n two=three\n}\nfour=five", unless: fact('osfamily') == 'Solaris') { + is_expected.to contain("one {\n two=three\n}\nfour=five") } end end @@ -98,14 +98,14 @@ EOS it 'applies the manifest twice' do - apply_manifest(pp, :catch_failures => true) - apply_manifest(pp, :catch_changes => true) + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) end describe file("#{tmpdir}/hocon_setting.conf") do - it { should be_file } - it { should contain('four=five') } - it { should_not contain("two=three") } + it { is_expected.to be_file } + it { is_expected.to contain('four=five') } + it { is_expected.not_to contain('two=three') } end end @@ -118,8 +118,8 @@ end end after :all do - shell("cat #{tmpdir}/hocon_setting.conf", :acceptable_exit_codes => [0,1,2]) - shell("rm #{tmpdir}/hocon_setting.conf", :acceptable_exit_codes => [0,1,2]) + shell("cat #{tmpdir}/hocon_setting.conf", acceptable_exit_codes: [0, 1, 2]) + shell("rm #{tmpdir}/hocon_setting.conf", acceptable_exit_codes: [0, 1, 2]) end pp = <<-EOS @@ -132,14 +132,14 @@ EOS it 'applies the manifest twice' do - apply_manifest(pp, :catch_failures => true) - apply_manifest(pp, :catch_changes => true) + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) end describe file("#{tmpdir}/hocon_setting.conf") do - it { should be_file } - it { should_not contain('four=five') } - it { should contain("one {\n two=three\n}") } + it { is_expected.to be_file } + it { is_expected.not_to contain('four=five') } + it { is_expected.to contain("one {\n two=three\n}") } end end end @@ -148,7 +148,7 @@ { "setting => 'test.foo', value => 'bar'," => "test {\n foo = bar\n}", "setting => 'more.baz', value => 'quux'," => "more {\n baz = quux\n}", - "setting => 'top', value => 'level'," => "top: \"level\"", + "setting => 'top', value => 'level'," => 'top: "level"', }.each do |parameter_list, content| context parameter_list do pp = <<-EOS @@ -164,8 +164,8 @@ end { - "" => /value is a required/, - "setting => 'test.foo'," => /value is a required/, + '' => %r{value is a required}, + "setting => 'test.foo'," => %r{value is a required}, }.each do |parameter_list, error| context parameter_list do pp = <<-EOS @@ -201,7 +201,7 @@ end end - context "path => foo" do + context 'path => foo' do pp = <<-EOS hocon_setting { 'path => foo': ensure => present, @@ -211,7 +211,7 @@ } EOS - it_behaves_like 'has_error', 'foo', pp, /must be fully qualified/ + it_behaves_like 'has_error', 'foo', pp, %r{must be fully qualified} end end @@ -232,7 +232,7 @@ } EOS - it_behaves_like 'has_error', 'foo', pp, /Cannot alias/ + it_behaves_like 'has_error', 'foo', pp, %r{Cannot alias} end context 'setting can be the same if path is different' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c808092..e117192 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,17 +1,36 @@ -# This file is generated by ModuleSync, do not edit. -require 'puppetlabs_spec_helper/module_spec_helper' -if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 - RSpec.configure do |c| - c.before :each do - Puppet.settings[:strict] = :error - end - end -end +require 'puppetlabs_spec_helper/module_spec_helper' +require 'rspec-puppet-facts' -# put local configuration and setup into spec_helper_local begin - require 'spec_helper_local' + require 'spec_helper_local' if File.file?(File.join(File.dirname(__FILE__), 'spec_helper_local.rb')) rescue LoadError => loaderror - puts "Could not require spec_helper_local: #{loaderror.message}" + warn "Could not require spec_helper_local: #{loaderror.message}" +end + +include RspecPuppetFacts + +default_facts = { + puppetversion: Puppet.version, + facterversion: Facter.version, +} + +default_facts_path = File.expand_path(File.join(File.dirname(__FILE__), 'default_facts.yml')) +default_module_facts_path = File.expand_path(File.join(File.dirname(__FILE__), 'default_module_facts.yml')) + +if File.exist?(default_facts_path) && File.readable?(default_facts_path) + default_facts.merge!(YAML.safe_load(File.read(default_facts_path))) +end + +if File.exist?(default_module_facts_path) && File.readable?(default_module_facts_path) + default_facts.merge!(YAML.safe_load(File.read(default_module_facts_path))) +end + +RSpec.configure do |c| + c.default_facts = default_facts + c.before :each do + # set to strictest setting for testing + # by default Puppet runs at warning level + Puppet.settings[:strict] = :warning + end end diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index 224f9ab..e82668d 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -7,7 +7,7 @@ hosts.each do |host| puppet_version = (on default, puppet('--version')).output.chomp - if puppet_version =~ /Puppet Enterprise / + if puppet_version =~ %r{Puppet Enterprise } on host, puppet('module install puppetlabs-pe_gem') on host, puppet('resource package hocon ensure=latest provider=pe_gem') elsif ENV['PUPPET_INSTALL_TYPE'] != 'foss' && Gem::Version.new(puppet_version) >= Gem::Version.new('4.0.0') @@ -29,12 +29,12 @@ c.before :suite do # Install module and dependencies hosts.each do |host| - if host['platform'] !~ /windows/i - copy_root_module_to(host, :source => proj_root, :module_name => 'hocon') + if host['platform'] !~ %r{windows}i + copy_root_module_to(host, source: proj_root, module_name: 'hocon') end end hosts.each do |host| - if host['platform'] =~ /windows/i + if host['platform'] =~ %r{windows}i on host, puppet('plugin download') end end diff --git a/spec/unit/puppet/provider/conf_setting/ruby_spec.rb b/spec/unit/puppet/provider/conf_setting/ruby_spec.rb index b6b0291..9fda936 100644 --- a/spec/unit/puppet/provider/conf_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/conf_setting/ruby_spec.rb @@ -5,31 +5,33 @@ describe provider_class do include PuppetlabsSpec::Files - let(:tmpfile) { tmpfilename("hocon_setting_test.conf") } - let(:emptyfile) { tmpfilename("hocon_setting_test_empty.conf") } + let(:tmpfile) { tmpfilename('hocon_setting_test.conf') } + let(:emptyfile) { tmpfilename('hocon_setting_test_empty.conf') } - let(:common_params) { { - :title => 'hocon_setting_ensure_present_test', - :path => tmpfile, - } } + let(:common_params) do + { + title: 'hocon_setting_ensure_present_test', + path: tmpfile, + } + end - def validate_file(expected_content,tmp = tmpfile) - tmpcontent = File.read(tmp) + def validate_file(expected_content, tmp = tmpfile) + _tmpcontent = File.read(tmp) expect(File.read(tmp)).to eq(expected_content) end - before :each do File.open(tmpfile, 'w') do |fh| fh.write(orig_content) end File.open(emptyfile, 'w') do |fh| - fh.write("") + fh.write('') end end - context "array_element" do - let(:orig_content) { + # rubocop:disable Layout/IndentHeredoc + context 'array_element' do + let(:orig_content) do <<-EOS test_key_1: [ { @@ -49,16 +51,17 @@ def validate_file(expected_content,tmp = tmpfile) } ] EOS - } + end - it "should add a new element to the array" do + it 'adds a new element to the array' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1', :value => [{ 'foo' => 'foovalue3' }, { 'bar' => 'barvalue3' }], :type => 'array_element')) + setting: 'test_key_1', value: [{ 'foo' => 'foovalue3' }, { 'bar' => 'barvalue3' }], type: 'array_element', + )) provider = described_class.new(resource) expect(provider.exists?).to be true provider.create validate_file( - <<-EOS + <<-EOS test_key_1: [ { "bar": "barvalue", @@ -82,17 +85,18 @@ def validate_file(expected_content,tmp = tmpfile) ] EOS -) + ) end - it "should remove elements from the array" do + it 'removes elements from the array' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1', :ensure => 'absent', :value => { 'foo' => 'foovalue3' }, :type => 'array_element')) + setting: 'test_key_1', ensure: 'absent', value: { 'foo' => 'foovalue3' }, type: 'array_element', + )) provider = described_class.new(resource) expect(provider.exists?).to be true provider.destroy validate_file( - <<-EOS + <<-EOS test_key_1: [ { "bar": "barvalue", @@ -108,20 +112,21 @@ def validate_file(expected_content,tmp = tmpfile) ] EOS -) + ) end - it "should add an array element even if the target setting does not yet exist" do + it 'adds an array element even if the target setting does not yet exist' do resource = Puppet::Type::Hocon_setting.new( - common_params.merge(:setting => 'test_key_2', - :ensure => 'present', - :value => { 'foo' => 'foovalue3' }, - :type => 'array_element')) + common_params.merge(setting: 'test_key_2', + ensure: 'present', + value: { 'foo' => 'foovalue3' }, + type: 'array_element'), + ) provider = described_class.new(resource) expect(provider.exists?).to be false provider.create validate_file( - <<-EOS + <<-EOS test_key_1: [ { foo: foovalue @@ -154,20 +159,21 @@ def validate_file(expected_content,tmp = tmpfile) ) end - it "should add an array element even if the target setting is not an array" do + it 'adds an array element even if the target setting is not an array' do File.open(tmpfile, 'a') do |fh| fh.write('test_key_2: 3') end resource = Puppet::Type::Hocon_setting.new( - common_params.merge(:setting => 'test_key_2', - :ensure => 'present', - :value => { 'foo' => 'foovalue3' }, - :type => 'array_element')) + common_params.merge(setting: 'test_key_2', + ensure: 'present', + value: { 'foo' => 'foovalue3' }, + type: 'array_element'), + ) provider = described_class.new(resource) expect(provider.exists?).to be false provider.create validate_file( - <<-EOS + <<-EOS test_key_1: [ { foo: foovalue @@ -195,15 +201,16 @@ def validate_file(expected_content,tmp = tmpfile) ) end - it "should convert a scalar key to a single element array when type is set" do + it 'converts a scalar key to a single element array when type is set' do File.open(tmpfile, 'w') do |fh| fh.write('ennui: yes') end resource = Puppet::Type::Hocon_setting.new( - common_params.merge(:setting => 'ennui', - :ensure => 'present', - :value => ['yes'], - :type => 'array')) + common_params.merge(setting: 'ennui', + ensure: 'present', + value: ['yes'], + type: 'array'), + ) provider = described_class.new(resource) expect(provider.exists?).to be false provider.create @@ -217,7 +224,7 @@ def validate_file(expected_content,tmp = tmpfile) ) end - it "should convert to a scalar from single element array when type is unset" do + it 'converts to a scalar from single element array when type is unset' do content = <<-EOS ennui: [ "yes" @@ -227,20 +234,20 @@ def validate_file(expected_content,tmp = tmpfile) fh.write(content) end resource = Puppet::Type::Hocon_setting.new( - common_params.merge(:setting => 'ennui', - :ensure => 'present', - :value => ['yes'],)) + common_params.merge(setting: 'ennui', + ensure: 'present', + value: ['yes']), + ) provider = described_class.new(resource) expect(provider.exists?).to be false provider.create expect(provider.exists?).to be true validate_file("ennui: \"yes\"\n") end - end - context "when ensuring that a setting is present" do - let(:orig_content) { + context 'when ensuring that a setting is present' do + let(:orig_content) do <<-EOS # This is a comment test_key_1: { @@ -265,16 +272,17 @@ def validate_file(expected_content,tmp = tmpfile) // yet another comment foo: bar EOS - } + end - it "should add a missing setting to the correct map" do + it 'adds a missing setting to the correct map' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.yahoo', :value => 'yippee')) + setting: 'test_key_1.yahoo', value: 'yippee', + )) provider = described_class.new(resource) expect(provider.exists?).to be false provider.create validate_file( - <<-EOS + <<-EOS # This is a comment test_key_1: { // This is also a comment @@ -299,17 +307,18 @@ def validate_file(expected_content,tmp = tmpfile) // yet another comment foo: bar EOS -) + ) end - it "should modify an existing setting with a different value" do + it 'modifies an existing setting with a different value' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_2.baz', :value => 'bazvalue2')) + setting: 'test_key_2.baz', value: 'bazvalue2', + )) provider = described_class.new(resource) expect(provider.exists?).to be true - provider.value=('bazvalue2') + provider.value = 'bazvalue2' validate_file( - <<-EOS + <<-EOS # This is a comment test_key_1: { // This is also a comment @@ -336,16 +345,17 @@ def validate_file(expected_content,tmp = tmpfile) ) end - it "should be able to handle settings with non alphanumbering settings " do + it 'is able to handle settings with non alphanumbering settings' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_2.url', :value => 'http://192.168.0.1:8080')) + setting: 'test_key_2.url', value: 'http://192.168.0.1:8080', + )) provider = described_class.new(resource) expect(provider.exists?).to be true expect(provider.value).to eq(['http://192.168.1.1:8080']) - provider.value=('http://192.168.0.1:8080') + provider.value = 'http://192.168.0.1:8080' validate_file( - <<-EOS + <<-EOS # This is a comment test_key_1: { // This is also a comment @@ -372,21 +382,23 @@ def validate_file(expected_content,tmp = tmpfile) ) end - it "should recognize an existing setting with the specified value" do + it 'recognizes an existing setting with the specified value' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_2.baz', :value => 'bazvalue')) + setting: 'test_key_2.baz', value: 'bazvalue', + )) provider = described_class.new(resource) expect(provider.exists?).to be true end - it "should add a new map if the path contains a non-existent map" do + it 'adds a new map if the path contains a non-existent map' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_4.huzzah', :value => 'shazaam')) + setting: 'test_key_4.huzzah', value: 'shazaam', + )) provider = described_class.new(resource) expect(provider.exists?).to be false provider.create validate_file( - <<-EOS + <<-EOS # This is a comment test_key_1: { // This is also a comment @@ -416,87 +428,96 @@ def validate_file(expected_content,tmp = tmpfile) ) end - it "should add a new map if no maps exists" do + it 'adds a new map if no maps exists' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.setting1', :value => 'helloworld', :path => emptyfile)) + setting: 'test_key_1.setting1', value: 'helloworld', path: emptyfile, + )) provider = described_class.new(resource) expect(provider.exists?).to be false provider.create validate_file("test_key_1: {\n setting1: \"helloworld\"\n}\n", emptyfile) end - it "should be able to handle variables of boolean type" do + it 'is able to handle variables of boolean type' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => false)) + setting: 'test_key_1.master', value: false, + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true - expect(provider.value[0]).to eql(false) + expect(provider.value[0]).to be(false) end - it "should be able to handle variables of integer type" do + it 'is able to handle variables of integer type' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => 12)) + setting: 'test_key_1.master', value: 12, + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true - expect(provider.value[0]).to eql(12) + expect(provider.value[0]).to be(12) end - it "should be able to handle variables of float type" do + it 'is able to handle variables of float type' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => 12.24)) + setting: 'test_key_1.master', value: 12.24, + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true - expect(provider.value[0]).to eql(12.24) + expect(provider.value[0]).to be(12.24) end - it "should be able to handle arrays" do + it 'is able to handle arrays' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => [1, 2, 3, 4])) + setting: 'test_key_1.master', value: [1, 2, 3, 4], + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true expect(provider.value).to eql([1, 2, 3, 4]) end - it "should be able to handle maps" do + it 'is able to handle maps' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => {"a" => 1, "b" => 2})) + setting: 'test_key_1.master', value: { 'a' => 1, 'b' => 2 }, + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true - expect(provider.value[0]).to eql({"a" => 1, "b" => 2}) + expect(provider.value[0]).to eql('a' => 1, 'b' => 2) end - it "should treat a single-element array as a single value if no value type is specified" do + it 'treats a single-element array as a single value if no value type is specified' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => [12])) + setting: 'test_key_1.master', value: [12], + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true - expect(provider.value[0]).to eql(12) + expect(provider.value[0]).to be(12) end - it "should treat a single-element array as a single-element array if value_type is specified" do + it 'treats a single-element array as a single-element array if value_type is specified' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => [12], :type => 'array')) + setting: 'test_key_1.master', value: [12], type: 'array', + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true expect(provider.value).to eql([12]) end - it "should allow setting the exact text of a value in the file" do + it 'allows setting the exact text of a value in the file' do text = "{\n # a comment\n a : b\n}" resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => text, :type => 'text')) + setting: 'test_key_1.master', value: text, type: 'text', + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true validate_file( - <<-EOS + <<-EOS # This is a comment test_key_1: { // This is also a comment @@ -527,8 +548,8 @@ def validate_file(expected_content,tmp = tmpfile) end end - context "when dealing with settings in the top level" do - let(:orig_content) { + context 'when dealing with settings in the top level' do + let(:orig_content) do <<-EOS # This is a comment foo=blah @@ -537,12 +558,12 @@ def validate_file(expected_content,tmp = tmpfile) foo="http://192.168.1.1:8080" } EOS - } - + end - it "should add a missing setting if it doesn't exist" do + it "adds a missing setting if it doesn't exist" do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'bar', :value => 'yippee')) + setting: 'bar', value: 'yippee', + )) provider = described_class.new(resource) expect(provider.exists?).to be false provider.create @@ -555,16 +576,17 @@ def validate_file(expected_content,tmp = tmpfile) } bar: "yippee" EOS - ) + ) end - it "should modify an existing setting with a different value" do + it 'modifies an existing setting with a different value' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'foo', :value => 'yippee')) + setting: 'foo', value: 'yippee', + )) provider = described_class.new(resource) expect(provider.exists?).to be true expect(provider.value[0]).to eq('blah') - provider.value=('yippee') + provider.value = 'yippee' validate_file(<<-EOS # This is a comment foo="yippee" @@ -573,29 +595,31 @@ def validate_file(expected_content,tmp = tmpfile) foo="http://192.168.1.1:8080" } EOS - ) + ) end - it "should recognize an existing setting with the specified value" do + it 'recognizes an existing setting with the specified value' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'foo', :value => 'blah')) + setting: 'foo', value: 'blah', + )) provider = described_class.new(resource) expect(provider.exists?).to be true end end - context "when the first line of the file is a section" do - let(:orig_content) { + context 'when the first line of the file is a section' do + let(:orig_content) do <<-EOS "test_key_2" { foo="http://192.168.1.1:8080" } EOS - } + end - it "should be able to add a setting to the top-level map" do + it 'is able to add a setting to the top-level map' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'foo', :value => 'yippee')) + setting: 'foo', value: 'yippee', + )) provider = described_class.new(resource) expect(provider.exists?).to be false provider.create @@ -605,12 +629,12 @@ def validate_file(expected_content,tmp = tmpfile) } foo: "yippee" EOS - ) + ) end end - context "when ensuring that a setting is absent" do - let(:orig_content) { + context 'when ensuring that a setting is absent' do + let(:orig_content) do <<-EOS "test_key_1" { # This is also a comment @@ -627,14 +651,16 @@ def validate_file(expected_content,tmp = tmpfile) subby=bar } EOS - } + end - it "should remove a setting that exists" do + it 'removes a setting that exists' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.foo', :ensure => 'absent')) + setting: 'test_key_1.foo', ensure: 'absent', + )) provider = described_class.new(resource) expect(provider.exists?).to be true provider.destroy + # rubocop:disable Layout/TrailingWhitespace - Validate fails without trailing whitespace validate_file(<<-EOS "test_key_1" { # This is also a comment @@ -651,12 +677,14 @@ def validate_file(expected_content,tmp = tmpfile) subby=bar } EOS - ) + ) end + # rubocop:enable Layout/TrailingWhitespace - it "should do nothing for a setting that does not exist" do + it 'does nothing for a setting that does not exist' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_3.foo', :ensure => 'absent')) + setting: 'test_key_3.foo', ensure: 'absent', + )) provider = described_class.new(resource) expect(provider.exists?).to be false provider.destroy @@ -676,112 +704,139 @@ def validate_file(expected_content,tmp = tmpfile) subby=bar } EOS - ) + ) end end - context "when validating a value type" do - let(:orig_content) { "" } - it "should throw when type is 'number' but value is not" do - expect {Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'foo', :type => 'number', :value => "abcdefg"))}.to raise_error + context 'when validating a value type' do + let(:orig_content) { '' } + + it "throws when type is 'number' but value is not" do + expect { + Puppet::Type::Hocon_setting.new(common_params.merge( + setting: 'foo', type: 'number', value: 'abcdefg', + )) + }.to raise_error end - it "should throw when type is 'boolean' but value is not" do - expect {Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'foo', :type => 'boolean', :value => "abcdefg"))}.to raise_error + it "throws when type is 'boolean' but value is not" do + expect { + Puppet::Type::Hocon_setting.new(common_params.merge( + setting: 'foo', type: 'boolean', value: 'abcdefg', + )) + }.to raise_error end - it "should throw when type is 'hash' but value is not" do - expect {Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'foo', :type => 'hash', :value => "abcdefg"))}.to raise_error + it "throws when type is 'hash' but value is not" do + expect { + Puppet::Type::Hocon_setting.new(common_params.merge( + setting: 'foo', type: 'hash', value: 'abcdefg', + )) + }.to raise_error end - it "should throw when type is 'string' but value is not" do - expect {Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'foo', :type => 'string', :value => 12))}.to raise_error + it "throws when type is 'string' but value is not" do + expect { + Puppet::Type::Hocon_setting.new(common_params.merge( + setting: 'foo', type: 'string', value: 12, + )) + }.to raise_error end - it "should throw when type is 'text' but value is not" do - expect {Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'foo', :type => 'text', :value => 12))}.to raise_error + it "throws when type is 'text' but value is not" do + expect { + Puppet::Type::Hocon_setting.new(common_params.merge( + setting: 'foo', type: 'text', value: 12, + )) + }.to raise_error end - it "should throw when type is a non-valid string" do - expect {Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'foo', :type => 'invalid', :value => "abcdefg"))}.to raise_error + it 'throws when type is a non-valid string' do + expect { + Puppet::Type::Hocon_setting.new(common_params.merge( + setting: 'foo', type: 'invalid', value: 'abcdefg', + )) + }.to raise_error end - it "should be able to handle value false with boolean type specified" do + it 'is able to handle value false with boolean type specified' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => false, :type => 'boolean')) + setting: 'test_key_1.master', value: false, type: 'boolean', + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true - expect(provider.value[0]).to eql(false) + expect(provider.value[0]).to be(false) end - it "should be able to handle value true with boolean type specified" do + it 'is able to handle value true with boolean type specified' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => true, :type => 'boolean')) + setting: 'test_key_1.master', value: true, type: 'boolean', + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true - expect(provider.value[0]).to eql(true) + expect(provider.value[0]).to be(true) end - it "should be able to handle an integer value with number type specified" do + it 'is able to handle an integer value with number type specified' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => 12, :type => 'number')) + setting: 'test_key_1.master', value: 12, type: 'number', + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true expect(provider.value[0].eql?(12)).to be(true) end - it "should be able to handle a float value with number type specified" do + it 'is able to handle a float value with number type specified' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => 13.37, :type => 'number')) + setting: 'test_key_1.master', value: 13.37, type: 'number', + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true expect(provider.value[0].eql?(13.37)).to be(true) end - it "should be able to handle an Integer string value with number type specified" do + it 'is able to handle an Integer string value with number type specified' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => '12', :type => 'number')) + setting: 'test_key_1.master', value: '12', type: 'number', + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true expect(provider.value[0].eql?(12)).to be(true) end - it "should be able to handle a Float string value with number type specified" do + it 'is able to handle a Float string value with number type specified' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => '13.37', :type => 'number')) + setting: 'test_key_1.master', value: '13.37', type: 'number', + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true expect(provider.value[0].eql?(13.37)).to be(true) end - it "should be able to handle string value with string type specified" do + it 'is able to handle string value with string type specified' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => "abc", :type => 'string')) + setting: 'test_key_1.master', value: 'abc', type: 'string', + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true - expect(provider.value[0]).to eql("abc") + expect(provider.value[0]).to eql('abc') end - it "should be able to handle hash value with hash type specified" do + it 'is able to handle hash value with hash type specified' do resource = Puppet::Type::Hocon_setting.new(common_params.merge( - :setting => 'test_key_1.master', :value => {'a' => 'b'}, :type => 'hash')) + setting: 'test_key_1.master', value: { 'a' => 'b' }, type: 'hash', + )) provider = described_class.new(resource) provider.create expect(provider.exists?).to be true - expect(provider.value[0]).to eql({'a' => 'b'}) + expect(provider.value[0]).to eql('a' => 'b') end end end diff --git a/spec/unit/puppet/type/hocon_setting_spec.rb b/spec/unit/puppet/type/hocon_setting_spec.rb index c7cc220..f80732b 100644 --- a/spec/unit/puppet/type/hocon_setting_spec.rb +++ b/spec/unit/puppet/type/hocon_setting_spec.rb @@ -1,15 +1,15 @@ require 'puppet' require 'puppet/type/hocon_setting' describe Puppet::Type.type(:hocon_setting) do - let(:resource) { + let(:resource) do Puppet::Type.type(:hocon_setting).new( - :title => 'hocon setting', - :path => '/tmp/hocon.setting', - :setting => 'test_key.master', - :value => 'value', - :type => 'text' + title: 'hocon setting', + path: '/tmp/hocon.setting', + setting: 'test_key.master', + value: 'value', + type: 'text', ) - } + end it 'is ensurable' do resource[:ensure] = :present @@ -20,18 +20,18 @@ it 'raises an error if an invalid ensure value is passed' do expect { resource[:ensure] = 'file' }.to raise_error \ - Puppet::Error, /Invalid value "file"/ + Puppet::Error, %r{Invalid value "file"} end it 'accepts a valid type value' do - valid_types = [ - 'boolean', - 'string', - 'text', - 'number', - 'array', - 'array_element', - 'hash' + valid_types = %w[ + boolean + string + text + number + array + array_element + hash ] valid_types.each do |t| @@ -41,9 +41,9 @@ end it 'raises an error with invalid type values when a value is specified' do - resource[:type] = 'blarg' + resource[:type] = 'blarg' expect { resource[:value] = 4 }.to raise_error \ - Puppet::Error, /Type was specified as blarg, but should have been one of 'boolean'/ + Puppet::Error, %r{Type was specified as blarg, but should have been one of 'boolean'} end it 'accepts valid boolean values' do @@ -55,13 +55,13 @@ end it 'raises an error with invalid boolean values' do - resource[:type] = 'boolean' + resource[:type] = 'boolean' expect { resource[:value] = 'not boolean' }.to raise_error \ - Puppet::Error, /Type specified as 'boolean' but was String/ + Puppet::Error, %r{Type specified as 'boolean' but was String} end it 'accepts valid string and text values' do - ['string', 'text'].each do |t| + %w[string text].each do |t| resource[:type] = t resource[:value] = 'string value' expect(resource[:value]).to eq(['string value']) @@ -69,10 +69,10 @@ end it 'raises an error with invalid string and text values' do - ['string', 'text'].each do |t| - resource[:type] = t + %w[string text].each do |t| + resource[:type] = t expect { resource[:value] = 4 }.to raise_error \ - Puppet::Error, /Type specified as #{t} but was (Fixnum|Integer)/ + Puppet::Error, %r{Type specified as #{t} but was (Fixnum|Integer)} end end @@ -87,7 +87,7 @@ it 'accepts valid number values as a string' do { '13' => 13, - '13.37' => 13.37 + '13.37' => 13.37, }.each do |key, val| resource[:type] = 'number' resource[:value] = key @@ -96,15 +96,15 @@ end it 'raises an error with invalid number values' do - ['string', '45g'].each do |t| - resource[:type] = 'number' + %w[string 45g].each do |t| + resource[:type] = 'number' expect { resource[:value] = t }.to raise_error \ - Puppet::Error, /Type specified as 'number' but was String/ + Puppet::Error, %r{Type specified as 'number' but was String} end end it 'accepts valid array values' do - array = ['foo', 'bar'] + array = %w[foo bar] resource[:type] = 'array' resource[:value] = array expect(resource[:value]).to eq(array) @@ -118,8 +118,8 @@ end it 'raises an error with invalid hash values' do - resource[:type] = 'hash' + resource[:type] = 'hash' expect { resource[:value] = 4 }.to raise_error \ - Puppet::Error, /Type specified as 'hash' but was (Fixnum|Integer)/ + Puppet::Error, %r{Type specified as 'hash' but was (Fixnum|Integer)} end end