diff --git a/.fixtures.yml b/.fixtures.yml index d7b3b389..85b50666 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -10,5 +10,5 @@ fixtures: facts: 'https://github.com/puppetlabs/puppetlabs-facts.git' puppet_agent: repo: 'https://github.com/puppetlabs/puppetlabs-puppet_agent.git' - ref: v4.12.1 + ref: v4.13.0 provision: 'https://github.com/puppetlabs/provision.git' diff --git a/.rubocop.yml b/.rubocop.yml index 31e8248f..83745bcb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,10 +1,15 @@ --- +inherit_from: .rubocop_todo.yml + require: - rubocop-performance - rubocop-rspec AllCops: + NewCops: enable DisplayCopNames: true - TargetRubyVersion: '2.5' + ExtraDetails: true + DisplayStyleGuide: true + TargetRubyVersion: '2.7' Include: - "**/*.rb" Exclude: @@ -79,441 +84,3 @@ Style/Documentation: - spec/**/* Style/WordArray: EnforcedStyle: brackets -Performance/AncestorsInclude: - Enabled: true -Performance/BigDecimalWithNumericArgument: - Enabled: true -Performance/BlockGivenWithExplicitBlock: - Enabled: true -Performance/CaseWhenSplat: - Enabled: true -Performance/ConstantRegexp: - Enabled: true -Performance/MethodObjectAsBlock: - Enabled: true -Performance/RedundantSortBlock: - Enabled: true -Performance/RedundantStringChars: - Enabled: true -Performance/ReverseFirst: - Enabled: true -Performance/SortReverse: - Enabled: true -Performance/Squeeze: - Enabled: true -Performance/StringInclude: - Enabled: true -Performance/Sum: - Enabled: true -Style/CollectionMethods: - Enabled: true -Style/MethodCalledOnDoEndBlock: - Enabled: true -Style/StringMethods: - Enabled: true -Bundler/InsecureProtocolSource: - Enabled: false -Gemspec/DuplicatedAssignment: - Enabled: false -Gemspec/OrderedDependencies: - Enabled: false -Gemspec/RequiredRubyVersion: - Enabled: false -Gemspec/RubyVersionGlobalsUsage: - Enabled: false -Layout/ArgumentAlignment: - Enabled: false -Layout/BeginEndAlignment: - Enabled: false -Layout/ClosingHeredocIndentation: - Enabled: false -Layout/EmptyComment: - Enabled: false -Layout/EmptyLineAfterGuardClause: - Enabled: false -Layout/EmptyLinesAroundArguments: - Enabled: false -Layout/EmptyLinesAroundAttributeAccessor: - Enabled: false -Layout/EndOfLine: - Enabled: false -Layout/FirstArgumentIndentation: - Enabled: false -Layout/HashAlignment: - Enabled: false -Layout/HeredocIndentation: - Enabled: false -Layout/LeadingEmptyLines: - Enabled: false -Layout/SpaceAroundMethodCallOperator: - Enabled: false -Layout/SpaceInsideArrayLiteralBrackets: - Enabled: false -Layout/SpaceInsideReferenceBrackets: - Enabled: false -Lint/BigDecimalNew: - Enabled: false -Lint/BooleanSymbol: - Enabled: false -Lint/ConstantDefinitionInBlock: - Enabled: false -Lint/DeprecatedOpenSSLConstant: - Enabled: false -Lint/DisjunctiveAssignmentInConstructor: - Enabled: false -Lint/DuplicateElsifCondition: - Enabled: false -Lint/DuplicateRequire: - Enabled: false -Lint/DuplicateRescueException: - Enabled: false -Lint/EmptyConditionalBody: - Enabled: false -Lint/EmptyFile: - Enabled: false -Lint/ErbNewArguments: - Enabled: false -Lint/FloatComparison: - Enabled: false -Lint/HashCompareByIdentity: - Enabled: false -Lint/IdentityComparison: - Enabled: false -Lint/InterpolationCheck: - Enabled: false -Lint/MissingCopEnableDirective: - Enabled: false -Lint/MixedRegexpCaptureTypes: - Enabled: false -Lint/NestedPercentLiteral: - Enabled: false -Lint/NonDeterministicRequireOrder: - Enabled: false -Lint/OrderedMagicComments: - Enabled: false -Lint/OutOfRangeRegexpRef: - Enabled: false -Lint/RaiseException: - Enabled: false -Lint/RedundantCopEnableDirective: - Enabled: false -Lint/RedundantRequireStatement: - Enabled: false -Lint/RedundantSafeNavigation: - Enabled: false -Lint/RedundantWithIndex: - Enabled: false -Lint/RedundantWithObject: - Enabled: false -Lint/RegexpAsCondition: - Enabled: false -Lint/ReturnInVoidContext: - Enabled: false -Lint/SafeNavigationConsistency: - Enabled: false -Lint/SafeNavigationWithEmpty: - Enabled: false -Lint/SelfAssignment: - Enabled: false -Lint/SendWithMixinArgument: - Enabled: false -Lint/ShadowedArgument: - Enabled: false -Lint/StructNewOverride: - Enabled: false -Lint/ToJSON: - Enabled: false -Lint/TopLevelReturnWithArgument: - Enabled: false -Lint/TrailingCommaInAttributeDeclaration: - Enabled: false -Lint/UnreachableLoop: - Enabled: false -Lint/UriEscapeUnescape: - Enabled: false -Lint/UriRegexp: - Enabled: false -Lint/UselessMethodDefinition: - Enabled: false -Lint/UselessTimes: - Enabled: false -Metrics/AbcSize: - Enabled: false -Metrics/BlockLength: - Enabled: false -Metrics/BlockNesting: - Enabled: false -Metrics/ClassLength: - Enabled: false -Metrics/CyclomaticComplexity: - Enabled: false -Metrics/MethodLength: - Enabled: false -Metrics/ModuleLength: - Enabled: false -Metrics/ParameterLists: - Enabled: false -Metrics/PerceivedComplexity: - Enabled: false -Migration/DepartmentName: - Enabled: false -Naming/AccessorMethodName: - Enabled: false -Naming/BlockParameterName: - Enabled: false -Naming/HeredocDelimiterCase: - Enabled: false -Naming/HeredocDelimiterNaming: - Enabled: false -Naming/MemoizedInstanceVariableName: - Enabled: false -Naming/MethodParameterName: - Enabled: false -Naming/RescuedExceptionsVariableName: - Enabled: false -Naming/VariableNumber: - Enabled: false -Performance/BindCall: - Enabled: false -Performance/DeletePrefix: - Enabled: false -Performance/DeleteSuffix: - Enabled: false -Performance/InefficientHashSearch: - Enabled: false -Performance/UnfreezeString: - Enabled: false -Performance/UriDefaultParser: - Enabled: false -RSpec/Be: - Enabled: false -RSpec/Capybara/CurrentPathExpectation: - Enabled: false -RSpec/Capybara/FeatureMethods: - Enabled: false -RSpec/Capybara/VisibilityMatcher: - Enabled: false -RSpec/ContextMethod: - Enabled: false -RSpec/ContextWording: - Enabled: false -RSpec/DescribeClass: - Enabled: false -RSpec/EmptyHook: - Enabled: false -RSpec/EmptyLineAfterExample: - Enabled: false -RSpec/EmptyLineAfterExampleGroup: - Enabled: false -RSpec/EmptyLineAfterHook: - Enabled: false -RSpec/ExampleLength: - Enabled: false -RSpec/ExampleWithoutDescription: - Enabled: false -RSpec/ExpectChange: - Enabled: false -RSpec/ExpectInHook: - Enabled: false -RSpec/FactoryBot/AttributeDefinedStatically: - Enabled: false -RSpec/FactoryBot/CreateList: - Enabled: false -RSpec/FactoryBot/FactoryClassName: - Enabled: false -RSpec/HooksBeforeExamples: - Enabled: false -RSpec/ImplicitBlockExpectation: - Enabled: false -RSpec/ImplicitSubject: - Enabled: false -RSpec/LeakyConstantDeclaration: - Enabled: false -RSpec/LetBeforeExamples: - Enabled: false -RSpec/MissingExampleGroupArgument: - Enabled: false -RSpec/MultipleExpectations: - Enabled: false -RSpec/MultipleMemoizedHelpers: - Enabled: false -RSpec/MultipleSubjects: - Enabled: false -RSpec/NestedGroups: - Enabled: false -RSpec/PredicateMatcher: - Enabled: false -RSpec/ReceiveCounts: - Enabled: false -RSpec/ReceiveNever: - Enabled: false -RSpec/RepeatedExampleGroupBody: - Enabled: false -RSpec/RepeatedExampleGroupDescription: - Enabled: false -RSpec/RepeatedIncludeExample: - Enabled: false -RSpec/ReturnFromStub: - Enabled: false -RSpec/SharedExamples: - Enabled: false -RSpec/StubbedMock: - Enabled: false -RSpec/UnspecifiedException: - Enabled: false -RSpec/VariableDefinition: - Enabled: false -RSpec/VoidExpect: - Enabled: false -RSpec/Yield: - Enabled: false -Security/Open: - Enabled: false -Style/AccessModifierDeclarations: - Enabled: false -Style/AccessorGrouping: - Enabled: false -Style/AsciiComments: - Enabled: false -Style/BisectedAttrAccessor: - Enabled: false -Style/CaseLikeIf: - Enabled: false -Style/ClassEqualityComparison: - Enabled: false -Style/ColonMethodDefinition: - Enabled: false -Style/CombinableLoops: - Enabled: false -Style/CommentedKeyword: - Enabled: false -Style/Dir: - Enabled: false -Style/DoubleCopDisableDirective: - Enabled: false -Style/EmptyBlockParameter: - Enabled: false -Style/EmptyLambdaParameter: - Enabled: false -Style/Encoding: - Enabled: false -Style/EvalWithLocation: - Enabled: false -Style/ExpandPathArguments: - Enabled: false -Style/ExplicitBlockArgument: - Enabled: false -Style/ExponentialNotation: - Enabled: false -Style/FloatDivision: - Enabled: false -Style/FrozenStringLiteralComment: - Enabled: false -Style/GlobalStdStream: - Enabled: false -Style/HashAsLastArrayItem: - Enabled: false -Style/HashLikeCase: - Enabled: false -Style/HashTransformKeys: - Enabled: false -Style/HashTransformValues: - Enabled: false -Style/IfUnlessModifier: - Enabled: false -Style/KeywordParametersOrder: - Enabled: false -Style/MinMax: - Enabled: false -Style/MixinUsage: - Enabled: false -Style/MultilineWhenThen: - Enabled: false -Style/NegatedUnless: - Enabled: false -Style/NumericPredicate: - Enabled: false -Style/OptionalBooleanParameter: - Enabled: false -Style/OrAssignment: - Enabled: false -Style/RandomWithOffset: - Enabled: false -Style/RedundantAssignment: - Enabled: false -Style/RedundantCondition: - Enabled: false -Style/RedundantConditional: - Enabled: false -Style/RedundantFetchBlock: - Enabled: false -Style/RedundantFileExtensionInRequire: - Enabled: false -Style/RedundantRegexpCharacterClass: - Enabled: false -Style/RedundantRegexpEscape: - Enabled: false -Style/RedundantSelfAssignment: - Enabled: false -Style/RedundantSort: - Enabled: false -Style/RescueStandardError: - Enabled: false -Style/SingleArgumentDig: - Enabled: false -Style/SlicingWithRange: - Enabled: false -Style/SoleNestedConditional: - Enabled: false -Style/StderrPuts: - Enabled: false -Style/StringConcatenation: - Enabled: false -Style/Strip: - Enabled: false -Style/SymbolProc: - Enabled: false -Style/TrailingBodyOnClass: - Enabled: false -Style/TrailingBodyOnMethodDefinition: - Enabled: false -Style/TrailingBodyOnModule: - Enabled: false -Style/TrailingCommaInHashLiteral: - Enabled: false -Style/TrailingMethodEndStatement: - Enabled: false -Style/UnpackFirst: - Enabled: false -Lint/DuplicateBranch: - Enabled: false -Lint/DuplicateRegexpCharacterClassElement: - Enabled: false -Lint/EmptyBlock: - Enabled: false -Lint/EmptyClass: - Enabled: false -Lint/NoReturnInBeginEndBlocks: - Enabled: false -Lint/ToEnumArguments: - Enabled: false -Lint/UnexpectedBlockArity: - Enabled: false -Lint/UnmodifiedReduceAccumulator: - Enabled: false -Performance/CollectionLiteralInLoop: - Enabled: false -Style/ArgumentsForwarding: - Enabled: false -Style/CollectionCompact: - Enabled: false -Style/DocumentDynamicEvalDefinition: - Enabled: false -Style/NegatedIfElseCondition: - Enabled: false -Style/NilLambda: - Enabled: false -Style/RedundantArgument: - Enabled: false -Style/SwapValues: - Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 00000000..478e5f8d --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,103 @@ +# Offense count: 30 +# This cop supports unsafe autocorrection (--autocorrect-all). +Lint/BooleanSymbol: + Exclude: + - 'lib/puppet/type/ini_setting.rb' + - 'lib/puppet/type/ini_subsetting.rb' + - 'lib/puppet/util/setting_value.rb' + +# Offense count: 11 +# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. +Metrics/AbcSize: + Max: 48 + +# Offense count: 8 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode. +# AllowedMethods: refine +Metrics/BlockLength: + Max: 128 + +# Offense count: 3 +# Configuration parameters: AllowedMethods, AllowedPatterns. +Metrics/CyclomaticComplexity: + Max: 18 + +# Offense count: 13 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +Metrics/MethodLength: + Max: 33 + +# Offense count: 2 +# Configuration parameters: AllowedMethods, AllowedPatterns. +Metrics/PerceivedComplexity: + Max: 20 + +# Offense count: 114 +# Configuration parameters: ForbiddenDelimiters. +# ForbiddenDelimiters: (?i-mx:(^|\s)(EO[A-Z]{1}|END)(\s|$)) +Naming/HeredocDelimiterNaming: + Exclude: + - 'lib/puppet/type/ini_subsetting.rb' + - 'spec/acceptance/ini_setting_spec.rb' + - 'spec/acceptance/ini_subsetting_spec.rb' + - 'spec/unit/puppet/provider/ini_setting/inheritance_spec.rb' + - 'spec/unit/puppet/provider/ini_setting/ruby_spec.rb' + - 'spec/unit/puppet/provider/ini_subsetting/ruby_spec.rb' + - 'spec/unit/puppet/util/ini_file_spec.rb' + +# Offense count: 12 +# Configuration parameters: IgnoredMetadata. +RSpec/DescribeClass: + Exclude: + - '**/spec/features/**/*' + - '**/spec/requests/**/*' + - '**/spec/routing/**/*' + - '**/spec/system/**/*' + - '**/spec/views/**/*' + - 'spec/acceptance/ini_setting_spec.rb' + - 'spec/acceptance/ini_subsetting_spec.rb' + - 'spec/classes/create_ini_settings_test_spec.rb' + - 'spec/classes/create_multiple_ini_settings_spec.rb' + - 'spec/classes/inherit_test1_spec.rb' + - 'spec/functions/create_ini_settings_spec.rb' + - 'spec/functions/inifile_create_ini_settings_spec.rb' + - 'spec/unit/puppet/provider/ini_setting/inheritance_spec.rb' + - 'spec/unit/puppet/provider/ini_setting/ruby_spec.rb' + - 'spec/unit/puppet/provider/ini_subsetting/ruby_spec.rb' + - 'spec/unit/puppet/type/ini_setting_spec.rb' + - 'spec/unit/puppet/type/ini_subetting_spec.rb' + +# Offense count: 8 +# Configuration parameters: CountAsOne. +RSpec/ExampleLength: + Max: 26 + +# Offense count: 5 +RSpec/MultipleExpectations: + Max: 4 + +# Offense count: 7 +# Configuration parameters: EnforcedStyle, IgnoreSharedExamples. +# SupportedStyles: always, named_only +RSpec/NamedSubject: + Exclude: + - 'spec/acceptance/ini_subsetting_spec.rb' + - 'spec/classes/create_ini_settings_test_spec.rb' + - 'spec/classes/create_multiple_ini_settings_spec.rb' + - 'spec/classes/inherit_test1_spec.rb' + +# Offense count: 29 +# Configuration parameters: AllowedGroups. +RSpec/NestedGroups: + Max: 6 + +# Offense count: 4 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: nested, compact +Style/ClassAndModuleChildren: + Exclude: + - 'lib/puppet/util/external_iterator.rb' + - 'lib/puppet/util/ini_file.rb' + - 'lib/puppet/util/ini_file/section.rb' + - 'lib/puppet/util/setting_value.rb' diff --git a/Gemfile b/Gemfile index 1881afe1..7fad5040 100644 --- a/Gemfile +++ b/Gemfile @@ -14,31 +14,31 @@ def location_for(place_or_version, fake_version = nil) end group :development do - gem "json", '= 2.1.0', require: false if Gem::Requirement.create(['>= 2.5.0', '< 2.7.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) - gem "json", '= 2.3.0', require: false if Gem::Requirement.create(['>= 2.7.0', '< 3.0.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) - gem "json", '= 2.5.1', require: false if Gem::Requirement.create(['>= 3.0.0', '< 3.0.5']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) - gem "json", '= 2.6.1', require: false if Gem::Requirement.create(['>= 3.1.0', '< 3.1.3']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) - gem "json", '= 2.6.3', require: false if Gem::Requirement.create(['>= 3.2.0', '< 4.0.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) - gem "voxpupuli-puppet-lint-plugins", '~> 3.1', require: false - gem "facterdb", '~> 1.18', require: false - gem "metadata-json-lint", '>= 2.0.2', '< 4.0.0', require: false - gem "puppetlabs_spec_helper", '>= 3.0.0', '< 5.0.0', require: false - gem "rspec-puppet-facts", '~> 2.0', require: false - gem "codecov", '~> 0.2', require: false - gem "dependency_checker", '~> 0.2', require: false - gem "parallel_tests", '~> 3.4', require: false - gem "pry", '~> 0.10', require: false - gem "simplecov-console", '~> 0.5', require: false - gem "puppet-debugger", '~> 1.0', require: false - gem "rubocop", '= 1.6.1', require: false - gem "rubocop-performance", '= 1.9.1', require: false - gem "rubocop-rspec", '= 2.0.1', require: false - gem "rb-readline", '= 0.5.5', require: false, platforms: [:mswin, :mingw, :x64_mingw] - gem "github_changelog_generator", '= 1.15.2', require: false + gem "json", '= 2.1.0', require: false if Gem::Requirement.create(['>= 2.5.0', '< 2.7.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "json", '= 2.3.0', require: false if Gem::Requirement.create(['>= 2.7.0', '< 3.0.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "json", '= 2.5.1', require: false if Gem::Requirement.create(['>= 3.0.0', '< 3.0.5']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "json", '= 2.6.1', require: false if Gem::Requirement.create(['>= 3.1.0', '< 3.1.3']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "json", '= 2.6.3', require: false if Gem::Requirement.create(['>= 3.2.0', '< 4.0.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "voxpupuli-puppet-lint-plugins", '~> 4.0', require: false + gem "facterdb", '~> 1.18', require: false + gem "metadata-json-lint", '>= 2.0.2', '< 4.0.0', require: false + gem "puppetlabs_spec_helper", '~> 5.0', require: false + gem "rspec-puppet-facts", '~> 2.0', require: false + gem "codecov", '~> 0.2', require: false + gem "dependency_checker", '~> 0.2', require: false + gem "parallel_tests", '= 3.12.1', require: false + gem "pry", '~> 0.10', require: false + gem "simplecov-console", '~> 0.5', require: false + gem "puppet-debugger", '~> 1.0', require: false + gem "rubocop", '~> 1.48.1', require: false + gem "rubocop-performance", '~> 1.16', require: false + gem "rubocop-rspec", '~> 2.19', require: false + gem "rb-readline", '= 0.5.5', require: false, platforms: [:mswin, :mingw, :x64_mingw] + gem "github_changelog_generator", '= 1.15.2', require: false end group :system_tests do - gem "puppet_litmus", '<= 0.34.6', require: false, platforms: [:ruby] - gem "serverspec", '~> 2.41', require: false + gem "puppet_litmus", '< 1.0.0', require: false, platforms: [:ruby, :x64_mingw] + gem "serverspec", '~> 2.41', require: false end puppet_version = ENV['PUPPET_GEM_VERSION'] diff --git a/Rakefile b/Rakefile index 0f8754eb..53152e21 100644 --- a/Rakefile +++ b/Rakefile @@ -4,7 +4,6 @@ require 'bundler' require 'puppet_litmus/rake_tasks' if Bundler.rubygems.find_name('puppet_litmus').any? require 'puppetlabs_spec_helper/rake_tasks' require 'puppet-syntax/tasks/puppet-syntax' -require 'puppet_blacksmith/rake_tasks' if Bundler.rubygems.find_name('puppet-blacksmith').any? require 'github_changelog_generator/task' if Bundler.rubygems.find_name('github_changelog_generator').any? require 'puppet-strings/tasks' if Bundler.rubygems.find_name('puppet-strings').any? diff --git a/lib/puppet/functions/inifile/create_ini_settings.rb b/lib/puppet/functions/inifile/create_ini_settings.rb index 072e3a7d..3ddd2621 100644 --- a/lib/puppet/functions/inifile/create_ini_settings.rb +++ b/lib/puppet/functions/inifile/create_ini_settings.rb @@ -23,9 +23,9 @@ def default_impl(settings, defaults = {}) settings[section].each do |setting, value| res["#{path} [#{section}] #{setting}"] = { - 'ensure' => 'present', + 'ensure' => 'present', 'section' => section, - 'setting' => setting, + 'setting' => setting }.merge(if value.is_a?(Hash) value else diff --git a/lib/puppet/provider/ini_setting/ruby.rb b/lib/puppet/provider/ini_setting/ruby.rb index c182d2b1..c3a1f663 100644 --- a/lib/puppet/provider/ini_setting/ruby.rb +++ b/lib/puppet/provider/ini_setting/ruby.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require File.expand_path('../../../util/ini_file', __FILE__) +require File.expand_path('../../util/ini_file', __dir__) Puppet::Type.type(:ini_setting).provide(:ruby) do def self.instances @@ -10,6 +10,7 @@ def self.instances self.file_path, and that will provide the value for the path to the ini file.' raise(Puppet::Error, 'Ini_settings only support collecting instances when a file path is hard coded') unless respond_to?(:file_path) + # figure out what to do about the seperator ini_file = Puppet::Util::IniFile.new(file_path, '=') resources = [] diff --git a/lib/puppet/provider/ini_subsetting/ruby.rb b/lib/puppet/provider/ini_subsetting/ruby.rb index 0f6b7721..34a03d90 100644 --- a/lib/puppet/provider/ini_subsetting/ruby.rb +++ b/lib/puppet/provider/ini_subsetting/ruby.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require File.expand_path('../../../util/ini_file', __FILE__) -require File.expand_path('../../../util/setting_value', __FILE__) +require File.expand_path('../../util/ini_file', __dir__) +require File.expand_path('../../util/setting_value', __dir__) Puppet::Type.type(:ini_subsetting).provide(:ruby) do desc ' diff --git a/lib/puppet/type/ini_setting.rb b/lib/puppet/type/ini_setting.rb index 474c4f4c..53cd5c22 100644 --- a/lib/puppet/type/ini_setting.rb +++ b/lib/puppet/type/ini_setting.rb @@ -47,9 +47,7 @@ def munge_boolean_md5(value) newparam(:setting) do desc 'The name of the setting to be defined.' munge do |value| - if value.match?(%r{(^\s|\s$)}) - Puppet.warn('Settings should not have spaces in the value, we are going to strip the whitespace') - end + Puppet.warn('Settings should not have spaces in the value, we are going to strip the whitespace') if value.match?(%r{(^\s|\s$)}) value.strip end end @@ -62,9 +60,7 @@ def munge_boolean_md5(value) newparam(:path) do desc 'The ini file Puppet will ensure contains the specified setting.' validate do |value| - unless Puppet::Util.absolute_path?(value) - raise(Puppet::Error, _("File paths must be fully qualified, not '%{value}'") % { value: value }) - end + raise(Puppet::Error, _("File paths must be fully qualified, not '%{value}'") % { value: value }) unless Puppet::Util.absolute_path?(value) end end @@ -101,7 +97,7 @@ def should_to_s(newvalue) if @resource[:show_diff] == :true && Puppet[:show_diff] newvalue elsif @resource[:show_diff] == :md5 && Puppet[:show_diff] - '{md5}' + Digest::MD5.hexdigest(newvalue.to_s) + "{md5}#{Digest::MD5.hexdigest(newvalue.to_s)}" else '[redacted sensitive information]' end @@ -145,9 +141,8 @@ def insync?(current) end def refresh - if self[:ensure] == :absent && self[:refreshonly] - return provider.destroy - end + return provider.destroy if self[:ensure] == :absent && self[:refreshonly] + # update the value in the provider, which will save the value to the ini file provider.value = self[:value] if self[:refreshonly] end diff --git a/lib/puppet/type/ini_subsetting.rb b/lib/puppet/type/ini_subsetting.rb index 403e0474..38589411 100644 --- a/lib/puppet/type/ini_subsetting.rb +++ b/lib/puppet/type/ini_subsetting.rb @@ -52,9 +52,7 @@ def munge_boolean_md5(value) newparam(:path) do desc 'The ini file Puppet will ensure contains the specified setting.' validate do |value| - unless Puppet::Util.absolute_path?(value) - raise(Puppet::Error, _("File paths must be fully qualified, not '%{value}'") % { value: value }) - end + raise(Puppet::Error, _("File paths must be fully qualified, not '%{value}'") % { value: value }) unless Puppet::Util.absolute_path?(value) end end newparam(:show_diff) do @@ -77,9 +75,7 @@ def munge_boolean_md5(value) defaultto('') validate do |value| - unless value.match?(%r{^["']?$}) - raise Puppet::Error, _(%q(:quote_char valid values are '', '"' and "'")) - end + raise Puppet::Error, _(%q(:quote_char valid values are '', '"' and "'")) unless value.match?(%r{^["']?$}) end end @@ -96,7 +92,7 @@ def should_to_s(newvalue) if @resource[:show_diff] == :true && Puppet[:show_diff] newvalue elsif @resource[:show_diff] == :md5 && Puppet[:show_diff] - '{md5}' + Digest::MD5.hexdigest(newvalue.to_s) + "{md5}#{Digest::MD5.hexdigest(newvalue.to_s)}" else '[redacted sensitive information]' end @@ -108,7 +104,7 @@ def is_to_s(value) # rubocop:disable Naming/PredicateName : Changing breaks the end newparam(:insert_type) do - desc <<-eof + desc <<-EOF Where the new subsetting item should be inserted * :start - insert at the beginning of the line. @@ -116,7 +112,7 @@ def is_to_s(value) # rubocop:disable Naming/PredicateName : Changing breaks the * :before - insert before the specified element if possible. * :after - insert after the specified element if possible. * :index - insert at the specified index number. - eof + EOF newvalues(:start, :end, :before, :after, :index) defaultto(:end) diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb index 2eaf401a..1636479f 100644 --- a/lib/puppet/util/ini_file.rb +++ b/lib/puppet/util/ini_file.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true -require File.expand_path('../external_iterator', __FILE__) -require File.expand_path('../ini_file/section', __FILE__) +require File.expand_path('external_iterator', __dir__) +require File.expand_path('ini_file/section', __dir__) module Puppet::Util # # ini_file.rb # - class IniFile - def initialize(path, key_val_separator = ' = ', section_prefix = '[', section_suffix = ']', + class IniFile # rubocop:disable Metrics/ClassLength + def initialize(path, key_val_separator = ' = ', section_prefix = '[', section_suffix = ']', # rubocop:disable Metrics/ParameterLists indent_char = ' ', indent_width = nil) k_v_s = (key_val_separator =~ %r{^\s+$}) ? ' ' : key_val_separator.strip @@ -16,7 +16,7 @@ def initialize(path, key_val_separator = ' = ', section_prefix = '[', section_su @section_prefix = section_prefix @section_suffix = section_suffix @indent_char = indent_char - @indent_width = indent_width ? indent_width.to_i : nil + @indent_width = indent_width&.to_i @section_regex = section_regex @setting_regex = %r{^(\s*)([^#;\s]|[^#;\s].*?[^\s#{k_v_s}])(\s*#{k_v_s}[ \t]*)(.*)\s*$} @@ -70,11 +70,9 @@ def set_value(*args) complete_setting = { setting: setting, separator: separator, - value: value, + value: value } - unless @sections_hash.key?(section_name) - add_section(Section.new(section_name, nil, nil, nil, nil)) - end + add_section(Section.new(section_name, nil, nil, nil, nil)) unless @sections_hash.key?(section_name) section = @sections_hash[section_name] @@ -110,6 +108,7 @@ def set_value(*args) def remove_setting(section_name, setting) section = @sections_hash[section_name] return unless section.existing_setting?(setting) + # If the setting is found, we have some work to do. # First, we remove the line from our array of lines: remove_line(section, setting) @@ -125,6 +124,7 @@ def remove_setting(section_name, setting) decrement_section_line_numbers(section_index + 1) return unless section.empty? + # By convention, it's time to remove this newly emptied out section lines.delete_at(section.start_line) decrement_section_line_numbers(section_index + 1) @@ -144,9 +144,7 @@ def save whitespace_buffer = [] if section.new_section? && !section.global? - if index == 1 && !global_empty || index > 1 - fh.puts('') - end + fh.puts('') if (index == 1 && !global_empty) || index > 1 fh.puts("#{@section_prefix}#{section.name}#{@section_suffix}") end @@ -247,18 +245,16 @@ def read_section(name, start_line, line_iter) def update_line(section, setting, value) (section.start_line..section.end_line).each do |line_num| next unless (match = @setting_regex.match(lines[line_num])) - if match[2] == setting - lines[line_num] = "#{match[1]}#{match[2]}#{match[3]}#{value}" - end + + lines[line_num] = "#{match[1]}#{match[2]}#{match[3]}#{value}" if match[2] == setting end end def remove_line(section, setting) (section.start_line..section.end_line).each do |line_num| next unless (match = @setting_regex.match(lines[line_num])) - if match[2] == setting - lines.delete_at(line_num) - end + + lines.delete_at(line_num) if match[2] == setting end end @@ -292,11 +288,10 @@ def self.readlines(path) # rubocop:disable Lint/IneffectiveAccessModifier : Atte # be used to mimic the whitespace from the comment line def find_commented_setting(section, setting) return nil if section.new_section? + (section.start_line..section.end_line).each do |line_num| next unless (match = @commented_setting_regex.match(lines[line_num])) - if match[3] == setting - return { match: match, line_num: line_num } - end + return { match: match, line_num: line_num } if match[3] == setting end nil end @@ -330,9 +325,10 @@ def increment_section_line_numbers(section_index) end end - def flush_buffer_to_file(buffer, fh) + def flush_buffer_to_file(buffer, file) return if buffer.empty? - buffer.each { |l| fh.puts(l) } + + buffer.each { |l| file.puts(l) } buffer.clear end end diff --git a/lib/puppet/util/setting_value.rb b/lib/puppet/util/setting_value.rb index 51e349dd..fb1c4741 100644 --- a/lib/puppet/util/setting_value.rb +++ b/lib/puppet/util/setting_value.rb @@ -17,11 +17,12 @@ def initialize(setting_value, subsetting_separator = ' ', default_quote_char = ' @subsetting_items = [] return unless @setting_value + unquoted, quote_char = unquote_setting_value(setting_value) @quote_char = quote_char unless quote_char.empty? # an item can contain escaped separator @subsetting_items = unquoted.scan(Regexp.new("(?:(?:[^\\#{@subsetting_separator}]|\\.)+)")) - @subsetting_items.map! { |item| item.strip } + @subsetting_items.map!(&:strip) end # If the setting value is quoted, the quotes are @@ -37,10 +38,10 @@ def unquote_setting_value(setting_value) quote_char = "'" end - unquoted = if quote_char != '' - setting_value[1, setting_value.length - 2] - else + unquoted = if quote_char == '' setting_value + else + setting_value[1, setting_value.length - 2] end [unquoted, quote_char] @@ -49,7 +50,7 @@ def unquote_setting_value(setting_value) # Get the resulting setting value by joining all the # subsettings, separator and quote characters. # @return [String] - def get_value + def get_value # rubocop:disable Naming/AccessorMethodName value = @subsetting_items.join @subsetting_separator @quote_char + value + @quote_char end @@ -66,8 +67,9 @@ def get_subsetting_value(subsetting, use_exact_match = :false) return nil unless index # the exact match is set and the item is found, the value should be true return true if use_exact_match == :true + item = @subsetting_items[index] - item[(subsetting.length + @key_val_separator.length)..-1] + item[(subsetting.length + @key_val_separator.length)..] end # Add a new subsetting item to the list of existing items @@ -110,7 +112,7 @@ def add_subsetting(subsetting, subsetting_value, use_exact_match = :false, inser before_index = insert_value.to_i before_index = @subsetting_items.length if before_index > @subsetting_items.length @subsetting_items.insert(before_index, new_item) - else + else # rubocop:disable Lint/DuplicateBranch @subsetting_items.push(new_item) end diff --git a/metadata.json b/metadata.json index ce0b268e..aa35a77a 100644 --- a/metadata.json +++ b/metadata.json @@ -101,10 +101,10 @@ "requirements": [ { "name": "puppet", - "version_requirement": ">= 6.0.0 < 8.0.0" + "version_requirement": ">= 7.0.0 < 9.0.0" } ], "template-url": "https://github.com/puppetlabs/pdk-templates.git#main", - "template-ref": "heads/main-0-g9375381", - "pdk-version": "2.6.1" + "template-ref": "heads/main-0-g604a3ec", + "pdk-version": "2.7.0" } diff --git a/spec/acceptance/ini_setting_spec.rb b/spec/acceptance/ini_setting_spec.rb index c6608878..3e70ccbf 100644 --- a/spec/acceptance/ini_setting_spec.rb +++ b/spec/acceptance/ini_setting_spec.rb @@ -15,7 +15,7 @@ end it 'applies the manifest twice' do - idempotent_apply(pp) + expect { idempotent_apply(pp) }.not_to raise_error end describe file(path) do @@ -43,7 +43,7 @@ end end - context 'ensure parameter => present for global and section' do + context 'when ensure parameter => present for global and section' do pp = <<-EOS ini_setting { 'ensure => present for section': ensure => present, @@ -64,7 +64,7 @@ it_behaves_like 'has_content', "#{basedir}/ini_setting.ini", pp, %r{four = five\R\R\[one\]\Rtwo = three} end - context 'ensure parameter => absent for key/value' do + context 'when ensure parameter => absent for key/value' do before :all do ipp = <<-MANIFEST file { '#{basedir}/ini_setting.ini': @@ -87,7 +87,7 @@ EOS it 'applies the manifest twice' do - idempotent_apply(pp) + expect { idempotent_apply(pp) }.not_to raise_error end describe file("#{basedir}/ini_setting.ini") do @@ -103,7 +103,7 @@ end end - context 'ensure parameter => absent for global' do + context 'when ensure parameter => absent for global' do before :all do ipp = <<-MANIFEST file { '#{basedir}/ini_setting.ini': @@ -130,7 +130,7 @@ EOS it 'applies the manifest twice' do - idempotent_apply(pp) + expect { idempotent_apply(pp) }.not_to raise_error end describe file("#{basedir}/ini_setting.ini") do @@ -147,7 +147,7 @@ end describe 'path parameter' do - context 'path => foo' do + context 'when path => foo' do pp = <<-EOS ini_setting { 'path => foo': ensure => present, @@ -162,7 +162,7 @@ end end - context 'ensure parameter => present and only section' do + context 'when ensure parameter => present and only section' do pp = <<-EOS ini_setting { 'ensure => present for section': ensure => present, @@ -192,11 +192,12 @@ } EOS - context "show_diff => #{i[:show_diff]}" do + context "when show_diff => #{i[:show_diff]}" do res = apply_manifest(pp, expect_changes: true) it 'applies manifest and expects changed value to be logged in proper form' do expect(res.stdout).to match(i[:matcher]) end + it 'applies manifest and expects changed value to be logged in proper form #optional test' do expect(res.stdout).not_to match(i[:value]) unless i[:show_diff] == true end @@ -222,7 +223,7 @@ before :each do ipp = <<-MANIFEST file { '#{basedir}/ini_setting.ini': - content => "[section1]\n valueinsection1 = 123\", + content => "[section1]\n valueinsection1 = 123", force => true, } MANIFEST @@ -233,8 +234,9 @@ after :each do run_shell("rm #{basedir}/ini_setting.ini", expect_failures: true) end + context 'when event is triggered' do - context 'update setting value' do + context 'when update setting value' do let(:update_value_manifest) do <<-EOS notify { foo: @@ -258,6 +260,7 @@ describe file("#{basedir}/ini_setting.ini") do it { is_expected.to be_file } + describe '#content' do subject { super().content } @@ -266,7 +269,7 @@ end end - context 'remove setting value' do + context 'when remove setting value' do let(:remove_setting_manifest) do <<-EOS notify { foo: @@ -289,6 +292,7 @@ describe file("#{basedir}/ini_setting.ini") do it { is_expected.to be_file } + describe '#content' do subject { super().content } @@ -299,7 +303,7 @@ end context 'when not receiving an event' do - context 'does not update setting' do + context 'when does not update setting' do let(:does_not_update_value_manifest) do <<-EOS file { "#{basedir}/ini_setting.ini": @@ -323,6 +327,7 @@ describe file("#{basedir}/ini_setting.ini") do it { is_expected.to be_file } + describe '#content' do subject { super().content } @@ -332,7 +337,7 @@ end end - context 'does not remove setting' do + context 'when does not remove setting' do let(:does_not_remove_setting_manifest) do <<-EOS file { "#{basedir}/ini_setting.ini": @@ -356,6 +361,7 @@ describe file("#{basedir}/ini_setting.ini") do it { is_expected.to be_file } + describe '#content' do subject { super().content } diff --git a/spec/acceptance/ini_subsetting_spec.rb b/spec/acceptance/ini_subsetting_spec.rb index aec0b7a1..e1548f57 100644 --- a/spec/acceptance/ini_subsetting_spec.rb +++ b/spec/acceptance/ini_subsetting_spec.rb @@ -15,7 +15,7 @@ end it 'applies the manifest twice' do - idempotent_apply(pp) + expect { idempotent_apply(pp) }.not_to raise_error end describe file(path) do @@ -25,7 +25,7 @@ subject { super().content } it { - is_expected.to match content + expect(subject).to match content } end end @@ -83,7 +83,7 @@ EOS it 'applies the manifest twice' do - idempotent_apply(pp) + expect { idempotent_apply(pp) }.not_to raise_error end describe file("#{basedir}/ini_subsetting.ini") do @@ -121,7 +121,7 @@ it 'applies the manifest twice' do run_shell("echo -e [one]\\\\nkey = alphabet betatrons > #{basedir}/ini_subsetting.ini", expect_failures: true) - idempotent_apply(pp) + expect { idempotent_apply(pp) }.not_to raise_error end describe file("#{basedir}/ini_subsetting.ini") do @@ -143,9 +143,9 @@ ['-Xmx'] => %r{args=""}, ['-Xmx', '256m'] => %r{args=-Xmx256m}, ['-Xmx', '512m'] => %r{args="-Xmx512m"}, - ['-Xms', '256m'] => %r{args="-Xmx256m -Xms256m"}, + ['-Xms', '256m'] => %r{args="-Xmx256m -Xms256m"} }.each do |parameter, content| - context %(with '#{parameter.first}' #{(parameter.length > 1) ? '=> \'' + parameter[1] + '\'' : 'absent'} makes '#{content}') do + context %(with '#{parameter.first}' #{(parameter.length > 1) ? "=> '#{parameter[1]}'" : 'absent'} makes '#{content}') do path = File.join(basedir, 'ini_subsetting.ini') before :all do ipp = <<-MANIFEST @@ -176,7 +176,7 @@ EOS it 'applies the manifest twice' do - idempotent_apply(pp) + expect { idempotent_apply(pp) }.not_to raise_error end describe file("#{basedir}/ini_subsetting.ini") do @@ -211,11 +211,12 @@ } EOS - context "show_diff => #{i[:show_diff]}" do + context "when show_diff => #{i[:show_diff]}" do res = apply_manifest(pp, expect_changes: true) it 'applies manifest and expects changed value to be logged in proper form' do expect(res.stdout).to match(i[:matcher]) end + it 'applies manifest and expects changed value to be logged in proper form #optional test' do expect(res.stdout).not_to match(i[:value]) unless i[:show_diff] == true end @@ -227,26 +228,26 @@ [ { insert_type: :start, - content: %r{d a b c}, + content: %r{d a b c} }, { insert_type: :end, - content: %r{a b c d}, + content: %r{a b c d} }, { insert_type: :before, insert_value: 'c', - content: %r{a b d c}, + content: %r{a b d c} }, { insert_type: :after, insert_value: 'a', - content: %r{a d b c}, + content: %r{a d b c} }, { insert_type: :index, insert_value: 2, - content: %r{a b d c}, + content: %r{a b d c} }, ].each do |params| context "with '#{params[:insert_type]}' makes '#{params[:content]}'" do diff --git a/spec/classes/create_ini_settings_test_spec.rb b/spec/classes/create_ini_settings_test_spec.rb index 5f5555bf..0208f5d9 100644 --- a/spec/classes/create_ini_settings_test_spec.rb +++ b/spec/classes/create_ini_settings_test_spec.rb @@ -4,22 +4,25 @@ # end-to-end test of the create_init_settings function describe 'create_ini_settings_test' do it { is_expected.to have_ini_setting_resource_count(3) } + it { - is_expected.to contain_ini_setting('/tmp/foo.ini [section1] setting1').with( + expect(subject).to contain_ini_setting('/tmp/foo.ini [section1] setting1').with( ensure: 'present', section: 'section1', setting: 'setting1', value: 'val1', path: '/tmp/foo.ini' ) } + it { - is_expected.to contain_ini_setting('/tmp/foo.ini [section2] setting2').with( + expect(subject).to contain_ini_setting('/tmp/foo.ini [section2] setting2').with( ensure: 'present', section: 'section2', setting: 'setting2', value: 'val2', path: '/tmp/foo.ini' ) } + it { - is_expected.to contain_ini_setting('/tmp/foo.ini [section2] setting3').with( + expect(subject).to contain_ini_setting('/tmp/foo.ini [section2] setting3').with( ensure: 'absent', section: 'section2', setting: 'setting3', path: '/tmp/foo.ini' ) diff --git a/spec/classes/create_multiple_ini_settings_spec.rb b/spec/classes/create_multiple_ini_settings_spec.rb index db797440..c5c83828 100644 --- a/spec/classes/create_multiple_ini_settings_spec.rb +++ b/spec/classes/create_multiple_ini_settings_spec.rb @@ -3,19 +3,19 @@ require 'spec_helper' describe 'create_multiple_ini_settings' do - context 'on a non-Windows platform', if: !Puppet::Util::Platform.windows? do + context 'when on a non-Windows platform', if: !Puppet::Util::Platform.windows? do let(:facts) do - { 'os' => { 'family' => 'RedHat', + { 'os' => { 'family' => 'RedHat', 'release' => { 'major' => '7', 'minor' => '1', - 'full' => '7.1.1503' } } } + 'full' => '7.1.1503' } } } end it { is_expected.to compile } it { is_expected.to have_ini_setting_resource_count(2) } it { - is_expected.to contain_ini_setting('/tmp/foo.ini [section1] setting1').with( + expect(subject).to contain_ini_setting('/tmp/foo.ini [section1] setting1').with( ensure: 'present', section: 'section1', setting: 'setting1', @@ -23,8 +23,9 @@ path: '/tmp/foo.ini', ) } + it { - is_expected.to contain_ini_setting('/tmp/foo.ini [section1] setting2').with( + expect(subject).to contain_ini_setting('/tmp/foo.ini [section1] setting2').with( ensure: 'absent', section: 'section1', setting: 'setting2', @@ -33,7 +34,7 @@ } end - context 'on a Windows platform', if: Puppet::Util::Platform.windows? do + context 'when on a Windows platform', if: Puppet::Util::Platform.windows? do let(:facts) do { 'os' => { 'family' => 'windows' } } end diff --git a/spec/classes/inherit_test1_spec.rb b/spec/classes/inherit_test1_spec.rb index 500810b2..5ed8ce4a 100644 --- a/spec/classes/inherit_test1_spec.rb +++ b/spec/classes/inherit_test1_spec.rb @@ -5,6 +5,6 @@ # parser OK. describe 'inherit_test1' do it { - is_expected.to contain_inherit_ini_setting('valid_type').with('value' => 'true') + expect(subject).to contain_inherit_ini_setting('valid_type').with('value' => 'true') } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 07db7342..1a8b68bd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,11 +9,11 @@ require 'spec_helper_local' if File.file?(File.join(File.dirname(__FILE__), 'spec_helper_local.rb')) -include RspecPuppetFacts +include RspecPuppetFacts # rubocop:disable Style/MixinUsage default_facts = { puppetversion: Puppet.version, - facterversion: Facter.version, + facterversion: Facter.version } default_fact_files = [ @@ -26,7 +26,7 @@ begin default_facts.merge!(YAML.safe_load(File.read(f), [], [], true)) - rescue => e + rescue StandardError => e RSpec.configuration.reporter.message "WARNING: Unable to load #{f}: #{e}" end end diff --git a/spec/spec_helper_local.rb b/spec/spec_helper_local.rb index 0d9188cb..570190ff 100644 --- a/spec/spec_helper_local.rb +++ b/spec/spec_helper_local.rb @@ -30,7 +30,7 @@ end shared_examples 'create_ini_settings function' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params({}) } it { is_expected.to run.with_params({}, {}) } diff --git a/spec/unit/puppet/provider/ini_setting/inheritance_spec.rb b/spec/unit/puppet/provider/ini_setting/inheritance_spec.rb index 0f22485e..42e1952f 100644 --- a/spec/unit/puppet/provider/ini_setting/inheritance_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/inheritance_spec.rb @@ -16,9 +16,7 @@ def validate_file(expected_content, tmpfile) end before :each do - File.open(tmpfile, 'w') do |fh| - fh.write(orig_content) - end + File.write(tmpfile, orig_content) end context 'when calling instances' do @@ -57,7 +55,7 @@ def validate_file(expected_content, tmpfile) resource = Puppet::Type::Inherit_ini_setting.new(setting: 'set_this', value: 'to_that') provider = described_class.new(resource) provider.create - validate_file("set_this=to_that\n", tmpfile) + expect(validate_file("set_this=to_that\n", tmpfile)).to be_truthy end end end diff --git a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb index d1f448eb..3015079f 100644 --- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb @@ -14,7 +14,7 @@ { title: 'ini_setting_ensure_present_test', path: tmpfile, - section: 'section2', + section: 'section2' } end @@ -23,12 +23,8 @@ def validate_file(expected_content, tmpfile) end before :each do - File.open(tmpfile, 'w') do |fh| - fh.write(orig_content) - end - File.open(emptyfile, 'w') do |fh| - fh.write('') - end + File.write(tmpfile, orig_content) + File.write(emptyfile, '') end context 'when calling instances' do @@ -49,9 +45,10 @@ def self.file_path end end it 'returns [] when file is empty' do - expect(child_one).to receive(:file_path).and_return(emptyfile) + allow(child_one).to receive(:file_path).and_return(emptyfile) expect(child_one.instances).to eq([]) end + child_two = Class.new(provider_class) do def self.file_path '/some/file/path' @@ -92,7 +89,7 @@ def self.file_path '/some/file/path' end end - expect(child_three).to receive(:file_path).exactly(2).times.and_return(tmpfile) + expect(child_three).to receive(:file_path).twice.and_return(tmpfile) expect(child_three.instances.size).to eq(7) expected_array = [ { name: 'section1/foo', value: 'foovalue' }, @@ -114,57 +111,55 @@ def self.file_path expect(ensure_array.uniq).to eq([:present]) expect((real_array - expected_array) && (expected_array - real_array)).to eq([]) end - # rubocop:enable RSpec/ExampleLength - # rubocop:enable RSpec/MultipleExpectations end end context 'when ensuring that a setting is present' do let(:orig_content) do - <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue - -bar = barvalue -main = true -[section2] - -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment - --nonstandard- - shoes = purple + <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + [section2] + + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment + + -nonstandard- + shoes = purple EOS end - expected_content_one = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_one = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -yahoo = yippee -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + yahoo = yippee + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple + -nonstandard- + shoes = purple EOS it 'adds a missing setting to the correct section' do resource = Puppet::Type::Ini_setting.new(common_params.merge(setting: 'yahoo', value: 'yippee')) @@ -174,27 +169,27 @@ def self.file_path validate_file(expected_content_one, tmpfile) end - expected_content_two = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_two = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple - yahoo = yippee + -nonstandard- + shoes = purple + yahoo = yippee EOS it 'adds a missing setting to the correct section with pre/suffix' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'nonstandard', setting: 'yahoo', value: 'yippee', section_prefix: '-', section_suffix: '-')) @@ -204,27 +199,27 @@ def self.file_path validate_file(expected_content_two, tmpfile) end - expected_content_three = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_three = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple - indented = weirdly + -nonstandard- + shoes = purple + indented = weirdly EOS it 'adds a missing setting to the correct section with indent_char' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'nonstandard', setting: 'indented', value: 'weirdly', section_prefix: '-', section_suffix: '-', indent_char: "\t")) @@ -234,27 +229,27 @@ def self.file_path validate_file(expected_content_three, tmpfile) end - expected_content_four = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_four = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple - indented = weirdly + -nonstandard- + shoes = purple + indented = weirdly EOS it 'adds a missing setting to the correct section indented by indent_char * indent_width' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'nonstandard', setting: 'indented', value: 'weirdly', @@ -265,27 +260,27 @@ def self.file_path validate_file(expected_content_four, tmpfile) end - expected_content_five = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_five = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple - indented = weirdly + -nonstandard- + shoes = purple + indented = weirdly EOS it 'treats a string indent_width as an integer' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'nonstandard', setting: 'indented', value: 'weirdly', @@ -296,27 +291,27 @@ def self.file_path validate_file(expected_content_five, tmpfile) end - expected_content_six = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_six = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple -yahoo = yippee + -nonstandard- + shoes = purple + yahoo = yippee EOS it 'adds a missing setting to the correct section with colon' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section:sub', setting: 'yahoo', value: 'yippee')) @@ -326,26 +321,26 @@ def self.file_path validate_file(expected_content_six, tmpfile) end - expected_content_seven = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_seven = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue2 -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue2 + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple + -nonstandard- + shoes = purple EOS it 'modifies an existing setting with a different value' do resource = Puppet::Type::Ini_setting.new(common_params.merge(setting: 'baz', value: 'bazvalue2')) @@ -355,57 +350,56 @@ def self.file_path validate_file(expected_content_seven, tmpfile) end - expected_content_eight = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_eight = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = false -[section2] + bar = barvalue + main = false + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple + -nonstandard- + shoes = purple EOS it 'modifies an existing setting with a different boolean value' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section1', setting: 'main', value: false)) provider = described_class.new(resource) expect(provider.exists?).to be true - transaction = instance_double('transaction', persistence: true) - expect(Puppet::Transaction::ResourceHarness.new(transaction).evaluate(provider.resource).out_of_sync).to eq(true) + transaction = instance_double('transaction', persistence: true) # rubocop:disable RSpec/VerifiedDoubleReference + expect(Puppet::Transaction::ResourceHarness.new(transaction).evaluate(provider.resource).out_of_sync).to be(true) validate_file(expected_content_eight, tmpfile) end - # rubocop:enable RSpec/MultipleExpectations : Unable to reduce without altering test - expected_content_nine = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_nine = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = orange + -nonstandard- + shoes = orange EOS it 'modifies an existing setting with pre/suffix with a different value' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'nonstandard', setting: 'shoes', value: 'orange', section_prefix: '-', section_suffix: '-')) @@ -415,26 +409,26 @@ def self.file_path validate_file(expected_content_nine, tmpfile) end - expected_content_ten = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_ten = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=foo - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=foo + #another comment + ; yet another comment --nonstandard- - shoes = purple + -nonstandard- + shoes = purple EOS it 'modifies an existing setting with a different value - with colon in section' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section:sub', setting: 'subby', value: 'foo')) @@ -444,26 +438,26 @@ def self.file_path validate_file(expected_content_ten, tmpfile) end - expected_content_eleven = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_eleven = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.0.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.0.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple + -nonstandard- + shoes = purple EOS it 'is able to handle settings with non alphanumbering settings' do resource = Puppet::Type::Ini_setting.new(common_params.merge(setting: 'url', value: 'http://192.168.0.1:8080')) @@ -473,26 +467,26 @@ def self.file_path validate_file(expected_content_eleven, tmpfile) end - expected_content_twelve = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_twelve = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = http://192.168.0.1:8080 + -nonstandard- + shoes = http://192.168.0.1:8080 EOS it 'is able to handle settings with pre/suffix with non alphanumbering settings' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'nonstandard', setting: 'shoes', value: 'http://192.168.0.1:8080', section_prefix: '-', section_suffix: '-')) @@ -515,29 +509,29 @@ def self.file_path expect(provider.exists?).to be true end - expected_content_thirteen = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_thirteen = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple + -nonstandard- + shoes = purple -[section3] -huzzah = shazaam + [section3] + huzzah = shazaam EOS it 'adds a new section if the section does not exist' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section3', setting: 'huzzah', value: 'shazaam')) @@ -547,29 +541,29 @@ def self.file_path validate_file(expected_content_thirteen, tmpfile) end - expected_content_fourteen = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_fourteen = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple + -nonstandard- + shoes = purple --section3- -huzzah = shazaam + -section3- + huzzah = shazaam EOS it 'adds a new section with pre/suffix if the section does not exist' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section3', setting: 'huzzah', value: 'shazaam', section_prefix: '-', section_suffix: '-')) @@ -579,29 +573,29 @@ def self.file_path validate_file(expected_content_fourteen, tmpfile) end - expected_content_fifteen = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_fifteen = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple + -nonstandard- + shoes = purple -[section:subsection] -huzzah = shazaam + [section:subsection] + huzzah = shazaam EOS it 'adds a new section if the section does not exist - with colon' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section:subsection', setting: 'huzzah', value: 'shazaam')) @@ -611,29 +605,29 @@ def self.file_path validate_file(expected_content_fifteen, tmpfile) end - expected_content_sixteen = <<-EOS -# This is a comment -[section1] -; This is also a comment -foo=foovalue + expected_content_sixteen = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue -bar = barvalue -main = true -[section2] + bar = barvalue + main = true + [section2] -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment --nonstandard- - shoes = purple + -nonstandard- + shoes = purple --section:subsection- -huzzah = shazaam + -section:subsection- + huzzah = shazaam EOS it 'adds a new section with pre/suffix if the section does not exist - with colon' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section:subsection', setting: 'huzzah', value: 'shazaam', section_prefix: '-', section_suffix: '-')) @@ -648,7 +642,7 @@ def self.file_path provider = described_class.new(resource) expect(provider.exists?).to be false provider.create - validate_file(orig_content + "\n[section]\n", tmpfile) + validate_file("#{orig_content}\n[section]\n", tmpfile) end it 'is able to handle variables of any type' do @@ -741,22 +735,22 @@ def self.file_path context 'when dealing with a global section' do let(:orig_content) do - <<-EOS -# This is a comment -foo=blah -[section2] -foo = http://192.168.1.1:8080 - ; yet another comment + <<~EOS + # This is a comment + foo=blah + [section2] + foo = http://192.168.1.1:8080 + ; yet another comment EOS end - expected_content_one = <<-EOS -# This is a comment -foo=blah -bar = yippee -[section2] -foo = http://192.168.1.1:8080 - ; yet another comment + expected_content_one = <<~EOS + # This is a comment + foo=blah + bar = yippee + [section2] + foo = http://192.168.1.1:8080 + ; yet another comment EOS it "adds a missing setting if it doesn't exist" do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: '', setting: 'bar', value: 'yippee')) @@ -766,12 +760,12 @@ def self.file_path validate_file(expected_content_one, tmpfile) end - expected_content_two = <<-EOS -# This is a comment -foo=yippee -[section2] -foo = http://192.168.1.1:8080 - ; yet another comment + expected_content_two = <<~EOS + # This is a comment + foo=yippee + [section2] + foo = http://192.168.1.1:8080 + ; yet another comment EOS it 'modifies an existing setting with a different value' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: '', setting: 'foo', value: 'yippee')) @@ -790,17 +784,17 @@ def self.file_path context 'when the first line of the file is a section' do let(:orig_content) do - <<-EOS -[section2] -foo = http://192.168.1.1:8080 + <<~EOS + [section2] + foo = http://192.168.1.1:8080 EOS end - expected_content_one = <<-EOS -foo = yippee + expected_content_one = <<~EOS + foo = yippee -[section2] -foo = http://192.168.1.1:8080 + [section2] + foo = http://192.168.1.1:8080 EOS it 'is able to add a global setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: '', setting: 'foo', value: 'yippee')) @@ -810,9 +804,9 @@ def self.file_path validate_file(expected_content_one, tmpfile) end - expected_content_two = <<-EOS -[section2] -foo = yippee + expected_content_two = <<~EOS + [section2] + foo = yippee EOS it 'modifies an existing setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'foo', value: 'yippee')) @@ -822,10 +816,10 @@ def self.file_path validate_file(expected_content_two, tmpfile) end - expected_content_three = <<-EOS -[section2] -foo = http://192.168.1.1:8080 -bar = baz + expected_content_three = <<~EOS + [section2] + foo = http://192.168.1.1:8080 + bar = baz EOS it 'adds a new setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'bar', value: 'baz')) @@ -838,15 +832,15 @@ def self.file_path context 'when overriding the separator' do let(:orig_content) do - <<-EOS -[section2] -foo=bar + <<~EOS + [section2] + foo=bar EOS end - expected_content_one = <<-EOS -[section2] -foo=yippee + expected_content_one = <<~EOS + [section2] + foo=yippee EOS it 'modifies an existing setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'foo', value: 'yippee', key_val_separator: '=')) @@ -859,15 +853,15 @@ def self.file_path context 'when overriding the separator to something other than =' do let(:orig_content) do - <<-EOS -[section2] -foo: bar + <<~EOS + [section2] + foo: bar EOS end - expected_content_one = <<-EOS -[section2] -foo: yippee + expected_content_one = <<~EOS + [section2] + foo: yippee EOS it 'modifies an existing setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'foo', value: 'yippee', key_val_separator: ': ')) @@ -877,10 +871,10 @@ def self.file_path validate_file(expected_content_one, tmpfile) end - expected_content_two = <<-EOS -[section2] -foo: bar -bar: baz + expected_content_two = <<~EOS + [section2] + foo: bar + bar: baz EOS it 'adds a new setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'bar', value: 'baz', key_val_separator: ': ')) @@ -893,15 +887,15 @@ def self.file_path context 'when overriding the separator to a space' do let(:orig_content) do - <<-EOS -[section2] -foo bar + <<~EOS + [section2] + foo bar EOS end - expected_content_one = <<-EOS -[section2] -foo yippee + expected_content_one = <<~EOS + [section2] + foo yippee EOS it 'modifies an existing setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'foo', value: 'yippee', key_val_separator: ' ')) @@ -911,10 +905,10 @@ def self.file_path validate_file(expected_content_one, tmpfile) end - expected_content_two = <<-EOS -[section2] -foo bar -bar baz + expected_content_two = <<~EOS + [section2] + foo bar + bar baz EOS it 'adds a new setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'bar', value: 'baz', key_val_separator: ' ')) @@ -927,56 +921,56 @@ def self.file_path context 'when ensuring that a setting is absent' do let(:orig_content) do - <<-EOS -[section1] -; This is also a comment -foo=foovalue - -bar = barvalue -main = true -[section2] - -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section3] -# com = ment -uncom = ment -[section4] -uncom = ment -[section:sub] -subby=bar - #another comment - ; yet another comment - - -nonstandard- - shoes = purple -EOS - end - - expected_content_one = <<-EOS -[section1] -; This is also a comment - -bar = barvalue -main = true -[section2] - -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section3] -# com = ment -uncom = ment -[section4] -uncom = ment -[section:sub] -subby=bar - #another comment - ; yet another comment - - -nonstandard- - shoes = purple + <<~EOS + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + [section2] + + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section3] + # com = ment + uncom = ment + [section4] + uncom = ment + [section:sub] + subby=bar + #another comment + ; yet another comment + + -nonstandard- + shoes = purple + EOS + end + + expected_content_one = <<~EOS + [section1] + ; This is also a comment + + bar = barvalue + main = true + [section2] + + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section3] + # com = ment + uncom = ment + [section4] + uncom = ment + [section:sub] + subby=bar + #another comment + ; yet another comment + + -nonstandard- + shoes = purple EOS it 'removes a setting that exists' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section1', setting: 'foo', ensure: 'absent')) @@ -986,27 +980,27 @@ def self.file_path validate_file(expected_content_one, tmpfile) end - expected_content_two = <<-EOS -[section1] -; This is also a comment -foo=foovalue - -bar = barvalue -main = true -[section2] - -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section3] -# com = ment -uncom = ment -[section4] -uncom = ment -[section:sub] -subby=bar - #another comment - ; yet another comment + expected_content_two = <<~EOS + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + [section2] + + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section3] + # com = ment + uncom = ment + [section4] + uncom = ment + [section:sub] + subby=bar + #another comment + ; yet another comment EOS it 'removes a setting with pre/suffix that exists' do @@ -1017,30 +1011,30 @@ def self.file_path validate_file(expected_content_two, tmpfile) end - expected_content_three = <<-EOS -[section1] -; This is also a comment -foo=foovalue - -bar = barvalue -main = true -[section2] - -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section3] -# com = ment -uncom = ment -[section4] -uncom = ment -[section:sub] -subby=bar - #another comment - ; yet another comment - - -nonstandard- - shoes = purple + expected_content_three = <<~EOS + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + [section2] + + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section3] + # com = ment + uncom = ment + [section4] + uncom = ment + [section:sub] + subby=bar + #another comment + ; yet another comment + + -nonstandard- + shoes = purple EOS it 'does nothing for a setting that does not exist' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section:sub', setting: 'foo', ensure: 'absent')) @@ -1050,30 +1044,30 @@ def self.file_path validate_file(expected_content_three, tmpfile) end - expected_content_four = <<-EOS -[section1] -; This is also a comment -foo=foovalue - -bar = barvalue -main = true -[section2] - -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section3] -# com = ment -uncom = ment -[section4] -uncom = ment -[section:sub] -subby=bar - #another comment - ; yet another comment - - -nonstandard- - shoes = purple + expected_content_four = <<~EOS + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + [section2] + + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section3] + # com = ment + uncom = ment + [section4] + uncom = ment + [section:sub] + subby=bar + #another comment + ; yet another comment + + -nonstandard- + shoes = purple EOS it 'does nothing for a setting with pre/suffix that does not exist' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'nonstandard', setting: 'foo', ensure: 'absent', section_prefix: '-', section_suffix: '-')) @@ -1083,29 +1077,29 @@ def self.file_path validate_file(expected_content_four, tmpfile) end - expected_content_five = <<-EOS -[section1] -; This is also a comment -foo=foovalue - -bar = barvalue -main = true -[section2] - -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section3] -# com = ment -[section4] -uncom = ment -[section:sub] -subby=bar - #another comment - ; yet another comment - - -nonstandard- - shoes = purple + expected_content_five = <<~EOS + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + [section2] + + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section3] + # com = ment + [section4] + uncom = ment + [section:sub] + subby=bar + #another comment + ; yet another comment + + -nonstandard- + shoes = purple EOS it 'does not remove a section when the last uncommented setting is removed if there are comments' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section3', setting: 'uncom', ensure: 'absent')) @@ -1115,28 +1109,28 @@ def self.file_path validate_file(expected_content_five, tmpfile) end - expected_content_six = <<-EOS -[section1] -; This is also a comment -foo=foovalue - -bar = barvalue -main = true -[section2] - -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section3] -# com = ment -uncom = ment -[section:sub] -subby=bar - #another comment - ; yet another comment - - -nonstandard- - shoes = purple + expected_content_six = <<~EOS + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + [section2] + + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section3] + # com = ment + uncom = ment + [section:sub] + subby=bar + #another comment + ; yet another comment + + -nonstandard- + shoes = purple EOS it 'removes the section when removing the last line in the section' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section4', setting: 'uncom', ensure: 'absent')) @@ -1149,46 +1143,46 @@ def self.file_path context 'when dealing with indentation in sections' do let(:orig_content) do - <<-EOS -# This is a comment - [section1] - ; This is also a comment - foo=foovalue - - bar = barvalue - main = true - -[section2] - foo= foovalue2 - baz=bazvalue - url = http://192.168.1.1:8080 -[section:sub] - subby=bar - #another comment - fleezy = flam - ; yet another comment + <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + + [section2] + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + fleezy = flam + ; yet another comment EOS end - expected_content_one = <<-EOS -# This is a comment - [section1] - ; This is also a comment - foo=foovalue - - bar = barvalue - main = true - yahoo = yippee - -[section2] - foo= foovalue2 - baz=bazvalue - url = http://192.168.1.1:8080 -[section:sub] - subby=bar - #another comment - fleezy = flam - ; yet another comment + expected_content_one = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + yahoo = yippee + + [section2] + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + fleezy = flam + ; yet another comment EOS it 'adds a missing setting at the correct indentation when the header is aligned' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section1', setting: 'yahoo', value: 'yippee')) @@ -1198,24 +1192,24 @@ def self.file_path validate_file(expected_content_one, tmpfile) end - expected_content_two = <<-EOS -# This is a comment - [section1] - ; This is also a comment - foo=foovalue - - bar = barvalue2 - main = true - -[section2] - foo= foovalue2 - baz=bazvalue - url = http://192.168.1.1:8080 -[section:sub] - subby=bar - #another comment - fleezy = flam - ; yet another comment + expected_content_two = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue2 + main = true + + [section2] + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + fleezy = flam + ; yet another comment EOS it 'updates an existing setting at the correct indentation when the header is aligned' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section1', setting: 'bar', value: 'barvalue2')) @@ -1225,25 +1219,25 @@ def self.file_path validate_file(expected_content_two, tmpfile) end - expected_content_three = <<-EOS -# This is a comment - [section1] - ; This is also a comment - foo=foovalue - - bar = barvalue - main = true - -[section2] - foo= foovalue2 - baz=bazvalue - url = http://192.168.1.1:8080 - yahoo = yippee -[section:sub] - subby=bar - #another comment - fleezy = flam - ; yet another comment + expected_content_three = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + + [section2] + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + yahoo = yippee + [section:sub] + subby=bar + #another comment + fleezy = flam + ; yet another comment EOS it 'adds a missing setting at the correct indentation when the header is not aligned' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'yahoo', value: 'yippee')) @@ -1253,24 +1247,24 @@ def self.file_path validate_file(expected_content_three, tmpfile) end - expected_content_four = <<-EOS -# This is a comment - [section1] - ; This is also a comment - foo=foovalue - - bar = barvalue - main = true - -[section2] - foo= foovalue2 - baz=bazvalue2 - url = http://192.168.1.1:8080 -[section:sub] - subby=bar - #another comment - fleezy = flam - ; yet another comment + expected_content_four = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + + [section2] + foo= foovalue2 + baz=bazvalue2 + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + fleezy = flam + ; yet another comment EOS it 'updates an existing setting at the correct indentation when the header is not aligned' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'baz', value: 'bazvalue2')) @@ -1280,25 +1274,25 @@ def self.file_path validate_file(expected_content_four, tmpfile) end - expected_content_five = <<-EOS -# This is a comment - [section1] - ; This is also a comment - foo=foovalue - - bar = barvalue - main = true - -[section2] - foo= foovalue2 - baz=bazvalue - url = http://192.168.1.1:8080 -[section:sub] - subby=bar - #another comment - fleezy = flam - ; yet another comment - yahoo = yippee + expected_content_five = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + + [section2] + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + fleezy = flam + ; yet another comment + yahoo = yippee EOS it 'adds a missing setting at the min indentation when the section is not aligned' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section:sub', setting: 'yahoo', value: 'yippee')) @@ -1308,24 +1302,24 @@ def self.file_path validate_file(expected_content_five, tmpfile) end - expected_content_six = <<-EOS -# This is a comment - [section1] - ; This is also a comment - foo=foovalue - - bar = barvalue - main = true - -[section2] - foo= foovalue2 - baz=bazvalue - url = http://192.168.1.1:8080 -[section:sub] - subby=bar - #another comment - fleezy = flam2 - ; yet another comment + expected_content_six = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + + [section2] + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + fleezy = flam2 + ; yet another comment EOS it 'updates an existing setting at the previous indentation when the section is not aligned' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section:sub', setting: 'fleezy', value: 'flam2')) @@ -1335,24 +1329,24 @@ def self.file_path validate_file(expected_content_six, tmpfile) end - expected_content_seven = <<-EOS -# This is a comment - [section1] - ; This is also a comment - foo=foovalue - - bar = barvalue - main = true - -[section2] - foo= foovalue2 - baz=bazvalue - url = http://192.168.1.1:8080 -[section:sub] - subby=bar - #another comment - fleezy = flam2 - ; yet another comment + expected_content_seven = <<~EOS + # This is a comment + [section1] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + + [section2] + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + fleezy = flam2 + ; yet another comment EOS it 'updates an existing setting at the previous indentation regardless of indent_char and indent_width settings' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section:sub', setting: 'fleezy', value: 'flam2', indent_char: 'ignore this', indent_width: 10)) @@ -1365,32 +1359,32 @@ def self.file_path context 'when dealing settings that have a commented version present' do let(:orig_content) do - <<-EOS - [section1] - # foo=foovalue - bar=barvalue - foo = foovalue2 - -[section2] -# foo = foovalue -;bar=barvalue -blah = blah -#baz= + <<~EOS + [section1] + # foo=foovalue + bar=barvalue + foo = foovalue2 + + [section2] + # foo = foovalue + ;bar=barvalue + blah = blah + #baz= EOS end - expected_content_eight = <<-EOS - [section1] - # foo=foovalue - bar=barvalue - foo = foovalue2 + expected_content_eight = <<~EOS + [section1] + # foo=foovalue + bar=barvalue + foo = foovalue2 -[section2] -# foo = foovalue -foo = foo3 -;bar=barvalue -blah = blah -#baz= + [section2] + # foo = foovalue + foo = foo3 + ;bar=barvalue + blah = blah + #baz= EOS it 'adds a new setting below a commented version of that setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'foo', value: 'foo3')) @@ -1400,17 +1394,17 @@ def self.file_path validate_file(expected_content_eight, tmpfile) end - expected_content_nine = <<-EOS - [section1] - # foo=foovalue - bar=barvalue - foo = foo3 + expected_content_nine = <<~EOS + [section1] + # foo=foovalue + bar=barvalue + foo = foo3 -[section2] -# foo = foovalue -;bar=barvalue -blah = blah -#baz= + [section2] + # foo = foovalue + ;bar=barvalue + blah = blah + #baz= EOS it 'updates an existing setting in place, even if there is a commented version of that setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section1', setting: 'foo', value: 'foo3')) @@ -1420,18 +1414,18 @@ def self.file_path validate_file(expected_content_nine, tmpfile) end - expected_content_ten = <<-EOS - [section1] - # foo=foovalue - bar=barvalue - foo = foovalue2 + expected_content_ten = <<~EOS + [section1] + # foo=foovalue + bar=barvalue + foo = foovalue2 -[section2] -# foo = foovalue -;bar=barvalue -bar = bar3 -blah = blah -#baz= + [section2] + # foo = foovalue + ;bar=barvalue + bar = bar3 + blah = blah + #baz= EOS it 'adds a new setting below a commented version of that setting, respecting semicolons as comments' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'bar', value: 'bar3')) @@ -1441,18 +1435,18 @@ def self.file_path validate_file(expected_content_ten, tmpfile) end - expected_content_four = <<-EOS - [section1] - # foo=foovalue - bar=barvalue - foo = foovalue2 + expected_content_four = <<~EOS + [section1] + # foo=foovalue + bar=barvalue + foo = foovalue2 -[section2] -# foo = foovalue -;bar=barvalue -blah = blah -#baz= -baz = bazvalue + [section2] + # foo = foovalue + ;bar=barvalue + blah = blah + #baz= + baz = bazvalue EOS it 'adds a new setting below an empty commented version of that setting' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section2', setting: 'baz', value: 'bazvalue')) @@ -1464,18 +1458,18 @@ def self.file_path context 'when a section only contains comments' do let(:orig_content) do - <<-EOS -[section1] -# foo=foovalue -# bar=bar2 -EOS + <<~EOS + [section1] + # foo=foovalue + # bar=bar2 + EOS end - expected_content_one = <<-EOS -[section1] -# foo=foovalue -foo = foovalue2 -# bar=bar2 + expected_content_one = <<~EOS + [section1] + # foo=foovalue + foo = foovalue2 + # bar=bar2 EOS it 'is able to add a new setting when a section contains only comments' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section1', setting: 'foo', value: 'foovalue2')) @@ -1485,11 +1479,11 @@ def self.file_path validate_file(expected_content_one, tmpfile) end - expected_content_two = <<-EOS -[section1] -# foo=foovalue -# bar=bar2 -bar = barvalue2 + expected_content_two = <<~EOS + [section1] + # foo=foovalue + # bar=bar2 + bar = barvalue2 EOS it 'is able to add a new setting when it matches a commented out line other than the first one' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section1', setting: 'bar', value: 'barvalue2')) @@ -1502,44 +1496,44 @@ def self.file_path context 'when sections have spaces and dashes' do let(:orig_content) do - <<-EOS -# This is a comment -[section - one] -; This is also a comment -foo=foovalue - -bar = barvalue -main = true -[section - two] - -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -[section:sub] -subby=bar - #another comment - ; yet another comment + <<~EOS + # This is a comment + [section - one] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + [section - two] + + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + [section:sub] + subby=bar + #another comment + ; yet another comment EOS end - expected_content_one = <<-EOS -# This is a comment -[section - one] -; This is also a comment -foo=foovalue - -bar = barvalue -main = true -[section - two] - -foo= foovalue2 -baz=bazvalue -url = http://192.168.1.1:8080 -yahoo = yippee -[section:sub] -subby=bar - #another comment - ; yet another comment + expected_content_one = <<~EOS + # This is a comment + [section - one] + ; This is also a comment + foo=foovalue + + bar = barvalue + main = true + [section - two] + + foo= foovalue2 + baz=bazvalue + url = http://192.168.1.1:8080 + yahoo = yippee + [section:sub] + subby=bar + #another comment + ; yet another comment EOS it 'adds a missing setting to the correct section' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section - two', setting: 'yahoo', value: 'yippee')) @@ -1553,30 +1547,30 @@ def self.file_path context 'when sections have spaces and quotations' do let(:orig_content) do - <<-EOS -[branch "main"] - remote = origin - merge = refs/heads/main - -[alias] -to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/main -[branch "production"] - remote = origin - merge = refs/heads/production + <<~EOS + [branch "main"] + remote = origin + merge = refs/heads/main + + [alias] + to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/main + [branch "production"] + remote = origin + merge = refs/heads/production EOS end - expected_content_one = <<-EOS -[branch "main"] - remote = origin - merge = refs/heads/main + expected_content_one = <<~EOS + [branch "main"] + remote = origin + merge = refs/heads/main -[alias] -to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/main -foo = bar -[branch "production"] - remote = origin - merge = refs/heads/production + [alias] + to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/main + foo = bar + [branch "production"] + remote = origin + merge = refs/heads/production EOS it 'adds a missing setting to the correct section' do resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'alias', setting: 'foo', value: 'bar')) @@ -1586,5 +1580,4 @@ def self.file_path validate_file(expected_content_one, tmpfile) end end - # rubocop:enable Layout/IndentHeredoc end diff --git a/spec/unit/puppet/provider/ini_subsetting/ruby_spec.rb b/spec/unit/puppet/provider/ini_subsetting/ruby_spec.rb index 8c732ca6..8596397e 100644 --- a/spec/unit/puppet/provider/ini_subsetting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_subsetting/ruby_spec.rb @@ -14,9 +14,7 @@ def validate_file(expected_content, tmpfile) end before :each do - File.open(tmpfile, 'w') do |fh| - fh.write(orig_content) - end + File.write(tmpfile, orig_content) end context 'when ensuring that a subsetting is present' do @@ -26,7 +24,7 @@ def validate_file(expected_content, tmpfile) path: tmpfile, section: '', key_val_separator: '=', - setting: 'JAVA_ARGS', + setting: 'JAVA_ARGS' } end @@ -153,7 +151,7 @@ def validate_file(expected_content, tmpfile) title: 'ini_setting_ensure_present_test', path: tmpfile, section: 'main', - setting: 'reports', + setting: 'reports' } end @@ -213,7 +211,7 @@ def validate_file(expected_content, tmpfile) path: tmpfile, section: 'main', setting: 'reports', - use_exact_match: true, + use_exact_match: true } end @@ -234,7 +232,7 @@ def validate_file(expected_content, tmpfile) resource = Puppet::Type::Ini_subsetting.new(common_params.merge(subsetting: 'fo', subsetting_separator: ',')) provider = described_class.new(resource) provider.value = '' - validate_file(expected_content_one, tmpfile) + expect(validate_file(expected_content_one, tmpfile)).to be_truthy end expected_content_two = <<-EOS @@ -247,7 +245,7 @@ def validate_file(expected_content, tmpfile) provider = described_class.new(resource) provider.value = '' provider.destroy - validate_file(expected_content_two, tmpfile) + expect(validate_file(expected_content_two, tmpfile)).to be_truthy end end @@ -259,7 +257,7 @@ def validate_file(expected_content, tmpfile) section: 'main', setting: 'reports', subsetting_separator: ',', - subsetting_key_val_separator: ':', + subsetting_key_val_separator: ':' } end @@ -280,7 +278,7 @@ def validate_file(expected_content, tmpfile) resource = Puppet::Type::Ini_subsetting.new(common_params.merge(subsetting: 'c', value: '3')) provider = described_class.new(resource) provider.value = '3' - validate_file(expected_content_one, tmpfile) + expect(validate_file(expected_content_one, tmpfile)).to be_truthy end expected_content_two = <<-EOS @@ -330,7 +328,7 @@ def validate_file(expected_content, tmpfile) title: 'ini_setting_delete_if_empty_test', path: tmpfile, section: 'main', - delete_if_empty: true, + delete_if_empty: true } end @@ -353,11 +351,11 @@ def validate_file(expected_content, tmpfile) resource = Puppet::Type::Ini_subsetting.new(common_params.merge(setting: 'reports', subsetting: 'http', subsetting_separator: ',')) provider = described_class.new(resource) provider.destroy - validate_file(expected_content_one, tmpfile) + expect(validate_file(expected_content_one, tmpfile)).to be_truthy resource = Puppet::Type::Ini_subsetting.new(common_params.merge(setting: 'something', subsetting: 'else', subsetting_separator: ',')) provider = described_class.new(resource) provider.destroy - validate_file(expected_content_two, tmpfile) + expect(validate_file(expected_content_two, tmpfile)).to be_truthy end end end diff --git a/spec/unit/puppet/type/ini_setting_spec.rb b/spec/unit/puppet/type/ini_setting_spec.rb index 00b3adf5..89923565 100644 --- a/spec/unit/puppet/type/ini_setting_spec.rb +++ b/spec/unit/puppet/type/ini_setting_spec.rb @@ -6,28 +6,29 @@ describe ini_setting do describe 'path validation' do - subject { -> { described_class.new(name: 'foo', path: path) } } + subject(:ini_setting_path) { described_class.new(name: 'foo', path: path) } - context 'on posix platforms' do + context 'when on posix platforms' do before(:each) do Puppet.features.stub(:posix?) { true } Puppet.features.stub(:microsoft_windows?) { false } Puppet::Util::Platform.stub(:windows?) { false } end + context 'with an absolute path' do let(:path) { '/absolute/path' } - it { is_expected.not_to raise_exception } + it { expect { ini_setting_path }.not_to raise_exception } end context 'with a relative path' do let(:path) { 'relative/path' } - it { is_expected.to raise_exception } + it { expect { ini_setting_path }.to raise_exception(Puppet::ResourceError) } end end - context 'on windows platforms' do + context 'when on windows platforms' do before(:each) do Puppet.features.stub(:posix?) { false } Puppet.features.stub(:microsoft_windows?) { true } @@ -37,34 +38,33 @@ context 'with an absolute path with front slashes' do let(:path) { 'c:/absolute/path' } - it { is_expected.not_to raise_exception } + it { expect { ini_setting_path }.not_to raise_exception } end context 'with an absolute path with backslashes' do let(:path) { 'c:\absolute\path' } - it { is_expected.not_to raise_exception } + it { expect { ini_setting_path }.not_to raise_exception } end context 'with an absolute path with mixed slashes' do let(:path) { 'c:/absolute\path' } - it { is_expected.not_to raise_exception } + it { expect { ini_setting_path }.not_to raise_exception } end context 'with a relative path with front slashes' do let(:path) { 'relative/path' } - it { is_expected.to raise_exception } + it { expect { ini_setting_path }.to raise_exception(Puppet::ResourceError) } end context 'with a relative path with back slashes' do let(:path) { 'relative\path' } - it { is_expected.to raise_exception } + it { expect { ini_setting_path }.to raise_exception(Puppet::ResourceError) } end end - # rubocop:enable RSpec/NestedGroups end [true, false].product([true, false, 'true', 'false', 'md5', :md5]).each do |cfg, param| @@ -72,9 +72,12 @@ before(:each) do Puppet[:show_diff] = cfg end + let(:value) { described_class.new(name: 'foo', value: 'whatever', show_diff: param).property(:value) } - if cfg && [true, 'true'].include?(param) + true_array = [true, 'true'] + md5_array = ['md5', :md5] + if cfg && true_array.include?(param) it 'displays diff' do expect(value.change_to_s('not_secret', 'at_all')).to include('not_secret', 'at_all') end @@ -86,13 +89,15 @@ it 'tells new value' do expect(value.should_to_s('not_secret_at_all')).to eq('not_secret_at_all') end - elsif cfg && ['md5', :md5].include?(param) + elsif cfg && md5_array.include?(param) it 'tells correct md5 hashes for multiple values' do expect(value.change_to_s('not_at', 'all_secret')).to include('6edef0c4f5ec664feff6ca6fbc290970', '1660308ab156754fa09af0e8dc2c6629') end + it 'does not tell singular value one' do expect(value.change_to_s('not_at #', 'all_secret')).not_to include('not_at') end + it 'does not tell singular value two' do expect(value.change_to_s('not_at', 'all_secret')).not_to include('all_secret') end @@ -100,6 +105,7 @@ it 'tells md5 of current value' do expect(value.is_to_s('not_at_all_secret')).to eq('{md5}858b46aee11b780b8f5c8853668efc05') end + it 'does not tell the current value' do expect(value.is_to_s('not_at_all_secret')).not_to include('not_secret_at_all') end @@ -107,6 +113,7 @@ it 'tells md5 of new value' do expect(value.should_to_s('not_at_all_secret')).to eq('{md5}858b46aee11b780b8f5c8853668efc05') end + it 'does not tell the new value' do expect(value.should_to_s('not_at_all_secret')).not_to include('not_secret_at_all') end @@ -114,9 +121,11 @@ it 'tells redaction warning in place of actual values' do expect(value.change_to_s('at_all', 'not_secret')).to include('[redacted sensitive information]') end + it 'does not tell actual value one' do expect(value.change_to_s('at_all', 'not_secret')).not_to include('not_secret') end + it 'does not tell actual value two' do expect(value.change_to_s('at_all', 'not_secret')).not_to include('at_all') end @@ -124,6 +133,7 @@ it 'tells redaction warning in place of current value' do expect(value.is_to_s('not_secret_at_all')).to eq('[redacted sensitive information]') end + it 'does not tell current value' do expect(value.is_to_s('not_secret_at_all')).not_to include('not_secret_at_all') end @@ -131,6 +141,7 @@ it 'tells redaction warning in place of new value' do expect(value.should_to_s('not_secret_at_all')).to eq('[redacted sensitive information]') end + it 'does not tell new value' do expect(value.should_to_s('not_secret_at_all')).not_to include('not_secret_at_all') end @@ -140,7 +151,7 @@ describe 'when parent of :path is in the catalog' do ['posix', 'windows'].each do |platform| - context "on #{platform} platforms" do + context "when on #{platform} platforms" do before(:each) do Puppet.features.stub(:posix?) { platform == 'posix' } Puppet.features.stub(:microsoft_windows?) { platform == 'windows' } @@ -161,9 +172,11 @@ it 'creates relationship' do expect(auto_req.size).to be 1 end + it 'links to ini_setting resource' do expect(auto_req[0].target).to eq(ini_setting_resource) end + it 'autorequires parent directory' do expect(auto_req[0].source).to eq(file_resource) end diff --git a/spec/unit/puppet/type/ini_subetting_spec.rb b/spec/unit/puppet/type/ini_subetting_spec.rb index d32de89e..9638ceae 100644 --- a/spec/unit/puppet/type/ini_subetting_spec.rb +++ b/spec/unit/puppet/type/ini_subetting_spec.rb @@ -6,20 +6,20 @@ describe ini_subsetting do describe 'quote_char validation' do - subject { -> { described_class.new(name: 'foo', path: path, quote_char: quote_char) } } + subject(:ini_subsetting_test) { described_class.new(name: 'foo', path: path, quote_char: quote_char) } - context 'on posix platforms' do + context 'when on posix platforms' do let(:path) { '/absolute/path' } let(:quote_char) { '\”' } - it { is_expected.to raise_exception } + it { expect { ini_subsetting_test }.to raise_exception(Puppet::ResourceError) } end end describe 'path validation' do - subject { -> { described_class.new(name: 'foo', path: path) } } + subject(:ini_subsetting_test) { described_class.new(name: 'foo', path: path) } - context 'on posix platforms' do + context 'when on posix platforms' do before(:each) do Puppet.features.stub(:posix?) { true } Puppet.features.stub(:microsoft_windows?) { false } @@ -29,17 +29,17 @@ context 'with an absolute path' do let(:path) { '/absolute/path' } - it { is_expected.not_to raise_exception } + it { expect { ini_subsetting_test }.not_to raise_exception } end context 'with a relative path' do let(:path) { 'relative/path' } - it { is_expected.to raise_exception } + it { expect { ini_subsetting_test }.to raise_exception(Puppet::ResourceError) } end end - context 'on windows platforms' do + context 'when on windows platforms' do before(:each) do Puppet.features.stub(:posix?) { false } Puppet.features.stub(:microsoft_windows?) { true } @@ -49,13 +49,13 @@ context 'with an absolute path with front slashes' do let(:path) { 'c:/absolute/path' } - it { is_expected.not_to raise_exception } + it { expect { ini_subsetting_test }.not_to raise_exception } end context 'with a relative path with back slashes' do let(:path) { 'relative\path' } - it { is_expected.to raise_exception } + it { expect { ini_subsetting_test }.to raise_exception(Puppet::ResourceError) } end end end @@ -65,6 +65,7 @@ before(:each) do Puppet[:show_diff] = cfg end + let(:value) { described_class.new(name: 'foo', value: 'whatever', show_diff: param).property(:value) } if cfg && param == true @@ -83,9 +84,11 @@ it 'tells correct md5 hashes for multiple values' do expect(value.change_to_s('not_secret', 'at_all')).to include('e9e8db547f8960ef32dbc34029735564', '46cd73a9509ba78c39f05faf078a8cbe') end + it 'does not tell singular value one' do expect(value.change_to_s('not_secret', 'at_all')).not_to include('not_secret') end + it 'does not tell singular value two' do expect(value.change_to_s('not_secret', 'at_all')).not_to include('at_all') end @@ -93,6 +96,7 @@ it 'tells md5 of current value' do expect(value.is_to_s('not_secret_at_all')).to eq('{md5}218fde79f501b8ab8d212f1059bb857f') end + it 'does not tell the current value' do expect(value.is_to_s('not_secret_at_all')).not_to include('not_secret_at_all') end @@ -100,6 +104,7 @@ it 'tells md5 of new value' do expect(value.should_to_s('not_secret_at_all')).not_to include('not_secret_at_all') end + it 'does not tell the new value' do expect(value.should_to_s('not_secret_at_all')).to eq('{md5}218fde79f501b8ab8d212f1059bb857f') end @@ -107,9 +112,11 @@ it 'tells redaction warning in place of actual values' do expect(value.change_to_s('not_at', 'all_secret')).to include('[redacted sensitive information]') end + it 'does not tell actual value one' do expect(value.change_to_s('not_at', 'all_secret')).not_to include('not_at') end + it 'does not tell actual value two' do expect(value.change_to_s('not_at', 'all_secret')).not_to include('all_secret') end @@ -117,6 +124,7 @@ it 'tells redaction warning in place of current value' do expect(value.is_to_s('not_at_all_secret')).to eq('[redacted sensitive information]') end + it 'does not tell current value' do expect(value.is_to_s('not_at_all_secret')).not_to include('not_at_all_secret') end @@ -124,6 +132,7 @@ it 'tells redaction warning in place of new value' do expect(value.should_to_s('not_at_all_secret')).to eq('[redacted sensitive information]') end + it 'does not tell new value' do expect(value.should_to_s('not_at_all_secret')).not_to include('not_at_all_secret') end diff --git a/spec/unit/puppet/util/external_iterator_spec.rb b/spec/unit/puppet/util/external_iterator_spec.rb index 4773d833..99a45a2a 100644 --- a/spec/unit/puppet/util/external_iterator_spec.rb +++ b/spec/unit/puppet/util/external_iterator_spec.rb @@ -12,7 +12,7 @@ expected_values = [['a', 0], ['b', 1], ['c', 2]] end - context '#next' do + describe '#next' do it 'iterates over the items' do expected_values.each do |expected_pair| expect(subject_class.next).to eq(expected_pair) @@ -20,9 +20,9 @@ end end - context '#peek' do + describe '#peek' do it 'returns the 0th item repeatedly' do - (0..2).each do |_i| + 3.times do |_i| expect(subject_class.peek).to eq(expected_values[0]) end end diff --git a/spec/unit/puppet/util/ini_file_spec.rb b/spec/unit/puppet/util/ini_file_spec.rb index abf4a25a..4e51b090 100644 --- a/spec/unit/puppet/util/ini_file_spec.rb +++ b/spec/unit/puppet/util/ini_file_spec.rb @@ -8,7 +8,7 @@ subject(:ini_sub) { described_class.new('/my/ini/file/path') } before :each do - allow(File).to receive(:file?).with('/my/ini/file/path') { true } + allow(File).to receive(:file?).with('/my/ini/file/path').and_return(true) allow(described_class).to receive(:readlines).once.with('/my/ini/file/path') do sample_content end @@ -53,6 +53,7 @@ 'baz' => '', 'foo' => 'foovalue') end + it 'exposes settings for sections #section2' do expect(ini_sub.get_settings('section2')).to eq('baz' => 'bazvalue', 'foo' => 'foovalue2', @@ -111,6 +112,7 @@ it 'exposes settings for sections #bar' do expect(ini_sub.get_value('', 'foo')).to eq('bar') end + it 'exposes settings for sections #foovalue' do expect(ini_sub.get_value('section1', 'foo')).to eq('foovalue') end @@ -127,10 +129,11 @@ template.split("\n") end + # rubocop:disable RSpec/ExpectInHook before :each do - expect(ini_sub.get_value('section1', 'far')).to eq(nil) - expect(ini_sub.get_value('section1', 'bar')).to eq(nil) - expect(ini_sub.get_value('section1', "xyzzy['thing1']['thing2']")).to eq(nil) + expect(ini_sub.get_value('section1', 'far')).to be_nil + expect(ini_sub.get_value('section1', 'bar')).to be_nil + expect(ini_sub.get_value('section1', "xyzzy['thing1']['thing2']")).to be_nil end # rubocop:enable RSpec/ExpectInHook @@ -175,7 +178,7 @@ end end - context 'the file has quotation marks in its section names' do + context 'when the file has quotation marks in its section names' do let(:sample_content) do template = <<-EOS [branch "main"] @@ -192,14 +195,11 @@ end it 'parses the sections' do - expect(ini_sub.section_names).to match_array ['', - 'branch "main"', - 'alias', - 'branch "production"'] + expect(ini_sub.section_names).to contain_exactly('', 'branch "main"', 'alias', 'branch "production"') end end - context 'Samba INI file with dollars in section names' do + context 'when Samba INI file with dollars in section names' do let(:sample_content) do template = <<-EOS [global] @@ -225,11 +225,11 @@ end it 'parses the correct section_names' do - expect(ini_sub.section_names).to match_array ['', 'global', 'printers', 'print$', 'Shares'] + expect(ini_sub.section_names).to contain_exactly('', 'global', 'printers', 'print$', 'Shares') end end - context 'section names with forward slashes in them' do + context 'when section names with forward slashes in them' do let(:sample_content) do template = <<-EOS [monitor:///var/log/*.log] @@ -239,14 +239,11 @@ end it 'parses the correct section_names' do - expect(ini_sub.section_names).to match_array [ - '', - 'monitor:///var/log/*.log', - ] + expect(ini_sub.section_names).to contain_exactly('', 'monitor:///var/log/*.log') end end - context 'KDE Configuration with braces in setting names' do + context 'when KDE Configuration with braces in setting names' do let(:sample_content) do template = <<-EOS [khotkeys] @@ -260,12 +257,13 @@ it 'exposes settings for sections #print' do expect(ini_sub.get_value('khotkeys', '{5465e8c7-d608-4493-a48f-b99d99fdb508}')).to eq('Print,none,PrintScreen') end + it 'exposes settings for sections #search' do expect(ini_sub.get_value('khotkeys', '{d03619b6-9b3c-48cc-9d9c-a2aadb485550}')).to eq('Search,none,Search') end end - context 'Configuration with colons in setting names' do + context 'when Configuration with colons in setting names' do let(:sample_content) do template = <<-EOS [Drive names] @@ -279,15 +277,17 @@ it 'exposes settings for sections #A' do expect(ini_sub.get_value('Drive names', 'A:')).to eq '5.25" Floppy' end + it 'exposes settings for sections #B' do expect(ini_sub.get_value('Drive names', 'B:')).to eq '3.5" Floppy' end + it 'exposes settings for sections #C' do expect(ini_sub.get_value('Drive names', 'C:')).to eq 'Winchester' end end - context 'Configuration with spaces in setting names' do + context 'when Configuration with spaces in setting names' do let(:sample_content) do template = <<-EOS [global] @@ -304,12 +304,15 @@ it 'exposes settings for sections #log' do expect(ini_sub.get_value('global', 'log file')).to eq '/var/log/samba/log.%m' end + it 'exposes settings for sections #kerberos' do expect(ini_sub.get_value('global', 'kerberos method')).to eq 'system keytab' end + it 'exposes settings for sections #passdb' do expect(ini_sub.get_value('global', 'passdb backend')).to eq 'tdbsam' end + it 'exposes settings for sections #security' do expect(ini_sub.get_value('global', 'security')).to eq 'ads' end diff --git a/spec/unit/puppet/util/setting_value_spec.rb b/spec/unit/puppet/util/setting_value_spec.rb index ce3847ea..e531f37c 100644 --- a/spec/unit/puppet/util/setting_value_spec.rb +++ b/spec/unit/puppet/util/setting_value_spec.rb @@ -3,10 +3,13 @@ require 'spec_helper' require 'puppet/util/setting_value' +INIT_VALUE_SPACE = '"-Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"' +INIT_VALUE_COMMA = '"-Xmx192m,-XX:+HeapDumpOnOutOfMemoryError,-XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"' +INIT_VALUE_UNQUOTED = '-Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof' +QUOTE_CHAR = '"' + describe Puppet::Util::SettingValue do describe 'space subsetting separator' do - INIT_VALUE_SPACE = '"-Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"' - let(:setting_value) { described_class.new(INIT_VALUE_SPACE, ' ') } it 'gets the original value' do @@ -24,7 +27,7 @@ it 'adds a new value #original' do setting_value.add_subsetting('-Xms', '256m') - expect(setting_value.get_value).to eq(INIT_VALUE_SPACE[0, INIT_VALUE_SPACE.length - 1] + ' -Xms256m"') + expect(setting_value.get_value).to eq("#{INIT_VALUE_SPACE[0, INIT_VALUE_SPACE.length - 1]} -Xms256m\"") end it 'changes existing value' do @@ -34,13 +37,11 @@ it 'removes existing value' do setting_value.remove_subsetting('-Xmx') - expect(setting_value.get_subsetting_value('-Xmx')).to eq(nil) + expect(setting_value.get_subsetting_value('-Xmx')).to be_nil end end describe 'comma subsetting separator' do - INIT_VALUE_COMMA = '"-Xmx192m,-XX:+HeapDumpOnOutOfMemoryError,-XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"' - let(:setting_value) { described_class.new(INIT_VALUE_COMMA, ',') } it 'gets the original value' do @@ -55,9 +56,10 @@ setting_value.add_subsetting('-Xms', '256m') expect(setting_value.get_subsetting_value('-Xms')).to eq('256m') end + it 'adds a new value #original' do setting_value.add_subsetting('-Xms', '256m') - expect(setting_value.get_value).to eq(INIT_VALUE_COMMA[0, INIT_VALUE_COMMA.length - 1] + ',-Xms256m"') + expect(setting_value.get_value).to eq("#{INIT_VALUE_COMMA[0, INIT_VALUE_COMMA.length - 1]},-Xms256m\"") end it 'changes existing value' do @@ -67,14 +69,11 @@ it 'removes existing value' do setting_value.remove_subsetting('-Xmx') - expect(setting_value.get_subsetting_value('-Xmx')).to eq(nil) + expect(setting_value.get_subsetting_value('-Xmx')).to be_nil end end describe 'quote_char parameter' do - QUOTE_CHAR = '"' - INIT_VALUE_UNQUOTED = '-Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof' - it 'gets quoted empty string if original value was empty' do setting_value = described_class.new(nil, ' ', QUOTE_CHAR) expect(setting_value.get_value).to eq(QUOTE_CHAR * 2) @@ -86,11 +85,12 @@ expect(setting_value.get_subsetting_value('-Xms')).to eq('256m') end + it 'quotes the setting when adding a value #original' do setting_value = described_class.new(INIT_VALUE_UNQUOTED, ' ', QUOTE_CHAR) setting_value.add_subsetting('-Xms', '256m') - expect(setting_value.get_value).to eq(QUOTE_CHAR + INIT_VALUE_UNQUOTED + ' -Xms256m' + QUOTE_CHAR) + expect(setting_value.get_value).to eq("#{QUOTE_CHAR}#{INIT_VALUE_UNQUOTED} -Xms256m#{QUOTE_CHAR}") end it 'quotes the setting when changing an existing value #value' do @@ -99,6 +99,7 @@ expect(setting_value.get_subsetting_value('-Xmx')).to eq('512m') end + it 'quotes the setting when changing an existing value #quotes' do setting_value = described_class.new(INIT_VALUE_UNQUOTED, ' ', QUOTE_CHAR) setting_value.add_subsetting('-Xmx', '512m') @@ -110,9 +111,10 @@ setting_value = described_class.new(INIT_VALUE_UNQUOTED, ' ', QUOTE_CHAR) setting_value.remove_subsetting('-Xmx') - expect(setting_value.get_subsetting_value('-Xmx')).to eq(nil) + expect(setting_value.get_subsetting_value('-Xmx')).to be_nil end end + it 'quotes the setting when removing an existing value #quotes' do setting_value = described_class.new(INIT_VALUE_UNQUOTED, ' ', QUOTE_CHAR) setting_value.remove_subsetting('-Xmx')