From 1999d46d5cb24702de458594f46d704fe115097f Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Fri, 21 Apr 2023 09:49:26 +0100 Subject: [PATCH 01/33] PDK update --- .devcontainer/devcontainer.json | 2 +- .github/workflows/release.yml | 3 +-- Gemfile | 46 ++++++++++++++++----------------- Rakefile | 1 - metadata.json | 4 +-- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index fe7a8b12b..cdd65d220 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -5,7 +5,7 @@ "settings": { "terminal.integrated.profiles.linux": { "bash": { - "path": "bash", + "path": "bash" } } }, diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 82caec76a..0b7b8a05d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,7 +4,6 @@ on: workflow_dispatch: jobs: - release: - name: "Release" + release: uses: "puppetlabs/cat-github-actions/.github/workflows/module_release.yml@main" secrets: "inherit" diff --git a/Gemfile b/Gemfile index 59f55b7b7..8456bee1c 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.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 end group :system_tests do - gem "puppet_litmus", '~> 1.0', require: false, platforms: [:ruby, :x64_mingw] - 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 6c56b665b..9aa3a64e3 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/metadata.json b/metadata.json index 7f56f78cf..af3d5b7fb 100644 --- a/metadata.json +++ b/metadata.json @@ -104,7 +104,7 @@ } ], "description": "Standard Library for Puppet Modules", - "pdk-version": "2.5.0", + "pdk-version": "2.7.1", "template-url": "https://github.com/puppetlabs/pdk-templates#main", - "template-ref": "2.7.1-0-g9a16c87" + "template-ref": "heads/main-0-ge5b0114" } From fb11fb0ae7ca5e0a460bf10cb4996ec745ec3e1c Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Fri, 21 Apr 2023 09:51:56 +0100 Subject: [PATCH 02/33] Gemfile/Metadata update --- Gemfile | 8 ++++---- metadata.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 8456bee1c..ffdd1801e 100644 --- a/Gemfile +++ b/Gemfile @@ -30,14 +30,14 @@ group :development do 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 "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", '< 1.0.0', require: false, platforms: [:ruby, :x64_mingw] + gem "puppet_litmus", '~> 1.0', require: false, platforms: [:ruby, :x64_mingw] gem "serverspec", '~> 2.41', require: false end diff --git a/metadata.json b/metadata.json index af3d5b7fb..bb2d54ca5 100644 --- a/metadata.json +++ b/metadata.json @@ -100,7 +100,7 @@ "requirements": [ { "name": "puppet", - "version_requirement": ">= 6.0.0 < 8.0.0" + "version_requirement": ">= 7.0.0 < 9.0.0" } ], "description": "Standard Library for Puppet Modules", From 8d75da17662c658e22104f5216748a97204afb55 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Fri, 21 Apr 2023 09:56:26 +0100 Subject: [PATCH 03/33] (CONT-801) Update rubocop config --- .rubocop.yml | 443 +---------------------------- .rubocop_todo.yml | 708 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 704 insertions(+), 447 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index a886d8dbf..3858fd5b0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,7 +4,10 @@ require: - rubocop-rspec AllCops: DisplayCopNames: true - TargetRubyVersion: '2.5' + NewCops: enable + TargetRubyVersion: '2.7' + ExtraDetails: true + DisplayStyleGuide: true Include: - "**/*.rb" Exclude: @@ -80,441 +83,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 index c3fb6926b..be93c27a9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,26 +1,718 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2020-12-17 20:35:36 UTC using RuboCop version 1.6.1. +# on 2023-04-21 08:55:52 UTC using RuboCop version 1.48.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 6 -# Configuration parameters: AllowComments. +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyleAlignWith, Severity. +# SupportedStylesAlignWith: start_of_line, begin +Layout/BeginEndAlignment: + Exclude: + - 'lib/puppet/parser/functions/any2bool.rb' + +# Offense count: 96 +# This cop supports safe autocorrection (--autocorrect). +Layout/ClosingHeredocIndentation: + Enabled: false + +# Offense count: 46 +# This cop supports safe autocorrection (--autocorrect). +Layout/EmptyLineAfterGuardClause: + Enabled: false + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Layout/EmptyLinesAroundArguments: + Exclude: + - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowAliasSyntax, AllowedMethods. +# AllowedMethods: alias_method, public, protected, private +Layout/EmptyLinesAroundAttributeAccessor: + Exclude: + - 'spec/functions/get_module_path_spec.rb' + +# Offense count: 11 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. +# SupportedHashRocketStyles: key, separator, table +# SupportedColonStyles: key, separator, table +# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit +Layout/HashAlignment: + Exclude: + - 'lib/puppet/parser/functions/pw_hash.rb' + - 'lib/puppet/parser/functions/str2saltedpbkdf2.rb' + - 'spec/functions/str2saltedpbkdf2_spec.rb' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +Layout/HeredocIndentation: + Exclude: + - 'spec/functions/validate_x509_rsa_key_pair_spec.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, IndentationWidth. +# SupportedStyles: aligned, indented +Layout/LineEndStringConcatenationIndentation: + Exclude: + - 'lib/puppet/parser/functions/glob.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Layout/RescueEnsureAlignment: + Exclude: + - 'lib/puppet/parser/functions/any2bool.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Lint/AmbiguousOperatorPrecedence: + Exclude: + - 'lib/puppet/parser/functions/str2saltedsha512.rb' + +# Offense count: 3 +# This cop supports unsafe autocorrection (--autocorrect-all). +Lint/BooleanSymbol: + Exclude: + - 'spec/unit/puppet/type/file_line_spec.rb' + +# Offense count: 2 +# Configuration parameters: AllowedMethods. +# AllowedMethods: enums +Lint/ConstantDefinitionInBlock: + Exclude: + - 'spec/functions/get_module_path_spec.rb' + - 'spec/unit/facter/util/puppet_settings_spec.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Lint/DeprecatedOpenSSLConstant: + Exclude: + - 'lib/puppet/parser/functions/str2saltedpbkdf2.rb' + +# Offense count: 3 +# Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches. +Lint/DuplicateBranch: + Exclude: + - 'lib/puppet/parser/functions/str2bool.rb' + - 'lib/puppet/provider/file_line/ruby.rb' + +# Offense count: 1 +# Configuration parameters: AllowComments, AllowEmptyLambdas. +Lint/EmptyBlock: + Exclude: + - 'spec/unit/puppet/parser/functions/is_absolute_path_spec.rb' + +# Offense count: 2 +# Configuration parameters: MaximumRangeSize. +Lint/MissingCopEnableDirective: + Exclude: + - 'spec/functions/merge_spec.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Lint/RedundantCopEnableDirective: + Exclude: + - 'spec/functions/validate_x509_rsa_key_pair_spec.rb' + +# Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowedMethods. +# AllowedMethods: instance_of?, kind_of?, is_a?, eql?, respond_to?, equal? +Lint/RedundantSafeNavigation: + Exclude: + - 'lib/puppet/parser/functions/assert_private.rb' + - 'lib/puppet/parser/functions/getparam.rb' + +# Offense count: 4 +# Configuration parameters: AllowComments, AllowNil. Lint/SuppressedException: Exclude: - - 'lib/facter/facter_dot_d.rb' - 'lib/puppet/functions/merge.rb' - 'lib/puppet/parser/functions/getvar.rb' - 'lib/puppet/parser/functions/has_interface_with.rb' -# Offense count: 62 -# Configuration parameters: IgnoreSharedExamples. +# Offense count: 6 +# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. +Metrics/AbcSize: + Max: 44 + +# Offense count: 21 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode. +# AllowedMethods: refine +Metrics/BlockLength: + Max: 162 + +# Offense count: 5 +# Configuration parameters: AllowedMethods, AllowedPatterns. +Metrics/CyclomaticComplexity: + Max: 15 + +# Offense count: 11 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +Metrics/MethodLength: + Max: 37 + +# Offense count: 1 +# Configuration parameters: CountComments, CountAsOne. +Metrics/ModuleLength: + Max: 104 + +# Offense count: 4 +# Configuration parameters: AllowedMethods, AllowedPatterns. +Metrics/PerceivedComplexity: + Max: 19 + +# Offense count: 2 +# Configuration parameters: ForbiddenDelimiters. +# ForbiddenDelimiters: (?i-mx:(^|\s)(EO[A-Z]{1}|END)(\s|$)) +Naming/HeredocDelimiterNaming: + Exclude: + - 'lib/puppet/parser/functions/pick.rb' + - 'spec/functions/stdlib_deferrable_epp_spec.rb' + +# Offense count: 2 +# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. +# AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to +Naming/MethodParameterName: + Exclude: + - 'spec/functions/pick_default_spec.rb' + +# Offense count: 8 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: PreferredName. +Naming/RescuedExceptionsVariableName: + Exclude: + - 'lib/puppet/functions/parsehocon.rb' + - 'lib/puppet/functions/parsepson.rb' + - 'lib/puppet/parser/functions/loadjson.rb' + - 'lib/puppet/parser/functions/loadyaml.rb' + - 'lib/puppet/parser/functions/num2bool.rb' + - 'lib/puppet/parser/functions/validate_cmd.rb' + +# Offense count: 1 +# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns. +# SupportedStyles: snake_case, normalcase, non_integer +# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64 +Naming/VariableNumber: + Exclude: + - 'spec/functions/delete_undef_values_spec.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +Performance/MapCompact: + Exclude: + - 'lib/puppet/parser/functions/values_at.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowRegexpMatch. +Performance/RedundantEqualityComparisonBlock: + Exclude: + - 'lib/puppet/parser/functions/bool2str.rb' + +# Offense count: 24 +# This cop supports safe autocorrection (--autocorrect). +Performance/StringIdentifierArgument: + Enabled: false + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +Performance/UnfreezeString: + Exclude: + - 'lib/puppet/parser/functions/pw_hash.rb' + +# Offense count: 150 +# This cop supports unsafe autocorrection (--autocorrect-all). +RSpec/BeEq: + Enabled: false + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: be, be_nil +RSpec/BeNil: + Exclude: + - 'spec/functions/seeded_rand_string_spec.rb' + - 'spec/unit/facter/root_home_spec.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: be_a, be_kind_of +RSpec/ClassCheck: + Exclude: + - 'spec/functions/type_of_spec.rb' + +# Offense count: 36 +# Configuration parameters: Prefixes, AllowedPatterns. +# Prefixes: when, with, without +RSpec/ContextWording: + Enabled: false + +# Offense count: 170 +# Configuration parameters: IgnoredMetadata. +RSpec/DescribeClass: + Enabled: false + +# Offense count: 214 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowConsecutiveOneLiners. +RSpec/EmptyLineAfterExample: + Enabled: false + +# Offense count: 32 +# This cop supports safe autocorrection (--autocorrect). +RSpec/EmptyLineAfterExampleGroup: + Exclude: + - 'spec/functions/defined_with_params_spec.rb' + - 'spec/functions/stdlib_ensure_spec.rb' + - 'spec/functions/swapcase_spec.rb' + - 'spec/functions/validate_cmd_spec.rb' + - 'spec/type_aliases/compat__ip_address.rb' + - 'spec/type_aliases/compat__ipv4_spec.rb' + - 'spec/type_aliases/compat__ipv6_spec.rb' + - 'spec/type_aliases/email_spec.rb' + - 'spec/type_aliases/ensure_package_spec.rb' + - 'spec/type_aliases/hash_spec.rb' + - 'spec/unit/facter/util/puppet_settings_spec.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' + +# Offense count: 26 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowConsecutiveOneLiners. +RSpec/EmptyLineAfterHook: + Enabled: false + +# Offense count: 5 +# Configuration parameters: CountAsOne. +RSpec/ExampleLength: + Max: 9 + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: CustomTransform, IgnoredWords, DisallowedExamples. +# DisallowedExamples: works +RSpec/ExampleWording: + Exclude: + - 'spec/functions/validate_legacy_spec.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +RSpec/ExcessiveDocstringSpacing: + Exclude: + - 'spec/functions/is_mac_address_spec.rb' + - 'spec/functions/is_string_spec.rb' + +# Offense count: 3 +# Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly. +# Include: **/*_spec*rb*, **/spec/**/* +RSpec/FilePath: + Exclude: + - 'spec/type_aliases/compat__ip_address.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +RSpec/HooksBeforeExamples: + Exclude: + - 'spec/functions/deprecation_spec.rb' + +# Offense count: 2 +RSpec/IdenticalEqualityAssertion: + Exclude: + - 'spec/functions/fqdn_rand_string_spec.rb' + - 'spec/functions/seeded_rand_spec.rb' + +# Offense count: 300 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: single_line_only, single_statement_only, disallow, require_implicit +RSpec/ImplicitSubject: + Enabled: false + +# Offense count: 2 +RSpec/LeakyConstantDeclaration: + Exclude: + - 'spec/functions/get_module_path_spec.rb' + - 'spec/unit/facter/util/puppet_settings_spec.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +RSpec/MatchArray: + Exclude: + - 'spec/functions/keys_spec.rb' + - 'spec/functions/values_spec.rb' + +# Offense count: 77 +RSpec/MultipleExpectations: + Max: 5 + +# Offense count: 15 +# Configuration parameters: AllowSubject. +RSpec/MultipleMemoizedHelpers: + Max: 9 + +# Offense count: 60 +# Configuration parameters: EnforcedStyle, IgnoreSharedExamples. +# SupportedStyles: always, named_only RSpec/NamedSubject: Enabled: false +# Offense count: 25 +# Configuration parameters: AllowedGroups. +RSpec/NestedGroups: + Max: 4 + +# Offense count: 12 +# Configuration parameters: AllowedPatterns. +# AllowedPatterns: ^expect_, ^assert_ +RSpec/NoExpectationExample: + Exclude: + - 'spec/acceptance/file_line_spec.rb' + - 'spec/unit/facter/util/puppet_settings_spec.rb' + - 'spec/unit/puppet/parser/functions/is_absolute_path_spec.rb' + +# Offense count: 2 +RSpec/PendingWithoutReason: + Exclude: + - 'spec/functions/is_a_spec.rb' + - 'spec/functions/type_of_spec.rb' + +# Offense count: 19 +# This cop supports safe autocorrection (--autocorrect). +RSpec/ReceiveNever: + Enabled: false + +# Offense count: 6 +RSpec/RepeatedExampleGroupDescription: + Exclude: + - 'spec/functions/delete_regex_spec.rb' + - 'spec/functions/delete_spec.rb' + - 'spec/functions/getvar_spec.rb' + +# Offense count: 33 +RSpec/StubbedMock: + Exclude: + - 'spec/functions/assert_private_spec.rb' + - 'spec/functions/loadjson_spec.rb' + - 'spec/functions/loadyaml_spec.rb' + - 'spec/functions/private_spec.rb' + - 'spec/functions/reverse_spec.rb' + - 'spec/functions/size_spec.rb' + - 'spec/functions/squeeze_spec.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: MinBranchesCount. +Style/CaseLikeIf: + Exclude: + - 'lib/puppet_x/stdlib/toml_dumper.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: nested, compact +Style/ClassAndModuleChildren: + Exclude: + - 'lib/puppet_x/stdlib/toml_dumper.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowedMethods, AllowedPatterns. +# AllowedMethods: ==, equal?, eql? +Style/ClassEqualityComparison: + Exclude: + - 'lib/puppet/parser/functions/fqdn_rotate.rb' + +# Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/CollectionCompact: + Exclude: + - 'lib/puppet/functions/to_json_pretty.rb' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +Style/EachForSimpleLoop: + Exclude: + - 'spec/functions/deprecation_spec.rb' + # Offense count: 2 -RSpec/SubjectStub: +# This cop supports safe autocorrection (--autocorrect). +Style/Encoding: + Exclude: + - 'spec/type_aliases/filemode_spec.rb' + - 'spec/type_aliases/yes_no_spec.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowedVars. +Style/FetchEnvVar: + Exclude: + - 'lib/puppet/parser/functions/time.rb' + +# Offense count: 32 +# This cop supports safe autocorrection (--autocorrect). +Style/FileWrite: + Exclude: + - 'lib/puppet/provider/file_line/ruby.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' + +# Offense count: 3 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, always_true, never +Style/FrozenStringLiteralComment: + Exclude: + - 'lib/puppet_x/stdlib.rb' + - 'lib/puppet_x/stdlib/toml_dumper.rb' + - 'spec/functions/stdlib_deferrable_epp_spec.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowSplatArgument. +Style/HashConversion: + Exclude: + - 'lib/puppet/functions/sprintf_hash.rb' + - 'lib/puppet/functions/to_json_pretty.rb' + - 'lib/puppet/parser/functions/prefix.rb' + - 'lib/puppet/parser/functions/suffix.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/HashTransformKeys: + Exclude: + - 'lib/puppet/functions/to_json_pretty.rb' + +# Offense count: 156 +# This cop supports safe autocorrection (--autocorrect). +Style/IfUnlessModifier: + Enabled: false + +# Offense count: 5 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowedMethods. +# AllowedMethods: nonzero? +Style/IfWithBooleanLiteralBranches: + Exclude: + - 'lib/facter/pe_version.rb' + - 'lib/puppet/parser/functions/fqdn_rotate.rb' + - 'lib/puppet/parser/functions/shuffle.rb' + - 'lib/puppet/parser/functions/unique.rb' + - 'lib/puppet/provider/file_line/ruby.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowedMethods, AllowedPatterns. +Style/MethodCallWithoutArgsParentheses: + Exclude: + - 'lib/puppet/functions/deprecation.rb' + +# Offense count: 1 +Style/MixinUsage: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/NegatedIfElseCondition: + Exclude: + - 'lib/puppet/parser/functions/pw_hash.rb' + +# Offense count: 7 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle, AllowedMethods, AllowedPatterns. +# SupportedStyles: predicate, comparison +Style/NumericPredicate: + Exclude: + - 'spec/**/*' + - 'lib/puppet/parser/functions/fqdn_rand_string.rb' + - 'lib/puppet/parser/functions/is_email_address.rb' + - 'lib/puppet/parser/functions/num2bool.rb' + - 'lib/puppet/parser/functions/seeded_rand.rb' + - 'lib/puppet/parser/functions/validate_slength.rb' + - 'lib/puppet/provider/file_line/ruby.rb' + +# Offense count: 3 +# Configuration parameters: AllowedMethods. +# AllowedMethods: respond_to_missing? +Style/OptionalBooleanParameter: + Exclude: + - 'lib/puppet/functions/to_json_pretty.rb' + - 'lib/puppet_x/stdlib/toml_dumper.rb' + +# Offense count: 3 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: Methods. +Style/RedundantArgument: + Exclude: + - 'lib/puppet/parser/functions/sort.rb' + - 'lib/puppet/provider/file_line/ruby.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantConstantBase: + Exclude: + - 'lib/puppet/functions/to_json_pretty.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantFileExtensionInRequire: + Exclude: + - 'lib/puppet/functions/to_toml.rb' + +# Offense count: 10 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantHeredocDelimiterQuotes: + Exclude: + - 'lib/puppet/parser/functions/convert_base.rb' + - 'lib/puppet/parser/functions/deep_merge.rb' + - 'lib/puppet/parser/functions/getparam.rb' + - 'lib/puppet/parser/functions/getvar.rb' + - 'lib/puppet/parser/functions/has_key.rb' + - 'lib/puppet/parser/functions/is_absolute_path.rb' + - 'lib/puppet/parser/functions/loadjson.rb' + - 'lib/puppet/parser/functions/loadyaml.rb' + - 'lib/puppet/parser/functions/merge.rb' + - 'lib/puppet/parser/functions/private.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantParentheses: + Exclude: + - 'lib/puppet/parser/functions/pw_hash.rb' + +# Offense count: 43 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantRegexpEscape: + Exclude: + - 'lib/puppet/parser/functions/is_email_address.rb' + - 'lib/puppet/parser/functions/loadjson.rb' + - 'lib/puppet/parser/functions/loadyaml.rb' + - 'lib/puppet/parser/functions/range.rb' + - 'lib/puppet/parser/functions/values_at.rb' + - 'spec/functions/load_module_metadata_spec.rb' + - 'spec/functions/loadjson_spec.rb' + - 'spec/functions/validate_cmd_spec.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantStringEscape: + Exclude: + - 'lib/puppet/type/file_line.rb' + +# Offense count: 6 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: implicit, explicit +Style/RescueStandardError: + Exclude: + - 'lib/puppet/functions/stdlib/crc32.rb' + - 'lib/puppet/functions/stdlib/sha256.rb' + - 'lib/puppet/parser/functions/any2bool.rb' + - 'lib/puppet/parser/functions/dig44.rb' + - 'spec/functions/private_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/SelectByRegexp: + Exclude: + - 'lib/puppet/parser/functions/reject.rb' + +# Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/SlicingWithRange: + Exclude: + - 'lib/puppet/parser/functions/is_domain_name.rb' + - 'spec/functions/validate_x509_rsa_key_pair_spec.rb' + +# Offense count: 7 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowModifier. +Style/SoleNestedConditional: + Exclude: + - 'lib/puppet/parser/functions/convert_base.rb' + - 'lib/puppet/parser/functions/getparam.rb' + - 'lib/puppet/parser/functions/join.rb' + - 'lib/puppet/parser/functions/prefix.rb' + - 'lib/puppet/parser/functions/suffix.rb' + - 'lib/puppet/type/file_line.rb' + +# Offense count: 4 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/StringChars: + Exclude: + - 'lib/puppet/parser/functions/fqdn_rotate.rb' + - 'lib/puppet/parser/functions/shuffle.rb' + - 'lib/puppet/parser/functions/sort.rb' + - 'lib/puppet/parser/functions/unique.rb' + +# Offense count: 5 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: Mode. +Style/StringConcatenation: + Exclude: + - 'lib/puppet_x/stdlib/toml_dumper.rb' + - 'spec/functions/get_module_path_spec.rb' + +# Offense count: 8 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowMethodsWithArguments, AllowedMethods, AllowedPatterns, AllowComments. +# AllowedMethods: define_method +Style/SymbolProc: + Exclude: + - 'lib/puppet/functions/to_json_pretty.rb' + - 'lib/puppet/parser/functions/camelcase.rb' + - 'lib/puppet/parser/functions/clamp.rb' + - 'lib/puppet/parser/functions/fqdn_rand_string.rb' + - 'lib/puppet/parser/functions/fqdn_uuid.rb' + - 'lib/puppet/parser/functions/shell_join.rb' + - 'lib/puppet/parser/functions/squeeze.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, AllowSafeAssignment. +# SupportedStyles: require_parentheses, require_no_parentheses, require_parentheses_when_complex +Style/TernaryParentheses: + Exclude: + - 'spec/unit/facter/root_home_spec.rb' + +# Offense count: 40 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyleForMultiline. +# SupportedStylesForMultiline: comma, consistent_comma, no_comma +Style/TrailingCommaInHashLiteral: + Exclude: + - 'lib/puppet/parser/functions/is_absolute_path.rb' + - 'lib/puppet/parser/functions/pw_hash.rb' + - 'lib/puppet/parser/functions/str2saltedpbkdf2.rb' + - 'spec/classes/manage_spec.rb' + - 'spec/functions/dig44_spec.rb' + - 'spec/functions/has_interface_with_spec.rb' + - 'spec/functions/has_ip_address_spec.rb' + - 'spec/functions/has_ip_network_spec.rb' + - 'spec/functions/os_version_gte_spec.rb' + - 'spec/functions/try_get_value_spec.rb' + - 'spec/spec_helper.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +Style/UnpackFirst: Exclude: - - 'spec/unit/facter/facter_dot_d_spec.rb' + - 'lib/puppet/parser/functions/str2saltedpbkdf2.rb' + - 'lib/puppet/parser/functions/str2saltedsha512.rb' From e2d8b18c60b3f24e159fdae24b74f8d3224e06df Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Fri, 21 Apr 2023 10:20:22 +0100 Subject: [PATCH 04/33] (CONT-801) Addressing unit failures --- lib/puppet/parser/functions/fqdn_rotate.rb | 2 +- spec/functions/fqdn_rotate_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb index d9773a3a9..962279dd2 100644 --- a/lib/puppet/parser/functions/fqdn_rotate.rb +++ b/lib/puppet/parser/functions/fqdn_rotate.rb @@ -38,7 +38,7 @@ elements = result.size - seed = Digest::MD5.hexdigest([lookupvar('::fqdn'), args].join(':')).hex + seed = Digest::MD5.hexdigest([lookupvar('facts'), args].join(':')).hex # deterministic_rand() was added in Puppet 3.2.0; reimplement if necessary if Puppet::Util.respond_to?(:deterministic_rand) offset = Puppet::Util.deterministic_rand(seed, elements).to_i diff --git a/spec/functions/fqdn_rotate_spec.rb b/spec/functions/fqdn_rotate_spec.rb index 8c68c3754..1af0325be 100644 --- a/spec/functions/fqdn_rotate_spec.rb +++ b/spec/functions/fqdn_rotate_spec.rb @@ -68,7 +68,7 @@ def fqdn_rotate(value, args = {}) # workaround not being able to use let(:facts) because some tests need # multiple different hostnames in one context - allow(scope).to receive(:lookupvar).with('::fqdn').and_return(host) + allow(scope).to receive(:lookupvar).with('facts').and_return(host) function_args = [value] + extra scope.function_fqdn_rotate(function_args) From c06b9d28eff0db16bd32953a5a3bb91182e7fee0 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Fri, 21 Apr 2023 10:38:42 +0100 Subject: [PATCH 05/33] (CONT-801) Update fixtures.yml --- .fixtures.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.fixtures.yml b/.fixtures.yml index f1df77eca..cf4ff2d7b 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -3,7 +3,7 @@ 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' symlinks: stdlib: "#{source_dir}" From 0818b8cef22fe95e311a1f1e5bdd8d42e811578e Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Mon, 24 Apr 2023 09:53:01 +0100 Subject: [PATCH 06/33] (CONT-801) Address rebase broken cops --- .rubocop_todo.yml | 26 +++++++++++++++++--------- spec/functions/ensure_packages_spec.rb | 6 +++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index be93c27a9..a2e6c326c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-04-21 08:55:52 UTC using RuboCop version 1.48.1. +# on 2023-04-24 08:52:31 UTC using RuboCop version 1.48.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -139,12 +139,12 @@ Lint/SuppressedException: - 'lib/puppet/parser/functions/getvar.rb' - 'lib/puppet/parser/functions/has_interface_with.rb' -# Offense count: 6 +# Offense count: 7 # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: Max: 44 -# Offense count: 21 +# Offense count: 22 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode. # AllowedMethods: refine Metrics/BlockLength: @@ -205,6 +205,12 @@ Naming/VariableNumber: Exclude: - 'spec/functions/delete_undef_values_spec.rb' +# Offense count: 2 +# Configuration parameters: MinSize. +Performance/CollectionLiteralInLoop: + Exclude: + - 'lib/puppet/functions/ensure_packages.rb' + # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). Performance/MapCompact: @@ -251,7 +257,7 @@ RSpec/ClassCheck: Exclude: - 'spec/functions/type_of_spec.rb' -# Offense count: 36 +# Offense count: 38 # Configuration parameters: Prefixes, AllowedPatterns. # Prefixes: when, with, without RSpec/ContextWording: @@ -353,7 +359,7 @@ RSpec/MatchArray: - 'spec/functions/keys_spec.rb' - 'spec/functions/values_spec.rb' -# Offense count: 77 +# Offense count: 78 RSpec/MultipleExpectations: Max: 5 @@ -362,13 +368,13 @@ RSpec/MultipleExpectations: RSpec/MultipleMemoizedHelpers: Max: 9 -# Offense count: 60 +# Offense count: 64 # Configuration parameters: EnforcedStyle, IgnoreSharedExamples. # SupportedStyles: always, named_only RSpec/NamedSubject: Enabled: false -# Offense count: 25 +# Offense count: 27 # Configuration parameters: AllowedGroups. RSpec/NestedGroups: Max: 4 @@ -469,7 +475,7 @@ Style/FileWrite: - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' -# Offense count: 3 +# Offense count: 5 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. # SupportedStyles: always, always_true, never @@ -478,6 +484,8 @@ Style/FrozenStringLiteralComment: - 'lib/puppet_x/stdlib.rb' - 'lib/puppet_x/stdlib/toml_dumper.rb' - 'spec/functions/stdlib_deferrable_epp_spec.rb' + - 'spec/type_aliases/http__method_spec.rb' + - 'spec/type_aliases/http__status_spec.rb' # Offense count: 4 # This cop supports safe autocorrection (--autocorrect). @@ -495,7 +503,7 @@ Style/HashTransformKeys: Exclude: - 'lib/puppet/functions/to_json_pretty.rb' -# Offense count: 156 +# Offense count: 155 # This cop supports safe autocorrection (--autocorrect). Style/IfUnlessModifier: Enabled: false diff --git a/spec/functions/ensure_packages_spec.rb b/spec/functions/ensure_packages_spec.rb index 326857bbd..ab4298482 100644 --- a/spec/functions/ensure_packages_spec.rb +++ b/spec/functions/ensure_packages_spec.rb @@ -34,9 +34,9 @@ subject.execute('ρǻ¢κầģẻ' => { 'ensure' => 'absent' }) subject.execute( { - 'package_one' => {}, - 'package_two' => {}, - 'package_three' => { 'provider' => 'puppetserver_gem' }, + 'package_one' => {}, + 'package_two' => {}, + 'package_three' => { 'provider' => 'puppetserver_gem' } }, { 'provider' => 'puppet_gem' }, ) From 255b51b8c9dc3e17a6bb76caf2332394b30cfc14 Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Wed, 10 May 2023 14:45:34 +0100 Subject: [PATCH 07/33] (CONT-801) Fixup some rubocop issues --- lib/puppet/functions/fqdn_rand_string.rb | 2 +- spec/functions/has_interface_with_spec.rb | 2 +- spec/unit/puppet/provider/file_line/ruby_spec_alter.rb | 9 --------- 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/puppet/functions/fqdn_rand_string.rb b/lib/puppet/functions/fqdn_rand_string.rb index 003549649..b695929af 100644 --- a/lib/puppet/functions/fqdn_rand_string.rb +++ b/lib/puppet/functions/fqdn_rand_string.rb @@ -25,7 +25,7 @@ def fqdn_rand_string(length, charset = '', *seed) charset = charset.chars.to_a - charset = (0..9).map { |i| i.to_s } + ('A'..'Z').to_a + ('a'..'z').to_a if charset.empty? + charset = (0..9).map(&:to_s) + ('A'..'Z').to_a + ('a'..'z').to_a if charset.empty? rand_string = '' length.times do |current| diff --git a/spec/functions/has_interface_with_spec.rb b/spec/functions/has_interface_with_spec.rb index 45d4d9680..ed1a6081d 100644 --- a/spec/functions/has_interface_with_spec.rb +++ b/spec/functions/has_interface_with_spec.rb @@ -22,7 +22,7 @@ 'network' => '127.0.0.0' }, ], - "bindings6": [ + bindings6: [ { 'address' => '::1', 'netmask' => 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', diff --git a/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb b/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb index f6f7e9340..823174ca4 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb @@ -34,9 +34,6 @@ end describe '#create' do - context 'when adding' do - pending('To be added.') - end context 'when replacing' do let :params do { @@ -69,9 +66,6 @@ end end end - describe '#destroy' do - pending('To be added?') - end context 'when matching' do # rubocop:disable RSpec/InstanceVariable : replacing before with let breaks the tests, variables need to be altered within it block : multi before :each do @@ -147,9 +141,6 @@ end end end - context 'when match+replace+append_on_no_match' do - pending('to do') - end context 'when after' do let :resource do Puppet::Type::File_line.new( From 6e00446b004e1539200607693aa55b0d4a5e1c29 Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Wed, 10 May 2023 14:48:15 +0100 Subject: [PATCH 08/33] (CONT-801) Use named kwargs when reading yaml --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 07db73426..335122063 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -25,7 +25,7 @@ next unless File.exist?(f) && File.readable?(f) && File.size?(f) begin - default_facts.merge!(YAML.safe_load(File.read(f), [], [], true)) + default_facts.merge!(YAML.safe_load(File.read(f), permitted_classes: [], permitted_symbols: [], aliases: true)) rescue => e RSpec.configuration.reporter.message "WARNING: Unable to load #{f}: #{e}" end From 1422a25e0ade1c5bc24e18f03876d22d64677748 Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Wed, 10 May 2023 16:10:30 +0100 Subject: [PATCH 09/33] (CONT-801) Initialise Puppet Parser for spec tests --- spec/unit/puppet/parser/functions/enclose_ipv6_spec.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/unit/puppet/parser/functions/enclose_ipv6_spec.rb b/spec/unit/puppet/parser/functions/enclose_ipv6_spec.rb index ca815de61..280b49603 100644 --- a/spec/unit/puppet/parser/functions/enclose_ipv6_spec.rb +++ b/spec/unit/puppet/parser/functions/enclose_ipv6_spec.rb @@ -2,8 +2,10 @@ require 'spec_helper' -describe 'the enclose_ipv6 function' do - let(:scope) { PuppetlabsSpec::PuppetInternals.scope } +describe 'enclose_ipv6' do + let(:node) { Puppet::Node.new('localhost') } + let(:compiler) { Puppet::Parser::Compiler.new(node) } + let(:scope) { Puppet::Parser::Scope.new(compiler) } it 'exists' do expect(Puppet::Parser::Functions.function('enclose_ipv6')).to eq('function_enclose_ipv6') @@ -29,7 +31,7 @@ expect { scope.function_enclose_ipv6(['127.0.0.1']) }.not_to raise_error end - it 'does not raise a ParseError when given * as ip string' do + it 'does not raise a ParseError when given * as ip strint g' do expect { scope.function_enclose_ipv6(['*']) }.not_to raise_error end From 8222a1261cf26fa1d198085b9d223b9be852cefa Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Thu, 11 May 2023 11:51:15 +0100 Subject: [PATCH 10/33] Address loadjson.rb issues with PSON --- .../parser/functions/load_module_metadata.rb | 6 +- lib/puppet/parser/functions/loadjson.rb | 31 +++++++--- spec/functions/loadjson_spec.rb | 60 +++++++++++++++---- 3 files changed, 76 insertions(+), 21 deletions(-) diff --git a/lib/puppet/parser/functions/load_module_metadata.rb b/lib/puppet/parser/functions/load_module_metadata.rb index 248e199d2..38ebfb9d1 100644 --- a/lib/puppet/parser/functions/load_module_metadata.rb +++ b/lib/puppet/parser/functions/load_module_metadata.rb @@ -24,7 +24,11 @@ module Puppet::Parser::Functions metadata_exists = File.exists?(metadata_json) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code if metadata_exists - metadata = PSON.load(File.read(metadata_json)) + metadata = if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? + PSON.load(File.read(metadata_json)) + else + JSON.parse(File.read(metadata_json)) + end else metadata = {} raise(Puppet::ParseError, "load_module_metadata(): No metadata.json file for module #{mod}") unless allow_empty_metadata diff --git a/lib/puppet/parser/functions/loadjson.rb b/lib/puppet/parser/functions/loadjson.rb index 90f9146ca..4f13d6bb6 100644 --- a/lib/puppet/parser/functions/loadjson.rb +++ b/lib/puppet/parser/functions/loadjson.rb @@ -42,17 +42,32 @@ module Puppet::Parser::Functions else url = args[0] end - begin - contents = OpenURI.open_uri(url, **http_options) - rescue OpenURI::HTTPError => err - res = err.io - warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'") - args[1] + if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? + begin + contents = OpenURI.open_uri(url, **http_options) + rescue OpenURI::HTTPError => err + res = err.io + warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'") + args[1] + end + PSON.load(contents) || args[1] + else + begin + contents = URI.open(url, **http_options) # rubocop:disable Security/Open : Temporarily disabling this cop. This is a security risk and must be addressed before release. + rescue URI::Error => err + res = err.io + warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'") + args[1] + end + JSON.parse(contents) || args[1] end - PSON.load(contents) || args[1] elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code content = File.read(args[0]) - PSON.load(content) || args[1] + if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? + PSON.load(content) || args[1] + else + JSON.parse(content) || args[1] + end else warning("Can't load '#{args[0]}' File does not exist!") args[1] diff --git a/spec/functions/loadjson_spec.rb b/spec/functions/loadjson_spec.rb index fbfee51d1..08988b338 100644 --- a/spec/functions/loadjson_spec.rb +++ b/spec/functions/loadjson_spec.rb @@ -27,7 +27,11 @@ before(:each) do allow(File).to receive(:exists?).with(filename).and_return(false).once - allow(PSON).to receive(:load).never + if Puppet::PUPPETVERSION[0].to_i < 8 + allow(PSON).to receive(:load).never + else + allow(JSON).to receive(:parse).never + end end it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') } @@ -49,7 +53,11 @@ allow(File).to receive(:exists?).with(filename).and_return(true).once allow(File).to receive(:read).with(filename).and_return(json).once allow(File).to receive(:read).with(filename).and_return(json).once - allow(PSON).to receive(:load).with(json).and_return(data).once + if Puppet::PUPPETVERSION[0].to_i < 8 + allow(PSON).to receive(:load).with(json).and_return(data).once + else + allow(JSON).to receive(:parse).with(json).and_return(data).once + end end it { is_expected.to run.with_params(filename).and_return(data) } end @@ -67,7 +75,11 @@ before(:each) do allow(File).to receive(:exists?).with(filename).and_return(true).once allow(File).to receive(:read).with(filename).and_return(json).once - allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' + if Puppet::PUPPETVERSION[0].to_i < 8 + allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' + else + allow(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!' + end end it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } end @@ -80,8 +92,13 @@ let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } it { - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) - expect(PSON).to receive(:load).with(json).and_return(data).once + if Puppet::PUPPETVERSION[0].to_i < 8 + expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) + expect(PSON).to receive(:load).with(json).and_return(data).once + else + expect(URI).to receive(:open).with(filename).and_return(json) + expect(JSON).to receive(:parse).with(json).and_return(data).once + end is_expected.to run.with_params(filename).and_return(data) } end @@ -96,8 +113,13 @@ let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } it { - expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) - expect(PSON).to receive(:load).with(json).and_return(data).once + if Puppet::PUPPETVERSION[0].to_i < 8 + expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) + expect(PSON).to receive(:load).with(json).and_return(data).once + else + expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json) + expect(JSON).to receive(:parse).with(json).and_return(data).once + end is_expected.to run.with_params(filename).and_return(data) } end @@ -112,8 +134,13 @@ let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } it { - expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) - expect(PSON).to receive(:load).with(json).and_return(data).once + if Puppet::PUPPETVERSION[0].to_i < 8 + expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) + expect(PSON).to receive(:load).with(json).and_return(data).once + else + expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json) + expect(JSON).to receive(:parse).with(json).and_return(data).once + end is_expected.to run.with_params(filename).and_return(data) } end @@ -125,8 +152,13 @@ let(:json) { ',;{"key":"value"}' } it { - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) - expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' + if Puppet::PUPPETVERSION[0].to_i < 8 + expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) + expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' + else + expect(URI).to receive(:open).with(filename).and_return(json) + expect(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!' + end is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } end @@ -137,7 +169,11 @@ end it { - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found' + if Puppet::PUPPETVERSION[0].to_i < 8 + expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found' + else + expect(URI).to receive(:open).with(filename).and_raise URI::Error, '404 File not Found' + end is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } end From 1781dfadb48193c1153fe2338c6ff825cba23e3e Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Fri, 12 May 2023 16:46:42 +0100 Subject: [PATCH 11/33] Addressing srand() changes --- spec/functions/shuffle_spec.rb | 43 ++++++++++++++++++------- spec/functions/str2saltedsha512_spec.rb | 39 +++++++++++++++------- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/spec/functions/shuffle_spec.rb b/spec/functions/shuffle_spec.rb index 0e4b1e578..87a5c6e44 100644 --- a/spec/functions/shuffle_spec.rb +++ b/spec/functions/shuffle_spec.rb @@ -20,21 +20,40 @@ it { is_expected.to run.with_params([]).and_return([]) } it { is_expected.to run.with_params(['a']).and_return(['a']) } it { is_expected.to run.with_params(['one']).and_return(['one']) } - it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(['two', 'one', 'three']) } - it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(['four', 'three', 'two', 'one']) } - it { is_expected.to run.with_params('').and_return('') } - it { is_expected.to run.with_params('a').and_return('a') } - it { is_expected.to run.with_params('abc').and_return('bac') } - it { is_expected.to run.with_params('abcd').and_return('dcba') } + if Puppet::PUPPETVERSION[0].to_i < 8 + it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(['two', 'one', 'three']) } + it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(['four', 'three', 'two', 'one']) } - context 'with UTF8 and double byte characters' do - it { is_expected.to run.with_params('ůţƒ8 ŧέχŧ şŧґíńģ').and_return('ģńş ůχţέƒŧí8ґŧŧ ') } - it { is_expected.to run.with_params('日本語の文字列').and_return('字本日語文列の') } - end + it { is_expected.to run.with_params('').and_return('') } + it { is_expected.to run.with_params('a').and_return('a') } + it { is_expected.to run.with_params('abc').and_return('bac') } + it { is_expected.to run.with_params('abcd').and_return('dcba') } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params('ůţƒ8 ŧέχŧ şŧґíńģ').and_return('ģńş ůχţέƒŧí8ґŧŧ ') } + it { is_expected.to run.with_params('日本語の文字列').and_return('字本日語文列の') } + end + + context 'when using a class extending String' do + it { is_expected.to run.with_params(AlsoString.new('asdfghjkl')).and_return('lkhdsfajg') } + end + else + it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(['one', 'three', 'two']) } + it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(['one', 'three', 'two', 'four']) } + + it { is_expected.to run.with_params('b').and_return('b') } + it { is_expected.to run.with_params('abc').and_return('acb') } + it { is_expected.to run.with_params('abcd').and_return('acbd') } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params('ůţƒ8 ŧέχŧ şŧґíńģ').and_return('ŧńş ģχţέƒůí8ґŧŧ ') } + it { is_expected.to run.with_params('日本語の文字列').and_return('日列語字本文の') } + end - context 'when using a class extending String' do - it { is_expected.to run.with_params(AlsoString.new('asdfghjkl')).and_return('lkhdsfajg') } + context 'when using a class extending String' do + it { is_expected.to run.with_params(AlsoString.new('asdfghjkl')).and_return('lakfdgjsh') } + end end end end diff --git a/spec/functions/str2saltedsha512_spec.rb b/spec/functions/str2saltedsha512_spec.rb index ae50ea76e..194dcba25 100644 --- a/spec/functions/str2saltedsha512_spec.rb +++ b/spec/functions/str2saltedsha512_spec.rb @@ -12,17 +12,32 @@ # make tests deterministic before(:each) { srand(2) } - it { - is_expected.to run.with_params('') - .and_return('0f8a612f4eeed08e47b3875d00f33c5688f7926298f2d9b5fe19d1323f910bc78b6f7b5892596d2fabaa65e7a8d99b3768c102610cf0432c4827eee01f09451e3fae4f7a') - } - it { - is_expected.to run.with_params('password') - .and_return('0f8a612f43134376566c5707718d600effcfb17581fc9d3fa64d7f447dfda317c174ffdb498d2c5bd5c2075dab41c9d7ada5afbdc6b55354980eb5ba61802371e6b64956') - } - it { - is_expected.to run.with_params('verylongpassword') - .and_return('0f8a612f7a448537540e062daa8621f9bae326ca8ccb899e1bdb10e7c218cebfceae2530b856662565fdc4d81e986fc50cfbbc46d50436610ed9429ff5e43f2c45b5d039') - } + if Puppet::PUPPETVERSION[0].to_i < 8 + it { + is_expected.to run.with_params('') + .and_return('0f8a612f4eeed08e47b3875d00f33c5688f7926298f2d9b5fe19d1323f910bc78b6f7b5892596d2fabaa65e7a8d99b3768c102610cf0432c4827eee01f09451e3fae4f7a') + } + it { + is_expected.to run.with_params('password') + .and_return('0f8a612f43134376566c5707718d600effcfb17581fc9d3fa64d7f447dfda317c174ffdb498d2c5bd5c2075dab41c9d7ada5afbdc6b55354980eb5ba61802371e6b64956') + } + it { + is_expected.to run.with_params('verylongpassword') + .and_return('0f8a612f7a448537540e062daa8621f9bae326ca8ccb899e1bdb10e7c218cebfceae2530b856662565fdc4d81e986fc50cfbbc46d50436610ed9429ff5e43f2c45b5d039') + } + else + it { + is_expected.to run.with_params('') + .and_return('a85c9d6f8c1eb1a625fd59e3cbca7dc7ab04ff1758d19ab99f098446e14a0a2a42e11afd1f4d6f17adfe2c772a3e6a821ee66a2564711431e14da96a3bff44593cf158ab') + } + it { + is_expected.to run.with_params('password') + .and_return('a85c9d6ff4e4dd6655ec2922ee9752550f2df4dc370e9739dd94899f62be6a42cc31fbfce3d62be35e0e8482696c931f63fb9286cf7b13d283660720c55f2a6304d06958') + } + it { + is_expected.to run.with_params('verylongpassword') + .and_return('a85c9d6fb810d0b8311c9a065c026e3179ae91fee3dbaf556f297e2fda2a8e3d8dd363977f9ef5c9b5da0cd518a5151a4e537928533291d68c9539d4d4b83da53b22a869') + } + end end end From 6bbc470c6ff5ebdf96baa6ca630c7b1eb5cd1bc7 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Mon, 15 May 2023 14:45:04 +0100 Subject: [PATCH 12/33] Limiting PSON testing to Puppet 7 --- spec/functions/parsepson_spec.rb | 94 ++++++++++++++++---------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/spec/functions/parsepson_spec.rb b/spec/functions/parsepson_spec.rb index 4be97a2a2..76e61be83 100644 --- a/spec/functions/parsepson_spec.rb +++ b/spec/functions/parsepson_spec.rb @@ -3,64 +3,66 @@ require 'spec_helper' describe 'parsepson' do - it 'exists' do - is_expected.not_to eq(nil) - end - - it 'raises an error if called without any arguments' do - is_expected.to run.with_params - .and_raise_error(%r{'parsepson' expects between 1 and 2 arguments, got none}i) - end - - context 'with correct PSON data' do - it 'is able to parse PSON data with a Hash' do - is_expected.to run.with_params('{"a":"1","b":"2"}') - .and_return('a' => '1', 'b' => '2') + if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? + it 'exists' do + is_expected.not_to eq(nil) end - it 'is able to parse PSON data with an Array' do - is_expected.to run.with_params('["a","b","c"]') - .and_return(['a', 'b', 'c']) + it 'raises an error if called without any arguments' do + is_expected.to run.with_params + .and_raise_error(%r{'parsepson' expects between 1 and 2 arguments, got none}i) end - it 'is able to parse empty PSON values' do - actual_array = ['[]', '{}'] - expected = [[], {}] - actual_array.each_with_index do |actual, index| - is_expected.to run.with_params(actual).and_return(expected[index]) + context 'with correct PSON data' do + it 'is able to parse PSON data with a Hash' do + is_expected.to run.with_params('{"a":"1","b":"2"}') + .and_return('a' => '1', 'b' => '2') end - end - it 'is able to parse PSON data with a mixed structure' do - is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}') - .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [true, false] }) - end + it 'is able to parse PSON data with an Array' do + is_expected.to run.with_params('["a","b","c"]') + .and_return(['a', 'b', 'c']) + end - it 'is able to parse PSON data with a UTF8 and double byte characters' do - is_expected.to run.with_params('{"×":"これ","ý":"記号","です":{"©":["Á","ß"]}}') - .and_return('×' => 'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] }) - end + it 'is able to parse empty PSON values' do + actual_array = ['[]', '{}'] + expected = [[], {}] + actual_array.each_with_index do |actual, index| + is_expected.to run.with_params(actual).and_return(expected[index]) + end + end - it 'does not return the default value if the data was parsed correctly' do - is_expected.to run.with_params('{"a":"1"}', 'default_value') - .and_return('a' => '1') - end - end + it 'is able to parse PSON data with a mixed structure' do + is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}') + .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [true, false] }) + end - context 'with incorrect PSON data' do - it 'raises an error with invalid PSON and no default' do - is_expected.to run.with_params('invalid') - .and_raise_error(PSON::ParserError) - end + it 'is able to parse PSON data with a UTF8 and double byte characters' do + is_expected.to run.with_params('{"×":"これ","ý":"記号","です":{"©":["Á","ß"]}}') + .and_return('×' => 'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] }) + end - it 'returns the default value for an invalid PSON and a given default' do - is_expected.to run.with_params('invalid', 'default_value') - .and_return('default_value') + it 'does not return the default value if the data was parsed correctly' do + is_expected.to run.with_params('{"a":"1"}', 'default_value') + .and_return('a' => '1') + end end - it 'supports a structure for a default value' do - is_expected.to run.with_params('invalid', 'a' => '1') - .and_return('a' => '1') + context 'with incorrect PSON data' do + it 'raises an error with invalid PSON and no default' do + is_expected.to run.with_params('invalid') + .and_raise_error(PSON::ParserError) + end + + it 'returns the default value for an invalid PSON and a given default' do + is_expected.to run.with_params('invalid', 'default_value') + .and_return('default_value') + end + + it 'supports a structure for a default value' do + is_expected.to run.with_params('invalid', 'a' => '1') + .and_return('a' => '1') + end end end end From 39c486ba77eaf9e97798b44013714f415caffdb7 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Tue, 16 May 2023 09:22:37 +0100 Subject: [PATCH 13/33] Addressing uriescape testing --- lib/puppet/parser/functions/uriescape.rb | 1 + spec/functions/uriescape_spec.rb | 55 +++++++++++++----------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb index 354ad2280..3146d38ec 100644 --- a/lib/puppet/parser/functions/uriescape.rb +++ b/lib/puppet/parser/functions/uriescape.rb @@ -4,6 +4,7 @@ # # uriescape.rb # Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. +# URI.escape has been fully removed as of Ruby 3. Therefore, it will not work as it stand on Puppet 8. Consider deprecating or updating this function. # module Puppet::Parser::Functions newfunction(:uriescape, type: :rvalue, doc: <<-DOC diff --git a/spec/functions/uriescape_spec.rb b/spec/functions/uriescape_spec.rb index 81b27c37d..4fbafeac8 100644 --- a/spec/functions/uriescape_spec.rb +++ b/spec/functions/uriescape_spec.rb @@ -3,36 +3,39 @@ require 'spec_helper' describe 'uriescape' do - describe 'signature validation' do - it { is_expected.not_to eq(nil) } - it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } - it { - pending('Current implementation ignores parameters after the first.') - is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) - } - it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } - it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } - it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } - end + # URI.escape has been fully removed as of Ruby 3. Therefore, it will not work as it stand on Puppet 8. + if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + end - describe 'handling normal strings' do - it 'calls ruby\'s URI::DEFAULT_PARSER.escape function' do - expect(URI::DEFAULT_PARSER).to receive(:escape).with('uri_string').and_return('escaped_uri_string').once - is_expected.to run.with_params('uri_string').and_return('escaped_uri_string') + describe 'handling normal strings' do + it 'calls ruby\'s URI::DEFAULT_PARSER.escape function' do + expect(URI::DEFAULT_PARSER).to receive(:escape).with('uri_string').and_return('escaped_uri_string').once + is_expected.to run.with_params('uri_string').and_return('escaped_uri_string') + end end - end - describe 'handling classes derived from String' do - it 'calls ruby\'s URI::DEFAULT_PARSER.escape function' do - uri_string = AlsoString.new('uri_string') - expect(URI::DEFAULT_PARSER).to receive(:escape).with(uri_string).and_return('escaped_uri_string').once - is_expected.to run.with_params(uri_string).and_return('escaped_uri_string') + describe 'handling classes derived from String' do + it 'calls ruby\'s URI::DEFAULT_PARSER.escape function' do + uri_string = AlsoString.new('uri_string') + expect(URI::DEFAULT_PARSER).to receive(:escape).with(uri_string).and_return('escaped_uri_string').once + is_expected.to run.with_params(uri_string).and_return('escaped_uri_string') + end end - end - describe 'strings in arrays handling' do - it { is_expected.to run.with_params([]).and_return([]) } - it { is_expected.to run.with_params(['one}', 'two']).and_return(['one%7D', 'two']) } - it { is_expected.to run.with_params(['one}', 1, true, {}, 'two']).and_return(['one%7D', 1, true, {}, 'two']) } + describe 'strings in arrays handling' do + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(['one}', 'two']).and_return(['one%7D', 'two']) } + it { is_expected.to run.with_params(['one}', 1, true, {}, 'two']).and_return(['one%7D', 1, true, {}, 'two']) } + end end end From 660b063277d5a0a77008fd747757da07fc0aee5d Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:07:42 +0100 Subject: [PATCH 14/33] Addressing legacy facts --- lib/puppet/functions/os_version_gte.rb | 4 ++-- spec/functions/os_version_gte_spec.rb | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/puppet/functions/os_version_gte.rb b/lib/puppet/functions/os_version_gte.rb index 151c3c536..b5fd2d602 100644 --- a/lib/puppet/functions/os_version_gte.rb +++ b/lib/puppet/functions/os_version_gte.rb @@ -21,7 +21,7 @@ def os_version_gte(os, version) facts = closure_scope['facts'] - (facts['operatingsystem'] == os && - Puppet::Util::Package.versioncmp(facts['operatingsystemmajrelease'], version) >= 0) + (facts['os']['name'] == os && + Puppet::Util::Package.versioncmp(facts['os']['release']['major'], version) >= 0) end end diff --git a/spec/functions/os_version_gte_spec.rb b/spec/functions/os_version_gte_spec.rb index aec498270..ed1a067af 100644 --- a/spec/functions/os_version_gte_spec.rb +++ b/spec/functions/os_version_gte_spec.rb @@ -6,8 +6,10 @@ context 'on Debian 9' do let(:facts) do { - operatingsystem: 'Debian', - operatingsystemmajrelease: '9', + os: { + name: 'Debian', + release: { major: '9' }, + } } end @@ -22,8 +24,10 @@ context 'on Ubuntu 16.04' do let(:facts) do { - operatingsystem: 'Ubuntu', - operatingsystemmajrelease: '16.04', + os: { + name: 'Ubuntu', + release: { major: '16.04' }, + } } end @@ -39,8 +43,10 @@ context 'with invalid params' do let(:facts) do { - operatingsystem: 'Ubuntu', - operatingsystemmajrelease: '16.04', + os: { + name: 'Ubuntu', + release: { major: '16.04' }, + } } end From ec677d832d6b57b8e1870c396be2e5048a6664ea Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:12:55 +0100 Subject: [PATCH 15/33] (CONT-801) Safe correct Layout/ClosingHeredocIndentation --- .rubocop_todo.yml | 161 ++++++------------ lib/puppet/parser/functions/assert_private.rb | 2 +- lib/puppet/parser/functions/base64.rb | 2 +- lib/puppet/parser/functions/basename.rb | 2 +- lib/puppet/parser/functions/bool2num.rb | 2 +- lib/puppet/parser/functions/bool2str.rb | 2 +- lib/puppet/parser/functions/clamp.rb | 2 +- lib/puppet/parser/functions/convert_base.rb | 2 +- lib/puppet/parser/functions/deep_merge.rb | 2 +- .../parser/functions/delete_undef_values.rb | 2 +- lib/puppet/parser/functions/delete_values.rb | 2 +- lib/puppet/parser/functions/deprecation.rb | 2 +- lib/puppet/parser/functions/difference.rb | 2 +- lib/puppet/parser/functions/dirname.rb | 2 +- lib/puppet/parser/functions/dos2unix.rb | 2 +- lib/puppet/parser/functions/fqdn_rotate.rb | 2 +- lib/puppet/parser/functions/fqdn_uuid.rb | 2 +- lib/puppet/parser/functions/getparam.rb | 2 +- lib/puppet/parser/functions/glob.rb | 2 +- lib/puppet/parser/functions/grep.rb | 2 +- .../parser/functions/has_interface_with.rb | 2 +- lib/puppet/parser/functions/has_ip_address.rb | 2 +- lib/puppet/parser/functions/has_ip_network.rb | 2 +- lib/puppet/parser/functions/intersection.rb | 2 +- .../parser/functions/join_keys_to_values.rb | 2 +- lib/puppet/parser/functions/member.rb | 2 +- lib/puppet/parser/functions/merge.rb | 2 +- lib/puppet/parser/functions/num2bool.rb | 2 +- lib/puppet/parser/functions/pick_default.rb | 2 +- lib/puppet/parser/functions/prefix.rb | 2 +- lib/puppet/parser/functions/pry.rb | 2 +- lib/puppet/parser/functions/pw_hash.rb | 2 +- lib/puppet/parser/functions/range.rb | 2 +- lib/puppet/parser/functions/regexpescape.rb | 2 +- lib/puppet/parser/functions/reject.rb | 2 +- lib/puppet/parser/functions/reverse.rb | 2 +- .../parser/functions/str2saltedsha512.rb | 2 +- lib/puppet/parser/functions/suffix.rb | 2 +- lib/puppet/parser/functions/swapcase.rb | 2 +- lib/puppet/parser/functions/time.rb | 2 +- lib/puppet/parser/functions/to_bytes.rb | 2 +- lib/puppet/parser/functions/union.rb | 2 +- lib/puppet/parser/functions/unix2dos.rb | 2 +- lib/puppet/parser/functions/uriescape.rb | 2 +- .../parser/functions/validate_augeas.rb | 2 +- lib/puppet/parser/functions/validate_cmd.rb | 2 +- .../functions/validate_x509_rsa_key_pair.rb | 2 +- lib/puppet/parser/functions/values_at.rb | 2 +- lib/puppet/parser/functions/zip.rb | 2 +- 49 files changed, 101 insertions(+), 156 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a2e6c326c..a541a0fab 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-04-24 08:52:31 UTC using RuboCop version 1.48.1. +# on 2023-05-17 10:10:18 UTC using RuboCop version 1.48.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -14,12 +14,7 @@ Layout/BeginEndAlignment: Exclude: - 'lib/puppet/parser/functions/any2bool.rb' -# Offense count: 96 -# This cop supports safe autocorrection (--autocorrect). -Layout/ClosingHeredocIndentation: - Enabled: false - -# Offense count: 46 +# Offense count: 33 # This cop supports safe autocorrection (--autocorrect). Layout/EmptyLineAfterGuardClause: Enabled: false @@ -103,12 +98,6 @@ Lint/DuplicateBranch: - 'lib/puppet/parser/functions/str2bool.rb' - 'lib/puppet/provider/file_line/ruby.rb' -# Offense count: 1 -# Configuration parameters: AllowComments, AllowEmptyLambdas. -Lint/EmptyBlock: - Exclude: - - 'spec/unit/puppet/parser/functions/is_absolute_path_spec.rb' - # Offense count: 2 # Configuration parameters: MaximumRangeSize. Lint/MissingCopEnableDirective: @@ -131,20 +120,19 @@ Lint/RedundantSafeNavigation: - 'lib/puppet/parser/functions/assert_private.rb' - 'lib/puppet/parser/functions/getparam.rb' -# Offense count: 4 +# Offense count: 3 # Configuration parameters: AllowComments, AllowNil. Lint/SuppressedException: Exclude: - 'lib/puppet/functions/merge.rb' - - 'lib/puppet/parser/functions/getvar.rb' - 'lib/puppet/parser/functions/has_interface_with.rb' -# Offense count: 7 +# Offense count: 6 # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: Max: 44 -# Offense count: 22 +# Offense count: 19 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode. # AllowedMethods: refine Metrics/BlockLength: @@ -155,16 +143,11 @@ Metrics/BlockLength: Metrics/CyclomaticComplexity: Max: 15 -# Offense count: 11 +# Offense count: 9 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: Max: 37 -# Offense count: 1 -# Configuration parameters: CountComments, CountAsOne. -Metrics/ModuleLength: - Max: 104 - # Offense count: 4 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/PerceivedComplexity: @@ -185,7 +168,7 @@ Naming/MethodParameterName: Exclude: - 'spec/functions/pick_default_spec.rb' -# Offense count: 8 +# Offense count: 9 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: PreferredName. Naming/RescuedExceptionsVariableName: @@ -224,10 +207,13 @@ Performance/RedundantEqualityComparisonBlock: Exclude: - 'lib/puppet/parser/functions/bool2str.rb' -# Offense count: 24 +# Offense count: 3 # This cop supports safe autocorrection (--autocorrect). Performance/StringIdentifierArgument: - Enabled: false + Exclude: + - 'lib/facter/util/puppet_settings.rb' + - 'lib/puppet/parser/functions/member.rb' + - 'lib/puppet/parser/functions/validate_cmd.rb' # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). @@ -235,7 +221,7 @@ Performance/UnfreezeString: Exclude: - 'lib/puppet/parser/functions/pw_hash.rb' -# Offense count: 150 +# Offense count: 95 # This cop supports unsafe autocorrection (--autocorrect-all). RSpec/BeEq: Enabled: false @@ -257,24 +243,24 @@ RSpec/ClassCheck: Exclude: - 'spec/functions/type_of_spec.rb' -# Offense count: 38 +# Offense count: 36 # Configuration parameters: Prefixes, AllowedPatterns. # Prefixes: when, with, without RSpec/ContextWording: Enabled: false -# Offense count: 170 +# Offense count: 110 # Configuration parameters: IgnoredMetadata. RSpec/DescribeClass: Enabled: false -# Offense count: 214 +# Offense count: 161 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowConsecutiveOneLiners. RSpec/EmptyLineAfterExample: Enabled: false -# Offense count: 32 +# Offense count: 25 # This cop supports safe autocorrection (--autocorrect). RSpec/EmptyLineAfterExampleGroup: Exclude: @@ -282,23 +268,25 @@ RSpec/EmptyLineAfterExampleGroup: - 'spec/functions/stdlib_ensure_spec.rb' - 'spec/functions/swapcase_spec.rb' - 'spec/functions/validate_cmd_spec.rb' - - 'spec/type_aliases/compat__ip_address.rb' - - 'spec/type_aliases/compat__ipv4_spec.rb' - - 'spec/type_aliases/compat__ipv6_spec.rb' - 'spec/type_aliases/email_spec.rb' - 'spec/type_aliases/ensure_package_spec.rb' - - 'spec/type_aliases/hash_spec.rb' - 'spec/unit/facter/util/puppet_settings_spec.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' -# Offense count: 26 +# Offense count: 13 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowConsecutiveOneLiners. RSpec/EmptyLineAfterHook: - Enabled: false + Exclude: + - 'spec/functions/delete_undef_values_spec.rb' + - 'spec/functions/deprecation_spec.rb' + - 'spec/functions/get_module_path_spec.rb' + - 'spec/functions/loadjson_spec.rb' + - 'spec/functions/time_spec.rb' + - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' -# Offense count: 5 +# Offense count: 10 # Configuration parameters: CountAsOne. RSpec/ExampleLength: Max: 9 @@ -312,18 +300,10 @@ RSpec/ExampleWording: - 'spec/functions/validate_legacy_spec.rb' # Offense count: 2 -# This cop supports safe autocorrection (--autocorrect). -RSpec/ExcessiveDocstringSpacing: - Exclude: - - 'spec/functions/is_mac_address_spec.rb' - - 'spec/functions/is_string_spec.rb' - -# Offense count: 3 # Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly. # Include: **/*_spec*rb*, **/spec/**/* RSpec/FilePath: Exclude: - - 'spec/type_aliases/compat__ip_address.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' @@ -333,13 +313,7 @@ RSpec/HooksBeforeExamples: Exclude: - 'spec/functions/deprecation_spec.rb' -# Offense count: 2 -RSpec/IdenticalEqualityAssertion: - Exclude: - - 'spec/functions/fqdn_rand_string_spec.rb' - - 'spec/functions/seeded_rand_spec.rb' - -# Offense count: 300 +# Offense count: 198 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle. # SupportedStyles: single_line_only, single_statement_only, disallow, require_implicit @@ -352,14 +326,7 @@ RSpec/LeakyConstantDeclaration: - 'spec/functions/get_module_path_spec.rb' - 'spec/unit/facter/util/puppet_settings_spec.rb' -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -RSpec/MatchArray: - Exclude: - - 'spec/functions/keys_spec.rb' - - 'spec/functions/values_spec.rb' - -# Offense count: 78 +# Offense count: 40 RSpec/MultipleExpectations: Max: 5 @@ -368,25 +335,24 @@ RSpec/MultipleExpectations: RSpec/MultipleMemoizedHelpers: Max: 9 -# Offense count: 64 +# Offense count: 55 # Configuration parameters: EnforcedStyle, IgnoreSharedExamples. # SupportedStyles: always, named_only RSpec/NamedSubject: Enabled: false -# Offense count: 27 +# Offense count: 25 # Configuration parameters: AllowedGroups. RSpec/NestedGroups: Max: 4 -# Offense count: 12 +# Offense count: 4 # Configuration parameters: AllowedPatterns. # AllowedPatterns: ^expect_, ^assert_ RSpec/NoExpectationExample: Exclude: - 'spec/acceptance/file_line_spec.rb' - 'spec/unit/facter/util/puppet_settings_spec.rb' - - 'spec/unit/puppet/parser/functions/is_absolute_path_spec.rb' # Offense count: 2 RSpec/PendingWithoutReason: @@ -394,27 +360,29 @@ RSpec/PendingWithoutReason: - 'spec/functions/is_a_spec.rb' - 'spec/functions/type_of_spec.rb' -# Offense count: 19 +# Offense count: 13 # This cop supports safe autocorrection (--autocorrect). RSpec/ReceiveNever: - Enabled: false + Exclude: + - 'spec/functions/deprecation_spec.rb' + - 'spec/functions/loadjson_spec.rb' + - 'spec/functions/loadyaml_spec.rb' + - 'spec/functions/validate_legacy_spec.rb' + - 'spec/unit/facter/util/puppet_settings_spec.rb' -# Offense count: 6 +# Offense count: 4 RSpec/RepeatedExampleGroupDescription: Exclude: - 'spec/functions/delete_regex_spec.rb' - 'spec/functions/delete_spec.rb' - - 'spec/functions/getvar_spec.rb' -# Offense count: 33 +# Offense count: 24 RSpec/StubbedMock: Exclude: - 'spec/functions/assert_private_spec.rb' - 'spec/functions/loadjson_spec.rb' - 'spec/functions/loadyaml_spec.rb' - - 'spec/functions/private_spec.rb' - 'spec/functions/reverse_spec.rb' - - 'spec/functions/size_spec.rb' - 'spec/functions/squeeze_spec.rb' # Offense count: 1 @@ -487,12 +455,11 @@ Style/FrozenStringLiteralComment: - 'spec/type_aliases/http__method_spec.rb' - 'spec/type_aliases/http__status_spec.rb' -# Offense count: 4 +# Offense count: 3 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowSplatArgument. Style/HashConversion: Exclude: - - 'lib/puppet/functions/sprintf_hash.rb' - 'lib/puppet/functions/to_json_pretty.rb' - 'lib/puppet/parser/functions/prefix.rb' - 'lib/puppet/parser/functions/suffix.rb' @@ -503,12 +470,12 @@ Style/HashTransformKeys: Exclude: - 'lib/puppet/functions/to_json_pretty.rb' -# Offense count: 155 +# Offense count: 85 # This cop supports safe autocorrection (--autocorrect). Style/IfUnlessModifier: Enabled: false -# Offense count: 5 +# Offense count: 4 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowedMethods. # AllowedMethods: nonzero? @@ -517,7 +484,6 @@ Style/IfWithBooleanLiteralBranches: - 'lib/facter/pe_version.rb' - 'lib/puppet/parser/functions/fqdn_rotate.rb' - 'lib/puppet/parser/functions/shuffle.rb' - - 'lib/puppet/parser/functions/unique.rb' - 'lib/puppet/provider/file_line/ruby.rb' # Offense count: 1 @@ -538,18 +504,14 @@ Style/NegatedIfElseCondition: Exclude: - 'lib/puppet/parser/functions/pw_hash.rb' -# Offense count: 7 +# Offense count: 3 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle, AllowedMethods, AllowedPatterns. # SupportedStyles: predicate, comparison Style/NumericPredicate: Exclude: - 'spec/**/*' - - 'lib/puppet/parser/functions/fqdn_rand_string.rb' - - 'lib/puppet/parser/functions/is_email_address.rb' - 'lib/puppet/parser/functions/num2bool.rb' - - 'lib/puppet/parser/functions/seeded_rand.rb' - - 'lib/puppet/parser/functions/validate_slength.rb' - 'lib/puppet/provider/file_line/ruby.rb' # Offense count: 3 @@ -560,12 +522,11 @@ Style/OptionalBooleanParameter: - 'lib/puppet/functions/to_json_pretty.rb' - 'lib/puppet_x/stdlib/toml_dumper.rb' -# Offense count: 3 +# Offense count: 2 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: Methods. Style/RedundantArgument: Exclude: - - 'lib/puppet/parser/functions/sort.rb' - 'lib/puppet/provider/file_line/ruby.rb' # Offense count: 1 @@ -580,20 +541,16 @@ Style/RedundantFileExtensionInRequire: Exclude: - 'lib/puppet/functions/to_toml.rb' -# Offense count: 10 +# Offense count: 6 # This cop supports safe autocorrection (--autocorrect). Style/RedundantHeredocDelimiterQuotes: Exclude: - 'lib/puppet/parser/functions/convert_base.rb' - 'lib/puppet/parser/functions/deep_merge.rb' - 'lib/puppet/parser/functions/getparam.rb' - - 'lib/puppet/parser/functions/getvar.rb' - - 'lib/puppet/parser/functions/has_key.rb' - - 'lib/puppet/parser/functions/is_absolute_path.rb' - 'lib/puppet/parser/functions/loadjson.rb' - 'lib/puppet/parser/functions/loadyaml.rb' - 'lib/puppet/parser/functions/merge.rb' - - 'lib/puppet/parser/functions/private.rb' # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). @@ -601,11 +558,10 @@ Style/RedundantParentheses: Exclude: - 'lib/puppet/parser/functions/pw_hash.rb' -# Offense count: 43 +# Offense count: 41 # This cop supports safe autocorrection (--autocorrect). Style/RedundantRegexpEscape: Exclude: - - 'lib/puppet/parser/functions/is_email_address.rb' - 'lib/puppet/parser/functions/loadjson.rb' - 'lib/puppet/parser/functions/loadyaml.rb' - 'lib/puppet/parser/functions/range.rb' @@ -620,7 +576,7 @@ Style/RedundantStringEscape: Exclude: - 'lib/puppet/type/file_line.rb' -# Offense count: 6 +# Offense count: 4 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle. # SupportedStyles: implicit, explicit @@ -629,8 +585,6 @@ Style/RescueStandardError: - 'lib/puppet/functions/stdlib/crc32.rb' - 'lib/puppet/functions/stdlib/sha256.rb' - 'lib/puppet/parser/functions/any2bool.rb' - - 'lib/puppet/parser/functions/dig44.rb' - - 'spec/functions/private_spec.rb' - 'spec/spec_helper.rb' # Offense count: 1 @@ -639,33 +593,29 @@ Style/SelectByRegexp: Exclude: - 'lib/puppet/parser/functions/reject.rb' -# Offense count: 2 +# Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). Style/SlicingWithRange: Exclude: - - 'lib/puppet/parser/functions/is_domain_name.rb' - 'spec/functions/validate_x509_rsa_key_pair_spec.rb' -# Offense count: 7 +# Offense count: 6 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowModifier. Style/SoleNestedConditional: Exclude: - 'lib/puppet/parser/functions/convert_base.rb' - 'lib/puppet/parser/functions/getparam.rb' - - 'lib/puppet/parser/functions/join.rb' - 'lib/puppet/parser/functions/prefix.rb' - 'lib/puppet/parser/functions/suffix.rb' - 'lib/puppet/type/file_line.rb' -# Offense count: 4 +# Offense count: 2 # This cop supports unsafe autocorrection (--autocorrect-all). Style/StringChars: Exclude: - 'lib/puppet/parser/functions/fqdn_rotate.rb' - 'lib/puppet/parser/functions/shuffle.rb' - - 'lib/puppet/parser/functions/sort.rb' - - 'lib/puppet/parser/functions/unique.rb' # Offense count: 5 # This cop supports unsafe autocorrection (--autocorrect-all). @@ -675,16 +625,14 @@ Style/StringConcatenation: - 'lib/puppet_x/stdlib/toml_dumper.rb' - 'spec/functions/get_module_path_spec.rb' -# Offense count: 8 +# Offense count: 5 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowMethodsWithArguments, AllowedMethods, AllowedPatterns, AllowComments. # AllowedMethods: define_method Style/SymbolProc: Exclude: - 'lib/puppet/functions/to_json_pretty.rb' - - 'lib/puppet/parser/functions/camelcase.rb' - 'lib/puppet/parser/functions/clamp.rb' - - 'lib/puppet/parser/functions/fqdn_rand_string.rb' - 'lib/puppet/parser/functions/fqdn_uuid.rb' - 'lib/puppet/parser/functions/shell_join.rb' - 'lib/puppet/parser/functions/squeeze.rb' @@ -697,22 +645,19 @@ Style/TernaryParentheses: Exclude: - 'spec/unit/facter/root_home_spec.rb' -# Offense count: 40 +# Offense count: 31 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyleForMultiline. # SupportedStylesForMultiline: comma, consistent_comma, no_comma Style/TrailingCommaInHashLiteral: Exclude: - - 'lib/puppet/parser/functions/is_absolute_path.rb' - 'lib/puppet/parser/functions/pw_hash.rb' - 'lib/puppet/parser/functions/str2saltedpbkdf2.rb' - 'spec/classes/manage_spec.rb' - - 'spec/functions/dig44_spec.rb' - 'spec/functions/has_interface_with_spec.rb' - 'spec/functions/has_ip_address_spec.rb' - 'spec/functions/has_ip_network_spec.rb' - 'spec/functions/os_version_gte_spec.rb' - - 'spec/functions/try_get_value_spec.rb' - 'spec/spec_helper.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' diff --git a/lib/puppet/parser/functions/assert_private.rb b/lib/puppet/parser/functions/assert_private.rb index 0e64bd67a..726b88484 100644 --- a/lib/puppet/parser/functions/assert_private.rb +++ b/lib/puppet/parser/functions/assert_private.rb @@ -12,7 +12,7 @@ module Puppet::Parser::Functions set the current class or definition as private. Calling the class or definition from outside the current module will fail. - DOC + DOC ) do |args| raise(Puppet::ParseError, "assert_private(): Wrong number of arguments given (#{args.size}}) for 0 or 1)") if args.size > 1 diff --git a/lib/puppet/parser/functions/base64.rb b/lib/puppet/parser/functions/base64.rb index 1fe2751b2..e070c82b9 100644 --- a/lib/puppet/parser/functions/base64.rb +++ b/lib/puppet/parser/functions/base64.rb @@ -33,7 +33,7 @@ module Puppet::Parser::Functions function for reading a file with binary (non UTF-8) content. @return [String] The encoded/decoded value - DOC + DOC require 'base64' diff --git a/lib/puppet/parser/functions/basename.rb b/lib/puppet/parser/functions/basename.rb index 176182122..21aad1b93 100644 --- a/lib/puppet/parser/functions/basename.rb +++ b/lib/puppet/parser/functions/basename.rb @@ -9,7 +9,7 @@ module Puppet::Parser::Functions Strips directory (and optional suffix) from a filename @return [String] The stripped filename - DOC + DOC ) do |arguments| raise(Puppet::ParseError, 'basename(): No arguments given') if arguments.empty? raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})") if arguments.size > 2 diff --git a/lib/puppet/parser/functions/bool2num.rb b/lib/puppet/parser/functions/bool2num.rb index a55e5cc3f..066bb2b10 100644 --- a/lib/puppet/parser/functions/bool2num.rb +++ b/lib/puppet/parser/functions/bool2num.rb @@ -28,7 +28,7 @@ module Puppet::Parser::Functions ``` @return [Integer] The converted value as a number - DOC + DOC ) do |arguments| raise(Puppet::ParseError, "bool2num(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? diff --git a/lib/puppet/parser/functions/bool2str.rb b/lib/puppet/parser/functions/bool2str.rb index 03d53d4f2..9a1021c58 100644 --- a/lib/puppet/parser/functions/bool2str.rb +++ b/lib/puppet/parser/functions/bool2str.rb @@ -37,7 +37,7 @@ module Puppet::Parser::Functions notice(String(false, '%y')) # Notices 'yes' notice(String(true, '%y')) # Notices 'no' ``` - DOC + DOC ) do |arguments| unless arguments.size == 1 || arguments.size == 3 raise(Puppet::ParseError, "bool2str(): Wrong number of arguments given (#{arguments.size} for 3)") diff --git a/lib/puppet/parser/functions/clamp.rb b/lib/puppet/parser/functions/clamp.rb index c05688498..543a152ad 100644 --- a/lib/puppet/parser/functions/clamp.rb +++ b/lib/puppet/parser/functions/clamp.rb @@ -23,7 +23,7 @@ module Puppet::Parser::Functions `[$minval, $maxval, $value_to_clamp].sort[1]` @return [Array[Integer]] The sorted Array - DOC + DOC ) do |args| args.flatten! diff --git a/lib/puppet/parser/functions/convert_base.rb b/lib/puppet/parser/functions/convert_base.rb index d6554192d..d58a03edd 100644 --- a/lib/puppet/parser/functions/convert_base.rb +++ b/lib/puppet/parser/functions/convert_base.rb @@ -27,7 +27,7 @@ module Puppet::Parser::Functions `$hex_repr = String(254, "%#x")` return `"0xfe"` @return [String] The converted value as a String - DOC + DOC raise Puppet::ParseError, 'convert_base(): First argument must be either a string or an integer' unless args[0].is_a?(Integer) || args[0].is_a?(String) raise Puppet::ParseError, 'convert_base(): Second argument must be either a string or an integer' unless args[1].is_a?(Integer) || args[1].is_a?(String) diff --git a/lib/puppet/parser/functions/deep_merge.rb b/lib/puppet/parser/functions/deep_merge.rb index bd796ec63..b6d14b4d8 100644 --- a/lib/puppet/parser/functions/deep_merge.rb +++ b/lib/puppet/parser/functions/deep_merge.rb @@ -22,7 +22,7 @@ module Puppet::Parser::Functions When there is a duplicate key that is not a hash, the key in the rightmost hash will "win." @return [Hash] The merged hash - DOC + DOC if args.length < 2 raise Puppet::ParseError, "deep_merge(): wrong number of arguments (#{args.length}; must be at least 2)" diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb index e537e9ae8..dbaab26e1 100644 --- a/lib/puppet/parser/functions/delete_undef_values.rb +++ b/lib/puppet/parser/functions/delete_undef_values.rb @@ -24,7 +24,7 @@ module Puppet::Parser::Functions $hash.filter |$key, $val| { $val =~ NotUndef } @return [Array] The given array now issing of undefined values. - DOC + DOC ) do |args| raise(Puppet::ParseError, "delete_undef_values(): Wrong number of arguments given (#{args.size})") if args.empty? diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb index 3aa8be724..b4b8a7355 100644 --- a/lib/puppet/parser/functions/delete_values.rb +++ b/lib/puppet/parser/functions/delete_values.rb @@ -20,7 +20,7 @@ module Puppet::Parser::Functions $hash.filter |$key, $val| { $val != 'B' } @return [Hash] The given hash now missing all instances of the targeted value - DOC + DOC ) do |arguments| raise(Puppet::ParseError, "delete_values(): Wrong number of arguments given (#{arguments.size} of 2)") if arguments.size != 2 diff --git a/lib/puppet/parser/functions/deprecation.rb b/lib/puppet/parser/functions/deprecation.rb index 1633723a5..5db654dd1 100644 --- a/lib/puppet/parser/functions/deprecation.rb +++ b/lib/puppet/parser/functions/deprecation.rb @@ -13,7 +13,7 @@ module Puppet::Parser::Functions @return [String] return deprecation warnings -DOC + DOC ) do |arguments| raise(Puppet::ParseError, "deprecation: Wrong number of arguments given (#{arguments.size} for 2)") unless arguments.size == 2 diff --git a/lib/puppet/parser/functions/difference.rb b/lib/puppet/parser/functions/difference.rb index 3c4461028..09440be61 100644 --- a/lib/puppet/parser/functions/difference.rb +++ b/lib/puppet/parser/functions/difference.rb @@ -24,7 +24,7 @@ module Puppet::Parser::Functions @return [Array] The difference between the two given arrays - DOC + DOC ) do |arguments| # Two arguments are required raise(Puppet::ParseError, "difference(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size != 2 diff --git a/lib/puppet/parser/functions/dirname.rb b/lib/puppet/parser/functions/dirname.rb index 4a07a299d..6fcfa5832 100644 --- a/lib/puppet/parser/functions/dirname.rb +++ b/lib/puppet/parser/functions/dirname.rb @@ -9,7 +9,7 @@ module Puppet::Parser::Functions Returns the dirname of a path. @return [String] the given path's dirname - DOC + DOC ) do |arguments| if arguments.empty? raise(Puppet::ParseError, 'dirname(): No arguments given') diff --git a/lib/puppet/parser/functions/dos2unix.rb b/lib/puppet/parser/functions/dos2unix.rb index 0e2b2b6a8..daa92b6b2 100644 --- a/lib/puppet/parser/functions/dos2unix.rb +++ b/lib/puppet/parser/functions/dos2unix.rb @@ -9,7 +9,7 @@ module Puppet::Parser::Functions Takes a single string argument. @return The retrieved version - DOC + DOC ) do |arguments| unless arguments[0].is_a?(String) raise(Puppet::ParseError, 'dos2unix(): Requires string as argument') diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb index 962279dd2..5cab44b24 100644 --- a/lib/puppet/parser/functions/fqdn_rotate.rb +++ b/lib/puppet/parser/functions/fqdn_rotate.rb @@ -15,7 +15,7 @@ fqdn_rotate(['a', 'b', 'c', 'd']) fqdn_rotate('abcd') fqdn_rotate([1, 2, 3], 'custom seed') - DOC +DOC ) do |args| raise(Puppet::ParseError, "fqdn_rotate(): Wrong number of arguments given (#{args.size} for 1)") if args.empty? diff --git a/lib/puppet/parser/functions/fqdn_uuid.rb b/lib/puppet/parser/functions/fqdn_uuid.rb index b3141c8f5..4ce2bfed3 100644 --- a/lib/puppet/parser/functions/fqdn_uuid.rb +++ b/lib/puppet/parser/functions/fqdn_uuid.rb @@ -16,7 +16,7 @@ module Puppet::Parser::Functions @example Example Usage: fqdn_uuid('puppetlabs.com') # Returns '9c70320f-6815-5fc5-ab0f-debe68bf764c' fqdn_uuid('google.com') # Returns '64ee70a4-8cc1-5d25-abf2-dea6c79a09c8' - DOC + DOC raise(ArgumentError, 'fqdn_uuid: No arguments given') if args.empty? raise(ArgumentError, "fqdn_uuid: Too many arguments given (#{args.length})") unless args.length == 1 diff --git a/lib/puppet/parser/functions/getparam.rb b/lib/puppet/parser/functions/getparam.rb index c0ae572f3..49e8043a6 100644 --- a/lib/puppet/parser/functions/getparam.rb +++ b/lib/puppet/parser/functions/getparam.rb @@ -42,7 +42,7 @@ and the [ ] operator. The example below is equivalent to a call to getparam(): ```Example_resource['example_resource_instance']['param']`` - DOC +DOC ) do |vals| reference, param = vals raise(ArgumentError, 'Must specify a reference') unless reference diff --git a/lib/puppet/parser/functions/glob.rb b/lib/puppet/parser/functions/glob.rb index be4e0d2cc..1ff3a9872 100644 --- a/lib/puppet/parser/functions/glob.rb +++ b/lib/puppet/parser/functions/glob.rb @@ -13,7 +13,7 @@ module Puppet::Parser::Functions @example Example Usage: $confs = glob(['/etc/**/*.conf', '/opt/**/*.conf']) - DOC + DOC ) do |arguments| unless arguments.size == 1 raise(Puppet::ParseError, 'glob(): Wrong number of arguments given ' \ diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb index 6ef3ee457..9290b9ebd 100644 --- a/lib/puppet/parser/functions/grep.rb +++ b/lib/puppet/parser/functions/grep.rb @@ -18,7 +18,7 @@ module Puppet::Parser::Functions [`filter`](https://puppet.com/docs/puppet/latest/function.html#filter) function does the "same" - as any logic can be used to filter, as opposed to just regular expressions: ```['aaa', 'bbb', 'ccc', 'aaaddd']. filter |$x| { $x =~ 'aaa' }``` - DOC + DOC ) do |arguments| if arguments.size != 2 raise(Puppet::ParseError, "grep(): Wrong number of arguments given #{arguments.size} for 2") diff --git a/lib/puppet/parser/functions/has_interface_with.rb b/lib/puppet/parser/functions/has_interface_with.rb index 0404217b2..d2295ef94 100644 --- a/lib/puppet/parser/functions/has_interface_with.rb +++ b/lib/puppet/parser/functions/has_interface_with.rb @@ -19,7 +19,7 @@ module Puppet::Parser::Functions @example If no "kind" is given, then the presence of the interface is checked: has_interface_with("lo") # Returns `true` - DOC + DOC ) do |args| raise(Puppet::ParseError, "has_interface_with(): Wrong number of arguments given (#{args.size} for 1 or 2)") if args.empty? || args.size > 2 diff --git a/lib/puppet/parser/functions/has_ip_address.rb b/lib/puppet/parser/functions/has_ip_address.rb index 0cea4a7da..763813c22 100644 --- a/lib/puppet/parser/functions/has_ip_address.rb +++ b/lib/puppet/parser/functions/has_ip_address.rb @@ -13,7 +13,7 @@ module Puppet::Parser::Functions This function iterates through the 'interfaces' fact and checks the 'ipaddress_IFACE' facts, performing a simple string comparison. - DOC + DOC ) do |args| raise(Puppet::ParseError, "has_ip_address(): Wrong number of arguments given (#{args.size} for 1)") if args.size != 1 diff --git a/lib/puppet/parser/functions/has_ip_network.rb b/lib/puppet/parser/functions/has_ip_network.rb index 7406ed522..b556d97c0 100644 --- a/lib/puppet/parser/functions/has_ip_network.rb +++ b/lib/puppet/parser/functions/has_ip_network.rb @@ -13,7 +13,7 @@ module Puppet::Parser::Functions This function iterates through the 'interfaces' fact and checks the 'network_IFACE' facts, performing a simple string comparision. - DOC + DOC ) do |args| raise(Puppet::ParseError, "has_ip_network(): Wrong number of arguments given (#{args.size} for 1)") if args.size != 1 diff --git a/lib/puppet/parser/functions/intersection.rb b/lib/puppet/parser/functions/intersection.rb index ead75807a..929a1b42a 100644 --- a/lib/puppet/parser/functions/intersection.rb +++ b/lib/puppet/parser/functions/intersection.rb @@ -14,7 +14,7 @@ module Puppet::Parser::Functions @example Example Usage: intersection(["a","b","c"],["b","c","d"]) # returns ["b","c"] intersection(["a","b","c"],[1,2,3,4]) # returns [] (true, when evaluated as a Boolean) - DOC + DOC ) do |arguments| # Two arguments are required raise(Puppet::ParseError, "intersection(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size != 2 diff --git a/lib/puppet/parser/functions/join_keys_to_values.rb b/lib/puppet/parser/functions/join_keys_to_values.rb index 3e709cb9c..72116956d 100644 --- a/lib/puppet/parser/functions/join_keys_to_values.rb +++ b/lib/puppet/parser/functions/join_keys_to_values.rb @@ -24,7 +24,7 @@ module Puppet::Parser::Functions line breaks, delimiters around arrays and hash entries, between key/values in hash entries, and individual formatting of values in the array) - see the `new` function for `String` and its formatting options for `Array` and `Hash`. - DOC + DOC ) do |arguments| # Validate the number of arguments. if arguments.size != 2 diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index 279548849..1ecade2b1 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -35,7 +35,7 @@ module Puppet::Parser::Functions > **Note** that since Puppet 5.2.0, the general form to test the content of an array or hash is to use the built-in [`any`](https://puppet.com/docs/puppet/latest/function.html#any) and [`all`](https://puppet.com/docs/puppet/latest/function.html#all) functions. - DOC + DOC ) do |arguments| raise(Puppet::ParseError, "member(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2 diff --git a/lib/puppet/parser/functions/merge.rb b/lib/puppet/parser/functions/merge.rb index 522e48401..9f7c1576c 100644 --- a/lib/puppet/parser/functions/merge.rb +++ b/lib/puppet/parser/functions/merge.rb @@ -20,7 +20,7 @@ module Puppet::Parser::Functions Note that since Puppet 4.0.0 the same merge can be achieved with the + operator. `$merged_hash = $hash1 + $hash2` - DOC + DOC if args.length < 2 raise Puppet::ParseError, "merge(): wrong number of arguments (#{args.length}; must be at least 2)" diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index c74ba0198..1aa9ef335 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -15,7 +15,7 @@ module Puppet::Parser::Functions @return [Boolean] Boolean(0) # false for any zero or negative number Boolean(1) # true for any positive number - DOC + DOC ) do |arguments| raise(Puppet::ParseError, "num2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 diff --git a/lib/puppet/parser/functions/pick_default.rb b/lib/puppet/parser/functions/pick_default.rb index 0915df144..dd2326bb6 100644 --- a/lib/puppet/parser/functions/pick_default.rb +++ b/lib/puppet/parser/functions/pick_default.rb @@ -28,7 +28,7 @@ module Puppet::Parser::Functions Contrary to the pick() function, the pick_default does not fail if all arguments are empty. This allows pick_default to use an empty value as default. - DOC + DOC ) do |args| raise 'Must receive at least one argument.' if args.empty? default = args.last diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 04ed267d6..c7ad107a1 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -18,7 +18,7 @@ module Puppet::Parser::Functions ['a', 'b', 'c'].map |$x| { "p${x}" } @return [Hash] or [Array] The passed values now contains the passed prefix - DOC + DOC ) do |arguments| # Technically we support two arguments but only first is mandatory ... raise(Puppet::ParseError, "prefix(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? diff --git a/lib/puppet/parser/functions/pry.rb b/lib/puppet/parser/functions/pry.rb index 8fb7d2053..8bafe940f 100644 --- a/lib/puppet/parser/functions/pry.rb +++ b/lib/puppet/parser/functions/pry.rb @@ -16,7 +16,7 @@ module Puppet::Parser::Functions `pry()` - DOC + DOC ) do |arguments| begin require 'pry' diff --git a/lib/puppet/parser/functions/pw_hash.rb b/lib/puppet/parser/functions/pw_hash.rb index a99a3ea29..eb48e0dbe 100644 --- a/lib/puppet/parser/functions/pw_hash.rb +++ b/lib/puppet/parser/functions/pw_hash.rb @@ -35,7 +35,7 @@ > *Note:*: this uses the Puppet Server's implementation of crypt(3). If your environment contains several different operating systems, ensure that they are compatible before using this function. - DOC +DOC ) do |args| raise ArgumentError, "pw_hash(): wrong number of arguments (#{args.size} for 3)" if args.size != 3 args.map! do |arg| diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index 541094374..a0c2f627f 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -43,7 +43,7 @@ module Puppet::Parser::Functions the step() function in Puppet for skipping values. Integer[0, 9].each |$x| { notice($x) } # notices 0, 1, 2, ... 9 - DOC + DOC ) do |arguments| raise(Puppet::ParseError, 'range(): Wrong number of arguments given (0 for 1)') if arguments.empty? diff --git a/lib/puppet/parser/functions/regexpescape.rb b/lib/puppet/parser/functions/regexpescape.rb index c5dcf8cfe..0c125525f 100644 --- a/lib/puppet/parser/functions/regexpescape.rb +++ b/lib/puppet/parser/functions/regexpescape.rb @@ -10,7 +10,7 @@ module Puppet::Parser::Functions Requires either a single string or an array as an input. @return [String] A string of characters with metacharacters converted to their escaped form. - DOC + DOC ) do |arguments| raise(Puppet::ParseError, "regexpescape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? diff --git a/lib/puppet/parser/functions/reject.rb b/lib/puppet/parser/functions/reject.rb index 490249c53..ffa1fa8fa 100644 --- a/lib/puppet/parser/functions/reject.rb +++ b/lib/puppet/parser/functions/reject.rb @@ -21,7 +21,7 @@ module Puppet::Parser::Functions > *Note:* Since Puppet 4.0.0 the same is in general done with the filter function. Here is the equivalence of the reject() function: ['aaa','bbb','ccc','aaaddd'].filter |$x| { $x !~ /aaa/ } -DOC + DOC if args.size != 2 raise Puppet::ParseError, diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb index 38250bb09..075bce3f4 100644 --- a/lib/puppet/parser/functions/reverse.rb +++ b/lib/puppet/parser/functions/reverse.rb @@ -12,7 +12,7 @@ module Puppet::Parser::Functions reversed string or array > *Note:* that the same can be done with the reverse_each() function in Puppet. - DOC + DOC ) do |arguments| raise(Puppet::ParseError, "reverse(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? diff --git a/lib/puppet/parser/functions/str2saltedsha512.rb b/lib/puppet/parser/functions/str2saltedsha512.rb index a1a278bc7..b9dabaf9f 100644 --- a/lib/puppet/parser/functions/str2saltedsha512.rb +++ b/lib/puppet/parser/functions/str2saltedsha512.rb @@ -16,7 +16,7 @@ module Puppet::Parser::Functions Given any simple string, you will get a hex version of a salted-SHA512 password hash that can be inserted into your Puppet manifests as a valid password attribute. - DOC + DOC ) do |arguments| require 'digest/sha2' diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb index 5c519ba93..cae9ade74 100644 --- a/lib/puppet/parser/functions/suffix.rb +++ b/lib/puppet/parser/functions/suffix.rb @@ -22,7 +22,7 @@ module Puppet::Parser::Functions ```['a', 'b', 'c'].map |$x| { "${x}p" }``` - DOC + DOC ) do |arguments| # Technically we support two arguments but only first is mandatory ... raise(Puppet::ParseError, "suffix(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb index cddd289fe..347d709e8 100644 --- a/lib/puppet/parser/functions/swapcase.rb +++ b/lib/puppet/parser/functions/swapcase.rb @@ -16,7 +16,7 @@ module Puppet::Parser::Functions swapcase("aBcD") Would result in: "AbCd" - DOC + DOC ) do |arguments| raise(Puppet::ParseError, "swapcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb index e154643f3..da6261d04 100644 --- a/lib/puppet/parser/functions/time.rb +++ b/lib/puppet/parser/functions/time.rb @@ -22,7 +22,7 @@ module Puppet::Parser::Functions ```Timestamp()``` - DOC + DOC ) do |arguments| # The Time Zone argument is optional ... time_zone = arguments[0] if arguments[0] diff --git a/lib/puppet/parser/functions/to_bytes.rb b/lib/puppet/parser/functions/to_bytes.rb index b1beb5fe8..9b903db77 100644 --- a/lib/puppet/parser/functions/to_bytes.rb +++ b/lib/puppet/parser/functions/to_bytes.rb @@ -14,7 +14,7 @@ module Puppet::Parser::Functions Takes a single string value as an argument. These conversions reflect a layperson's understanding of 1 MB = 1024 KB, when in fact 1 MB = 1000 KB, and 1 MiB = 1024 KiB. - DOC + DOC ) do |arguments| raise(Puppet::ParseError, "to_bytes(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 diff --git a/lib/puppet/parser/functions/union.rb b/lib/puppet/parser/functions/union.rb index 353e51e8e..eb2c9f0ce 100644 --- a/lib/puppet/parser/functions/union.rb +++ b/lib/puppet/parser/functions/union.rb @@ -14,7 +14,7 @@ module Puppet::Parser::Functions union(["a","b","c"],["b","c","d"]) Would return: ["a","b","c","d"] - DOC + DOC ) do |arguments| # Check that 2 or more arguments have been given ... raise(Puppet::ParseError, "union(): Wrong number of arguments given (#{arguments.size} for < 2)") if arguments.size < 2 diff --git a/lib/puppet/parser/functions/unix2dos.rb b/lib/puppet/parser/functions/unix2dos.rb index 95d9dfc50..dfae1b607 100644 --- a/lib/puppet/parser/functions/unix2dos.rb +++ b/lib/puppet/parser/functions/unix2dos.rb @@ -10,7 +10,7 @@ module Puppet::Parser::Functions the DOS version of the given string. Takes a single string argument. - DOC + DOC ) do |arguments| unless arguments[0].is_a?(String) raise(Puppet::ParseError, 'unix2dos(): Requires string as argument') diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb index 3146d38ec..e14070f01 100644 --- a/lib/puppet/parser/functions/uriescape.rb +++ b/lib/puppet/parser/functions/uriescape.rb @@ -15,7 +15,7 @@ module Puppet::Parser::Functions @return [String] a string that contains the converted value - DOC + DOC ) do |arguments| raise(Puppet::ParseError, "uriescape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? diff --git a/lib/puppet/parser/functions/validate_augeas.rb b/lib/puppet/parser/functions/validate_augeas.rb index 55f932f29..cadd3e3c0 100644 --- a/lib/puppet/parser/functions/validate_augeas.rb +++ b/lib/puppet/parser/functions/validate_augeas.rb @@ -41,7 +41,7 @@ module Puppet::Parser::Functions validate_augeas($sudoerscontent, 'Sudoers.lns', [], 'Failed to validate sudoers content with Augeas') - DOC + DOC ) do |args| unless Puppet.features.augeas? raise Puppet::ParseError, 'validate_augeas(): this function requires the augeas feature. See http://docs.puppetlabs.com/guides/augeas.html#pre-requisites for how to activate it.' diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 16b4ab932..fb92913c7 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -32,7 +32,7 @@ module Puppet::Parser::Functions % as file location validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to validate config content') - DOC + DOC ) do |args| if (args.length < 2) || (args.length > 3) raise Puppet::ParseError, "validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)" diff --git a/lib/puppet/parser/functions/validate_x509_rsa_key_pair.rb b/lib/puppet/parser/functions/validate_x509_rsa_key_pair.rb index ee52a11bb..4261cc09d 100644 --- a/lib/puppet/parser/functions/validate_x509_rsa_key_pair.rb +++ b/lib/puppet/parser/functions/validate_x509_rsa_key_pair.rb @@ -17,7 +17,7 @@ module Puppet::Parser::Functions ```validate_x509_rsa_key_pair($cert, $key)``` - DOC + DOC ) do |args| require 'openssl' diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb index 29301b8e1..da5749fb7 100644 --- a/lib/puppet/parser/functions/values_at.rb +++ b/lib/puppet/parser/functions/values_at.rb @@ -37,7 +37,7 @@ module Puppet::Parser::Functions `['a', 'b', 'c', 'd'][2, -1]` results in `['c', 'd']` `['a', 'b', 'c', 'd'][1, -2]` results in `['b', 'c']` - DOC + DOC ) do |arguments| raise(Puppet::ParseError, "values_at(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2 diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb index 100618340..47941f626 100644 --- a/lib/puppet/parser/functions/zip.rb +++ b/lib/puppet/parser/functions/zip.rb @@ -14,7 +14,7 @@ module Puppet::Parser::Functions @example zip(['1','2','3'],['4','5','6']) Would result in: ["1", "4"], ["2", "5"], ["3", "6"] - DOC + DOC ) do |arguments| # Technically we support three arguments but only first is mandatory ... raise(Puppet::ParseError, "zip(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2 From 42a1f1a651c38e053c23291f22fcef0ee11e3f3d Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:16:22 +0100 Subject: [PATCH 16/33] (CONT-801) Autocorrect safe group 1 --- .rubocop_todo.yml | 39 ------------------- lib/puppet/functions/parsehocon.rb | 1 + lib/puppet/functions/parsepson.rb | 1 + lib/puppet/parser/functions/any2array.rb | 1 + lib/puppet/parser/functions/any2bool.rb | 9 +++-- lib/puppet/parser/functions/basename.rb | 1 + .../parser/functions/defined_with_params.rb | 1 + .../parser/functions/delete_undef_values.rb | 1 + lib/puppet/parser/functions/delete_values.rb | 1 + .../parser/functions/ensure_resource.rb | 1 + .../parser/functions/ensure_resources.rb | 2 + lib/puppet/parser/functions/fqdn_uuid.rb | 1 + .../parser/functions/get_module_path.rb | 2 + .../parser/functions/load_module_metadata.rb | 1 + lib/puppet/parser/functions/loadjson.rb | 2 + lib/puppet/parser/functions/loadyaml.rb | 2 + lib/puppet/parser/functions/merge.rb | 1 + lib/puppet/parser/functions/parsejson.rb | 1 + lib/puppet/parser/functions/parseyaml.rb | 2 + lib/puppet/parser/functions/pick.rb | 1 + lib/puppet/parser/functions/pick_default.rb | 1 + lib/puppet/parser/functions/pw_hash.rb | 18 +++++---- .../parser/functions/str2saltedpbkdf2.rb | 4 +- lib/puppet/provider/file_line/ruby.rb | 3 ++ lib/puppet/type/file_line.rb | 1 + spec/functions/get_module_path_spec.rb | 1 + spec/functions/str2saltedpbkdf2_spec.rb | 4 +- .../provider/file_line/ruby_spec_use_cases.rb | 1 - 28 files changed, 49 insertions(+), 55 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a541a0fab..0bf5b38bd 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,45 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyleAlignWith, Severity. -# SupportedStylesAlignWith: start_of_line, begin -Layout/BeginEndAlignment: - Exclude: - - 'lib/puppet/parser/functions/any2bool.rb' - -# Offense count: 33 -# This cop supports safe autocorrection (--autocorrect). -Layout/EmptyLineAfterGuardClause: - Enabled: false - -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Layout/EmptyLinesAroundArguments: - Exclude: - - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' - -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowAliasSyntax, AllowedMethods. -# AllowedMethods: alias_method, public, protected, private -Layout/EmptyLinesAroundAttributeAccessor: - Exclude: - - 'spec/functions/get_module_path_spec.rb' - -# Offense count: 11 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. -# SupportedHashRocketStyles: key, separator, table -# SupportedColonStyles: key, separator, table -# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit -Layout/HashAlignment: - Exclude: - - 'lib/puppet/parser/functions/pw_hash.rb' - - 'lib/puppet/parser/functions/str2saltedpbkdf2.rb' - - 'spec/functions/str2saltedpbkdf2_spec.rb' - # Offense count: 3 # This cop supports safe autocorrection (--autocorrect). Layout/HeredocIndentation: diff --git a/lib/puppet/functions/parsehocon.rb b/lib/puppet/functions/parsehocon.rb index be490ba24..82aefc2d4 100644 --- a/lib/puppet/functions/parsehocon.rb +++ b/lib/puppet/functions/parsehocon.rb @@ -25,6 +25,7 @@ def parsehocon(hocon_string, default = :no_default_provided) rescue Hocon::ConfigError::ConfigParseError => err Puppet.debug("Parsing hocon failed with error: #{err.message}") raise err if default == :no_default_provided + default end end diff --git a/lib/puppet/functions/parsepson.rb b/lib/puppet/functions/parsepson.rb index 08e4d8782..854dae510 100644 --- a/lib/puppet/functions/parsepson.rb +++ b/lib/puppet/functions/parsepson.rb @@ -24,6 +24,7 @@ def parsepson(pson_string, default = :no_default_provided) rescue StandardError => err Puppet.debug("Parsing PSON failed with error: #{err.message}") raise err if default == :no_default_provided + default end end diff --git a/lib/puppet/parser/functions/any2array.rb b/lib/puppet/parser/functions/any2array.rb index 107c11dce..08ca02bb9 100644 --- a/lib/puppet/parser/functions/any2array.rb +++ b/lib/puppet/parser/functions/any2array.rb @@ -43,6 +43,7 @@ module Puppet::Parser::Functions return arguments unless arguments.length == 1 return arguments[0] if arguments[0].is_a?(Array) return [] if arguments == [''] + if arguments[0].is_a?(Hash) result = [] arguments[0].each do |key, value| diff --git a/lib/puppet/parser/functions/any2bool.rb b/lib/puppet/parser/functions/any2bool.rb index 0215d19bc..58e1c2cf3 100644 --- a/lib/puppet/parser/functions/any2bool.rb +++ b/lib/puppet/parser/functions/any2bool.rb @@ -40,10 +40,10 @@ module Puppet::Parser::Functions end valid_float = begin - !!Float(arg) # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean - rescue - false - end + !!Float(arg) # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean + rescue + false + end if arg.is_a?(Numeric) return function_num2bool([arguments[0]]) @@ -51,6 +51,7 @@ module Puppet::Parser::Functions if arg.is_a?(String) return function_num2bool([arguments[0]]) if valid_float + return function_str2bool([arguments[0]]) end diff --git a/lib/puppet/parser/functions/basename.rb b/lib/puppet/parser/functions/basename.rb index 21aad1b93..b7aa80b38 100644 --- a/lib/puppet/parser/functions/basename.rb +++ b/lib/puppet/parser/functions/basename.rb @@ -18,6 +18,7 @@ module Puppet::Parser::Functions rv = File.basename(arguments[0]) if arguments.size == 1 if arguments.size == 2 raise(Puppet::ParseError, 'basename(): Requires string as second argument') unless arguments[1].is_a?(String) + rv = File.basename(arguments[0], arguments[1]) end diff --git a/lib/puppet/parser/functions/defined_with_params.rb b/lib/puppet/parser/functions/defined_with_params.rb index 8cfd9a123..14339f861 100644 --- a/lib/puppet/parser/functions/defined_with_params.rb +++ b/lib/puppet/parser/functions/defined_with_params.rb @@ -26,6 +26,7 @@ ) do |vals| reference, params = vals raise(ArgumentError, 'Must specify a reference') unless reference + if !params || params == '' params = {} end diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb index dbaab26e1..75ba298e0 100644 --- a/lib/puppet/parser/functions/delete_undef_values.rb +++ b/lib/puppet/parser/functions/delete_undef_values.rb @@ -31,6 +31,7 @@ module Puppet::Parser::Functions unless args[0].is_a?(Array) || args[0].is_a?(Hash) raise(Puppet::ParseError, "delete_undef_values(): expected an array or hash, got #{args[0]} type #{args[0].class} ") end + result = args[0].dup if result.is_a?(Hash) result.delete_if { |_, val| val.equal?(:undef) || val.nil? } diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb index b4b8a7355..e7e731fdd 100644 --- a/lib/puppet/parser/functions/delete_values.rb +++ b/lib/puppet/parser/functions/delete_values.rb @@ -29,6 +29,7 @@ module Puppet::Parser::Functions unless hash.is_a?(Hash) raise(TypeError, "delete_values(): First argument must be a Hash. Given an argument of class #{hash.class}.") end + hash.dup.delete_if { |_key, val| item == val } end end diff --git a/lib/puppet/parser/functions/ensure_resource.rb b/lib/puppet/parser/functions/ensure_resource.rb index 46b2c4637..48497ee54 100644 --- a/lib/puppet/parser/functions/ensure_resource.rb +++ b/lib/puppet/parser/functions/ensure_resource.rb @@ -35,6 +35,7 @@ type, title, params = vals raise(ArgumentError, 'Must specify a type') unless type raise(ArgumentError, 'Must specify a title') unless title + params ||= {} items = [title].flatten diff --git a/lib/puppet/parser/functions/ensure_resources.rb b/lib/puppet/parser/functions/ensure_resources.rb index d6a890667..a7fc9daca 100644 --- a/lib/puppet/parser/functions/ensure_resources.rb +++ b/lib/puppet/parser/functions/ensure_resources.rb @@ -38,9 +38,11 @@ type, title, params = vals raise(ArgumentError, 'Must specify a type') unless type raise(ArgumentError, 'Must specify a title') unless title + params ||= {} raise(Puppet::ParseError, 'ensure_resources(): Requires second argument to be a Hash') unless title.is_a?(Hash) + resource_hash = title.dup resources = resource_hash.keys diff --git a/lib/puppet/parser/functions/fqdn_uuid.rb b/lib/puppet/parser/functions/fqdn_uuid.rb index 4ce2bfed3..83e94a559 100644 --- a/lib/puppet/parser/functions/fqdn_uuid.rb +++ b/lib/puppet/parser/functions/fqdn_uuid.rb @@ -20,6 +20,7 @@ module Puppet::Parser::Functions raise(ArgumentError, 'fqdn_uuid: No arguments given') if args.empty? raise(ArgumentError, "fqdn_uuid: Too many arguments given (#{args.length})") unless args.length == 1 + fqdn = args[0] # Code lovingly taken from diff --git a/lib/puppet/parser/functions/get_module_path.rb b/lib/puppet/parser/functions/get_module_path.rb index 3595d5377..cebe550b2 100644 --- a/lib/puppet/parser/functions/get_module_path.rb +++ b/lib/puppet/parser/functions/get_module_path.rb @@ -24,8 +24,10 @@ module Puppet::Parser::Functions DOC ) do |args| raise(Puppet::ParseError, 'get_module_path(): Wrong number of arguments, expects one') unless args.size == 1 + module_path = Puppet::Module.find(args[0], compiler.environment.to_s) raise(Puppet::ParseError, "Could not find module #{args[0]} in environment #{compiler.environment}") unless module_path + module_path.path end end diff --git a/lib/puppet/parser/functions/load_module_metadata.rb b/lib/puppet/parser/functions/load_module_metadata.rb index 38ebfb9d1..9a44b6186 100644 --- a/lib/puppet/parser/functions/load_module_metadata.rb +++ b/lib/puppet/parser/functions/load_module_metadata.rb @@ -17,6 +17,7 @@ module Puppet::Parser::Functions DOC ) do |args| raise(Puppet::ParseError, 'load_module_metadata(): Wrong number of arguments, expects one or two') unless [1, 2].include?(args.size) + mod = args[0] allow_empty_metadata = args[1] module_path = function_get_module_path([mod]) diff --git a/lib/puppet/parser/functions/loadjson.rb b/lib/puppet/parser/functions/loadjson.rb index 4f13d6bb6..8ec37007a 100644 --- a/lib/puppet/parser/functions/loadjson.rb +++ b/lib/puppet/parser/functions/loadjson.rb @@ -25,6 +25,7 @@ module Puppet::Parser::Functions DOC raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1 + require 'open-uri' begin if args[0].start_with?('http://', 'https://') @@ -74,6 +75,7 @@ module Puppet::Parser::Functions end rescue StandardError => e raise e unless args[1] + args[1] end end diff --git a/lib/puppet/parser/functions/loadyaml.rb b/lib/puppet/parser/functions/loadyaml.rb index 70f8e5fe3..4f89a68a2 100644 --- a/lib/puppet/parser/functions/loadyaml.rb +++ b/lib/puppet/parser/functions/loadyaml.rb @@ -24,6 +24,7 @@ module Puppet::Parser::Functions DOC raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1 + require 'yaml' require 'open-uri' begin @@ -57,6 +58,7 @@ module Puppet::Parser::Functions end rescue StandardError => e raise e unless args[1] + args[1] end end diff --git a/lib/puppet/parser/functions/merge.rb b/lib/puppet/parser/functions/merge.rb index 9f7c1576c..e867f435b 100644 --- a/lib/puppet/parser/functions/merge.rb +++ b/lib/puppet/parser/functions/merge.rb @@ -34,6 +34,7 @@ module Puppet::Parser::Functions unless arg.is_a?(Hash) raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments" end + accumulator.merge!(arg) end # Return the fully merged hash diff --git a/lib/puppet/parser/functions/parsejson.rb b/lib/puppet/parser/functions/parsejson.rb index 8dd94e223..fa7c66f30 100644 --- a/lib/puppet/parser/functions/parsejson.rb +++ b/lib/puppet/parser/functions/parsejson.rb @@ -25,6 +25,7 @@ module Puppet::Parser::Functions Puppet::Util::Json.load(arguments[0]) || arguments[1] rescue StandardError => e raise e unless arguments[1] + arguments[1] end end diff --git a/lib/puppet/parser/functions/parseyaml.rb b/lib/puppet/parser/functions/parseyaml.rb index f80c293d2..e1036386a 100644 --- a/lib/puppet/parser/functions/parseyaml.rb +++ b/lib/puppet/parser/functions/parseyaml.rb @@ -18,6 +18,7 @@ module Puppet::Parser::Functions DOC ) do |arguments| raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless arguments.length >= 1 + require 'yaml' begin @@ -27,6 +28,7 @@ module Puppet::Parser::Functions # do not have Psych available. rescue StandardError, Psych::SyntaxError => e # rubocop:disable Lint/ShadowedException : See above raise e unless arguments[1] + arguments[1] end end diff --git a/lib/puppet/parser/functions/pick.rb b/lib/puppet/parser/functions/pick.rb index fc4ebb635..c344f7bbc 100644 --- a/lib/puppet/parser/functions/pick.rb +++ b/lib/puppet/parser/functions/pick.rb @@ -31,6 +31,7 @@ module Puppet::Parser::Functions args.delete(:undefined) args.delete('') raise Puppet::ParseError, 'pick(): must receive at least one non empty value' if args[0].to_s.empty? + return args[0] end end diff --git a/lib/puppet/parser/functions/pick_default.rb b/lib/puppet/parser/functions/pick_default.rb index dd2326bb6..f1d546493 100644 --- a/lib/puppet/parser/functions/pick_default.rb +++ b/lib/puppet/parser/functions/pick_default.rb @@ -31,6 +31,7 @@ module Puppet::Parser::Functions DOC ) do |args| raise 'Must receive at least one argument.' if args.empty? + default = args.last args = args[0..-2].compact args.delete(:undef) diff --git a/lib/puppet/parser/functions/pw_hash.rb b/lib/puppet/parser/functions/pw_hash.rb index eb48e0dbe..e0c542e4d 100644 --- a/lib/puppet/parser/functions/pw_hash.rb +++ b/lib/puppet/parser/functions/pw_hash.rb @@ -38,6 +38,7 @@ DOC ) do |args| raise ArgumentError, "pw_hash(): wrong number of arguments (#{args.size} for 3)" if args.size != 3 + args.map! do |arg| if (defined? Puppet::Pops::Types::PSensitiveType::Sensitive) && (arg.is_a? Puppet::Pops::Types::PSensitiveType::Sensitive) arg.unwrap @@ -47,21 +48,23 @@ end hashes = { - 'md5' => { prefix: '1' }, - 'sha-256' => { prefix: '5' }, - 'sha-512' => { prefix: '6' }, - 'bcrypt' => { prefix: '2b', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, - 'bcrypt-a' => { prefix: '2a', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, - 'bcrypt-x' => { prefix: '2x', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, - 'bcrypt-y' => { prefix: '2y', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, + 'md5' => { prefix: '1' }, + 'sha-256' => { prefix: '5' }, + 'sha-512' => { prefix: '6' }, + 'bcrypt' => { prefix: '2b', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, + 'bcrypt-a' => { prefix: '2a', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, + 'bcrypt-x' => { prefix: '2x', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, + 'bcrypt-y' => { prefix: '2y', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, } raise ArgumentError, 'pw_hash(): first argument must be a string' unless args[0].is_a?(String) || args[0].nil? raise ArgumentError, 'pw_hash(): second argument must be a string' unless args[1].is_a? String + hash_type = hashes[args[1].downcase] raise ArgumentError, "pw_hash(): #{args[1]} is not a valid hash type" if hash_type.nil? raise ArgumentError, 'pw_hash(): third argument must be a string' unless args[2].is_a? String raise ArgumentError, 'pw_hash(): third argument must not be empty' if args[2].empty? + salt_doc = hash_type.include?(:salt) ? "match #{hash_type[:salt]}" : 'be in the set [a-zA-Z0-9./]' salt_regex = hash_type.fetch(:salt, %r{\A[a-zA-Z0-9./]+\z}) raise ArgumentError, "pw_hash(): characters in salt must #{salt_doc}" unless salt_regex.match?(args[2]) @@ -77,6 +80,7 @@ # JRuby < 1.7.17 # MS Windows and other systems that don't support enhanced salts raise Puppet::ParseError, 'system does not support enhanced salts' unless RUBY_PLATFORM == 'java' + # puppetserver bundles Apache Commons Codec org.apache.commons.codec.digest.Crypt.crypt(password.to_java_bytes, salt) else diff --git a/lib/puppet/parser/functions/str2saltedpbkdf2.rb b/lib/puppet/parser/functions/str2saltedpbkdf2.rb index bbb92f99c..2b219f012 100644 --- a/lib/puppet/parser/functions/str2saltedpbkdf2.rb +++ b/lib/puppet/parser/functions/str2saltedpbkdf2.rb @@ -63,8 +63,8 @@ module Puppet::Parser::Functions { 'password_hex' => hash.unpack('H*').first, - 'salt_hex' => salt.unpack('H*').first, - 'iterations' => iterations, + 'salt_hex' => salt.unpack('H*').first, + 'iterations' => iterations, } end end diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index e84060303..3695af7b9 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -53,6 +53,7 @@ def exists? def create return if resource[:replace].to_s != 'true' && count_matches(new_match_regex) > 0 + if resource[:match] handle_create_with_match elsif resource[:after] @@ -85,6 +86,7 @@ def lines @lines ||= File.readlines(resource[:path]) rescue Errno::ENOENT raise unless resource.noop? + @lines ||= [] end @@ -119,6 +121,7 @@ def handle_create_with_match lines.each do |line| fh.puts(match_regex.match(line) ? resource[:line] : line) next unless match_count.zero? && after_regex + if after_regex.match(line) fh.puts(resource[:line]) match_count += 1 # Increment match_count to indicate that the new line has been inserted. diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 00a6512cb..7712f7be0 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -193,6 +193,7 @@ def retrieve if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:replace].to_s == 'false' raise(Puppet::Error, 'replace must be true when replace_all_matches_not_matching_line is true') end + unless self[:line] unless (self[:ensure].to_s == 'absent') && (self[:match_for_absence].to_s == 'true') && self[:match] raise(Puppet::Error, 'line is a required attribute') diff --git a/spec/functions/get_module_path_spec.rb b/spec/functions/get_module_path_spec.rb index 1d6d3be1f..b2d415dc1 100644 --- a/spec/functions/get_module_path_spec.rb +++ b/spec/functions/get_module_path_spec.rb @@ -12,6 +12,7 @@ # class Stubmodule class StubModule attr_reader :path + def initialize(path) @path = path end diff --git a/spec/functions/str2saltedpbkdf2_spec.rb b/spec/functions/str2saltedpbkdf2_spec.rb index c46df7a6d..94eb5b177 100644 --- a/spec/functions/str2saltedpbkdf2_spec.rb +++ b/spec/functions/str2saltedpbkdf2_spec.rb @@ -17,8 +17,8 @@ it { is_expected.to run.with_params('Pa55w0rd', 'Using s0m3 s@lt', 50_000) .and_return('password_hex' => '3577f79f7d2e73df1cf1eecc36da16fffcd3650126d79e797a8b227492d13de4cdd0656933b43118b7361692f755e5b3c1e0536f826d12442400f3467bcc8fb4ac2235d5648b0f1b0906d0712aecd265834319b5a42e98af2ced81597fd78d1ac916f6eff6122c3577bb120a9f534e2a5c9a58c7d1209e3914c967c6a467b594', - 'salt_hex' => '5573696e672073306d332073406c74', - 'iterations' => 50_000) + 'salt_hex' => '5573696e672073306d332073406c74', + 'iterations' => 50_000) } # rubocop:enable Layout/LineLength end diff --git a/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb b/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb index 456ddd614..da5f60df0 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb @@ -74,7 +74,6 @@ line: "*\thard\tcore\t0", match: "^[ \t]*\\*[ \t]+hard[ \t]+core[ \t]+.*", multiple: true, - }.merge(replace_all_matches_not_matching_line: true) end let(:content) { "* hard core 90\n* hard core 0\n" } From 710446975dce7e94b5abf69855dd72d190afccac Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:17:59 +0100 Subject: [PATCH 17/33] (CONT-801) Autocorrect safe group 2 --- .rubocop_todo.yml | 32 ---- lib/puppet/parser/functions/glob.rb | 4 +- .../parser/functions/str2saltedpbkdf2.rb | 2 +- .../parser/functions/str2saltedsha512.rb | 2 +- .../validate_x509_rsa_key_pair_spec.rb | 156 +++++++++--------- 5 files changed, 82 insertions(+), 114 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0bf5b38bd..fa23a2984 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,32 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 3 -# This cop supports safe autocorrection (--autocorrect). -Layout/HeredocIndentation: - Exclude: - - 'spec/functions/validate_x509_rsa_key_pair_spec.rb' - -# Offense count: 2 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, IndentationWidth. -# SupportedStyles: aligned, indented -Layout/LineEndStringConcatenationIndentation: - Exclude: - - 'lib/puppet/parser/functions/glob.rb' - -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Layout/RescueEnsureAlignment: - Exclude: - - 'lib/puppet/parser/functions/any2bool.rb' - -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Lint/AmbiguousOperatorPrecedence: - Exclude: - - 'lib/puppet/parser/functions/str2saltedsha512.rb' - # Offense count: 3 # This cop supports unsafe autocorrection (--autocorrect-all). Lint/BooleanSymbol: @@ -46,12 +20,6 @@ Lint/ConstantDefinitionInBlock: - 'spec/functions/get_module_path_spec.rb' - 'spec/unit/facter/util/puppet_settings_spec.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Lint/DeprecatedOpenSSLConstant: - Exclude: - - 'lib/puppet/parser/functions/str2saltedpbkdf2.rb' - # Offense count: 3 # Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches. Lint/DuplicateBranch: diff --git a/lib/puppet/parser/functions/glob.rb b/lib/puppet/parser/functions/glob.rb index 1ff3a9872..94bc77028 100644 --- a/lib/puppet/parser/functions/glob.rb +++ b/lib/puppet/parser/functions/glob.rb @@ -17,14 +17,14 @@ module Puppet::Parser::Functions ) do |arguments| unless arguments.size == 1 raise(Puppet::ParseError, 'glob(): Wrong number of arguments given ' \ - "(#{arguments.size} for 1)") + "(#{arguments.size} for 1)") end pattern = arguments[0] unless pattern.is_a?(String) || pattern.is_a?(Array) raise(Puppet::ParseError, 'glob(): Requires either array or string ' \ - 'to work') + 'to work') end Dir.glob(pattern) diff --git a/lib/puppet/parser/functions/str2saltedpbkdf2.rb b/lib/puppet/parser/functions/str2saltedpbkdf2.rb index 2b219f012..bb26eb585 100644 --- a/lib/puppet/parser/functions/str2saltedpbkdf2.rb +++ b/lib/puppet/parser/functions/str2saltedpbkdf2.rb @@ -58,7 +58,7 @@ module Puppet::Parser::Functions salt = args[1] iterations = args[2] keylen = 128 - digest = OpenSSL::Digest::SHA512.new + digest = OpenSSL::Digest.new('SHA512') hash = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, keylen, digest) { diff --git a/lib/puppet/parser/functions/str2saltedsha512.rb b/lib/puppet/parser/functions/str2saltedsha512.rb index b9dabaf9f..b8f7b929c 100644 --- a/lib/puppet/parser/functions/str2saltedsha512.rb +++ b/lib/puppet/parser/functions/str2saltedsha512.rb @@ -28,7 +28,7 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "str2saltedsha512(): Requires a String argument, you passed: #{password.class}") end - seedint = rand(2**31 - 1) + seedint = rand((2**31) - 1) seedstring = Array(seedint).pack('L') saltedpass = Digest::SHA512.digest(seedstring + password) (seedstring + saltedpass).unpack('H*')[0] diff --git a/spec/functions/validate_x509_rsa_key_pair_spec.rb b/spec/functions/validate_x509_rsa_key_pair_spec.rb index 08a24234e..2d19d9a33 100644 --- a/spec/functions/validate_x509_rsa_key_pair_spec.rb +++ b/spec/functions/validate_x509_rsa_key_pair_spec.rb @@ -4,90 +4,90 @@ describe 'validate_x509_rsa_key_pair' do let(:valid_cert) do - < Date: Wed, 17 May 2023 11:21:25 +0100 Subject: [PATCH 18/33] (CONT-801) Autocorrect safe group 3 --- .rubocop_todo.yml | 43 ------------------- lib/facter/util/puppet_settings.rb | 2 +- lib/puppet/functions/parsehocon.rb | 6 +-- lib/puppet/functions/parsepson.rb | 6 +-- lib/puppet/parser/functions/loadjson.rb | 8 ++-- lib/puppet/parser/functions/loadyaml.rb | 4 +- lib/puppet/parser/functions/member.rb | 2 +- lib/puppet/parser/functions/num2bool.rb | 8 ++-- lib/puppet/parser/functions/validate_cmd.rb | 10 ++--- spec/functions/seeded_rand_string_spec.rb | 2 +- spec/functions/type_of_spec.rb | 4 +- .../validate_x509_rsa_key_pair_spec.rb | 2 - spec/unit/facter/root_home_spec.rb | 2 +- 13 files changed, 27 insertions(+), 72 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fa23a2984..edfeec7e7 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -34,12 +34,6 @@ Lint/MissingCopEnableDirective: - 'spec/functions/merge_spec.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Lint/RedundantCopEnableDirective: - Exclude: - - 'spec/functions/validate_x509_rsa_key_pair_spec.rb' - # Offense count: 2 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowedMethods. @@ -97,18 +91,6 @@ Naming/MethodParameterName: Exclude: - 'spec/functions/pick_default_spec.rb' -# Offense count: 9 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: PreferredName. -Naming/RescuedExceptionsVariableName: - Exclude: - - 'lib/puppet/functions/parsehocon.rb' - - 'lib/puppet/functions/parsepson.rb' - - 'lib/puppet/parser/functions/loadjson.rb' - - 'lib/puppet/parser/functions/loadyaml.rb' - - 'lib/puppet/parser/functions/num2bool.rb' - - 'lib/puppet/parser/functions/validate_cmd.rb' - # Offense count: 1 # Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns. # SupportedStyles: snake_case, normalcase, non_integer @@ -136,14 +118,6 @@ Performance/RedundantEqualityComparisonBlock: Exclude: - 'lib/puppet/parser/functions/bool2str.rb' -# Offense count: 3 -# This cop supports safe autocorrection (--autocorrect). -Performance/StringIdentifierArgument: - Exclude: - - 'lib/facter/util/puppet_settings.rb' - - 'lib/puppet/parser/functions/member.rb' - - 'lib/puppet/parser/functions/validate_cmd.rb' - # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). Performance/UnfreezeString: @@ -155,23 +129,6 @@ Performance/UnfreezeString: RSpec/BeEq: Enabled: false -# Offense count: 2 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: be, be_nil -RSpec/BeNil: - Exclude: - - 'spec/functions/seeded_rand_string_spec.rb' - - 'spec/unit/facter/root_home_spec.rb' - -# Offense count: 2 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: be_a, be_kind_of -RSpec/ClassCheck: - Exclude: - - 'spec/functions/type_of_spec.rb' - # Offense count: 36 # Configuration parameters: Prefixes, AllowedPatterns. # Prefixes: when, with, without diff --git a/lib/facter/util/puppet_settings.rb b/lib/facter/util/puppet_settings.rb index df2ed5963..d808f264c 100644 --- a/lib/facter/util/puppet_settings.rb +++ b/lib/facter/util/puppet_settings.rb @@ -9,7 +9,7 @@ module Facter::Util::PuppetSettings # facter without the --puppet flag and they happen to be working in a lib # directory of a module. def self.with_puppet - Module.const_get('Puppet') + Module.const_get(:Puppet) rescue NameError nil else diff --git a/lib/puppet/functions/parsehocon.rb b/lib/puppet/functions/parsehocon.rb index 82aefc2d4..61902a640 100644 --- a/lib/puppet/functions/parsehocon.rb +++ b/lib/puppet/functions/parsehocon.rb @@ -22,9 +22,9 @@ def parsehocon(hocon_string, default = :no_default_provided) begin data = Hocon::ConfigFactory.parse_string(hocon_string) data.resolve.root.unwrapped - rescue Hocon::ConfigError::ConfigParseError => err - Puppet.debug("Parsing hocon failed with error: #{err.message}") - raise err if default == :no_default_provided + rescue Hocon::ConfigError::ConfigParseError => e + Puppet.debug("Parsing hocon failed with error: #{e.message}") + raise e if default == :no_default_provided default end diff --git a/lib/puppet/functions/parsepson.rb b/lib/puppet/functions/parsepson.rb index 854dae510..41f56c563 100644 --- a/lib/puppet/functions/parsepson.rb +++ b/lib/puppet/functions/parsepson.rb @@ -21,9 +21,9 @@ def parsepson(pson_string, default = :no_default_provided) PSON.load(pson_string) - rescue StandardError => err - Puppet.debug("Parsing PSON failed with error: #{err.message}") - raise err if default == :no_default_provided + rescue StandardError => e + Puppet.debug("Parsing PSON failed with error: #{e.message}") + raise e if default == :no_default_provided default end diff --git a/lib/puppet/parser/functions/loadjson.rb b/lib/puppet/parser/functions/loadjson.rb index 8ec37007a..1c908799d 100644 --- a/lib/puppet/parser/functions/loadjson.rb +++ b/lib/puppet/parser/functions/loadjson.rb @@ -46,8 +46,8 @@ module Puppet::Parser::Functions if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? begin contents = OpenURI.open_uri(url, **http_options) - rescue OpenURI::HTTPError => err - res = err.io + rescue OpenURI::HTTPError => e + res = e.io warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'") args[1] end @@ -55,8 +55,8 @@ module Puppet::Parser::Functions else begin contents = URI.open(url, **http_options) # rubocop:disable Security/Open : Temporarily disabling this cop. This is a security risk and must be addressed before release. - rescue URI::Error => err - res = err.io + rescue URI::Error => e + res = e.io warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'") args[1] end diff --git a/lib/puppet/parser/functions/loadyaml.rb b/lib/puppet/parser/functions/loadyaml.rb index 4f89a68a2..1055cb830 100644 --- a/lib/puppet/parser/functions/loadyaml.rb +++ b/lib/puppet/parser/functions/loadyaml.rb @@ -44,8 +44,8 @@ module Puppet::Parser::Functions end begin contents = OpenURI.open_uri(url, http_basic_authentication: [username, password]) - rescue OpenURI::HTTPError => err - res = err.io + rescue OpenURI::HTTPError => e + res = e.io warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'") args[1] end diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index 1ecade2b1..c7372a04f 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -55,7 +55,7 @@ module Puppet::Parser::Functions arguments[1] end - raise(Puppet::ParseError, 'member(): You must provide item to search for within array given') if item.respond_to?('empty?') && item.empty? + raise(Puppet::ParseError, 'member(): You must provide item to search for within array given') if item.respond_to?(:empty?) && item.empty? result = (item - array).empty? diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 1aa9ef335..f856c1294 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -27,14 +27,14 @@ module Puppet::Parser::Functions when String begin number = Float(number) - rescue ArgumentError => ex - raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}") + rescue ArgumentError => e + raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{e.message}") end else begin number = number.to_s - rescue NoMethodError => ex - raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}") + rescue NoMethodError => e + raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{e.message}") end end diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index fb92913c7..312d7d7f9 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -55,16 +55,16 @@ module Puppet::Parser::Functions "#{checkscript} #{tmpfile.path}" end - if Puppet::Util::Execution.respond_to?('execute') + if Puppet::Util::Execution.respond_to?(:execute) Puppet::Util::Execution.execute(check_with_correct_location) else Puppet::Util.execute(check_with_correct_location) end - rescue Puppet::ExecutionFailure => detail - msg += "\n#{detail}" + rescue Puppet::ExecutionFailure => e + msg += "\n#{e}" raise Puppet::ParseError, msg - rescue StandardError => detail - msg += "\n#{detail.class.name} #{detail}" + rescue StandardError => e + msg += "\n#{e.class.name} #{e}" raise Puppet::ParseError, msg ensure tmpfile.unlink diff --git a/spec/functions/seeded_rand_string_spec.rb b/spec/functions/seeded_rand_string_spec.rb index 66a5a16b9..487f316f0 100644 --- a/spec/functions/seeded_rand_string_spec.rb +++ b/spec/functions/seeded_rand_string_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'seeded_rand_string' do - it { is_expected.not_to be(nil) } + it { is_expected.not_to be_nil } # Test for erroneous params it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects between.+got none}i) } diff --git a/spec/functions/type_of_spec.rb b/spec/functions/type_of_spec.rb index 243b17355..f1131c9fb 100644 --- a/spec/functions/type_of_spec.rb +++ b/spec/functions/type_of_spec.rb @@ -17,11 +17,11 @@ it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) } it 'gives the type of a string' do - expect(subject.call({}, 'hello world')).to be_kind_of(Puppet::Pops::Types::PStringType) + expect(subject.call({}, 'hello world')).to be_a(Puppet::Pops::Types::PStringType) end it 'gives the type of an integer' do - expect(subject.call({}, 5)).to be_kind_of(Puppet::Pops::Types::PIntegerType) + expect(subject.call({}, 5)).to be_a(Puppet::Pops::Types::PIntegerType) end end end diff --git a/spec/functions/validate_x509_rsa_key_pair_spec.rb b/spec/functions/validate_x509_rsa_key_pair_spec.rb index 2d19d9a33..ff7be1f2b 100644 --- a/spec/functions/validate_x509_rsa_key_pair_spec.rb +++ b/spec/functions/validate_x509_rsa_key_pair_spec.rb @@ -89,8 +89,6 @@ -----END RSA PRIVATE KEY----- DOC end - # rubocop:enable Layout/IndentHeredoc - let(:valid_cert_but_indented) do valid_cert.gsub(%r{^}, ' ') end diff --git a/spec/unit/facter/root_home_spec.rb b/spec/unit/facter/root_home_spec.rb index 4b9648d6c..194abe361 100644 --- a/spec/unit/facter/root_home_spec.rb +++ b/spec/unit/facter/root_home_spec.rb @@ -10,7 +10,7 @@ after(:each) { Facter.clear } context 'when Windows', if: Facter.value(:kernel) == 'Windows' do - it { expect(subject.value).to be(nil) } + it { expect(subject.value).to be_nil } end context 'when non-Windows', if: Facter.value(:kernel) != 'Windows' do From 76b40d37d9a5e7ed8b9fe87663b993f719093e81 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:23:37 +0100 Subject: [PATCH 19/33] (CONT-801) Correct RSpec/EmptyLineAfterExample --- .rubocop_todo.yml | 6 ------ spec/functions/base64_spec.rb | 8 ++++++++ spec/functions/batch_escape_spec.rb | 1 + spec/functions/bool2str_spec.rb | 1 + spec/functions/count_spec.rb | 2 ++ spec/functions/deep_merge_spec.rb | 3 +++ spec/functions/defined_with_params_spec.rb | 1 + spec/functions/delete_at_spec.rb | 1 + spec/functions/delete_regex_spec.rb | 3 +++ spec/functions/delete_spec.rb | 5 +++++ spec/functions/delete_undef_values_spec.rb | 1 + spec/functions/delete_values_spec.rb | 5 +++++ spec/functions/dos2unix_spec.rb | 5 +++++ spec/functions/end_with_spec.rb | 1 + spec/functions/ensure_resource_spec.rb | 1 + spec/functions/grep_spec.rb | 3 +++ spec/functions/member_spec.rb | 2 ++ spec/functions/merge_spec.rb | 10 ++++++++++ spec/functions/powershell_escape_spec.rb | 1 + spec/functions/prefix_spec.rb | 3 +++ spec/functions/pw_hash_spec.rb | 2 ++ spec/functions/range_spec.rb | 14 ++++++++++++++ spec/functions/regexpescape_spec.rb | 2 ++ spec/functions/reject_spec.rb | 2 ++ spec/functions/reverse_spec.rb | 2 ++ spec/functions/seeded_rand_string_spec.rb | 1 + spec/functions/shell_escape_spec.rb | 1 + spec/functions/shell_join_spec.rb | 1 + spec/functions/shell_split_spec.rb | 2 ++ spec/functions/shuffle_spec.rb | 2 ++ spec/functions/squeeze_spec.rb | 2 ++ spec/functions/str2bool_spec.rb | 2 ++ spec/functions/str2saltedsha512_spec.rb | 4 ++++ spec/functions/suffix_spec.rb | 3 +++ spec/functions/swapcase_spec.rb | 4 ++++ spec/functions/to_json_pretty_spec.rb | 2 ++ spec/functions/to_json_spec.rb | 1 + spec/functions/to_python_spec.rb | 1 + spec/functions/to_ruby_spec.rb | 1 + spec/functions/to_toml_spec.rb | 1 + spec/functions/to_yaml_spec.rb | 1 + spec/functions/unix2dos_spec.rb | 5 +++++ spec/functions/uriescape_spec.rb | 2 ++ spec/functions/validate_cmd_spec.rb | 5 +++++ spec/functions/values_at_spec.rb | 3 +++ spec/functions/zip_spec.rb | 2 ++ spec/unit/facter/util/puppet_settings_spec.rb | 2 ++ spec/unit/puppet/provider/file_line/ruby_spec.rb | 8 ++++++++ .../puppet/provider/file_line/ruby_spec_alter.rb | 8 ++++++++ .../provider/file_line/ruby_spec_use_cases.rb | 3 +++ spec/unit/puppet/type/file_line_spec.rb | 15 +++++++++++++++ 51 files changed, 161 insertions(+), 6 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index edfeec7e7..a8bdfdc69 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -140,12 +140,6 @@ RSpec/ContextWording: RSpec/DescribeClass: Enabled: false -# Offense count: 161 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowConsecutiveOneLiners. -RSpec/EmptyLineAfterExample: - Enabled: false - # Offense count: 25 # This cop supports safe autocorrection (--autocorrect). RSpec/EmptyLineAfterExampleGroup: diff --git a/spec/functions/base64_spec.rb b/spec/functions/base64_spec.rb index 3a1081190..a79c5c5cc 100644 --- a/spec/functions/base64_spec.rb +++ b/spec/functions/base64_spec.rb @@ -27,34 +27,42 @@ is_expected.to run.with_params('encode', 'a very long string that will cause the base64 encoder to produce output with multiple lines') .and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") } + it { is_expected.to run.with_params('decode', "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') } + it { is_expected.to run.with_params('decode', 'YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==') .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') } + it { is_expected.to run.with_params('encode', 'a very long string that will cause the base64 encoder to produce output with multiple lines', 'strict') .and_return('YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==') } + it { is_expected.to run.with_params('decode', 'YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==', 'strict') .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') } + it { is_expected.to run.with_params('encode', 'https://www.google.com.tw/?gws_rd=ssl#q=hello+world', 'urlsafe') .and_return('aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk') } + it { is_expected.to run.with_params('decode', 'aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk', 'urlsafe') .and_return('https://www.google.com.tw/?gws_rd=ssl#q=hello+world') } + it { is_expected.to run.with_params('encode', 'https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add', 'urlsafe') .and_return('aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=') } + it { is_expected.to run.with_params('decode', 'aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=', 'urlsafe') .and_return('https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add') diff --git a/spec/functions/batch_escape_spec.rb b/spec/functions/batch_escape_spec.rb index 9feca261f..151fa6611 100644 --- a/spec/functions/batch_escape_spec.rb +++ b/spec/functions/batch_escape_spec.rb @@ -18,6 +18,7 @@ describe 'escaping' do it { is_expected.to run.with_params('foo').and_return('"foo"') } it { is_expected.to run.with_params('foo bar').and_return('"foo bar"') } + it { is_expected.to run.with_params('~`!@#$%^&*()_-=[]\{}|;\':",./<>?') .and_return('"~`!@#\\$%^&*()_-=[]\\\{}|;\':"",./<>?"') diff --git a/spec/functions/bool2str_spec.rb b/spec/functions/bool2str_spec.rb index 013d5514f..9bf618653 100644 --- a/spec/functions/bool2str_spec.rb +++ b/spec/functions/bool2str_spec.rb @@ -5,6 +5,7 @@ describe 'bool2str' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + ['true', 'false', nil, :undef, ''].each do |invalid| it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError) } end diff --git a/spec/functions/count_spec.rb b/spec/functions/count_spec.rb index 564974a23..02937afa9 100644 --- a/spec/functions/count_spec.rb +++ b/spec/functions/count_spec.rb @@ -7,10 +7,12 @@ it { is_expected.to run.with_params.and_raise_error(ArgumentError) } it { is_expected.to run.with_params('one').and_raise_error(ArgumentError) } it { is_expected.to run.with_params('one', 'two').and_return(1) } + it { pending('should actually be like this, and not like above') is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(ArgumentError) } it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(3) } it { is_expected.to run.with_params(['one', 'two', 'two'], 'two').and_return(2) } diff --git a/spec/functions/deep_merge_spec.rb b/spec/functions/deep_merge_spec.rb index bd40d6720..7ed8f3f08 100644 --- a/spec/functions/deep_merge_spec.rb +++ b/spec/functions/deep_merge_spec.rb @@ -20,6 +20,7 @@ .with_params({ 'key1' => 'value1', 'key2' => 'value2' }, 'key2' => 'replacement_value', 'key3' => 'value3') \ .and_return('key1' => 'value1', 'key2' => 'replacement_value', 'key3' => 'value3') end + it { is_expected.to run \ .with_params({ 'key1' => 'value1' }, { 'key1' => 'value2' }, 'key1' => 'value3') \ @@ -33,11 +34,13 @@ .with_params({ 'key1' => 'value1' }, 'key2' => 'value2', 'key3' => { 'subkey1' => 'value4' }) \ .and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => { 'subkey1' => 'value4' }) } + it { is_expected.to run \ .with_params({ 'key1' => { 'subkey1' => 'value1' } }, 'key1' => { 'subkey2' => 'value2' }) \ .and_return('key1' => { 'subkey1' => 'value1', 'subkey2' => 'value2' }) } + it { is_expected.to run \ .with_params({ 'key1' => { 'subkey1' => { 'subsubkey1' => 'value1' } } }, 'key1' => { 'subkey1' => { 'subsubkey1' => 'value2' } }) \ diff --git a/spec/functions/defined_with_params_spec.rb b/spec/functions/defined_with_params_spec.rb index e372cd319..e9afe8744 100644 --- a/spec/functions/defined_with_params_spec.rb +++ b/spec/functions/defined_with_params_spec.rb @@ -88,6 +88,7 @@ it { is_expected.to run.with_params('Test::Deftype[foo]', {}).and_return(true) } it { is_expected.to run.with_params('Test::Deftype[bar]', {}).and_return(false) } it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[foo]'), {}).and_return(true) } + it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false) diff --git a/spec/functions/delete_at_spec.rb b/spec/functions/delete_at_spec.rb index 7bc714044..e69b8b530 100644 --- a/spec/functions/delete_at_spec.rb +++ b/spec/functions/delete_at_spec.rb @@ -8,6 +8,7 @@ it { is_expected.to run.with_params('one', 1).and_raise_error(Puppet::ParseError, %r{Requires array}) } it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError, %r{Requires array}) } it { is_expected.to run.with_params(['one'], 'two').and_raise_error(Puppet::ParseError, %r{You must provide non-negative numeric}) } + it { pending('Current implementation ignores parameters after the first two.') is_expected.to run.with_params(['one'], 0, 1).and_raise_error(Puppet::ParseError) diff --git a/spec/functions/delete_regex_spec.rb b/spec/functions/delete_regex_spec.rb index ab9516f1b..671de9eba 100644 --- a/spec/functions/delete_regex_spec.rb +++ b/spec/functions/delete_regex_spec.rb @@ -30,11 +30,13 @@ it { is_expected.to run.with_params({}, '').and_return({}) } it { is_expected.to run.with_params({}, 'key').and_return({}) } it { is_expected.to run.with_params({ 'key' => 'value' }, 'key').and_return({}) } + it { is_expected.to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'key2') \ .and_return('key1' => 'value1', 'key3' => 'value3') } + it { is_expected.to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, ['key1', 'key2']) \ @@ -48,6 +50,7 @@ subject.execute(argument1, 'two') expect(argument1).to eq(original1) end + it 'leaves the original hash intact' do argument1 = { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' } original1 = argument1.dup diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb index 5fe782513..9fec33740 100644 --- a/spec/functions/delete_spec.rb +++ b/spec/functions/delete_spec.rb @@ -43,16 +43,19 @@ it { is_expected.to run.with_params({}, '').and_return({}) } it { is_expected.to run.with_params({}, 'key').and_return({}) } it { is_expected.to run.with_params({ 'key' => 'value' }, 'key').and_return({}) } + it { is_expected.to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'key2') \ .and_return('key1' => 'value1', 'key3' => 'value3') } + it { is_expected.to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, ['key1', 'key2']) \ .and_return('key3' => 'value3') } + it { is_expected.to run \ .with_params({ 'ĸəұ1' => 'νãŀủĕ1', 'ĸəұ2' => 'νãŀủĕ2', 'ĸəұ3' => 'νãŀủĕ3' }, ['ĸəұ1', 'ĸəұ2']) \ @@ -66,12 +69,14 @@ _result = subject.execute(argument1, 'two') expect(argument1).to eq(original1) end + it 'leaves the original string intact' do argument1 = 'onetwothree' original1 = argument1.dup _result = subject.execute(argument1, 'two') expect(argument1).to eq(original1) end + it 'leaves the original hash intact' do argument1 = { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' } original1 = argument1.dup diff --git a/spec/functions/delete_undef_values_spec.rb b/spec/functions/delete_undef_values_spec.rb index 068217f61..3fa643c40 100644 --- a/spec/functions/delete_undef_values_spec.rb +++ b/spec/functions/delete_undef_values_spec.rb @@ -43,6 +43,7 @@ pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == :undef && is_puppet_6 end it { is_expected.to run.with_params('key' => undef_value).and_return({}) } + it { is_expected.to run \ .with_params('key1' => 'value1', 'undef_key' => undef_value, 'key2' => 'value2') \ diff --git a/spec/functions/delete_values_spec.rb b/spec/functions/delete_values_spec.rb index 4a488934a..89c37b24b 100644 --- a/spec/functions/delete_values_spec.rb +++ b/spec/functions/delete_values_spec.rb @@ -8,6 +8,7 @@ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } + describe 'when the first argument is not a hash' do it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError, %r{First argument must be a Hash}) } it { is_expected.to run.with_params('one', 'two').and_raise_error(TypeError, %r{First argument must be a Hash}) } @@ -16,21 +17,25 @@ describe 'when deleting from a hash' do it { is_expected.to run.with_params({}, 'value').and_return({}) } + it { is_expected.to run \ .with_params({ 'key1' => 'value1' }, 'non-existing value') \ .and_return('key1' => 'value1') } + it { is_expected.to run \ .with_params({ 'ҝếỵ1 ' => 'νâĺūẹ1', 'ҝếỵ2' => 'value to delete' }, 'value to delete') \ .and_return('ҝếỵ1 ' => 'νâĺūẹ1') } + it { is_expected.to run \ .with_params({ 'key1' => 'value1', 'key2' => 'νǎŀữ℮ ťớ đêłểťė' }, 'νǎŀữ℮ ťớ đêłểťė') \ .and_return('key1' => 'value1') } + it { is_expected.to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value to delete', 'key3' => 'value to delete' }, 'value to delete') \ diff --git a/spec/functions/dos2unix_spec.rb b/spec/functions/dos2unix_spec.rb index d86d93ba8..5cd696cbb 100644 --- a/spec/functions/dos2unix_spec.rb +++ b/spec/functions/dos2unix_spec.rb @@ -5,18 +5,23 @@ describe 'dos2unix' do context 'when checking parameter validity' do it { is_expected.not_to eq(nil) } + it do is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) end + it do is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{Wrong number of arguments}) end + it do is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{Requires string as argument}) end + it do is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires string as argument}) end + it do is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires string as argument}) end diff --git a/spec/functions/end_with_spec.rb b/spec/functions/end_with_spec.rb index 19c4ed68a..89082c4bd 100644 --- a/spec/functions/end_with_spec.rb +++ b/spec/functions/end_with_spec.rb @@ -7,6 +7,7 @@ it { is_expected.to run.with_params('foobar', 'bar').and_return(true) } it { is_expected.to run.with_params('foobar', 'foo').and_return(false) } it { is_expected.to run.with_params('foobar', ['foo', 'baz']).and_return(false) } + it do is_expected.to run.with_params('foobar', '').and_raise_error( ArgumentError, %r{'stdlib::end_with' parameter 'suffixes' expects a value of type String\[1\] or Array\[String\[1\], 1\]} diff --git a/spec/functions/ensure_resource_spec.rb b/spec/functions/ensure_resource_spec.rb index b7266f75b..3ec6dff7a 100644 --- a/spec/functions/ensure_resource_spec.rb +++ b/spec/functions/ensure_resource_spec.rb @@ -6,6 +6,7 @@ it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Must specify a type}) } it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, %r{Must specify a title}) } + if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0 it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(ArgumentError) } else diff --git a/spec/functions/grep_spec.rb b/spec/functions/grep_spec.rb index b428d2daa..a0ada3606 100644 --- a/spec/functions/grep_spec.rb +++ b/spec/functions/grep_spec.rb @@ -6,14 +6,17 @@ it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('grep does not actually check this, and raises NoMethodError instead') is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) } + it { pending('grep does not actually check this, and raises NoMethodError instead') is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([], 'two').and_return([]) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['two']) } diff --git a/spec/functions/member_spec.rb b/spec/functions/member_spec.rb index f4a5bc216..f5248de3f 100644 --- a/spec/functions/member_spec.rb +++ b/spec/functions/member_spec.rb @@ -6,10 +6,12 @@ it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the first.') is_expected.to run.with_params([], [], []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([], '').and_return(false) } it { is_expected.to run.with_params([], ['']).and_return(false) } it { is_expected.to run.with_params([''], '').and_return(true) } diff --git a/spec/functions/merge_spec.rb b/spec/functions/merge_spec.rb index 2a5747fc2..f0edabf7f 100644 --- a/spec/functions/merge_spec.rb +++ b/spec/functions/merge_spec.rb @@ -4,6 +4,7 @@ describe 'merge' do it { is_expected.not_to eq(nil) } + it { is_expected.to run \ .with_params({}, 'two') \ @@ -12,11 +13,13 @@ Regexp.new(Regexp.escape("rejected: parameter 'args' expects a value of type Undef, Hash[Scalar, Any], or String[0, 0], got String")), ) } + it { is_expected.to run \ .with_params({}, 1) \ .and_raise_error(ArgumentError, %r{parameter 'args' expects a value of type Undef, Hash, or String, got Integer}) } + it { is_expected.to run \ .with_params({ 'one' => 1, 'three' => { 'four' => 4 } }, 'two' => 'dos', 'three' => { 'five' => 5 }) \ @@ -35,11 +38,13 @@ it { is_expected.to run.with_params({ 'key' => 'value' }, {}).and_return('key' => 'value') } it { is_expected.to run.with_params({}, 'key' => 'value').and_return('key' => 'value') } it { is_expected.to run.with_params({ 'key' => 'value1' }, 'key' => 'value2').and_return('key' => 'value2') } + it { is_expected.to run \ .with_params({ 'key1' => 'value1' }, { 'key2' => 'value2' }, 'key3' => 'value3') \ .and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3') } + describe 'should accept iterable and merge produced hashes' do it { is_expected.to run \ @@ -47,12 +52,14 @@ .with_lambda { |_hsh, val| { val => val } } \ .and_return(1 => 1, 2 => 2, 3 => 3) } + it { is_expected.to run \ .with_params([1, 2, 3]) \ .with_lambda { |_hsh, val| { val => val } unless val == 2 } \ .and_return(1 => 1, 3 => 3) } + it { is_expected.to run \ .with_params([1, 2, 3]) \ @@ -60,18 +67,21 @@ .with_lambda { |_hsh, val| raise StopIteration if val == 3; { val => val } } \ .and_return(1 => 1, 2 => 2) } + it { is_expected.to run \ .with_params(['a', 'b', 'b', 'c', 'b']) \ .with_lambda { |hsh, val| { val => (hsh[val] || 0) + 1 } } \ .and_return('a' => 1, 'b' => 3, 'c' => 1) } + it { is_expected.to run \ .with_params(['a', 'b', 'c']) \ .with_lambda { |_hsh, idx, val| { idx => val } } \ .and_return(0 => 'a', 1 => 'b', 2 => 'c') } + it { is_expected.to run \ .with_params('a' => 'A', 'b' => 'B', 'c' => 'C') \ diff --git a/spec/functions/powershell_escape_spec.rb b/spec/functions/powershell_escape_spec.rb index 20c772888..8f0502251 100644 --- a/spec/functions/powershell_escape_spec.rb +++ b/spec/functions/powershell_escape_spec.rb @@ -18,6 +18,7 @@ describe 'escaping' do it { is_expected.to run.with_params('foo').and_return('foo') } it { is_expected.to run.with_params('foo bar').and_return('foo` bar') } + it { is_expected.to run.with_params('~`!@#$%^&*()_-=[]\{}|;\':",./<>?') .and_return('~``!@#`$%^&*()_-=[]\{}`|;`\':\\`",./<>?') diff --git a/spec/functions/prefix_spec.rb b/spec/functions/prefix_spec.rb index 7353857d7..db6b2b91b 100644 --- a/spec/functions/prefix_spec.rb +++ b/spec/functions/prefix_spec.rb @@ -5,10 +5,12 @@ describe 'prefix' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the second.') is_expected.to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{expected first argument to be an Array or a Hash}) } it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, %r{expected second argument to be a String}) } it { is_expected.to run.with_params([]).and_return([]) } @@ -23,6 +25,7 @@ it { is_expected.to run.with_params({}, '').and_return({}) } it { is_expected.to run.with_params({ 'key' => 'value' }, '').and_return('key' => 'value') } it { is_expected.to run.with_params({ 'key' => 'value' }, 'pre').and_return('prekey' => 'value') } + it { is_expected.to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'pre') \ diff --git a/spec/functions/pw_hash_spec.rb b/spec/functions/pw_hash_spec.rb index 595f136eb..7af868d35 100644 --- a/spec/functions/pw_hash_spec.rb +++ b/spec/functions/pw_hash_spec.rb @@ -101,10 +101,12 @@ if Puppet::Util::Package.versioncmp(Puppet.version, '4.7.0') >= 0 describe 'when arguments are sensitive' do it { is_expected.to run.with_params(Puppet::Pops::Types::PSensitiveType::Sensitive.new('password'), 'md5', 'salt').and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') } + it { is_expected.to run.with_params(Puppet::Pops::Types::PSensitiveType::Sensitive.new('password'), 'md5', Puppet::Pops::Types::PSensitiveType::Sensitive.new('salt')) .and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') } + it { is_expected.to run.with_params('password', 'md5', Puppet::Pops::Types::PSensitiveType::Sensitive.new('salt')).and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') } end end diff --git a/spec/functions/range_spec.rb b/spec/functions/range_spec.rb index f6d9a2f6f..cd3d645da 100644 --- a/spec/functions/range_spec.rb +++ b/spec/functions/range_spec.rb @@ -7,10 +7,12 @@ describe 'signature validation in puppet3', unless: RSpec.configuration.puppet_future do it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the third.') is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('1..2..3').and_raise_error(Puppet::ParseError, %r{Unable to compute range}i) } it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{Unknown range format}i) } end @@ -20,41 +22,51 @@ pending 'the puppet 4 implementation' is_expected.to run.with_params.and_raise_error(ArgumentError) } + it { pending 'the puppet 4 implementation' is_expected.to run.with_params('').and_raise_error(ArgumentError) } + it { pending 'the puppet 4 implementation' is_expected.to run.with_params({}).and_raise_error(ArgumentError) } + it { pending 'the puppet 4 implementation' is_expected.to run.with_params([]).and_raise_error(ArgumentError) } + it { pending 'the puppet 4 implementation' is_expected.to run.with_params(true).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params(1, 2, 'foo').and_raise_error(ArgumentError) } + it { pending 'the puppet 4 implementation' is_expected.to run.with_params(1, 2, []).and_raise_error(ArgumentError) } + it { pending 'the puppet 4 implementation' is_expected.to run.with_params(1, 2, {}).and_raise_error(ArgumentError) } + it { pending 'the puppet 4 implementation' is_expected.to run.with_params(1, 2, true).and_raise_error(ArgumentError) } + it { pending 'the puppet 4 implementation' is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(ArgumentError) } + it { pending 'the puppet 4 implementation' is_expected.to run.with_params('1..2..3').and_raise_error(ArgumentError) @@ -147,10 +159,12 @@ pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Layout/LineLength : unable to cut line to required length is_expected.to run.with_params('0', 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) } + it { pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Layout/LineLength : unable to cut line to required length is_expected.to run.with_params(0, 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) } + it { pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Layout/LineLength : unable to cut line to required length is_expected.to run.with_params('h0', 'ha').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) diff --git a/spec/functions/regexpescape_spec.rb b/spec/functions/regexpescape_spec.rb index b8d66cff2..053aeafe5 100644 --- a/spec/functions/regexpescape_spec.rb +++ b/spec/functions/regexpescape_spec.rb @@ -6,10 +6,12 @@ describe 'signature validation' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the first.') is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } diff --git a/spec/functions/reject_spec.rb b/spec/functions/reject_spec.rb index efc3b693e..78a86bdd9 100644 --- a/spec/functions/reject_spec.rb +++ b/spec/functions/reject_spec.rb @@ -11,10 +11,12 @@ pending('reject does not actually check this, and raises NoMethodError instead') is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) } + it { pending('reject does not actually check this, and raises NoMethodError instead') is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) } + it { is_expected.to run.with_params([], 'two').and_return([]) } it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) } it { is_expected.to run.with_params(['one', 'two', 'three'], 't(wo|hree)').and_return(['one']) } diff --git a/spec/functions/reverse_spec.rb b/spec/functions/reverse_spec.rb index cad7086d8..6e1c7e49b 100644 --- a/spec/functions/reverse_spec.rb +++ b/spec/functions/reverse_spec.rb @@ -5,10 +5,12 @@ describe 'reverse' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the first.') is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } diff --git a/spec/functions/seeded_rand_string_spec.rb b/spec/functions/seeded_rand_string_spec.rb index 487f316f0..caa9bd635 100644 --- a/spec/functions/seeded_rand_string_spec.rb +++ b/spec/functions/seeded_rand_string_spec.rb @@ -27,6 +27,7 @@ expect(rand_str_one).to eq(rand_str_two) end + it 'generates different strings if seeded differently' do rand_str_one = call_function(:seeded_rand_string, 300, 'my_seed_one') rand_str_two = call_function(:seeded_rand_string, 300, 'my_seed_two') diff --git a/spec/functions/shell_escape_spec.rb b/spec/functions/shell_escape_spec.rb index 2d4c0c094..09d09e19a 100644 --- a/spec/functions/shell_escape_spec.rb +++ b/spec/functions/shell_escape_spec.rb @@ -18,6 +18,7 @@ describe 'escaping' do it { is_expected.to run.with_params('foo').and_return('foo') } it { is_expected.to run.with_params('foo bar').and_return('foo\ bar') } + it { is_expected.to run.with_params('~`!@#$%^&*()_-=[]\{}|;\':",./<>?') .and_return('\~\`\!@\#\$\%\^\&\*\(\)_-\=\[\]\\\\\{\}\|\;\\\':\",./\<\>\?') diff --git a/spec/functions/shell_join_spec.rb b/spec/functions/shell_join_spec.rb index f29bf53ce..435f170de 100644 --- a/spec/functions/shell_join_spec.rb +++ b/spec/functions/shell_join_spec.rb @@ -15,6 +15,7 @@ it { is_expected.to run.with_params(['foo']).and_return('foo') } it { is_expected.to run.with_params(['foo', 'bar']).and_return('foo bar') } it { is_expected.to run.with_params(['foo', 'bar baz']).and_return('foo bar\ baz') } + it { is_expected.to run.with_params(['~`!@#$', '%^&*()_-=', '[]\{}|;\':"', ',./<>?']) .and_return('\~\`\!@\#\$ \%\^\&\*\(\)_-\= \[\]\\\\\{\}\|\;\\\':\" ,./\<\>\?') diff --git a/spec/functions/shell_split_spec.rb b/spec/functions/shell_split_spec.rb index 82d41f138..fc1156de4 100644 --- a/spec/functions/shell_split_spec.rb +++ b/spec/functions/shell_split_spec.rb @@ -18,10 +18,12 @@ describe 'shell line spliting' do it { is_expected.to run.with_params('foo').and_return(['foo']) } it { is_expected.to run.with_params('foo bar').and_return(['foo', 'bar']) } + it { is_expected.to run.with_params('\~\`\!@\#\$\%\^\&\*\(\)_\+-\=\[\]\\\\\{\}\|\;\\\':\",./\<\>\?') .and_return(['~`!@#$%^&*()_+-=[]\{}|;\':",./<>?']) } + it { is_expected.to run.with_params('\~\`\!@\#\$ \%\^\&\*\(\)_\+-\= \[\]\\\\\{\}\|\;\\\':\" ,./\<\>\?') .and_return(['~`!@#$', '%^&*()_+-=', '[]\{}|;\':"', ',./<>?']) diff --git a/spec/functions/shuffle_spec.rb b/spec/functions/shuffle_spec.rb index 87a5c6e44..af57e40b1 100644 --- a/spec/functions/shuffle_spec.rb +++ b/spec/functions/shuffle_spec.rb @@ -5,10 +5,12 @@ describe 'shuffle' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the first.') is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } diff --git a/spec/functions/squeeze_spec.rb b/spec/functions/squeeze_spec.rb index 281846171..6eee9dd6f 100644 --- a/spec/functions/squeeze_spec.rb +++ b/spec/functions/squeeze_spec.rb @@ -30,11 +30,13 @@ .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc']) \ .and_return(['', 'a', 'a', 'abc']) } + it { is_expected.to run \ .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc'], 'a') \ .and_return(['', 'a', 'a', 'abbbbbbbbbbcccccccccc']) } + it { is_expected.to run \ .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc'], 'b-c') \ diff --git a/spec/functions/str2bool_spec.rb b/spec/functions/str2bool_spec.rb index 070955ceb..a3553edfc 100644 --- a/spec/functions/str2bool_spec.rb +++ b/spec/functions/str2bool_spec.rb @@ -5,10 +5,12 @@ describe 'str2bool' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the first.') is_expected.to run.with_params('true', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{Unknown type of boolean given}) } describe 'when testing values that mean "true"' do diff --git a/spec/functions/str2saltedsha512_spec.rb b/spec/functions/str2saltedsha512_spec.rb index 194dcba25..8552d4236 100644 --- a/spec/functions/str2saltedsha512_spec.rb +++ b/spec/functions/str2saltedsha512_spec.rb @@ -17,10 +17,12 @@ is_expected.to run.with_params('') .and_return('0f8a612f4eeed08e47b3875d00f33c5688f7926298f2d9b5fe19d1323f910bc78b6f7b5892596d2fabaa65e7a8d99b3768c102610cf0432c4827eee01f09451e3fae4f7a') } + it { is_expected.to run.with_params('password') .and_return('0f8a612f43134376566c5707718d600effcfb17581fc9d3fa64d7f447dfda317c174ffdb498d2c5bd5c2075dab41c9d7ada5afbdc6b55354980eb5ba61802371e6b64956') } + it { is_expected.to run.with_params('verylongpassword') .and_return('0f8a612f7a448537540e062daa8621f9bae326ca8ccb899e1bdb10e7c218cebfceae2530b856662565fdc4d81e986fc50cfbbc46d50436610ed9429ff5e43f2c45b5d039') @@ -30,10 +32,12 @@ is_expected.to run.with_params('') .and_return('a85c9d6f8c1eb1a625fd59e3cbca7dc7ab04ff1758d19ab99f098446e14a0a2a42e11afd1f4d6f17adfe2c772a3e6a821ee66a2564711431e14da96a3bff44593cf158ab') } + it { is_expected.to run.with_params('password') .and_return('a85c9d6ff4e4dd6655ec2922ee9752550f2df4dc370e9739dd94899f62be6a42cc31fbfce3d62be35e0e8482696c931f63fb9286cf7b13d283660720c55f2a6304d06958') } + it { is_expected.to run.with_params('verylongpassword') .and_return('a85c9d6fb810d0b8311c9a065c026e3179ae91fee3dbaf556f297e2fda2a8e3d8dd363977f9ef5c9b5da0cd518a5151a4e537928533291d68c9539d4d4b83da53b22a869') diff --git a/spec/functions/suffix_spec.rb b/spec/functions/suffix_spec.rb index 0c12100be..b4cedff9d 100644 --- a/spec/functions/suffix_spec.rb +++ b/spec/functions/suffix_spec.rb @@ -5,10 +5,12 @@ describe 'suffix' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the second.') is_expected.to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{expected first argument to be an Array}) } it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, %r{expected second argument to be a String}) } it { is_expected.to run.with_params([]).and_return([]) } @@ -24,6 +26,7 @@ it { is_expected.to run.with_params({}, '').and_return({}) } it { is_expected.to run.with_params({ 'key' => 'value' }, '').and_return('key' => 'value') } it { is_expected.to run.with_params({ 'key' => 'value' }, 'post').and_return('keypost' => 'value') } + it { is_expected.to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'post') \ diff --git a/spec/functions/swapcase_spec.rb b/spec/functions/swapcase_spec.rb index 49d2117de..642f5a7db 100644 --- a/spec/functions/swapcase_spec.rb +++ b/spec/functions/swapcase_spec.rb @@ -5,13 +5,16 @@ describe 'swapcase' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the first.') is_expected.to run.with_params('a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + describe 'with strings as inputs' do it { is_expected.to run.with_params('').and_return('') } it { is_expected.to run.with_params('one').and_return('ONE') } @@ -20,6 +23,7 @@ end describe 'with arrays as inputs' do it { is_expected.to run.with_params([]).and_return([]) } + describe 'only containing strings' do it { is_expected.to run.with_params(['']).and_return(['']) } it { is_expected.to run.with_params(['one']).and_return(['ONE']) } diff --git a/spec/functions/to_json_pretty_spec.rb b/spec/functions/to_json_pretty_spec.rb index 071e88a9b..e14dc7e92 100644 --- a/spec/functions/to_json_pretty_spec.rb +++ b/spec/functions/to_json_pretty_spec.rb @@ -9,10 +9,12 @@ it { is_expected.to run.with_params(['one', 'two']).and_return("[\n \"one\",\n \"two\"\n]\n") } it { is_expected.to run.with_params({}).and_return("{\n}\n") } it { is_expected.to run.with_params('key' => 'value').and_return("{\n \"key\": \"value\"\n}\n") } + it { is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) .and_return("{\n \"one\": {\n \"oneA\": \"A\",\n \"oneB\": {\n \"oneB1\": \"1\",\n \"oneB2\": \"2\"\n }\n },\n \"two\": [\n \"twoA\",\n \"twoB\"\n ]\n}\n") # rubocop:disable Layout/LineLength : Unable to reduce line to required length } + it { is_expected.to run.with_params({ 'one' => '1', 'two' => nil }, true).and_return("{\n \"one\": \"1\"\n}\n") } it { is_expected.to run.with_params(['one', 'two', nil, 'three'], true).and_return("[\n \"one\",\n \"two\",\n \"three\"\n]\n") } it { is_expected.to run.with_params(['one', 'two', nil, 'three'], true, 'indent' => '@@@@').and_return("[\n@@@@\"one\",\n@@@@\"two\",\n@@@@\"three\"\n]\n") } diff --git a/spec/functions/to_json_spec.rb b/spec/functions/to_json_spec.rb index 32184d6dd..c3b732783 100644 --- a/spec/functions/to_json_spec.rb +++ b/spec/functions/to_json_spec.rb @@ -12,6 +12,7 @@ it { is_expected.to run.with_params(['one', 'two']).and_return('["one","two"]') } it { is_expected.to run.with_params({}).and_return('{}') } it { is_expected.to run.with_params('key' => 'value').and_return('{"key":"value"}') } + it { is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) .and_return('{"one":{"oneA":"A","oneB":{"oneB1":"1","oneB2":"2"}},"two":["twoA","twoB"]}') diff --git a/spec/functions/to_python_spec.rb b/spec/functions/to_python_spec.rb index cea8a80cf..01b69224a 100644 --- a/spec/functions/to_python_spec.rb +++ b/spec/functions/to_python_spec.rb @@ -15,6 +15,7 @@ it { is_expected.to run.with_params(['one', 'two']).and_return('["one", "two"]') } it { is_expected.to run.with_params({}).and_return('{}') } it { is_expected.to run.with_params('key' => 'value').and_return('{"key": "value"}') } + it { is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) .and_return('{"one": {"oneA": "A", "oneB": {"oneB1": "1", "oneB2": "2"}}, "two": ["twoA", "twoB"]}') diff --git a/spec/functions/to_ruby_spec.rb b/spec/functions/to_ruby_spec.rb index f269a878c..e2c9dd6f0 100644 --- a/spec/functions/to_ruby_spec.rb +++ b/spec/functions/to_ruby_spec.rb @@ -14,6 +14,7 @@ it { is_expected.to run.with_params(['one', 'two']).and_return('["one", "two"]') } it { is_expected.to run.with_params({}).and_return('{}') } it { is_expected.to run.with_params('key' => 'value').and_return('{"key" => "value"}') } + it { is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) .and_return('{"one" => {"oneA" => "A", "oneB" => {"oneB1" => "1", "oneB2" => "2"}}, "two" => ["twoA", "twoB"]}') diff --git a/spec/functions/to_toml_spec.rb b/spec/functions/to_toml_spec.rb index 7a4090498..17559c0f5 100644 --- a/spec/functions/to_toml_spec.rb +++ b/spec/functions/to_toml_spec.rb @@ -5,6 +5,7 @@ describe 'to_toml' do context 'fails on invalid params' do it { is_expected.not_to eq(nil) } + [ nil, '', diff --git a/spec/functions/to_yaml_spec.rb b/spec/functions/to_yaml_spec.rb index de0e392eb..c7162b05f 100644 --- a/spec/functions/to_yaml_spec.rb +++ b/spec/functions/to_yaml_spec.rb @@ -12,6 +12,7 @@ it { is_expected.to run.with_params(['one', 'two']).and_return("---\n- one\n- two\n") } it { is_expected.to run.with_params({}).and_return("--- {}\n") } it { is_expected.to run.with_params('key' => 'value').and_return("---\nkey: value\n") } + it { is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) .and_return("---\none:\n oneA: A\n oneB:\n oneB1: '1'\n oneB2: '2'\ntwo:\n- twoA\n- twoB\n") diff --git a/spec/functions/unix2dos_spec.rb b/spec/functions/unix2dos_spec.rb index cd94e150e..d683daf8e 100644 --- a/spec/functions/unix2dos_spec.rb +++ b/spec/functions/unix2dos_spec.rb @@ -5,18 +5,23 @@ describe 'unix2dos' do context 'when checking parameter validity' do it { is_expected.not_to eq(nil) } + it do is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) end + it do is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{Wrong number of arguments}) end + it do is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) end + it do is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError) end + it do is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) end diff --git a/spec/functions/uriescape_spec.rb b/spec/functions/uriescape_spec.rb index 4fbafeac8..004b7e8be 100644 --- a/spec/functions/uriescape_spec.rb +++ b/spec/functions/uriescape_spec.rb @@ -8,10 +8,12 @@ describe 'signature validation' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the first.') is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } diff --git a/spec/functions/validate_cmd_spec.rb b/spec/functions/validate_cmd_spec.rb index 7d8a8a166..6f31e57c3 100644 --- a/spec/functions/validate_cmd_spec.rb +++ b/spec/functions/validate_cmd_spec.rb @@ -10,14 +10,17 @@ it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('', '', '', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('should implement stricter type checking') is_expected.to run.with_params([], '', '').and_raise_error(Puppet::ParseError, %r{content must be a string}) } + it { pending('should implement stricter type checking') is_expected.to run.with_params('', [], '').and_raise_error(Puppet::ParseError, %r{checkscript must be a string}) } + it { pending('should implement stricter type checking') is_expected.to run.with_params('', '', []).and_raise_error(Puppet::ParseError, %r{custom error message must be a string}) @@ -30,6 +33,7 @@ is_expected.to run .with_params('', "#{touch} % /no/such/file").and_raise_error(Puppet::ParseError, %r{Execution of '#{touch} \S+ \/no\/such\/file' returned 1:.*(cannot touch|o such file or)}) } + it { is_expected.to run.with_params('', "#{touch} % /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, %r{custom error}) } end context 'without % placeholder' do @@ -37,6 +41,7 @@ is_expected.to run .with_params('', "#{touch} /no/such/file").and_raise_error(Puppet::ParseError, %r{Execution of '#{touch} \/no\/such\/file \S+' returned 1:.*(cannot touch|o such file or)}) } + it { is_expected.to run.with_params('', "#{touch} /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, %r{custom error}) } end end diff --git a/spec/functions/values_at_spec.rb b/spec/functions/values_at_spec.rb index 61c9f4d2f..ed9b1bfff 100644 --- a/spec/functions/values_at_spec.rb +++ b/spec/functions/values_at_spec.rb @@ -7,10 +7,12 @@ it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the first two.') is_expected.to run.with_params([], 0, 1).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('', 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) } it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) } it { is_expected.to run.with_params(true, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) } @@ -47,6 +49,7 @@ it { is_expected.to run.with_params([0, 1, 2], '0-2').and_return([0, 1, 2]) } it { is_expected.to run.with_params([0, 1, 2], '0..2').and_return([0, 1, 2]) } it { is_expected.to run.with_params([0, 1, 2], '0...2').and_return([0, 1]) } + it { pending('fix this bounds check') is_expected.to run.with_params([0, 1, 2], '0...3').and_return([0, 1, 2]) diff --git a/spec/functions/zip_spec.rb b/spec/functions/zip_spec.rb index 77c7f9435..a24895768 100644 --- a/spec/functions/zip_spec.rb +++ b/spec/functions/zip_spec.rb @@ -6,10 +6,12 @@ it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { pending('Current implementation ignores parameters after the third.') is_expected.to run.with_params([], [], true, []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([], []).and_return([]) } it { is_expected.to run.with_params([1, 2, 3], [4, 5, 6]).and_return([[1, 4], [2, 5], [3, 6]]) } it { is_expected.to run.with_params([1, 2, 3], [4, 5, 6], false).and_return([[1, 4], [2, 5], [3, 6]]) } diff --git a/spec/unit/facter/util/puppet_settings_spec.rb b/spec/unit/facter/util/puppet_settings_spec.rb index edd63a86e..4b468d91e 100644 --- a/spec/unit/facter/util/puppet_settings_spec.rb +++ b/spec/unit/facter/util/puppet_settings_spec.rb @@ -13,6 +13,7 @@ it 'is nil' do expect(subject.with_puppet { Puppet[:vardir] }).to be_nil end + it 'does not yield to the block' do expect(Puppet).to receive(:[]).never expect(subject.with_puppet { Puppet[:vardir] }).to be_nil @@ -30,6 +31,7 @@ module Puppet; end it 'yields to the block' do subject.with_puppet { Puppet[:vardir] } end + it 'returns the nodes vardir' do expect(subject.with_puppet { Puppet[:vardir] }).to eq vardir end diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index 506160bee..d26db1271 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -47,6 +47,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'appends the line' do provider.create expect(File.read(tmpfile).chomp).to eq("foo bar\nfoo") @@ -63,6 +64,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'replaces the match' do provider.create expect(File.read(tmpfile).chomp).to eq("foo bar\nfoo") @@ -102,6 +104,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'replaces the match' do provider.create expect(File.read(tmpfile).chomp).to eq("foo\nbar") @@ -123,6 +126,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'replaces the match' do provider.create expect(File.read(tmpfile).chomp).to eql("foo\nbar") @@ -164,6 +168,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'replaces the matches' do provider.create expect(File.read(tmpfile).chomp).to eql("foo\nfoo\nbar\nfoo") @@ -198,6 +203,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'replaces the matches' do provider.create expect(File.read(tmpfile).chomp).to eql("foo\nfoo bar\nfoo\nfoo") @@ -217,6 +223,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'replaces the matches' do provider.create expect(File.read(tmpfile).chomp).to eql("foo\nfoo\nfoo\nfoo") @@ -252,6 +259,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'appends the line' do provider.create expect(File.read(tmpfile).chomp).to eql("foo bar\nfoo") diff --git a/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb b/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb index 823174ca4..753c7d6b5 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb @@ -47,16 +47,19 @@ it "providor 'be_exists'" do expect(provider).to be_exists end + it 'does not replace the matching line' do provider.create expect(File.read(tmpfile).chomp).to eql("foo1\nfoo=blah\nfoo2\nfoo3") end + it 'appends the line if no matches are found' do File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") } expect(provider.exists?).to be false provider.create expect(File.read(tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar") end + it 'raises an error with invalid values' do expect { @resource = Puppet::Type::File_line.new( @@ -113,11 +116,13 @@ @provider.create expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end + it 'adds a new line if no lines match' do File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") } @provider.create expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n") end + it 'does nothing if the exact line already exists' do File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = bar\nfoo2") } @provider.create @@ -258,16 +263,19 @@ @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2") end + it 'removes the line without touching the last new line' do File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\n") } @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2\n") end + it 'removes any occurence of the line' do File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") } @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2\n") end + it 'example in the docs' do @resource = Puppet::Type::File_line.new(name: 'bashrc_proxy', ensure: 'absent', path: tmpfile, line: 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128') @provider = provider_class.new(@resource) diff --git a/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb b/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb index da5f60df0..88c8ef9c4 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb @@ -47,6 +47,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'replaces the matches' do provider.create expect(File.read(tmpfile).chomp).to eq("* hard core 0\n* hard core 0") @@ -81,6 +82,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'replaces the matches' do provider.create expect(File.read(tmpfile).chomp).to eq("* hard core 0\n* hard core 0") @@ -99,6 +101,7 @@ it 'requests changes' do expect(provider).not_to be_exists end + it 'replaces the match' do provider.create expect(File.read(tmpfile).chomp).to eq("LogLevel=notice\nstuff") diff --git a/spec/unit/puppet/type/file_line_spec.rb b/spec/unit/puppet/type/file_line_spec.rb index e506b133a..17c4d8c7e 100644 --- a/spec/unit/puppet/type/file_line_spec.rb +++ b/spec/unit/puppet/type/file_line_spec.rb @@ -25,10 +25,12 @@ file_line[:line] = 'my_line' expect(file_line[:line]).to eq('my_line') end + it 'accepts a path' do file_line[:path] = my_path expect(file_line[:path]).to eq(my_path) end + it 'accepts a match regex' do file_line[:match] = '^foo.*$' expect(file_line[:match]).to eq('^foo.*$') @@ -41,6 +43,7 @@ ) }.not_to raise_error end + it 'accepts a match regex that does match the specified line' do expect { Puppet::Type.type(:file_line).new( @@ -48,6 +51,7 @@ ) }.not_to raise_error end + it 'accepts utf8 characters' do expect { Puppet::Type.type(:file_line).new( @@ -55,6 +59,7 @@ ) }.not_to raise_error end + it 'accepts double byte characters' do expect { Puppet::Type.type(:file_line).new( @@ -62,34 +67,44 @@ ) }.not_to raise_error end + it 'accepts posix filenames' do file_line[:path] = tmp_path expect(file_line[:path]).to eq(tmp_path) end + it 'does not accept unqualified path' do expect { file_line[:path] = 'file' }.to raise_error(Puppet::Error, %r{File paths must be fully qualified}) end + it 'requires that a line is specified' do expect { Puppet::Type.type(:file_line).new(name: 'foo', path: tmp_path) }.to raise_error(Puppet::Error, %r{line is a required attribute}) end + it 'does not require that a line is specified when matching for absence' do expect { Puppet::Type.type(:file_line).new(name: 'foo', path: tmp_path, ensure: :absent, match_for_absence: :true, match: 'match') }.not_to raise_error end + it 'although if a line is specified anyway when matching for absence it still works and the line is silently ignored' do expect { Puppet::Type.type(:file_line).new(name: 'foo', path: tmp_path, line: 'i_am_irrelevant', ensure: :absent, match_for_absence: :true, match: 'match') }.not_to raise_error end + it 'requires that a file is specified' do expect { Puppet::Type.type(:file_line).new(name: 'foo', line: 'path') }.to raise_error(Puppet::Error, %r{path is a required attribute}) end + it 'defaults to ensure => present' do expect(file_line[:ensure]).to eq :present end + it 'defaults to replace => true' do expect(file_line[:replace]).to eq :true end + it 'defaults to encoding => UTF-8' do expect(file_line[:encoding]).to eq 'UTF-8' end + it 'accepts encoding => iso-8859-1' do expect { Puppet::Type.type(:file_line).new(name: 'foo', path: tmp_path, ensure: :present, encoding: 'iso-8859-1', line: 'bar') }.not_to raise_error end From 02988680f566162359b4d1aa69ebac7329be9553 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:24:54 +0100 Subject: [PATCH 20/33] (CONT-801) Autocorrect safe group 4 --- .rubocop_todo.yml | 50 ------------------- spec/functions/defined_with_params_spec.rb | 2 + spec/functions/delete_undef_values_spec.rb | 2 + spec/functions/deprecation_spec.rb | 16 +++--- spec/functions/get_module_path_spec.rb | 2 + spec/functions/loadjson_spec.rb | 7 ++- spec/functions/loadyaml_spec.rb | 2 +- spec/functions/stdlib_ensure_spec.rb | 2 + spec/functions/swapcase_spec.rb | 3 ++ spec/functions/time_spec.rb | 1 + spec/functions/validate_cmd_spec.rb | 1 + spec/functions/validate_legacy_spec.rb | 14 +++--- spec/type_aliases/email_spec.rb | 1 + spec/type_aliases/ensure_package_spec.rb | 1 + spec/unit/facter/util/puppet_settings_spec.rb | 3 +- .../puppet/provider/file_line/ruby_spec.rb | 2 + .../provider/file_line/ruby_spec_alter.rb | 15 ++++++ 17 files changed, 56 insertions(+), 68 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a8bdfdc69..f104c4c82 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -140,45 +140,11 @@ RSpec/ContextWording: RSpec/DescribeClass: Enabled: false -# Offense count: 25 -# This cop supports safe autocorrection (--autocorrect). -RSpec/EmptyLineAfterExampleGroup: - Exclude: - - 'spec/functions/defined_with_params_spec.rb' - - 'spec/functions/stdlib_ensure_spec.rb' - - 'spec/functions/swapcase_spec.rb' - - 'spec/functions/validate_cmd_spec.rb' - - 'spec/type_aliases/email_spec.rb' - - 'spec/type_aliases/ensure_package_spec.rb' - - 'spec/unit/facter/util/puppet_settings_spec.rb' - - 'spec/unit/puppet/provider/file_line/ruby_spec.rb' - - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' - -# Offense count: 13 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowConsecutiveOneLiners. -RSpec/EmptyLineAfterHook: - Exclude: - - 'spec/functions/delete_undef_values_spec.rb' - - 'spec/functions/deprecation_spec.rb' - - 'spec/functions/get_module_path_spec.rb' - - 'spec/functions/loadjson_spec.rb' - - 'spec/functions/time_spec.rb' - - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' - # Offense count: 10 # Configuration parameters: CountAsOne. RSpec/ExampleLength: Max: 9 -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: CustomTransform, IgnoredWords, DisallowedExamples. -# DisallowedExamples: works -RSpec/ExampleWording: - Exclude: - - 'spec/functions/validate_legacy_spec.rb' - # Offense count: 2 # Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly. # Include: **/*_spec*rb*, **/spec/**/* @@ -187,12 +153,6 @@ RSpec/FilePath: - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -RSpec/HooksBeforeExamples: - Exclude: - - 'spec/functions/deprecation_spec.rb' - # Offense count: 198 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle. @@ -240,16 +200,6 @@ RSpec/PendingWithoutReason: - 'spec/functions/is_a_spec.rb' - 'spec/functions/type_of_spec.rb' -# Offense count: 13 -# This cop supports safe autocorrection (--autocorrect). -RSpec/ReceiveNever: - Exclude: - - 'spec/functions/deprecation_spec.rb' - - 'spec/functions/loadjson_spec.rb' - - 'spec/functions/loadyaml_spec.rb' - - 'spec/functions/validate_legacy_spec.rb' - - 'spec/unit/facter/util/puppet_settings_spec.rb' - # Offense count: 4 RSpec/RepeatedExampleGroupDescription: Exclude: diff --git a/spec/functions/defined_with_params_spec.rb b/spec/functions/defined_with_params_spec.rb index e9afe8744..c10b67bbf 100644 --- a/spec/functions/defined_with_params_spec.rb +++ b/spec/functions/defined_with_params_spec.rb @@ -6,6 +6,7 @@ describe 'when no resource is specified' do it { is_expected.to run.with_params.and_raise_error(ArgumentError) } end + describe 'when compared against a resource with no attributes' do let :pre_condition do 'user { "dan": }' @@ -52,6 +53,7 @@ context 'with reference' do it { is_expected.to run.with_params(Puppet::Resource.new('User[dan]'), {}).and_return(true) } end + if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0 context 'with array' do it 'fails' do diff --git a/spec/functions/delete_undef_values_spec.rb b/spec/functions/delete_undef_values_spec.rb index 3fa643c40..422fef02e 100644 --- a/spec/functions/delete_undef_values_spec.rb +++ b/spec/functions/delete_undef_values_spec.rb @@ -19,6 +19,7 @@ pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == '' pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == :undef && is_puppet_6 end + it { is_expected.to run.with_params([undef_value]).and_return([]) } it { is_expected.to run.with_params(['one', undef_value, 'two', 'three']).and_return(['one', 'two', 'three']) } it { is_expected.to run.with_params(['ớņέ', undef_value, 'ŧשּׁō', 'ŧħґëə']).and_return(['ớņέ', 'ŧשּׁō', 'ŧħґëə']) } @@ -42,6 +43,7 @@ pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == '' pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == :undef && is_puppet_6 end + it { is_expected.to run.with_params('key' => undef_value).and_return({}) } it { diff --git a/spec/functions/deprecation_spec.rb b/spec/functions/deprecation_spec.rb index d03b5e2d2..eac4fdd58 100644 --- a/spec/functions/deprecation_spec.rb +++ b/spec/functions/deprecation_spec.rb @@ -9,6 +9,11 @@ Puppet.settings[:strict] = :warning end + after(:each) do + # this is to reset the strict variable to default + Puppet.settings[:strict] = :warning + end + it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(ArgumentError) } @@ -36,7 +41,7 @@ it 'fails twice with message, with multiple calls. when strict= :error' do Puppet.settings[:strict] = :error - expect(Puppet).to receive(:warning).with(include('heelo')).never + expect(Puppet).not_to receive(:warning).with(include('heelo')) (0..1).each do |_i| is_expected.to run.with_params('key', 'heelo').and_raise_error(RuntimeError, %r{deprecation. key. heelo}) end @@ -44,16 +49,11 @@ it 'displays nothing, despite multiple calls. strict= :off' do Puppet.settings[:strict] = :off - expect(Puppet).to receive(:warning).with(include('heelo')).never + expect(Puppet).not_to receive(:warning).with(include('heelo')) (0..1).each do |_i| is_expected.to run.with_params('key', 'heelo') end end - - after(:each) do - # this is to reset the strict variable to default - Puppet.settings[:strict] = :warning - end end elsif Puppet.version.to_f < 4.0 # Puppet version < 4 will use these tests. @@ -61,9 +61,11 @@ after(:each) do ENV.delete('STDLIB_LOG_DEPRECATIONS') end + before(:each) do ENV['STDLIB_LOG_DEPRECATIONS'] = 'true' end + it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/get_module_path_spec.rb b/spec/functions/get_module_path_spec.rb index b2d415dc1..0896b9633 100644 --- a/spec/functions/get_module_path_spec.rb +++ b/spec/functions/get_module_path_spec.rb @@ -30,6 +30,7 @@ def initialize(path) before(:each) do allow(Puppet::Module).to receive(:find).with('foo', 'rp_env').and_return(path_of_module_foo) end + it 'runs against foo' do is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) end @@ -47,6 +48,7 @@ def initialize(path) before(:each) do allow(Puppet::Module).to receive(:find).with('foo', 'test').and_return(path_of_module_foo) end + it 'runs against foo' do is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) end diff --git a/spec/functions/loadjson_spec.rb b/spec/functions/loadjson_spec.rb index 08988b338..bf88e0070 100644 --- a/spec/functions/loadjson_spec.rb +++ b/spec/functions/loadjson_spec.rb @@ -28,11 +28,12 @@ before(:each) do allow(File).to receive(:exists?).with(filename).and_return(false).once if Puppet::PUPPETVERSION[0].to_i < 8 - allow(PSON).to receive(:load).never + allow(PSON).not_to receive(:load) else - allow(JSON).to receive(:parse).never + allow(JSON).not_to receive(:parse) end end + it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') } it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') } @@ -59,6 +60,7 @@ allow(JSON).to receive(:parse).with(json).and_return(data).once end end + it { is_expected.to run.with_params(filename).and_return(data) } end @@ -81,6 +83,7 @@ allow(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!' end end + it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } end diff --git a/spec/functions/loadyaml_spec.rb b/spec/functions/loadyaml_spec.rb index 4de82ec35..156095dde 100644 --- a/spec/functions/loadyaml_spec.rb +++ b/spec/functions/loadyaml_spec.rb @@ -11,7 +11,7 @@ it "'default' => 'value'" do expect(File).to receive(:exists?).with(filename).and_return(false).once - expect(YAML).to receive(:load_file).never + expect(YAML).not_to receive(:load_file) is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') end end diff --git a/spec/functions/stdlib_ensure_spec.rb b/spec/functions/stdlib_ensure_spec.rb index f4e5f7935..b81d744e0 100644 --- a/spec/functions/stdlib_ensure_spec.rb +++ b/spec/functions/stdlib_ensure_spec.rb @@ -7,12 +7,14 @@ it { is_expected.to run.with_params(true).and_return('present') } it { is_expected.to run.with_params(false).and_return('absent') } end + context 'work with service resource' do it { is_expected.to run.with_params('present', 'service').and_return('running') } it { is_expected.to run.with_params(true, 'service').and_return('running') } it { is_expected.to run.with_params('absent', 'service').and_return('stopped') } it { is_expected.to run.with_params(false, 'service').and_return('stopped') } end + ['directory', 'link', 'mounted', 'file'].each do |resource| context "work with #{resource} resource" do it { is_expected.to run.with_params('present', resource).and_return(resource) } diff --git a/spec/functions/swapcase_spec.rb b/spec/functions/swapcase_spec.rb index 642f5a7db..4c77c81ae 100644 --- a/spec/functions/swapcase_spec.rb +++ b/spec/functions/swapcase_spec.rb @@ -21,6 +21,7 @@ it { is_expected.to run.with_params('ONE').and_return('one') } it { is_expected.to run.with_params('oNe').and_return('OnE') } end + describe 'with arrays as inputs' do it { is_expected.to run.with_params([]).and_return([]) } @@ -33,6 +34,7 @@ it { is_expected.to run.with_params(['ONE', 'OnE']).and_return(['one', 'oNe']) } it { is_expected.to run.with_params(['oNe', 'one']).and_return(['OnE', 'ONE']) } end + describe 'containing mixed types' do it { is_expected.to run.with_params(['OnE', {}]).and_return(['oNe', {}]) } it { is_expected.to run.with_params(['OnE', 1]).and_return(['oNe', 1]) } @@ -40,6 +42,7 @@ it { is_expected.to run.with_params(['OnE', ['two']]).and_return(['oNe', ['two']]) } end end + it 'accepts objects which extend String' do is_expected.to run.with_params(AlsoString.new('OnE')).and_return('oNe') end diff --git a/spec/functions/time_spec.rb b/spec/functions/time_spec.rb index 181bb056e..42024f679 100644 --- a/spec/functions/time_spec.rb +++ b/spec/functions/time_spec.rb @@ -12,6 +12,7 @@ test_time = Time.utc(2006, 10, 13, 8, 15, 11) allow(Time).to receive(:new).with(no_args).and_return(test_time).once end + it { is_expected.to run.with_params.and_return(1_160_727_311) } it { is_expected.to run.with_params('').and_return(1_160_727_311) } it { is_expected.to run.with_params([]).and_return(1_160_727_311) } diff --git a/spec/functions/validate_cmd_spec.rb b/spec/functions/validate_cmd_spec.rb index 6f31e57c3..2cbcc1b98 100644 --- a/spec/functions/validate_cmd_spec.rb +++ b/spec/functions/validate_cmd_spec.rb @@ -36,6 +36,7 @@ it { is_expected.to run.with_params('', "#{touch} % /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, %r{custom error}) } end + context 'without % placeholder' do it { is_expected.to run diff --git a/spec/functions/validate_legacy_spec.rb b/spec/functions/validate_legacy_spec.rb index 4ab955334..1b2573dec 100644 --- a/spec/functions/validate_legacy_spec.rb +++ b/spec/functions/validate_legacy_spec.rb @@ -10,8 +10,8 @@ describe 'when passing the type assertion' do it 'passes with a deprecation warning' do expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once - expect(scope).to receive(:function_validate_foo).never - expect(Puppet).to receive(:notice).never + expect(scope).not_to receive(:function_validate_foo) + expect(Puppet).not_to receive(:notice) is_expected.to run.with_params('Integer', 'validate_foo', 5) end end @@ -19,7 +19,7 @@ describe 'when failing the type assertion' do it 'fails with a helpful message' do expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once - expect(scope).to receive(:function_validate_foo).never + expect(scope).not_to receive(:function_validate_foo) expect(subject.func).to receive(:call_function).with('fail', 'validate_legacy(Integer, ...) expects an Integer value, got String').once is_expected.to run.with_params('Integer', 'validate_foo', '5') end @@ -28,8 +28,8 @@ describe 'when passing in undef' do it 'works' do expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once - expect(scope).to receive(:function_validate_foo).never - expect(Puppet).to receive(:notice).never + expect(scope).not_to receive(:function_validate_foo) + expect(Puppet).not_to receive(:notice) is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef) end end @@ -37,8 +37,8 @@ describe 'when passing in multiple arguments' do it 'passes with a deprecation message' do expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once - expect(scope).to receive(:function_validate_foo).never - expect(Puppet).to receive(:notice).never + expect(scope).not_to receive(:function_validate_foo) + expect(Puppet).not_to receive(:notice) is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef, 1, 'foo') end end diff --git a/spec/type_aliases/email_spec.rb b/spec/type_aliases/email_spec.rb index add96eeff..307de118a 100644 --- a/spec/type_aliases/email_spec.rb +++ b/spec/type_aliases/email_spec.rb @@ -36,6 +36,7 @@ end end end + describe 'invalid handling' do ['plainaddress', '#@%^%#$@#$@#.com', diff --git a/spec/type_aliases/ensure_package_spec.rb b/spec/type_aliases/ensure_package_spec.rb index a907747e2..0eccb106a 100644 --- a/spec/type_aliases/ensure_package_spec.rb +++ b/spec/type_aliases/ensure_package_spec.rb @@ -20,6 +20,7 @@ end end end + describe 'No complex types can match' do context 'Garbage inputs, no complex or non string types can match' do [ diff --git a/spec/unit/facter/util/puppet_settings_spec.rb b/spec/unit/facter/util/puppet_settings_spec.rb index 4b468d91e..7bb7da611 100644 --- a/spec/unit/facter/util/puppet_settings_spec.rb +++ b/spec/unit/facter/util/puppet_settings_spec.rb @@ -15,10 +15,11 @@ end it 'does not yield to the block' do - expect(Puppet).to receive(:[]).never + expect(Puppet).not_to receive(:[]) expect(subject.with_puppet { Puppet[:vardir] }).to be_nil end end + context 'with Puppet loaded' do # module Puppet module Puppet; end diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index d26db1271..fdca0510d 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -41,6 +41,7 @@ expect(provider).to be_exists end end + context 'when line does not exist' do let(:content) { 'foo bar' } @@ -132,6 +133,7 @@ expect(File.read(tmpfile).chomp).to eql("foo\nbar") end end + context 'when not matching' do let(:content) { "foo3\nbar" } diff --git a/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb b/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb index 753c7d6b5..2296ce845 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb @@ -69,6 +69,7 @@ end end end + context 'when matching' do # rubocop:disable RSpec/InstanceVariable : replacing before with let breaks the tests, variables need to be altered within it block : multi before :each do @@ -80,6 +81,7 @@ ) @provider = provider_class.new(@resource) end + describe 'using match' do it 'raises an error if more than one line matches, and should not have modified the file' do File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") } @@ -129,6 +131,7 @@ expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end end + describe 'using match+append_on_no_match - when there is a match' do it 'replaces line' do @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'inserted = line', match: '^foo3$', append_on_no_match: false) @@ -137,6 +140,7 @@ expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") end end + describe 'using match+append_on_no_match - when there is no match' do it 'does not add line after no matches found' do @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'inserted = line', match: '^foo3$', append_on_no_match: false) @@ -146,6 +150,7 @@ end end end + context 'when after' do let :resource do Puppet::Type::File_line.new( @@ -174,9 +179,11 @@ ) end end + before :each do File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nfoo = baz") } end + describe 'inserts at match' do include_context 'resource_create' it { @@ -184,6 +191,7 @@ expect(File.read(tmpfile).chomp).to eq("foo1\ninserted = line\nfoo = baz") } end + describe 'inserts a new line after when no match' do include_context 'resource_create' do let(:match) { '^nevergoingtomatch$' } @@ -193,6 +201,7 @@ expect(File.read(tmpfile).chomp).to eq("foo1\ninserted = line\nfoo2\nfoo = baz") } end + describe 'append to end of file if no match for both after and match' do include_context 'resource_create' do let(:match) { '^nevergoingtomatch$' } @@ -204,6 +213,7 @@ } end end + context 'with one line matching the after expression' do before :each do File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } @@ -214,6 +224,7 @@ expect(File.read(tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz") end end + context 'with multiple lines matching the after expression' do before :each do File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz") } @@ -230,6 +241,7 @@ expect(File.read(tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz") end end + context 'with no lines matching the after expression' do let :content do "foo3\nfoo = blah\nfoo2\nfoo = baz\n" @@ -245,6 +257,7 @@ end end end + context 'when removing with a line' do before :each do # TODO: these should be ported over to use the PuppetLabs spec_helper @@ -258,6 +271,7 @@ ) @provider = provider_class.new(@resource) end + it 'removes the line if it exists' do File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } @provider.destroy @@ -284,6 +298,7 @@ expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n") end end + context 'when removing with a match' do before :each do @resource = Puppet::Type::File_line.new( From 773e26d82bbe3d938797f11b5b0fb61fc3fcc6cf Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:27:42 +0100 Subject: [PATCH 21/33] (CONT-801) Correct RSpec/ImplicitSubject --- .rubocop_todo.yml | 7 ---- spec/functions/assert_private_spec.rb | 8 ++-- spec/functions/base64_spec.rb | 36 ++++++++-------- spec/functions/batch_escape_spec.rb | 4 +- spec/functions/convert_base_spec.rb | 4 +- spec/functions/count_spec.rb | 2 +- spec/functions/deep_merge_spec.rb | 10 ++--- spec/functions/defined_with_params_spec.rb | 2 +- spec/functions/delete_at_spec.rb | 2 +- spec/functions/delete_regex_spec.rb | 4 +- spec/functions/delete_spec.rb | 6 +-- spec/functions/delete_undef_values_spec.rb | 2 +- spec/functions/delete_values_spec.rb | 8 ++-- spec/functions/deprecation_spec.rb | 10 ++--- spec/functions/dos2unix_spec.rb | 16 +++---- spec/functions/end_with_spec.rb | 2 +- spec/functions/ensure_resource_spec.rb | 4 +- spec/functions/get_module_path_spec.rb | 8 ++-- spec/functions/grep_spec.rb | 4 +- spec/functions/is_a_spec.rb | 6 +-- spec/functions/join_keys_to_values_spec.rb | 4 +- spec/functions/loadjson_spec.rb | 10 ++--- spec/functions/loadyaml_spec.rb | 16 +++---- spec/functions/member_spec.rb | 2 +- spec/functions/merge_spec.rb | 20 ++++----- spec/functions/parsejson_spec.rb | 44 ++++++++++---------- spec/functions/parsepson_spec.rb | 40 +++++++++--------- spec/functions/parseyaml_spec.rb | 44 ++++++++++---------- spec/functions/powershell_escape_spec.rb | 4 +- spec/functions/prefix_spec.rb | 4 +- spec/functions/pw_hash_spec.rb | 4 +- spec/functions/range_spec.rb | 30 ++++++------- spec/functions/regexpescape_spec.rb | 6 +-- spec/functions/reject_spec.rb | 4 +- spec/functions/reverse_spec.rb | 2 +- spec/functions/shell_escape_spec.rb | 4 +- spec/functions/shell_join_spec.rb | 4 +- spec/functions/shell_split_spec.rb | 8 ++-- spec/functions/shuffle_spec.rb | 2 +- spec/functions/squeeze_spec.rb | 6 +-- spec/functions/stdlib_deferrable_epp_spec.rb | 4 +- spec/functions/str2bool_spec.rb | 2 +- spec/functions/str2saltedpbkdf2_spec.rb | 8 ++-- spec/functions/str2saltedsha512_spec.rb | 24 +++++------ spec/functions/suffix_spec.rb | 4 +- spec/functions/swapcase_spec.rb | 4 +- spec/functions/to_json_pretty_spec.rb | 8 ++-- spec/functions/to_json_spec.rb | 4 +- spec/functions/to_python_spec.rb | 4 +- spec/functions/to_ruby_spec.rb | 4 +- spec/functions/to_yaml_spec.rb | 4 +- spec/functions/unix2dos_spec.rb | 14 +++---- spec/functions/uriescape_spec.rb | 6 +-- spec/functions/validate_cmd_spec.rb | 10 ++--- spec/functions/validate_legacy_spec.rb | 10 ++--- spec/functions/values_at_spec.rb | 4 +- spec/functions/xml_encode_spec.rb | 2 +- spec/functions/zip_spec.rb | 2 +- 58 files changed, 257 insertions(+), 264 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f104c4c82..ccde6551f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -153,13 +153,6 @@ RSpec/FilePath: - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' -# Offense count: 198 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: single_line_only, single_statement_only, disallow, require_implicit -RSpec/ImplicitSubject: - Enabled: false - # Offense count: 2 RSpec/LeakyConstantDeclaration: Exclude: diff --git a/spec/functions/assert_private_spec.rb b/spec/functions/assert_private_spec.rb index cb1e896bb..8edfe4158 100644 --- a/spec/functions/assert_private_spec.rb +++ b/spec/functions/assert_private_spec.rb @@ -8,7 +8,7 @@ expect(scope).to receive(:lookupvar).with('module_name').and_return('foo') expect(scope).to receive(:lookupvar).with('caller_module_name').and_return('foo') - is_expected.to run.with_params + expect(subject).to run.with_params end end @@ -19,11 +19,11 @@ expect(scope.source).to receive(:name).and_return('foo::baz') expect(scope.source).to receive(:type).and_return('hostclass') - is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Class foo::baz is private}) + expect(subject).to run.with_params.and_raise_error(Puppet::ParseError, %r{Class foo::baz is private}) end it 'fails with an explicit failure message' do - is_expected.to run.with_params('failure message!').and_raise_error(Puppet::ParseError, %r{failure message!}) + expect(subject).to run.with_params('failure message!').and_raise_error(Puppet::ParseError, %r{failure message!}) end end @@ -34,7 +34,7 @@ expect(scope.source).to receive(:name).and_return('foo::baz') expect(scope.source).to receive(:type).and_return('definition') - is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Definition foo::baz is private}) + expect(subject).to run.with_params.and_raise_error(Puppet::ParseError, %r{Definition foo::baz is private}) end end end diff --git a/spec/functions/base64_spec.rb b/spec/functions/base64_spec.rb index a79c5c5cc..b2592be89 100644 --- a/spec/functions/base64_spec.rb +++ b/spec/functions/base64_spec.rb @@ -24,47 +24,47 @@ it { is_expected.to run.with_params('decode', 'dGhlc3RyaW5n', 'strict').and_return('thestring') } it { - is_expected.to run.with_params('encode', 'a very long string that will cause the base64 encoder to produce output with multiple lines') - .and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") + expect(subject).to run.with_params('encode', 'a very long string that will cause the base64 encoder to produce output with multiple lines') + .and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") } it { - is_expected.to run.with_params('decode', "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") - .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') + expect(subject).to run.with_params('decode', "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") + .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') } it { - is_expected.to run.with_params('decode', 'YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==') - .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') + expect(subject).to run.with_params('decode', 'YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==') + .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') } it { - is_expected.to run.with_params('encode', 'a very long string that will cause the base64 encoder to produce output with multiple lines', 'strict') - .and_return('YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==') + expect(subject).to run.with_params('encode', 'a very long string that will cause the base64 encoder to produce output with multiple lines', 'strict') + .and_return('YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==') } it { - is_expected.to run.with_params('decode', 'YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==', 'strict') - .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') + expect(subject).to run.with_params('decode', 'YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==', 'strict') + .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') } it { - is_expected.to run.with_params('encode', 'https://www.google.com.tw/?gws_rd=ssl#q=hello+world', 'urlsafe') - .and_return('aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk') + expect(subject).to run.with_params('encode', 'https://www.google.com.tw/?gws_rd=ssl#q=hello+world', 'urlsafe') + .and_return('aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk') } it { - is_expected.to run.with_params('decode', 'aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk', 'urlsafe') - .and_return('https://www.google.com.tw/?gws_rd=ssl#q=hello+world') + expect(subject).to run.with_params('decode', 'aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk', 'urlsafe') + .and_return('https://www.google.com.tw/?gws_rd=ssl#q=hello+world') } it { - is_expected.to run.with_params('encode', 'https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add', 'urlsafe') - .and_return('aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=') + expect(subject).to run.with_params('encode', 'https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add', 'urlsafe') + .and_return('aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=') } it { - is_expected.to run.with_params('decode', 'aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=', 'urlsafe') - .and_return('https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add') + expect(subject).to run.with_params('decode', 'aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=', 'urlsafe') + .and_return('https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add') } end diff --git a/spec/functions/batch_escape_spec.rb b/spec/functions/batch_escape_spec.rb index 151fa6611..ab609abd5 100644 --- a/spec/functions/batch_escape_spec.rb +++ b/spec/functions/batch_escape_spec.rb @@ -20,8 +20,8 @@ it { is_expected.to run.with_params('foo bar').and_return('"foo bar"') } it { - is_expected.to run.with_params('~`!@#$%^&*()_-=[]\{}|;\':",./<>?') - .and_return('"~`!@#\\$%^&*()_-=[]\\\{}|;\':"",./<>?"') + expect(subject).to run.with_params('~`!@#$%^&*()_-=[]\{}|;\':",./<>?') + .and_return('"~`!@#\\$%^&*()_-=[]\\\{}|;\':"",./<>?"') } end end diff --git a/spec/functions/convert_base_spec.rb b/spec/functions/convert_base_spec.rb index a2de736c9..88cd93eb2 100644 --- a/spec/functions/convert_base_spec.rb +++ b/spec/functions/convert_base_spec.rb @@ -13,11 +13,11 @@ it { is_expected.to run.with_params('1', 37).and_raise_error(Puppet::ParseError, %r{base must be at least 2 and must not be greater than 36}) } it 'raises a ParseError if argument 1 is a string that does not correspond to an integer in base 10' do - is_expected.to run.with_params('ten', 6).and_raise_error(Puppet::ParseError, %r{argument must be an integer or a string corresponding to an integer in base 10}) + expect(subject).to run.with_params('ten', 6).and_raise_error(Puppet::ParseError, %r{argument must be an integer or a string corresponding to an integer in base 10}) end it 'raises a ParseError if argument 2 is a string and does not correspond to an integer in base 10' do - is_expected.to run.with_params(100, 'hex').and_raise_error(Puppet::ParseError, %r{argument must be an integer or a string corresponding to an integer in base 10}) + expect(subject).to run.with_params(100, 'hex').and_raise_error(Puppet::ParseError, %r{argument must be an integer or a string corresponding to an integer in base 10}) end it { is_expected.to run.with_params('11', '16').and_return('b') } diff --git a/spec/functions/count_spec.rb b/spec/functions/count_spec.rb index 02937afa9..bab41e3c7 100644 --- a/spec/functions/count_spec.rb +++ b/spec/functions/count_spec.rb @@ -10,7 +10,7 @@ it { pending('should actually be like this, and not like above') - is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError) + expect(subject).to run.with_params('one', 'two').and_raise_error(ArgumentError) } it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(ArgumentError) } diff --git a/spec/functions/deep_merge_spec.rb b/spec/functions/deep_merge_spec.rb index 7ed8f3f08..07291f8a1 100644 --- a/spec/functions/deep_merge_spec.rb +++ b/spec/functions/deep_merge_spec.rb @@ -16,13 +16,13 @@ describe 'when arguments have key collisions' do it 'prefers values from the last hash' do - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2' }, 'key2' => 'replacement_value', 'key3' => 'value3') \ .and_return('key1' => 'value1', 'key2' => 'replacement_value', 'key3' => 'value3') end it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1' }, { 'key1' => 'value2' }, 'key1' => 'value3') \ .and_return('key1' => 'value3') } @@ -30,19 +30,19 @@ describe 'when arguments have subhashes' do it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1' }, 'key2' => 'value2', 'key3' => { 'subkey1' => 'value4' }) \ .and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => { 'subkey1' => 'value4' }) } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => { 'subkey1' => 'value1' } }, 'key1' => { 'subkey2' => 'value2' }) \ .and_return('key1' => { 'subkey1' => 'value1', 'subkey2' => 'value2' }) } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => { 'subkey1' => { 'subsubkey1' => 'value1' } } }, 'key1' => { 'subkey1' => { 'subsubkey1' => 'value2' } }) \ .and_return('key1' => { 'subkey1' => { 'subsubkey1' => 'value2' } }) } diff --git a/spec/functions/defined_with_params_spec.rb b/spec/functions/defined_with_params_spec.rb index c10b67bbf..6997ba9b9 100644 --- a/spec/functions/defined_with_params_spec.rb +++ b/spec/functions/defined_with_params_spec.rb @@ -92,7 +92,7 @@ it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[foo]'), {}).and_return(true) } it { - is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false) + expect(subject).to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false) expect(catalogue.resource('Notify[Duplicate found somewhere]')).not_to be_nil expect(catalogue.resource('Notify[Should not find me]')).to be_nil diff --git a/spec/functions/delete_at_spec.rb b/spec/functions/delete_at_spec.rb index e69b8b530..13e45ebaf 100644 --- a/spec/functions/delete_at_spec.rb +++ b/spec/functions/delete_at_spec.rb @@ -11,7 +11,7 @@ it { pending('Current implementation ignores parameters after the first two.') - is_expected.to run.with_params(['one'], 0, 1).and_raise_error(Puppet::ParseError) + expect(subject).to run.with_params(['one'], 0, 1).and_raise_error(Puppet::ParseError) } describe 'argument validation' do diff --git a/spec/functions/delete_regex_spec.rb b/spec/functions/delete_regex_spec.rb index 671de9eba..3c2cc39f8 100644 --- a/spec/functions/delete_regex_spec.rb +++ b/spec/functions/delete_regex_spec.rb @@ -32,13 +32,13 @@ it { is_expected.to run.with_params({ 'key' => 'value' }, 'key').and_return({}) } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'key2') \ .and_return('key1' => 'value1', 'key3' => 'value3') } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, ['key1', 'key2']) \ .and_return('key3' => 'value3') } diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb index 9fec33740..82eba5364 100644 --- a/spec/functions/delete_spec.rb +++ b/spec/functions/delete_spec.rb @@ -45,19 +45,19 @@ it { is_expected.to run.with_params({ 'key' => 'value' }, 'key').and_return({}) } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'key2') \ .and_return('key1' => 'value1', 'key3' => 'value3') } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, ['key1', 'key2']) \ .and_return('key3' => 'value3') } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'ĸəұ1' => 'νãŀủĕ1', 'ĸəұ2' => 'νãŀủĕ2', 'ĸəұ3' => 'νãŀủĕ3' }, ['ĸəұ1', 'ĸəұ2']) \ .and_return('ĸəұ3' => 'νãŀủĕ3') } diff --git a/spec/functions/delete_undef_values_spec.rb b/spec/functions/delete_undef_values_spec.rb index 422fef02e..d93b1cdc8 100644 --- a/spec/functions/delete_undef_values_spec.rb +++ b/spec/functions/delete_undef_values_spec.rb @@ -47,7 +47,7 @@ it { is_expected.to run.with_params('key' => undef_value).and_return({}) } it { - is_expected.to run \ + expect(subject).to run \ .with_params('key1' => 'value1', 'undef_key' => undef_value, 'key2' => 'value2') \ .and_return('key1' => 'value1', 'key2' => 'value2') } diff --git a/spec/functions/delete_values_spec.rb b/spec/functions/delete_values_spec.rb index 89c37b24b..f63af7133 100644 --- a/spec/functions/delete_values_spec.rb +++ b/spec/functions/delete_values_spec.rb @@ -19,25 +19,25 @@ it { is_expected.to run.with_params({}, 'value').and_return({}) } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1' }, 'non-existing value') \ .and_return('key1' => 'value1') } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'ҝếỵ1 ' => 'νâĺūẹ1', 'ҝếỵ2' => 'value to delete' }, 'value to delete') \ .and_return('ҝếỵ1 ' => 'νâĺūẹ1') } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1', 'key2' => 'νǎŀữ℮ ťớ đêłểťė' }, 'νǎŀữ℮ ťớ đêłểťė') \ .and_return('key1' => 'value1') } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value to delete', 'key3' => 'value to delete' }, 'value to delete') \ .and_return('key1' => 'value1') } diff --git a/spec/functions/deprecation_spec.rb b/spec/functions/deprecation_spec.rb index eac4fdd58..612f89825 100644 --- a/spec/functions/deprecation_spec.rb +++ b/spec/functions/deprecation_spec.rb @@ -24,7 +24,7 @@ else expect(Puppet).to receive(:warning).with(include('heelo')).once end - is_expected.to run.with_params('key', 'heelo') + expect(subject).to run.with_params('key', 'heelo') end it 'displays a single warning, despite multiple calls' do @@ -35,7 +35,7 @@ expect(Puppet).to receive(:warning).with(include('heelo')).once end (0..1).each do |_i| - is_expected.to run.with_params('key', 'heelo') + expect(subject).to run.with_params('key', 'heelo') end end @@ -43,7 +43,7 @@ Puppet.settings[:strict] = :error expect(Puppet).not_to receive(:warning).with(include('heelo')) (0..1).each do |_i| - is_expected.to run.with_params('key', 'heelo').and_raise_error(RuntimeError, %r{deprecation. key. heelo}) + expect(subject).to run.with_params('key', 'heelo').and_raise_error(RuntimeError, %r{deprecation. key. heelo}) end end @@ -51,7 +51,7 @@ Puppet.settings[:strict] = :off expect(Puppet).not_to receive(:warning).with(include('heelo')) (0..1).each do |_i| - is_expected.to run.with_params('key', 'heelo') + expect(subject).to run.with_params('key', 'heelo') end end end @@ -71,7 +71,7 @@ it 'displays a single warning' do expect(scope).to receive(:warning).with(include('heelo')) - is_expected.to run.with_params('key', 'heelo') + expect(subject).to run.with_params('key', 'heelo') end end end diff --git a/spec/functions/dos2unix_spec.rb b/spec/functions/dos2unix_spec.rb index 5cd696cbb..e7f588852 100644 --- a/spec/functions/dos2unix_spec.rb +++ b/spec/functions/dos2unix_spec.rb @@ -7,23 +7,23 @@ it { is_expected.not_to eq(nil) } it do - is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) + expect(subject).to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) end it do - is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{Wrong number of arguments}) + expect(subject).to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{Wrong number of arguments}) end it do - is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{Requires string as argument}) + expect(subject).to run.with_params([]).and_raise_error(Puppet::ParseError, %r{Requires string as argument}) end it do - is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires string as argument}) + expect(subject).to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires string as argument}) end it do - is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires string as argument}) + expect(subject).to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires string as argument}) end end @@ -32,7 +32,7 @@ desired_output = "Hello\nWorld\n" it 'outputs unix format' do - is_expected.to run.with_params(sample_text).and_return(desired_output) + expect(subject).to run.with_params(sample_text).and_return(desired_output) end end @@ -44,11 +44,11 @@ desired_output_doublebyte = "こんにちは\n世界\n" it 'outputs uft8 string' do - is_expected.to run.with_params(sample_text_utf8).and_return(desired_output_utf8) + expect(subject).to run.with_params(sample_text_utf8).and_return(desired_output_utf8) end it 'outputs double byte string' do - is_expected.to run.with_params(sample_text_doublebyte).and_return(desired_output_doublebyte) + expect(subject).to run.with_params(sample_text_doublebyte).and_return(desired_output_doublebyte) end end end diff --git a/spec/functions/end_with_spec.rb b/spec/functions/end_with_spec.rb index 89082c4bd..058f9a77a 100644 --- a/spec/functions/end_with_spec.rb +++ b/spec/functions/end_with_spec.rb @@ -9,7 +9,7 @@ it { is_expected.to run.with_params('foobar', ['foo', 'baz']).and_return(false) } it do - is_expected.to run.with_params('foobar', '').and_raise_error( + expect(subject).to run.with_params('foobar', '').and_raise_error( ArgumentError, %r{'stdlib::end_with' parameter 'suffixes' expects a value of type String\[1\] or Array\[String\[1\], 1\]} ) end diff --git a/spec/functions/ensure_resource_spec.rb b/spec/functions/ensure_resource_spec.rb index 3ec6dff7a..7508eed94 100644 --- a/spec/functions/ensure_resource_spec.rb +++ b/spec/functions/ensure_resource_spec.rb @@ -15,7 +15,7 @@ it { pending('should not accept numbers as arguments') - is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError) + expect(subject).to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError) } context 'when given an empty catalog' do @@ -122,7 +122,7 @@ context 'when trying to add params' do it { - is_expected.to run \ + expect(subject).to run \ .with_params('User', 'username1', 'ensure' => 'present', 'shell' => true) \ .and_raise_error(Puppet::Resource::Catalog::DuplicateResourceError, %r{User\[username1\] is already declared}) } diff --git a/spec/functions/get_module_path_spec.rb b/spec/functions/get_module_path_spec.rb index 0896b9633..364e889d7 100644 --- a/spec/functions/get_module_path_spec.rb +++ b/spec/functions/get_module_path_spec.rb @@ -32,13 +32,13 @@ def initialize(path) end it 'runs against foo' do - is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) + expect(subject).to run.with_params('foo').and_return(path_of_module_foo.path) end it 'when the modulepath is a list' do Puppet[:modulepath] = modulepath + 'tmp/something_else' - is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) + expect(subject).to run.with_params('foo').and_return(path_of_module_foo.path) end end @@ -50,12 +50,12 @@ def initialize(path) end it 'runs against foo' do - is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) + expect(subject).to run.with_params('foo').and_return(path_of_module_foo.path) end it 'when the modulepath is a list' do Puppet[:modulepath] = modulepath + 'tmp/something_else' - is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) + expect(subject).to run.with_params('foo').and_return(path_of_module_foo.path) end end end diff --git a/spec/functions/grep_spec.rb b/spec/functions/grep_spec.rb index a0ada3606..2fd927fc8 100644 --- a/spec/functions/grep_spec.rb +++ b/spec/functions/grep_spec.rb @@ -9,12 +9,12 @@ it { pending('grep does not actually check this, and raises NoMethodError instead') - is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) + expect(subject).to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) } it { pending('grep does not actually check this, and raises NoMethodError instead') - is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) + expect(subject).to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) } it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/is_a_spec.rb b/spec/functions/is_a_spec.rb index fd510b277..a5796695f 100644 --- a/spec/functions/is_a_spec.rb +++ b/spec/functions/is_a_spec.rb @@ -17,17 +17,17 @@ it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) } it 'succeeds when comparing a string and a string' do - is_expected.to run.with_params('hello world', String).and_return(true) + expect(subject).to run.with_params('hello world', String).and_return(true) end it 'fails when comparing an integer and a string' do - is_expected.to run.with_params(5, String).and_return(false) + expect(subject).to run.with_params(5, String).and_return(false) end it 'suceeds when comparing an UTF8 and double byte characters' do comparison_array = ['このテキスト', 'ŧћịś ŧêχŧ'] comparison_array.each do |comparison_value| - is_expected.to run.with_params(comparison_value, String).and_return(true) + expect(subject).to run.with_params(comparison_value, String).and_return(true) end end end diff --git a/spec/functions/join_keys_to_values_spec.rb b/spec/functions/join_keys_to_values_spec.rb index 5d9942e0e..591e51085 100644 --- a/spec/functions/join_keys_to_values_spec.rb +++ b/spec/functions/join_keys_to_values_spec.rb @@ -26,11 +26,11 @@ end it 'runs join_keys_to_values(, ":") and return the proper array' do - is_expected.to run.with_params({ 'key1' => 'value1', 'key2' => 'value2' }, ':').and_return(['key1:value1', 'key2:value2']) + expect(subject).to run.with_params({ 'key1' => 'value1', 'key2' => 'value2' }, ':').and_return(['key1:value1', 'key2:value2']) end it 'runs join_keys_to_values(, " ") and return the proper array' do expected_result = ['key1 value1', 'key2 value2', 'key2 value3'] - is_expected.to run.with_params({ 'key1' => 'value1', 'key2' => ['value2', 'value3'] }, ' ').and_return(expected_result) + expect(subject).to run.with_params({ 'key1' => 'value1', 'key2' => ['value2', 'value3'] }, ' ').and_return(expected_result) end end diff --git a/spec/functions/loadjson_spec.rb b/spec/functions/loadjson_spec.rb index bf88e0070..0196bfe69 100644 --- a/spec/functions/loadjson_spec.rb +++ b/spec/functions/loadjson_spec.rb @@ -102,7 +102,7 @@ expect(URI).to receive(:open).with(filename).and_return(json) expect(JSON).to receive(:parse).with(json).and_return(data).once end - is_expected.to run.with_params(filename).and_return(data) + expect(subject).to run.with_params(filename).and_return(data) } end @@ -123,7 +123,7 @@ expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json) expect(JSON).to receive(:parse).with(json).and_return(data).once end - is_expected.to run.with_params(filename).and_return(data) + expect(subject).to run.with_params(filename).and_return(data) } end @@ -144,7 +144,7 @@ expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json) expect(JSON).to receive(:parse).with(json).and_return(data).once end - is_expected.to run.with_params(filename).and_return(data) + expect(subject).to run.with_params(filename).and_return(data) } end @@ -162,7 +162,7 @@ expect(URI).to receive(:open).with(filename).and_return(json) expect(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!' end - is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') + expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } end @@ -177,7 +177,7 @@ else expect(URI).to receive(:open).with(filename).and_raise URI::Error, '404 File not Found' end - is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') + expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } end end diff --git a/spec/functions/loadyaml_spec.rb b/spec/functions/loadyaml_spec.rb index 156095dde..68df64e37 100644 --- a/spec/functions/loadyaml_spec.rb +++ b/spec/functions/loadyaml_spec.rb @@ -12,7 +12,7 @@ it "'default' => 'value'" do expect(File).to receive(:exists?).with(filename).and_return(false).once expect(YAML).not_to receive(:load_file) - is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') + expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') end end @@ -23,7 +23,7 @@ it "returns 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値'" do expect(File).to receive(:exists?).with(filename).and_return(true).once expect(YAML).to receive(:load_file).with(filename).and_return(data).once - is_expected.to run.with_params(filename).and_return(data) + expect(subject).to run.with_params(filename).and_return(data) end end @@ -33,7 +33,7 @@ it 'filename /tmp/doesexist' do expect(File).to receive(:exists?).with(filename).and_return(true).once allow(YAML).to receive(:load_file).with(filename).once.and_raise(StandardError, 'Something terrible have happened!') - is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') + expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') end end @@ -46,7 +46,7 @@ it { expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(yaml) expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once - is_expected.to run.with_params(filename).and_return(data) + expect(subject).to run.with_params(filename).and_return(data) } end @@ -60,7 +60,7 @@ it { expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(yaml) expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once - is_expected.to run.with_params(filename).and_return(data) + expect(subject).to run.with_params(filename).and_return(data) } end @@ -74,7 +74,7 @@ it { expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(yaml) expect(YAML).to receive(:safe_load).with(yaml).and_return(data).once - is_expected.to run.with_params(filename).and_return(data) + expect(subject).to run.with_params(filename).and_return(data) } end @@ -86,7 +86,7 @@ it { expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(yaml) expect(YAML).to receive(:safe_load).with(yaml).and_raise StandardError, 'Cannot parse data' - is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') + expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } end @@ -97,7 +97,7 @@ it { expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_raise OpenURI::HTTPError, '404 File not Found' - is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') + expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } end end diff --git a/spec/functions/member_spec.rb b/spec/functions/member_spec.rb index f5248de3f..1a5c00ad0 100644 --- a/spec/functions/member_spec.rb +++ b/spec/functions/member_spec.rb @@ -9,7 +9,7 @@ it { pending('Current implementation ignores parameters after the first.') - is_expected.to run.with_params([], [], []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params([], [], []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([], '').and_return(false) } diff --git a/spec/functions/merge_spec.rb b/spec/functions/merge_spec.rb index f0edabf7f..04472e34d 100644 --- a/spec/functions/merge_spec.rb +++ b/spec/functions/merge_spec.rb @@ -6,7 +6,7 @@ it { is_expected.not_to eq(nil) } it { - is_expected.to run \ + expect(subject).to run \ .with_params({}, 'two') \ .and_raise_error( ArgumentError, \ @@ -15,13 +15,13 @@ } it { - is_expected.to run \ + expect(subject).to run \ .with_params({}, 1) \ .and_raise_error(ArgumentError, %r{parameter 'args' expects a value of type Undef, Hash, or String, got Integer}) } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'one' => 1, 'three' => { 'four' => 4 } }, 'two' => 'dos', 'three' => { 'five' => 5 }) \ .and_return('one' => 1, 'three' => { 'five' => 5 }, 'two' => 'dos') } @@ -40,28 +40,28 @@ it { is_expected.to run.with_params({ 'key' => 'value1' }, 'key' => 'value2').and_return('key' => 'value2') } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1' }, { 'key2' => 'value2' }, 'key3' => 'value3') \ .and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3') } describe 'should accept iterable and merge produced hashes' do it { - is_expected.to run \ + expect(subject).to run \ .with_params([1, 2, 3]) \ .with_lambda { |_hsh, val| { val => val } } \ .and_return(1 => 1, 2 => 2, 3 => 3) } it { - is_expected.to run \ + expect(subject).to run \ .with_params([1, 2, 3]) \ .with_lambda { |_hsh, val| { val => val } unless val == 2 } \ .and_return(1 => 1, 3 => 3) } it { - is_expected.to run \ + expect(subject).to run \ .with_params([1, 2, 3]) \ # rubocop:disable Style/Semicolon .with_lambda { |_hsh, val| raise StopIteration if val == 3; { val => val } } \ @@ -69,21 +69,21 @@ } it { - is_expected.to run \ + expect(subject).to run \ .with_params(['a', 'b', 'b', 'c', 'b']) \ .with_lambda { |hsh, val| { val => (hsh[val] || 0) + 1 } } \ .and_return('a' => 1, 'b' => 3, 'c' => 1) } it { - is_expected.to run \ + expect(subject).to run \ .with_params(['a', 'b', 'c']) \ .with_lambda { |_hsh, idx, val| { idx => val } } \ .and_return(0 => 'a', 1 => 'b', 2 => 'c') } it { - is_expected.to run \ + expect(subject).to run \ .with_params('a' => 'A', 'b' => 'B', 'c' => 'C') \ .with_lambda { |_hsh, key, val| { key => "#{key}#{val}" } } \ .and_return('a' => 'aA', 'b' => 'bB', 'c' => 'cC') diff --git a/spec/functions/parsejson_spec.rb b/spec/functions/parsejson_spec.rb index 991d52e6c..74c0c701c 100644 --- a/spec/functions/parsejson_spec.rb +++ b/spec/functions/parsejson_spec.rb @@ -4,72 +4,72 @@ describe 'parsejson' do it 'exists' do - is_expected.not_to eq(nil) + expect(subject).not_to eq(nil) end it 'raises an error if called without any arguments' do - is_expected.to run.with_params - .and_raise_error(%r{wrong number of arguments}i) + expect(subject).to run.with_params + .and_raise_error(%r{wrong number of arguments}i) end context 'with correct JSON data' do it 'is able to parse JSON this is a null' do - is_expected.to run.with_params('null').and_return(nil) + expect(subject).to run.with_params('null').and_return(nil) end it 'is able to parse JSON that is a string' do - is_expected.to run.with_params('"a string"').and_return('a string') + expect(subject).to run.with_params('"a string"').and_return('a string') end it 'is able to parse JSON data with a Hash' do - is_expected.to run.with_params('{"a":"1","b":"2"}') - .and_return('a' => '1', 'b' => '2') + expect(subject).to run.with_params('{"a":"1","b":"2"}') + .and_return('a' => '1', 'b' => '2') end it 'is able to parse JSON data with an Array' do - is_expected.to run.with_params('["a","b","c"]') - .and_return(['a', 'b', 'c']) + expect(subject).to run.with_params('["a","b","c"]') + .and_return(['a', 'b', 'c']) end it 'is able to parse empty JSON values' do actual_array = ['[]', '{}'] expected = [[], {}] actual_array.each_with_index do |actual, index| - is_expected.to run.with_params(actual).and_return(expected[index]) + expect(subject).to run.with_params(actual).and_return(expected[index]) end end it 'is able to parse JSON data with a mixed structure' do - is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}') - .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [true, false] }) + expect(subject).to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}') + .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [true, false] }) end it 'is able to parse JSON data with a UTF8 and double byte characters' do - is_expected.to run.with_params('{"×":"これ","ý":"記号","です":{"©":["Á","ß"]}}') - .and_return('×' => 'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] }) + expect(subject).to run.with_params('{"×":"これ","ý":"記号","です":{"©":["Á","ß"]}}') + .and_return('×' => 'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] }) end it 'does not return the default value if the data was parsed correctly' do - is_expected.to run.with_params('{"a":"1"}', 'default_value') - .and_return('a' => '1') + expect(subject).to run.with_params('{"a":"1"}', 'default_value') + .and_return('a' => '1') end end context 'with incorrect JSON data' do it 'raises an error with invalid JSON and no default' do - is_expected.to run.with_params('error') - .and_raise_error(Puppet::Util::Json::ParseError) + expect(subject).to run.with_params('error') + .and_raise_error(Puppet::Util::Json::ParseError) end it 'supports a structure for a default value' do - is_expected.to run.with_params('', 'a' => '1') - .and_return('a' => '1') + expect(subject).to run.with_params('', 'a' => '1') + .and_return('a' => '1') end [1, 1.2, nil, true, false, [], {}, :yaml].each do |value| it "returns the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do - is_expected.to run.with_params(value, 'default_value') - .and_return('default_value') + expect(subject).to run.with_params(value, 'default_value') + .and_return('default_value') end end end diff --git a/spec/functions/parsepson_spec.rb b/spec/functions/parsepson_spec.rb index 76e61be83..26227fb22 100644 --- a/spec/functions/parsepson_spec.rb +++ b/spec/functions/parsepson_spec.rb @@ -5,63 +5,63 @@ describe 'parsepson' do if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? it 'exists' do - is_expected.not_to eq(nil) + expect(subject).not_to eq(nil) end it 'raises an error if called without any arguments' do - is_expected.to run.with_params - .and_raise_error(%r{'parsepson' expects between 1 and 2 arguments, got none}i) + expect(subject).to run.with_params + .and_raise_error(%r{'parsepson' expects between 1 and 2 arguments, got none}i) end context 'with correct PSON data' do it 'is able to parse PSON data with a Hash' do - is_expected.to run.with_params('{"a":"1","b":"2"}') - .and_return('a' => '1', 'b' => '2') + expect(subject).to run.with_params('{"a":"1","b":"2"}') + .and_return('a' => '1', 'b' => '2') end it 'is able to parse PSON data with an Array' do - is_expected.to run.with_params('["a","b","c"]') - .and_return(['a', 'b', 'c']) + expect(subject).to run.with_params('["a","b","c"]') + .and_return(['a', 'b', 'c']) end it 'is able to parse empty PSON values' do actual_array = ['[]', '{}'] expected = [[], {}] actual_array.each_with_index do |actual, index| - is_expected.to run.with_params(actual).and_return(expected[index]) + expect(subject).to run.with_params(actual).and_return(expected[index]) end end it 'is able to parse PSON data with a mixed structure' do - is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}') - .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [true, false] }) + expect(subject).to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}') + .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [true, false] }) end it 'is able to parse PSON data with a UTF8 and double byte characters' do - is_expected.to run.with_params('{"×":"これ","ý":"記号","です":{"©":["Á","ß"]}}') - .and_return('×' => 'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] }) + expect(subject).to run.with_params('{"×":"これ","ý":"記号","です":{"©":["Á","ß"]}}') + .and_return('×' => 'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] }) end it 'does not return the default value if the data was parsed correctly' do - is_expected.to run.with_params('{"a":"1"}', 'default_value') - .and_return('a' => '1') + expect(subject).to run.with_params('{"a":"1"}', 'default_value') + .and_return('a' => '1') end end context 'with incorrect PSON data' do it 'raises an error with invalid PSON and no default' do - is_expected.to run.with_params('invalid') - .and_raise_error(PSON::ParserError) + expect(subject).to run.with_params('invalid') + .and_raise_error(PSON::ParserError) end it 'returns the default value for an invalid PSON and a given default' do - is_expected.to run.with_params('invalid', 'default_value') - .and_return('default_value') + expect(subject).to run.with_params('invalid', 'default_value') + .and_return('default_value') end it 'supports a structure for a default value' do - is_expected.to run.with_params('invalid', 'a' => '1') - .and_return('a' => '1') + expect(subject).to run.with_params('invalid', 'a' => '1') + .and_return('a' => '1') end end end diff --git a/spec/functions/parseyaml_spec.rb b/spec/functions/parseyaml_spec.rb index f4ce41993..5e114c4a9 100644 --- a/spec/functions/parseyaml_spec.rb +++ b/spec/functions/parseyaml_spec.rb @@ -4,71 +4,71 @@ describe 'parseyaml' do it 'exists' do - is_expected.not_to eq(nil) + expect(subject).not_to eq(nil) end it 'raises an error if called without any arguments' do - is_expected.to run.with_params - .and_raise_error(%r{wrong number of arguments}i) + expect(subject).to run.with_params + .and_raise_error(%r{wrong number of arguments}i) end context 'with correct YAML data' do it 'is able to parse a YAML data with a String' do actual_array = ['--- just a string', 'just a string'] actual_array.each do |actual| - is_expected.to run.with_params(actual).and_return('just a string') + expect(subject).to run.with_params(actual).and_return('just a string') end end it 'is able to parse YAML data with a Hash' do - is_expected.to run.with_params("---\na: '1'\nb: '2'\n") - .and_return('a' => '1', 'b' => '2') + expect(subject).to run.with_params("---\na: '1'\nb: '2'\n") + .and_return('a' => '1', 'b' => '2') end it 'is able to parse YAML data with an Array' do - is_expected.to run.with_params("---\n- a\n- b\n- c\n") - .and_return(['a', 'b', 'c']) + expect(subject).to run.with_params("---\n- a\n- b\n- c\n") + .and_return(['a', 'b', 'c']) end it 'is able to parse YAML data with a mixed structure' do - is_expected.to run.with_params("---\na: '1'\nb: 2\nc:\n d:\n - :a\n - true\n - false\n") - .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [:a, true, false] }) + expect(subject).to run.with_params("---\na: '1'\nb: 2\nc:\n d:\n - :a\n - true\n - false\n") + .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [:a, true, false] }) end it 'is able to parse YAML data with a UTF8 and double byte characters' do - is_expected.to run.with_params("---\na: ×\nこれ: 記号\nです:\n ©:\n - Á\n - ß\n") - .and_return('a' => '×', 'これ' => '記号', 'です' => { '©' => ['Á', 'ß'] }) + expect(subject).to run.with_params("---\na: ×\nこれ: 記号\nです:\n ©:\n - Á\n - ß\n") + .and_return('a' => '×', 'これ' => '記号', 'です' => { '©' => ['Á', 'ß'] }) end it 'does not return the default value if the data was parsed correctly' do - is_expected.to run.with_params("---\na: '1'\n", 'default_value') - .and_return('a' => '1') + expect(subject).to run.with_params("---\na: '1'\n", 'default_value') + .and_return('a' => '1') end end it 'raises an error with invalid YAML and no default' do - is_expected.to run.with_params('["one"') - .and_raise_error(Psych::SyntaxError) + expect(subject).to run.with_params('["one"') + .and_raise_error(Psych::SyntaxError) end context 'with incorrect YAML data' do it 'supports a structure for a default value' do - is_expected.to run.with_params('', 'a' => '1') - .and_return('a' => '1') + expect(subject).to run.with_params('', 'a' => '1') + .and_return('a' => '1') end [1, 1.2, nil, true, false, [], {}, :yaml].each do |value| it "returns the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do - is_expected.to run.with_params(value, 'default_value') - .and_return('default_value') + expect(subject).to run.with_params(value, 'default_value') + .and_return('default_value') end end context 'when running on modern rubies' do ['---', '...', '*8', ''].each do |value| it "returns the default value for an incorrect #{value.inspect} string parameter" do - is_expected.to run.with_params(value, 'default_value') - .and_return('default_value') + expect(subject).to run.with_params(value, 'default_value') + .and_return('default_value') end end end diff --git a/spec/functions/powershell_escape_spec.rb b/spec/functions/powershell_escape_spec.rb index 8f0502251..804015761 100644 --- a/spec/functions/powershell_escape_spec.rb +++ b/spec/functions/powershell_escape_spec.rb @@ -20,8 +20,8 @@ it { is_expected.to run.with_params('foo bar').and_return('foo` bar') } it { - is_expected.to run.with_params('~`!@#$%^&*()_-=[]\{}|;\':",./<>?') - .and_return('~``!@#`$%^&*()_-=[]\{}`|;`\':\\`",./<>?') + expect(subject).to run.with_params('~`!@#$%^&*()_-=[]\{}|;\':",./<>?') + .and_return('~``!@#`$%^&*()_-=[]\{}`|;`\':\\`",./<>?') } end end diff --git a/spec/functions/prefix_spec.rb b/spec/functions/prefix_spec.rb index db6b2b91b..9ede98655 100644 --- a/spec/functions/prefix_spec.rb +++ b/spec/functions/prefix_spec.rb @@ -8,7 +8,7 @@ it { pending('Current implementation ignores parameters after the second.') - is_expected.to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{expected first argument to be an Array or a Hash}) } @@ -27,7 +27,7 @@ it { is_expected.to run.with_params({ 'key' => 'value' }, 'pre').and_return('prekey' => 'value') } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'pre') \ .and_return('prekey1' => 'value1', 'prekey2' => 'value2', 'prekey3' => 'value3') } diff --git a/spec/functions/pw_hash_spec.rb b/spec/functions/pw_hash_spec.rb index 7af868d35..a1579870a 100644 --- a/spec/functions/pw_hash_spec.rb +++ b/spec/functions/pw_hash_spec.rb @@ -103,8 +103,8 @@ it { is_expected.to run.with_params(Puppet::Pops::Types::PSensitiveType::Sensitive.new('password'), 'md5', 'salt').and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') } it { - is_expected.to run.with_params(Puppet::Pops::Types::PSensitiveType::Sensitive.new('password'), 'md5', Puppet::Pops::Types::PSensitiveType::Sensitive.new('salt')) - .and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') + expect(subject).to run.with_params(Puppet::Pops::Types::PSensitiveType::Sensitive.new('password'), 'md5', Puppet::Pops::Types::PSensitiveType::Sensitive.new('salt')) + .and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') } it { is_expected.to run.with_params('password', 'md5', Puppet::Pops::Types::PSensitiveType::Sensitive.new('salt')).and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') } diff --git a/spec/functions/range_spec.rb b/spec/functions/range_spec.rb index cd3d645da..384a43992 100644 --- a/spec/functions/range_spec.rb +++ b/spec/functions/range_spec.rb @@ -10,7 +10,7 @@ it { pending('Current implementation ignores parameters after the third.') - is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('1..2..3').and_raise_error(Puppet::ParseError, %r{Unable to compute range}i) } @@ -20,56 +20,56 @@ describe 'signature validation in puppet4', if: RSpec.configuration.puppet_future do it { pending 'the puppet 4 implementation' - is_expected.to run.with_params.and_raise_error(ArgumentError) + expect(subject).to run.with_params.and_raise_error(ArgumentError) } it { pending 'the puppet 4 implementation' - is_expected.to run.with_params('').and_raise_error(ArgumentError) + expect(subject).to run.with_params('').and_raise_error(ArgumentError) } it { pending 'the puppet 4 implementation' - is_expected.to run.with_params({}).and_raise_error(ArgumentError) + expect(subject).to run.with_params({}).and_raise_error(ArgumentError) } it { pending 'the puppet 4 implementation' - is_expected.to run.with_params([]).and_raise_error(ArgumentError) + expect(subject).to run.with_params([]).and_raise_error(ArgumentError) } it { pending 'the puppet 4 implementation' - is_expected.to run.with_params(true).and_raise_error(ArgumentError) + expect(subject).to run.with_params(true).and_raise_error(ArgumentError) } it { - is_expected.to run.with_params(1, 2, 'foo').and_raise_error(ArgumentError) + expect(subject).to run.with_params(1, 2, 'foo').and_raise_error(ArgumentError) } it { pending 'the puppet 4 implementation' - is_expected.to run.with_params(1, 2, []).and_raise_error(ArgumentError) + expect(subject).to run.with_params(1, 2, []).and_raise_error(ArgumentError) } it { pending 'the puppet 4 implementation' - is_expected.to run.with_params(1, 2, {}).and_raise_error(ArgumentError) + expect(subject).to run.with_params(1, 2, {}).and_raise_error(ArgumentError) } it { pending 'the puppet 4 implementation' - is_expected.to run.with_params(1, 2, true).and_raise_error(ArgumentError) + expect(subject).to run.with_params(1, 2, true).and_raise_error(ArgumentError) } it { pending 'the puppet 4 implementation' - is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(ArgumentError) + expect(subject).to run.with_params(1, 2, 3, 4).and_raise_error(ArgumentError) } it { pending 'the puppet 4 implementation' - is_expected.to run.with_params('1..2..3').and_raise_error(ArgumentError) + expect(subject).to run.with_params('1..2..3').and_raise_error(ArgumentError) } end @@ -157,17 +157,17 @@ describe 'when passing mixed arguments as bounds' do it { pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Layout/LineLength : unable to cut line to required length - is_expected.to run.with_params('0', 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) + expect(subject).to run.with_params('0', 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) } it { pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Layout/LineLength : unable to cut line to required length - is_expected.to run.with_params(0, 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) + expect(subject).to run.with_params(0, 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) } it { pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Layout/LineLength : unable to cut line to required length - is_expected.to run.with_params('h0', 'ha').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) + expect(subject).to run.with_params('h0', 'ha').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) } end end diff --git a/spec/functions/regexpescape_spec.rb b/spec/functions/regexpescape_spec.rb index 053aeafe5..ecc9c06ca 100644 --- a/spec/functions/regexpescape_spec.rb +++ b/spec/functions/regexpescape_spec.rb @@ -9,7 +9,7 @@ it { pending('Current implementation ignores parameters after the first.') - is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } @@ -20,7 +20,7 @@ describe 'handling normal strings' do it 'calls ruby\'s Regexp.escape function' do expect(Regexp).to receive(:escape).with('regexp_string').and_return('escaped_regexp_string').once - is_expected.to run.with_params('regexp_string').and_return('escaped_regexp_string') + expect(subject).to run.with_params('regexp_string').and_return('escaped_regexp_string') end end @@ -28,7 +28,7 @@ it 'calls ruby\'s Regexp.escape function' do regexp_string = AlsoString.new('regexp_string') expect(Regexp).to receive(:escape).with(regexp_string).and_return('escaped_regexp_string').once - is_expected.to run.with_params(regexp_string).and_return('escaped_regexp_string') + expect(subject).to run.with_params(regexp_string).and_return('escaped_regexp_string') end end diff --git a/spec/functions/reject_spec.rb b/spec/functions/reject_spec.rb index 78a86bdd9..5c52e8f5e 100644 --- a/spec/functions/reject_spec.rb +++ b/spec/functions/reject_spec.rb @@ -9,12 +9,12 @@ it { pending('reject does not actually check this, and raises NoMethodError instead') - is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) + expect(subject).to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) } it { pending('reject does not actually check this, and raises NoMethodError instead') - is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) + expect(subject).to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) } it { is_expected.to run.with_params([], 'two').and_return([]) } diff --git a/spec/functions/reverse_spec.rb b/spec/functions/reverse_spec.rb index 6e1c7e49b..ac63a03d1 100644 --- a/spec/functions/reverse_spec.rb +++ b/spec/functions/reverse_spec.rb @@ -8,7 +8,7 @@ it { pending('Current implementation ignores parameters after the first.') - is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } diff --git a/spec/functions/shell_escape_spec.rb b/spec/functions/shell_escape_spec.rb index 09d09e19a..941a2472b 100644 --- a/spec/functions/shell_escape_spec.rb +++ b/spec/functions/shell_escape_spec.rb @@ -20,8 +20,8 @@ it { is_expected.to run.with_params('foo bar').and_return('foo\ bar') } it { - is_expected.to run.with_params('~`!@#$%^&*()_-=[]\{}|;\':",./<>?') - .and_return('\~\`\!@\#\$\%\^\&\*\(\)_-\=\[\]\\\\\{\}\|\;\\\':\",./\<\>\?') + expect(subject).to run.with_params('~`!@#$%^&*()_-=[]\{}|;\':",./<>?') + .and_return('\~\`\!@\#\$\%\^\&\*\(\)_-\=\[\]\\\\\{\}\|\;\\\':\",./\<\>\?') } end diff --git a/spec/functions/shell_join_spec.rb b/spec/functions/shell_join_spec.rb index 435f170de..9563efcbe 100644 --- a/spec/functions/shell_join_spec.rb +++ b/spec/functions/shell_join_spec.rb @@ -17,8 +17,8 @@ it { is_expected.to run.with_params(['foo', 'bar baz']).and_return('foo bar\ baz') } it { - is_expected.to run.with_params(['~`!@#$', '%^&*()_-=', '[]\{}|;\':"', ',./<>?']) - .and_return('\~\`\!@\#\$ \%\^\&\*\(\)_-\= \[\]\\\\\{\}\|\;\\\':\" ,./\<\>\?') + expect(subject).to run.with_params(['~`!@#$', '%^&*()_-=', '[]\{}|;\':"', ',./<>?']) + .and_return('\~\`\!@\#\$ \%\^\&\*\(\)_-\= \[\]\\\\\{\}\|\;\\\':\" ,./\<\>\?') } context 'with UTF8 and double byte characters' do diff --git a/spec/functions/shell_split_spec.rb b/spec/functions/shell_split_spec.rb index fc1156de4..68faece7a 100644 --- a/spec/functions/shell_split_spec.rb +++ b/spec/functions/shell_split_spec.rb @@ -20,13 +20,13 @@ it { is_expected.to run.with_params('foo bar').and_return(['foo', 'bar']) } it { - is_expected.to run.with_params('\~\`\!@\#\$\%\^\&\*\(\)_\+-\=\[\]\\\\\{\}\|\;\\\':\",./\<\>\?') - .and_return(['~`!@#$%^&*()_+-=[]\{}|;\':",./<>?']) + expect(subject).to run.with_params('\~\`\!@\#\$\%\^\&\*\(\)_\+-\=\[\]\\\\\{\}\|\;\\\':\",./\<\>\?') + .and_return(['~`!@#$%^&*()_+-=[]\{}|;\':",./<>?']) } it { - is_expected.to run.with_params('\~\`\!@\#\$ \%\^\&\*\(\)_\+-\= \[\]\\\\\{\}\|\;\\\':\" ,./\<\>\?') - .and_return(['~`!@#$', '%^&*()_+-=', '[]\{}|;\':"', ',./<>?']) + expect(subject).to run.with_params('\~\`\!@\#\$ \%\^\&\*\(\)_\+-\= \[\]\\\\\{\}\|\;\\\':\" ,./\<\>\?') + .and_return(['~`!@#$', '%^&*()_+-=', '[]\{}|;\':"', ',./<>?']) } context 'with UTF8 and double byte characters' do diff --git a/spec/functions/shuffle_spec.rb b/spec/functions/shuffle_spec.rb index af57e40b1..938f279f7 100644 --- a/spec/functions/shuffle_spec.rb +++ b/spec/functions/shuffle_spec.rb @@ -8,7 +8,7 @@ it { pending('Current implementation ignores parameters after the first.') - is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } diff --git a/spec/functions/squeeze_spec.rb b/spec/functions/squeeze_spec.rb index 6eee9dd6f..337ac85c2 100644 --- a/spec/functions/squeeze_spec.rb +++ b/spec/functions/squeeze_spec.rb @@ -26,19 +26,19 @@ context 'when squeezing values in an array' do it { - is_expected.to run \ + expect(subject).to run \ .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc']) \ .and_return(['', 'a', 'a', 'abc']) } it { - is_expected.to run \ + expect(subject).to run \ .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc'], 'a') \ .and_return(['', 'a', 'a', 'abbbbbbbbbbcccccccccc']) } it { - is_expected.to run \ + expect(subject).to run \ .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc'], 'b-c') \ .and_return(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabc']) } diff --git a/spec/functions/stdlib_deferrable_epp_spec.rb b/spec/functions/stdlib_deferrable_epp_spec.rb index 71e959925..e50301575 100644 --- a/spec/functions/stdlib_deferrable_epp_spec.rb +++ b/spec/functions/stdlib_deferrable_epp_spec.rb @@ -7,7 +7,7 @@ end it { - is_expected.to run.with_params('mymod/template.epp', { 'foo' => 'bar' }).and_return('rendered') + expect(subject).to run.with_params('mymod/template.epp', { 'foo' => 'bar' }).and_return('rendered') } end @@ -23,7 +23,7 @@ it { foo = Puppet::Pops::Types::TypeFactory.deferred.create('join', [1, 2, 3]) # This kind_of matcher requires https://github.com/puppetlabs/rspec-puppet/pull/24 - is_expected.to run.with_params('mymod/template.epp', { 'foo' => foo }) # .and_return(kind_of Puppet::Pops::Types::PuppetObject) + expect(subject).to run.with_params('mymod/template.epp', { 'foo' => foo }) # .and_return(kind_of Puppet::Pops::Types::PuppetObject) } end end diff --git a/spec/functions/str2bool_spec.rb b/spec/functions/str2bool_spec.rb index a3553edfc..be126bcdd 100644 --- a/spec/functions/str2bool_spec.rb +++ b/spec/functions/str2bool_spec.rb @@ -8,7 +8,7 @@ it { pending('Current implementation ignores parameters after the first.') - is_expected.to run.with_params('true', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params('true', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{Unknown type of boolean given}) } diff --git a/spec/functions/str2saltedpbkdf2_spec.rb b/spec/functions/str2saltedpbkdf2_spec.rb index 94eb5b177..6da1bad0f 100644 --- a/spec/functions/str2saltedpbkdf2_spec.rb +++ b/spec/functions/str2saltedpbkdf2_spec.rb @@ -15,10 +15,10 @@ context 'when running with "Pa55w0rd", "Using s0m3 s@lt",and "50000" as params' do # rubocop:disable Layout/LineLength it { - is_expected.to run.with_params('Pa55w0rd', 'Using s0m3 s@lt', 50_000) - .and_return('password_hex' => '3577f79f7d2e73df1cf1eecc36da16fffcd3650126d79e797a8b227492d13de4cdd0656933b43118b7361692f755e5b3c1e0536f826d12442400f3467bcc8fb4ac2235d5648b0f1b0906d0712aecd265834319b5a42e98af2ced81597fd78d1ac916f6eff6122c3577bb120a9f534e2a5c9a58c7d1209e3914c967c6a467b594', - 'salt_hex' => '5573696e672073306d332073406c74', - 'iterations' => 50_000) + expect(subject).to run.with_params('Pa55w0rd', 'Using s0m3 s@lt', 50_000) + .and_return('password_hex' => '3577f79f7d2e73df1cf1eecc36da16fffcd3650126d79e797a8b227492d13de4cdd0656933b43118b7361692f755e5b3c1e0536f826d12442400f3467bcc8fb4ac2235d5648b0f1b0906d0712aecd265834319b5a42e98af2ced81597fd78d1ac916f6eff6122c3577bb120a9f534e2a5c9a58c7d1209e3914c967c6a467b594', + 'salt_hex' => '5573696e672073306d332073406c74', + 'iterations' => 50_000) } # rubocop:enable Layout/LineLength end diff --git a/spec/functions/str2saltedsha512_spec.rb b/spec/functions/str2saltedsha512_spec.rb index 8552d4236..0ee16fa05 100644 --- a/spec/functions/str2saltedsha512_spec.rb +++ b/spec/functions/str2saltedsha512_spec.rb @@ -14,33 +14,33 @@ if Puppet::PUPPETVERSION[0].to_i < 8 it { - is_expected.to run.with_params('') - .and_return('0f8a612f4eeed08e47b3875d00f33c5688f7926298f2d9b5fe19d1323f910bc78b6f7b5892596d2fabaa65e7a8d99b3768c102610cf0432c4827eee01f09451e3fae4f7a') + expect(subject).to run.with_params('') + .and_return('0f8a612f4eeed08e47b3875d00f33c5688f7926298f2d9b5fe19d1323f910bc78b6f7b5892596d2fabaa65e7a8d99b3768c102610cf0432c4827eee01f09451e3fae4f7a') } it { - is_expected.to run.with_params('password') - .and_return('0f8a612f43134376566c5707718d600effcfb17581fc9d3fa64d7f447dfda317c174ffdb498d2c5bd5c2075dab41c9d7ada5afbdc6b55354980eb5ba61802371e6b64956') + expect(subject).to run.with_params('password') + .and_return('0f8a612f43134376566c5707718d600effcfb17581fc9d3fa64d7f447dfda317c174ffdb498d2c5bd5c2075dab41c9d7ada5afbdc6b55354980eb5ba61802371e6b64956') } it { - is_expected.to run.with_params('verylongpassword') - .and_return('0f8a612f7a448537540e062daa8621f9bae326ca8ccb899e1bdb10e7c218cebfceae2530b856662565fdc4d81e986fc50cfbbc46d50436610ed9429ff5e43f2c45b5d039') + expect(subject).to run.with_params('verylongpassword') + .and_return('0f8a612f7a448537540e062daa8621f9bae326ca8ccb899e1bdb10e7c218cebfceae2530b856662565fdc4d81e986fc50cfbbc46d50436610ed9429ff5e43f2c45b5d039') } else it { - is_expected.to run.with_params('') - .and_return('a85c9d6f8c1eb1a625fd59e3cbca7dc7ab04ff1758d19ab99f098446e14a0a2a42e11afd1f4d6f17adfe2c772a3e6a821ee66a2564711431e14da96a3bff44593cf158ab') + expect(subject).to run.with_params('') + .and_return('a85c9d6f8c1eb1a625fd59e3cbca7dc7ab04ff1758d19ab99f098446e14a0a2a42e11afd1f4d6f17adfe2c772a3e6a821ee66a2564711431e14da96a3bff44593cf158ab') } it { - is_expected.to run.with_params('password') - .and_return('a85c9d6ff4e4dd6655ec2922ee9752550f2df4dc370e9739dd94899f62be6a42cc31fbfce3d62be35e0e8482696c931f63fb9286cf7b13d283660720c55f2a6304d06958') + expect(subject).to run.with_params('password') + .and_return('a85c9d6ff4e4dd6655ec2922ee9752550f2df4dc370e9739dd94899f62be6a42cc31fbfce3d62be35e0e8482696c931f63fb9286cf7b13d283660720c55f2a6304d06958') } it { - is_expected.to run.with_params('verylongpassword') - .and_return('a85c9d6fb810d0b8311c9a065c026e3179ae91fee3dbaf556f297e2fda2a8e3d8dd363977f9ef5c9b5da0cd518a5151a4e537928533291d68c9539d4d4b83da53b22a869') + expect(subject).to run.with_params('verylongpassword') + .and_return('a85c9d6fb810d0b8311c9a065c026e3179ae91fee3dbaf556f297e2fda2a8e3d8dd363977f9ef5c9b5da0cd518a5151a4e537928533291d68c9539d4d4b83da53b22a869') } end end diff --git a/spec/functions/suffix_spec.rb b/spec/functions/suffix_spec.rb index b4cedff9d..682f8b159 100644 --- a/spec/functions/suffix_spec.rb +++ b/spec/functions/suffix_spec.rb @@ -8,7 +8,7 @@ it { pending('Current implementation ignores parameters after the second.') - is_expected.to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{expected first argument to be an Array}) } @@ -28,7 +28,7 @@ it { is_expected.to run.with_params({ 'key' => 'value' }, 'post').and_return('keypost' => 'value') } it { - is_expected.to run \ + expect(subject).to run \ .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'post') \ .and_return('key1post' => 'value1', 'key2post' => 'value2', 'key3post' => 'value3') } diff --git a/spec/functions/swapcase_spec.rb b/spec/functions/swapcase_spec.rb index 4c77c81ae..1221f6afa 100644 --- a/spec/functions/swapcase_spec.rb +++ b/spec/functions/swapcase_spec.rb @@ -8,7 +8,7 @@ it { pending('Current implementation ignores parameters after the first.') - is_expected.to run.with_params('a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params('a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } @@ -44,6 +44,6 @@ end it 'accepts objects which extend String' do - is_expected.to run.with_params(AlsoString.new('OnE')).and_return('oNe') + expect(subject).to run.with_params(AlsoString.new('OnE')).and_return('oNe') end end diff --git a/spec/functions/to_json_pretty_spec.rb b/spec/functions/to_json_pretty_spec.rb index e14dc7e92..7158290ed 100644 --- a/spec/functions/to_json_pretty_spec.rb +++ b/spec/functions/to_json_pretty_spec.rb @@ -11,8 +11,8 @@ it { is_expected.to run.with_params('key' => 'value').and_return("{\n \"key\": \"value\"\n}\n") } it { - is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) - .and_return("{\n \"one\": {\n \"oneA\": \"A\",\n \"oneB\": {\n \"oneB1\": \"1\",\n \"oneB2\": \"2\"\n }\n },\n \"two\": [\n \"twoA\",\n \"twoB\"\n ]\n}\n") # rubocop:disable Layout/LineLength : Unable to reduce line to required length + expect(subject).to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) + .and_return("{\n \"one\": {\n \"oneA\": \"A\",\n \"oneB\": {\n \"oneB1\": \"1\",\n \"oneB2\": \"2\"\n }\n },\n \"two\": [\n \"twoA\",\n \"twoB\"\n ]\n}\n") # rubocop:disable Layout/LineLength : Unable to reduce line to required length } it { is_expected.to run.with_params({ 'one' => '1', 'two' => nil }, true).and_return("{\n \"one\": \"1\"\n}\n") } @@ -21,11 +21,11 @@ it { pending('Current implementation only elides nil values for arrays of depth=1') - is_expected.to run.with_params([[nil], 'two', nil, 'three'], true).and_return("[\n [\n\n ],\n \"two\",\n \"three\"\n]\n") + expect(subject).to run.with_params([[nil], 'two', nil, 'three'], true).and_return("[\n [\n\n ],\n \"two\",\n \"three\"\n]\n") } it { pending('Current implementation only elides nil values for hashes of depth=1') - is_expected.to run.with_params({ 'omg' => { 'lol' => nil }, 'what' => nil }, true).and_return("{\n}\n") + expect(subject).to run.with_params({ 'omg' => { 'lol' => nil }, 'what' => nil }, true).and_return("{\n}\n") } end diff --git a/spec/functions/to_json_spec.rb b/spec/functions/to_json_spec.rb index c3b732783..c27fa709a 100644 --- a/spec/functions/to_json_spec.rb +++ b/spec/functions/to_json_spec.rb @@ -14,8 +14,8 @@ it { is_expected.to run.with_params('key' => 'value').and_return('{"key":"value"}') } it { - is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) - .and_return('{"one":{"oneA":"A","oneB":{"oneB1":"1","oneB2":"2"}},"two":["twoA","twoB"]}') + expect(subject).to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) + .and_return('{"one":{"oneA":"A","oneB":{"oneB1":"1","oneB2":"2"}},"two":["twoA","twoB"]}') } it { is_expected.to run.with_params('‰').and_return('"‰"') } diff --git a/spec/functions/to_python_spec.rb b/spec/functions/to_python_spec.rb index 01b69224a..8a3899692 100644 --- a/spec/functions/to_python_spec.rb +++ b/spec/functions/to_python_spec.rb @@ -17,8 +17,8 @@ it { is_expected.to run.with_params('key' => 'value').and_return('{"key": "value"}') } it { - is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) - .and_return('{"one": {"oneA": "A", "oneB": {"oneB1": "1", "oneB2": "2"}}, "two": ["twoA", "twoB"]}') + expect(subject).to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) + .and_return('{"one": {"oneA": "A", "oneB": {"oneB1": "1", "oneB2": "2"}}, "two": ["twoA", "twoB"]}') } it { is_expected.to run.with_params('‰').and_return('"‰"') } diff --git a/spec/functions/to_ruby_spec.rb b/spec/functions/to_ruby_spec.rb index e2c9dd6f0..94d5de0f0 100644 --- a/spec/functions/to_ruby_spec.rb +++ b/spec/functions/to_ruby_spec.rb @@ -16,8 +16,8 @@ it { is_expected.to run.with_params('key' => 'value').and_return('{"key" => "value"}') } it { - is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) - .and_return('{"one" => {"oneA" => "A", "oneB" => {"oneB1" => "1", "oneB2" => "2"}}, "two" => ["twoA", "twoB"]}') + expect(subject).to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) + .and_return('{"one" => {"oneA" => "A", "oneB" => {"oneB1" => "1", "oneB2" => "2"}}, "two" => ["twoA", "twoB"]}') } it { is_expected.to run.with_params('‰').and_return('"‰"') } diff --git a/spec/functions/to_yaml_spec.rb b/spec/functions/to_yaml_spec.rb index c7162b05f..a014cabca 100644 --- a/spec/functions/to_yaml_spec.rb +++ b/spec/functions/to_yaml_spec.rb @@ -14,8 +14,8 @@ it { is_expected.to run.with_params('key' => 'value').and_return("---\nkey: value\n") } it { - is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) - .and_return("---\none:\n oneA: A\n oneB:\n oneB1: '1'\n oneB2: '2'\ntwo:\n- twoA\n- twoB\n") + expect(subject).to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB']) + .and_return("---\none:\n oneA: A\n oneB:\n oneB1: '1'\n oneB2: '2'\ntwo:\n- twoA\n- twoB\n") } it { is_expected.to run.with_params('‰').and_return("--- \"‰\"\n") } diff --git a/spec/functions/unix2dos_spec.rb b/spec/functions/unix2dos_spec.rb index d683daf8e..33c3e4799 100644 --- a/spec/functions/unix2dos_spec.rb +++ b/spec/functions/unix2dos_spec.rb @@ -7,23 +7,23 @@ it { is_expected.not_to eq(nil) } it do - is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) + expect(subject).to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) end it do - is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{Wrong number of arguments}) + expect(subject).to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{Wrong number of arguments}) end it do - is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) + expect(subject).to run.with_params([]).and_raise_error(Puppet::ParseError) end it do - is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError) + expect(subject).to run.with_params({}).and_raise_error(Puppet::ParseError) end it do - is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) + expect(subject).to run.with_params(1).and_raise_error(Puppet::ParseError) end end @@ -32,7 +32,7 @@ desired_output = "Hello\r\nWorld\r\n" it 'outputs dos format' do - is_expected.to run.with_params(sample_text).and_return(desired_output) + expect(subject).to run.with_params(sample_text).and_return(desired_output) end end @@ -41,7 +41,7 @@ desired_output = "Hello\r\nWorld\r\n" it 'outputs dos format' do - is_expected.to run.with_params(sample_text).and_return(desired_output) + expect(subject).to run.with_params(sample_text).and_return(desired_output) end end end diff --git a/spec/functions/uriescape_spec.rb b/spec/functions/uriescape_spec.rb index 004b7e8be..3304a3ccb 100644 --- a/spec/functions/uriescape_spec.rb +++ b/spec/functions/uriescape_spec.rb @@ -11,7 +11,7 @@ it { pending('Current implementation ignores parameters after the first.') - is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } @@ -22,7 +22,7 @@ describe 'handling normal strings' do it 'calls ruby\'s URI::DEFAULT_PARSER.escape function' do expect(URI::DEFAULT_PARSER).to receive(:escape).with('uri_string').and_return('escaped_uri_string').once - is_expected.to run.with_params('uri_string').and_return('escaped_uri_string') + expect(subject).to run.with_params('uri_string').and_return('escaped_uri_string') end end @@ -30,7 +30,7 @@ it 'calls ruby\'s URI::DEFAULT_PARSER.escape function' do uri_string = AlsoString.new('uri_string') expect(URI::DEFAULT_PARSER).to receive(:escape).with(uri_string).and_return('escaped_uri_string').once - is_expected.to run.with_params(uri_string).and_return('escaped_uri_string') + expect(subject).to run.with_params(uri_string).and_return('escaped_uri_string') end end diff --git a/spec/functions/validate_cmd_spec.rb b/spec/functions/validate_cmd_spec.rb index 2cbcc1b98..99f9529a3 100644 --- a/spec/functions/validate_cmd_spec.rb +++ b/spec/functions/validate_cmd_spec.rb @@ -13,24 +13,24 @@ it { pending('should implement stricter type checking') - is_expected.to run.with_params([], '', '').and_raise_error(Puppet::ParseError, %r{content must be a string}) + expect(subject).to run.with_params([], '', '').and_raise_error(Puppet::ParseError, %r{content must be a string}) } it { pending('should implement stricter type checking') - is_expected.to run.with_params('', [], '').and_raise_error(Puppet::ParseError, %r{checkscript must be a string}) + expect(subject).to run.with_params('', [], '').and_raise_error(Puppet::ParseError, %r{checkscript must be a string}) } it { pending('should implement stricter type checking') - is_expected.to run.with_params('', '', []).and_raise_error(Puppet::ParseError, %r{custom error message must be a string}) + expect(subject).to run.with_params('', '', []).and_raise_error(Puppet::ParseError, %r{custom error message must be a string}) } end context 'when validation fails' do context 'with % placeholder' do it { - is_expected.to run + expect(subject).to run .with_params('', "#{touch} % /no/such/file").and_raise_error(Puppet::ParseError, %r{Execution of '#{touch} \S+ \/no\/such\/file' returned 1:.*(cannot touch|o such file or)}) } @@ -39,7 +39,7 @@ context 'without % placeholder' do it { - is_expected.to run + expect(subject).to run .with_params('', "#{touch} /no/such/file").and_raise_error(Puppet::ParseError, %r{Execution of '#{touch} \/no\/such\/file \S+' returned 1:.*(cannot touch|o such file or)}) } diff --git a/spec/functions/validate_legacy_spec.rb b/spec/functions/validate_legacy_spec.rb index 1b2573dec..b4d66b171 100644 --- a/spec/functions/validate_legacy_spec.rb +++ b/spec/functions/validate_legacy_spec.rb @@ -12,7 +12,7 @@ expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once expect(scope).not_to receive(:function_validate_foo) expect(Puppet).not_to receive(:notice) - is_expected.to run.with_params('Integer', 'validate_foo', 5) + expect(subject).to run.with_params('Integer', 'validate_foo', 5) end end @@ -21,16 +21,16 @@ expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once expect(scope).not_to receive(:function_validate_foo) expect(subject.func).to receive(:call_function).with('fail', 'validate_legacy(Integer, ...) expects an Integer value, got String').once - is_expected.to run.with_params('Integer', 'validate_foo', '5') + expect(subject).to run.with_params('Integer', 'validate_foo', '5') end end describe 'when passing in undef' do - it 'works' do + it 'passes without failure' do expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once expect(scope).not_to receive(:function_validate_foo) expect(Puppet).not_to receive(:notice) - is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef) + expect(subject).to run.with_params('Optional[Integer]', 'validate_foo', :undef) end end @@ -39,7 +39,7 @@ expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once expect(scope).not_to receive(:function_validate_foo) expect(Puppet).not_to receive(:notice) - is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef, 1, 'foo') + expect(subject).to run.with_params('Optional[Integer]', 'validate_foo', :undef, 1, 'foo') end end end diff --git a/spec/functions/values_at_spec.rb b/spec/functions/values_at_spec.rb index ed9b1bfff..2d21d5406 100644 --- a/spec/functions/values_at_spec.rb +++ b/spec/functions/values_at_spec.rb @@ -10,7 +10,7 @@ it { pending('Current implementation ignores parameters after the first two.') - is_expected.to run.with_params([], 0, 1).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params([], 0, 1).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('', 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) } @@ -52,7 +52,7 @@ it { pending('fix this bounds check') - is_expected.to run.with_params([0, 1, 2], '0...3').and_return([0, 1, 2]) + expect(subject).to run.with_params([0, 1, 2], '0...3').and_return([0, 1, 2]) } end end diff --git a/spec/functions/xml_encode_spec.rb b/spec/functions/xml_encode_spec.rb index 6337db514..18a20a0c0 100644 --- a/spec/functions/xml_encode_spec.rb +++ b/spec/functions/xml_encode_spec.rb @@ -6,7 +6,7 @@ let(:string) { 'this is "my" complicated ' } it 'exists' do - is_expected.not_to be_nil + expect(subject).not_to be_nil end describe 'for XML text' do diff --git a/spec/functions/zip_spec.rb b/spec/functions/zip_spec.rb index a24895768..28049a05e 100644 --- a/spec/functions/zip_spec.rb +++ b/spec/functions/zip_spec.rb @@ -9,7 +9,7 @@ it { pending('Current implementation ignores parameters after the third.') - is_expected.to run.with_params([], [], true, []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + expect(subject).to run.with_params([], [], true, []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([], []).and_return([]) } From 19e72dcf3ac146bd5f842e7b31d1b3f9f0a4372b Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:29:53 +0100 Subject: [PATCH 22/33] (CONT-801) Autocorrect safe group 5 --- .rubocop_todo.yml | 37 ------------ lib/puppet/parser/functions/fqdn_rotate.rb | 2 +- lib/puppet/parser/functions/time.rb | 2 +- lib/puppet/provider/file_line/ruby.rb | 8 +-- spec/functions/deprecation_spec.rb | 6 +- spec/type_aliases/filemode_spec.rb | 1 - spec/type_aliases/yes_no_spec.rb | 1 - .../puppet/provider/file_line/ruby_spec.rb | 4 +- .../provider/file_line/ruby_spec_alter.rb | 58 +++++++++---------- .../provider/file_line/ruby_spec_use_cases.rb | 4 +- 10 files changed, 37 insertions(+), 86 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ccde6551f..be4180d5a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -223,49 +223,12 @@ Style/ClassAndModuleChildren: Exclude: - 'lib/puppet_x/stdlib/toml_dumper.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowedMethods, AllowedPatterns. -# AllowedMethods: ==, equal?, eql? -Style/ClassEqualityComparison: - Exclude: - - 'lib/puppet/parser/functions/fqdn_rotate.rb' - # Offense count: 2 # This cop supports unsafe autocorrection (--autocorrect-all). Style/CollectionCompact: Exclude: - 'lib/puppet/functions/to_json_pretty.rb' -# Offense count: 3 -# This cop supports safe autocorrection (--autocorrect). -Style/EachForSimpleLoop: - Exclude: - - 'spec/functions/deprecation_spec.rb' - -# Offense count: 2 -# This cop supports safe autocorrection (--autocorrect). -Style/Encoding: - Exclude: - - 'spec/type_aliases/filemode_spec.rb' - - 'spec/type_aliases/yes_no_spec.rb' - -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowedVars. -Style/FetchEnvVar: - Exclude: - - 'lib/puppet/parser/functions/time.rb' - -# Offense count: 32 -# This cop supports safe autocorrection (--autocorrect). -Style/FileWrite: - Exclude: - - 'lib/puppet/provider/file_line/ruby.rb' - - 'spec/unit/puppet/provider/file_line/ruby_spec.rb' - - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' - - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' - # Offense count: 5 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb index 5cab44b24..e6fec1413 100644 --- a/lib/puppet/parser/functions/fqdn_rotate.rb +++ b/lib/puppet/parser/functions/fqdn_rotate.rb @@ -43,7 +43,7 @@ if Puppet::Util.respond_to?(:deterministic_rand) offset = Puppet::Util.deterministic_rand(seed, elements).to_i else - return offset = Random.new(seed).rand(elements) if defined?(Random) == 'constant' && Random.class == Class + return offset = Random.new(seed).rand(elements) if defined?(Random) == 'constant' && Random.instance_of?(Class) old_seed = srand(seed) offset = rand(elements) diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb index da6261d04..147a9e994 100644 --- a/lib/puppet/parser/functions/time.rb +++ b/lib/puppet/parser/functions/time.rb @@ -35,7 +35,7 @@ module Puppet::Parser::Functions # There is probably a better way to handle Time Zone ... if time_zone && !time_zone.empty? - original_zone = ENV['TZ'] + original_zone = ENV.fetch('TZ', nil) local_time = time.clone local_time = local_time.utc diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 3695af7b9..b93de31e8 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -164,16 +164,12 @@ def handle_destroy_with_match end local_lines = lines - File.open(resource[:path], 'w') do |fh| - fh.write(local_lines.reject { |line| match_regex.match(line) }.join('')) - end + File.write(resource[:path], local_lines.reject { |line| match_regex.match(line) }.join('')) end def handle_destroy_line local_lines = lines - File.open(resource[:path], 'w') do |fh| - fh.write(local_lines.reject { |line| line.chomp == resource[:line] }.join('')) - end + File.write(resource[:path], local_lines.reject { |line| line.chomp == resource[:line] }.join('')) end def handle_append_line diff --git a/spec/functions/deprecation_spec.rb b/spec/functions/deprecation_spec.rb index 612f89825..1d60c9976 100644 --- a/spec/functions/deprecation_spec.rb +++ b/spec/functions/deprecation_spec.rb @@ -34,7 +34,7 @@ else expect(Puppet).to receive(:warning).with(include('heelo')).once end - (0..1).each do |_i| + 2.times do |_i| expect(subject).to run.with_params('key', 'heelo') end end @@ -42,7 +42,7 @@ it 'fails twice with message, with multiple calls. when strict= :error' do Puppet.settings[:strict] = :error expect(Puppet).not_to receive(:warning).with(include('heelo')) - (0..1).each do |_i| + 2.times do |_i| expect(subject).to run.with_params('key', 'heelo').and_raise_error(RuntimeError, %r{deprecation. key. heelo}) end end @@ -50,7 +50,7 @@ it 'displays nothing, despite multiple calls. strict= :off' do Puppet.settings[:strict] = :off expect(Puppet).not_to receive(:warning).with(include('heelo')) - (0..1).each do |_i| + 2.times do |_i| expect(subject).to run.with_params('key', 'heelo') end end diff --git a/spec/type_aliases/filemode_spec.rb b/spec/type_aliases/filemode_spec.rb index 56b5d8051..8e96c3c6f 100644 --- a/spec/type_aliases/filemode_spec.rb +++ b/spec/type_aliases/filemode_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'spec_helper' diff --git a/spec/type_aliases/yes_no_spec.rb b/spec/type_aliases/yes_no_spec.rb index b61d10a70..4adbb0ff5 100644 --- a/spec/type_aliases/yes_no_spec.rb +++ b/spec/type_aliases/yes_no_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'spec_helper' diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index fdca0510d..0871daf20 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -28,9 +28,7 @@ end before :each do - File.open(tmpfile, 'w') do |fh| - fh.write(content) - end + File.write(tmpfile, content) end describe 'line parameter' do diff --git a/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb b/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb index 2296ce845..c3cabed65 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb @@ -28,9 +28,7 @@ end before :each do - File.open(tmpfile, 'w') do |fh| - fh.write(content) - end + File.write(tmpfile, content) end describe '#create' do @@ -54,7 +52,7 @@ end it 'appends the line if no matches are found' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") } + File.write(tmpfile, "foo1\nfoo2") expect(provider.exists?).to be false provider.create expect(File.read(tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar") @@ -84,7 +82,7 @@ describe 'using match' do it 'raises an error if more than one line matches, and should not have modified the file' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") } + File.write(tmpfile, "foo1\nfoo=blah\nfoo2\nfoo=baz") expect { @provider.create }.to raise_error(Puppet::Error, %r{More than one line.*matches}) expect(File.read(tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz") end @@ -92,7 +90,7 @@ it 'replaces all lines that matches' do @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo = bar', match: '^foo\s*=.*$', multiple: true) @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") } + File.write(tmpfile, "foo1\nfoo=blah\nfoo2\nfoo=baz") @provider.create expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar") end @@ -100,7 +98,7 @@ it 'replaces all lines that match, even when some lines are correct' do @resource = Puppet::Type::File_line.new(name: 'neil', path: tmpfile, line: "\thard\tcore\t0\n", match: '^[ \t]hard[ \t]+core[ \t]+.*', multiple: true) @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("\thard\tcore\t90\n\thard\tcore\t0\n") } + File.write(tmpfile, "\thard\tcore\t90\n\thard\tcore\t0\n") @provider.create expect(File.read(tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0") end @@ -114,19 +112,19 @@ end it 'replaces a line that matches' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2") } + File.write(tmpfile, "foo1\nfoo=blah\nfoo2") @provider.create expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end it 'adds a new line if no lines match' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") } + File.write(tmpfile, "foo1\nfoo2") @provider.create expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n") end it 'does nothing if the exact line already exists' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = bar\nfoo2") } + File.write(tmpfile, "foo1\nfoo = bar\nfoo2") @provider.create expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end @@ -136,7 +134,7 @@ it 'replaces line' do @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'inserted = line', match: '^foo3$', append_on_no_match: false) @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } + File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo = baz") expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") end end @@ -145,7 +143,7 @@ it 'does not add line after no matches found' do @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'inserted = line', match: '^foo3$', append_on_no_match: false) @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } + File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo = baz") expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") end end @@ -181,7 +179,7 @@ end before :each do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nfoo = baz") } + File.write(tmpfile, "foo1\nfoo2\nfoo = baz") end describe 'inserts at match' do @@ -216,7 +214,7 @@ context 'with one line matching the after expression' do before :each do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } + File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo = baz") end it 'inserts the specified line after the line matching the "after" expression' do @@ -227,7 +225,7 @@ context 'with multiple lines matching the after expression' do before :each do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz") } + File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz") end it 'errors out stating "One or no line must match the pattern"' do @@ -248,7 +246,7 @@ end before :each do - File.open(tmpfile, 'w') { |fh| fh.write(content) } + File.write(tmpfile, content) end it 'appends the specified line to the file' do @@ -273,19 +271,19 @@ end it 'removes the line if it exists' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + File.write(tmpfile, "foo1\nfoo\nfoo2") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2") end it 'removes the line without touching the last new line' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\n") } + File.write(tmpfile, "foo1\nfoo\nfoo2\n") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2\n") end it 'removes any occurence of the line' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") } + File.write(tmpfile, "foo1\nfoo\nfoo2\nfoo\nfoo") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2\n") end @@ -293,7 +291,7 @@ it 'example in the docs' do @resource = Puppet::Type::File_line.new(name: 'bashrc_proxy', ensure: 'absent', path: tmpfile, line: 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128') @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=http://squid.puppetlabs.vm:3128\nfoo4\n") } + File.write(tmpfile, "foo1\nfoo2\nexport HTTP_PROXY=http://squid.puppetlabs.vm:3128\nfoo4\n") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n") end @@ -313,12 +311,12 @@ end it 'finds a line to match' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + File.write(tmpfile, "foo1\nfoo\nfoo2") expect(@provider.exists?).to be true end it 'removes one line if it matches' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + File.write(tmpfile, "foo1\nfoo\nfoo2") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2") end @@ -326,7 +324,7 @@ it 'the line parameter is actually not used at all but is silently ignored if here' do @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'supercalifragilisticexpialidocious', ensure: 'absent', match: 'o$', match_for_absence: true) @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + File.write(tmpfile, "foo1\nfoo\nfoo2") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2") end @@ -334,20 +332,20 @@ it 'and may not be here and does not need to be here' do @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, ensure: 'absent', match: 'o$', match_for_absence: true) @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + File.write(tmpfile, "foo1\nfoo\nfoo2") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2") end it 'raises an error if more than one line matches' do - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") } + File.write(tmpfile, "foo1\nfoo\nfoo2\nfoo\nfoo") expect { @provider.destroy }.to raise_error(Puppet::Error, %r{More than one line}) end it 'removes multiple lines if :multiple is true' do @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo2', ensure: 'absent', match: 'o$', multiple: true, match_for_absence: true) @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") } + File.write(tmpfile, "foo1\nfoo\nfoo2\nfoo\nfoo") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2\n") end @@ -355,7 +353,7 @@ it 'ignores the match if match_for_absence is not specified' do @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo2', ensure: 'absent', match: 'o$') @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + File.write(tmpfile, "foo1\nfoo\nfoo2") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo\n") end @@ -363,7 +361,7 @@ it 'ignores the match if match_for_absence is false' do @resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo2', ensure: 'absent', match: 'o$', match_for_absence: false) @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + File.write(tmpfile, "foo1\nfoo\nfoo2") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo\n") end @@ -374,7 +372,7 @@ match: '^export\ HTTP_PROXY\=', match_for_absence: true ) @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") } + File.write(tmpfile, "foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n") end @@ -382,7 +380,7 @@ it 'example in the docs showing line is redundant' do @resource = Puppet::Type::File_line.new(name: 'bashrc_proxy', ensure: 'absent', path: tmpfile, match: '^export\ HTTP_PROXY\=', match_for_absence: true) @provider = provider_class.new(@resource) - File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") } + File.write(tmpfile, "foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") @provider.destroy expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n") end diff --git a/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb b/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb index 88c8ef9c4..fb9cedb46 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb @@ -28,9 +28,7 @@ end before :each do - File.open(tmpfile, 'w') do |fh| - fh.write(content) - end + File.write(tmpfile, content) end describe 'customer use cases - no lines' do From 8215008da3e4c82d61c2ea8e7d05f2c39fb9446e Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:41:11 +0100 Subject: [PATCH 23/33] (CONT-801) Autocorrect safe group 6 --- .rubocop.yml | 2 +- .rubocop_todo.yml | 51 ++++--------------- lib/facter/pe_version.rb | 12 ++--- lib/puppet/functions/deprecation.rb | 6 +-- lib/puppet/functions/stdlib/str2resource.rb | 4 +- lib/puppet/functions/to_json_pretty.rb | 8 ++- lib/puppet/parser/functions/any2array.rb | 4 +- lib/puppet/parser/functions/any2bool.rb | 16 ++---- lib/puppet/parser/functions/base64.rb | 12 ++--- lib/puppet/parser/functions/bool2str.rb | 12 ++--- lib/puppet/parser/functions/concat.rb | 4 +- lib/puppet/parser/functions/count.rb | 4 +- lib/puppet/parser/functions/deep_merge.rb | 8 +-- .../parser/functions/defined_with_params.rb | 4 +- lib/puppet/parser/functions/delete_at.rb | 12 ++--- .../parser/functions/delete_undef_values.rb | 4 +- lib/puppet/parser/functions/delete_values.rb | 4 +- lib/puppet/parser/functions/deprecation.rb | 4 +- lib/puppet/parser/functions/difference.rb | 4 +- lib/puppet/parser/functions/dirname.rb | 16 ++---- lib/puppet/parser/functions/dos2unix.rb | 4 +- lib/puppet/parser/functions/enclose_ipv6.rb | 12 ++--- lib/puppet/parser/functions/fqdn_rotate.rb | 4 +- lib/puppet/parser/functions/grep.rb | 4 +- .../parser/functions/has_interface_with.rb | 8 +-- lib/puppet/parser/functions/intersection.rb | 4 +- .../parser/functions/join_keys_to_values.rb | 12 ++--- lib/puppet/parser/functions/member.rb | 8 +-- lib/puppet/parser/functions/merge.rb | 8 +-- lib/puppet/parser/functions/prefix.rb | 12 ++--- lib/puppet/parser/functions/pw_hash.rb | 6 +-- lib/puppet/parser/functions/regexpescape.rb | 4 +- lib/puppet/parser/functions/reverse.rb | 4 +- lib/puppet/parser/functions/shuffle.rb | 4 +- lib/puppet/parser/functions/squeeze.rb | 4 +- lib/puppet/parser/functions/str2bool.rb | 8 +-- .../parser/functions/str2saltedsha512.rb | 4 +- lib/puppet/parser/functions/suffix.rb | 12 ++--- lib/puppet/parser/functions/swapcase.rb | 4 +- lib/puppet/parser/functions/time.rb | 4 +- lib/puppet/parser/functions/unix2dos.rb | 4 +- lib/puppet/parser/functions/uriescape.rb | 4 +- .../parser/functions/validate_augeas.rb | 4 +- lib/puppet/parser/functions/validate_cmd.rb | 4 +- .../functions/validate_x509_rsa_key_pair.rb | 8 +-- lib/puppet/parser/functions/values_at.rb | 16 ++---- lib/puppet/parser/functions/zip.rb | 4 +- lib/puppet/provider/file_line/ruby.rb | 24 +++------ lib/puppet/type/file_line.rb | 20 ++------ lib/puppet_x/stdlib/toml_dumper.rb | 4 +- 50 files changed, 106 insertions(+), 307 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 3858fd5b0..ab25b4603 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,7 +5,7 @@ require: AllCops: DisplayCopNames: true NewCops: enable - TargetRubyVersion: '2.7' + TargetRubyVersion: '2.6' ExtraDetails: true DisplayStyleGuide: true Include: diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index be4180d5a..1e869a3c5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-05-17 10:10:18 UTC using RuboCop version 1.48.1. +# on 2023-05-17 10:40:49 UTC using RuboCop version 1.48.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -59,7 +59,7 @@ Metrics/AbcSize: # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode. # AllowedMethods: refine Metrics/BlockLength: - Max: 162 + Max: 152 # Offense count: 5 # Configuration parameters: AllowedMethods, AllowedPatterns. @@ -69,7 +69,7 @@ Metrics/CyclomaticComplexity: # Offense count: 9 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: - Max: 37 + Max: 35 # Offense count: 4 # Configuration parameters: AllowedMethods, AllowedPatterns. @@ -105,12 +105,6 @@ Performance/CollectionLiteralInLoop: Exclude: - 'lib/puppet/functions/ensure_packages.rb' -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -Performance/MapCompact: - Exclude: - - 'lib/puppet/parser/functions/values_at.rb' - # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowRegexpMatch. @@ -168,7 +162,7 @@ RSpec/MultipleExpectations: RSpec/MultipleMemoizedHelpers: Max: 9 -# Offense count: 55 +# Offense count: 253 # Configuration parameters: EnforcedStyle, IgnoreSharedExamples. # SupportedStyles: always, named_only RSpec/NamedSubject: @@ -241,26 +235,12 @@ Style/FrozenStringLiteralComment: - 'spec/type_aliases/http__method_spec.rb' - 'spec/type_aliases/http__status_spec.rb' -# Offense count: 3 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowSplatArgument. -Style/HashConversion: - Exclude: - - 'lib/puppet/functions/to_json_pretty.rb' - - 'lib/puppet/parser/functions/prefix.rb' - - 'lib/puppet/parser/functions/suffix.rb' - # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). Style/HashTransformKeys: Exclude: - 'lib/puppet/functions/to_json_pretty.rb' -# Offense count: 85 -# This cop supports safe autocorrection (--autocorrect). -Style/IfUnlessModifier: - Enabled: false - # Offense count: 4 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowedMethods. @@ -272,24 +252,19 @@ Style/IfWithBooleanLiteralBranches: - 'lib/puppet/parser/functions/shuffle.rb' - 'lib/puppet/provider/file_line/ruby.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowedMethods, AllowedPatterns. -Style/MethodCallWithoutArgsParentheses: +# Offense count: 3 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/MapToHash: Exclude: - - 'lib/puppet/functions/deprecation.rb' + - 'lib/puppet/functions/to_json_pretty.rb' + - 'lib/puppet/parser/functions/prefix.rb' + - 'lib/puppet/parser/functions/suffix.rb' # Offense count: 1 Style/MixinUsage: Exclude: - 'spec/spec_helper.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Style/NegatedIfElseCondition: - Exclude: - - 'lib/puppet/parser/functions/pw_hash.rb' - # Offense count: 3 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle, AllowedMethods, AllowedPatterns. @@ -315,12 +290,6 @@ Style/RedundantArgument: Exclude: - 'lib/puppet/provider/file_line/ruby.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantConstantBase: - Exclude: - - 'lib/puppet/functions/to_json_pretty.rb' - # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). Style/RedundantFileExtensionInRequire: diff --git a/lib/facter/pe_version.rb b/lib/facter/pe_version.rb index 9f514316e..2e1cb13f5 100644 --- a/lib/facter/pe_version.rb +++ b/lib/facter/pe_version.rb @@ -43,9 +43,7 @@ confine is_pe: true setcode do pe_version = Facter.value(:pe_version) - if pe_version - pe_version.to_s.split('.')[0] - end + pe_version.to_s.split('.')[0] if pe_version end end @@ -54,9 +52,7 @@ confine is_pe: true setcode do pe_version = Facter.value(:pe_version) - if pe_version - pe_version.to_s.split('.')[1] - end + pe_version.to_s.split('.')[1] if pe_version end end @@ -65,8 +61,6 @@ confine is_pe: true setcode do pe_version = Facter.value(:pe_version) - if pe_version - pe_version.to_s.split('.')[2] - end + pe_version.to_s.split('.')[2] if pe_version end end diff --git a/lib/puppet/functions/deprecation.rb b/lib/puppet/functions/deprecation.rb index f71924fd9..ff5865b26 100644 --- a/lib/puppet/functions/deprecation.rb +++ b/lib/puppet/functions/deprecation.rb @@ -20,7 +20,7 @@ def deprecation(key, message) if defined? Puppet::Pops::PuppetStack.stacktrace - stacktrace = Puppet::Pops::PuppetStack.stacktrace() + stacktrace = Puppet::Pops::PuppetStack.stacktrace file = stacktrace[0] line = stacktrace[1] message = "#{message} at #{file}:#{line}" @@ -32,9 +32,7 @@ def deprecation(key, message) when :error raise("deprecation. #{key}. #{message}") else - unless ENV['STDLIB_LOG_DEPRECATIONS'] == 'false' - Puppet.deprecation_warning(message, key) - end + Puppet.deprecation_warning(message, key) unless ENV['STDLIB_LOG_DEPRECATIONS'] == 'false' end end end diff --git a/lib/puppet/functions/stdlib/str2resource.rb b/lib/puppet/functions/stdlib/str2resource.rb index 67188abd7..b2b586992 100644 --- a/lib/puppet/functions/stdlib/str2resource.rb +++ b/lib/puppet/functions/stdlib/str2resource.rb @@ -28,9 +28,7 @@ def str2resource(res_string) resource = closure_scope.findresource(type_name, title) - if resource.nil? - raise(Puppet::ParseError, "stdlib::str2resource(): could not find #{type_name}[#{title}], this is parse order dependent and values should not be quoted") - end + raise(Puppet::ParseError, "stdlib::str2resource(): could not find #{type_name}[#{title}], this is parse order dependent and values should not be quoted") if resource.nil? resource end diff --git a/lib/puppet/functions/to_json_pretty.rb b/lib/puppet/functions/to_json_pretty.rb index 7d46fc2dc..237d0c089 100644 --- a/lib/puppet/functions/to_json_pretty.rb +++ b/lib/puppet/functions/to_json_pretty.rb @@ -61,11 +61,9 @@ def to_json_pretty(data, skip_undef = false, opts = nil) # It's not possible to make an abstract type that can be either a boolean # false or an integer, so we use -1 as the falsey value if opts - opts = Hash[opts.map { |k, v| [k.to_sym, v] }] + opts = opts.map { |k, v| [k.to_sym, v] }.to_h - if opts[:max_nesting] == -1 - opts[:max_nesting] = false - end + opts[:max_nesting] = false if opts[:max_nesting] == -1 end if skip_undef @@ -77,6 +75,6 @@ def to_json_pretty(data, skip_undef = false, opts = nil) end # Call ::JSON to ensure it references the JSON library from Ruby's standard library # instead of a random JSON namespace that might be in scope due to user code. - ::JSON.pretty_generate(data, opts) << "\n" + JSON.pretty_generate(data, opts) << "\n" end end diff --git a/lib/puppet/parser/functions/any2array.rb b/lib/puppet/parser/functions/any2array.rb index 08ca02bb9..81efd77d7 100644 --- a/lib/puppet/parser/functions/any2array.rb +++ b/lib/puppet/parser/functions/any2array.rb @@ -36,9 +36,7 @@ module Puppet::Parser::Functions @return [Array] The new array containing the given object DOC ) do |arguments| - if arguments.empty? - return [] - end + return [] if arguments.empty? return arguments unless arguments.length == 1 return arguments[0] if arguments[0].is_a?(Array) diff --git a/lib/puppet/parser/functions/any2bool.rb b/lib/puppet/parser/functions/any2bool.rb index 58e1c2cf3..86d9bdfee 100644 --- a/lib/puppet/parser/functions/any2bool.rb +++ b/lib/puppet/parser/functions/any2bool.rb @@ -25,19 +25,13 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "any2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? # If argument is already Boolean, return it - if !!arguments[0] == arguments[0] # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean - return arguments[0] - end + return arguments[0] if !!arguments[0] == arguments[0] # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean arg = arguments[0] - if arg.nil? - return false - end + return false if arg.nil? - if arg == :undef - return false - end + return false if arg == :undef valid_float = begin !!Float(arg) # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean @@ -45,9 +39,7 @@ module Puppet::Parser::Functions false end - if arg.is_a?(Numeric) - return function_num2bool([arguments[0]]) - end + return function_num2bool([arguments[0]]) if arg.is_a?(Numeric) if arg.is_a?(String) return function_num2bool([arguments[0]]) if valid_float diff --git a/lib/puppet/parser/functions/base64.rb b/lib/puppet/parser/functions/base64.rb index e070c82b9..b65458e6e 100644 --- a/lib/puppet/parser/functions/base64.rb +++ b/lib/puppet/parser/functions/base64.rb @@ -41,13 +41,9 @@ module Puppet::Parser::Functions actions = ['encode', 'decode'] - unless actions.include?(args[0]) - raise Puppet::ParseError, "base64(): the first argument must be one of 'encode' or 'decode'" - end + raise Puppet::ParseError, "base64(): the first argument must be one of 'encode' or 'decode'" unless actions.include?(args[0]) - unless args[1].is_a?(String) - raise Puppet::ParseError, 'base64(): the second argument must be a string to base64' - end + raise Puppet::ParseError, 'base64(): the second argument must be a string to base64' unless args[1].is_a?(String) method = ['default', 'strict', 'urlsafe'] @@ -57,9 +53,7 @@ module Puppet::Parser::Functions args[2] end - unless method.include?(chosen_method) - raise Puppet::ParseError, "base64(): the third argument must be one of 'default', 'strict', or 'urlsafe'" - end + raise Puppet::ParseError, "base64(): the third argument must be one of 'default', 'strict', or 'urlsafe'" unless method.include?(chosen_method) case args[0] when 'encode' diff --git a/lib/puppet/parser/functions/bool2str.rb b/lib/puppet/parser/functions/bool2str.rb index 9a1021c58..584bb1f0d 100644 --- a/lib/puppet/parser/functions/bool2str.rb +++ b/lib/puppet/parser/functions/bool2str.rb @@ -39,9 +39,7 @@ module Puppet::Parser::Functions ``` DOC ) do |arguments| - unless arguments.size == 1 || arguments.size == 3 - raise(Puppet::ParseError, "bool2str(): Wrong number of arguments given (#{arguments.size} for 3)") - end + raise(Puppet::ParseError, "bool2str(): Wrong number of arguments given (#{arguments.size} for 3)") unless arguments.size == 1 || arguments.size == 3 value = arguments[0] true_string = arguments[1] || 'true' @@ -49,13 +47,9 @@ module Puppet::Parser::Functions klass = value.class # We can have either true or false, and nothing else - unless [FalseClass, TrueClass].include?(klass) - raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with') - end + raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with') unless [FalseClass, TrueClass].include?(klass) - unless [true_string, false_string].all? { |x| x.is_a?(String) } - raise(Puppet::ParseError, 'bool2str(): Requires strings to convert to') - end + raise(Puppet::ParseError, 'bool2str(): Requires strings to convert to') unless [true_string, false_string].all? { |x| x.is_a?(String) } return value ? true_string : false_string end diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 2ee1c2ab4..af9b3eb53 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -30,9 +30,7 @@ module Puppet::Parser::Functions a = arguments[0] # Check that the first parameter is an array - unless a.is_a?(Array) - raise(Puppet::ParseError, 'concat(): Requires array to work with') - end + raise(Puppet::ParseError, 'concat(): Requires array to work with') unless a.is_a?(Array) result = a arguments.shift diff --git a/lib/puppet/parser/functions/count.rb b/lib/puppet/parser/functions/count.rb index b2bd89a3e..8e1f7ab98 100644 --- a/lib/puppet/parser/functions/count.rb +++ b/lib/puppet/parser/functions/count.rb @@ -27,9 +27,7 @@ module Puppet::Parser::Functions @return [Integer] The amount of elements counted within the array DOC ) do |args| - if args.size > 2 - raise(ArgumentError, "count(): Wrong number of arguments given #{args.size} for 1 or 2.") - end + raise(ArgumentError, "count(): Wrong number of arguments given #{args.size} for 1 or 2.") if args.size > 2 collection, item = args diff --git a/lib/puppet/parser/functions/deep_merge.rb b/lib/puppet/parser/functions/deep_merge.rb index b6d14b4d8..6e6fc837f 100644 --- a/lib/puppet/parser/functions/deep_merge.rb +++ b/lib/puppet/parser/functions/deep_merge.rb @@ -24,9 +24,7 @@ module Puppet::Parser::Functions @return [Hash] The merged hash DOC - if args.length < 2 - raise Puppet::ParseError, "deep_merge(): wrong number of arguments (#{args.length}; must be at least 2)" - end + raise Puppet::ParseError, "deep_merge(): wrong number of arguments (#{args.length}; must be at least 2)" if args.length < 2 deep_merge = proc do |hash1, hash2| hash1.merge(hash2) do |_key, old_value, new_value| @@ -42,9 +40,7 @@ module Puppet::Parser::Functions args.each do |arg| next if arg.is_a?(String) && arg.empty? # empty string is synonym for puppet's undef # If the argument was not a hash, skip it. - unless arg.is_a?(Hash) - raise Puppet::ParseError, "deep_merge: unexpected argument type #{arg.class}, only expects hash arguments" - end + raise Puppet::ParseError, "deep_merge: unexpected argument type #{arg.class}, only expects hash arguments" unless arg.is_a?(Hash) result = deep_merge.call(result, arg) end diff --git a/lib/puppet/parser/functions/defined_with_params.rb b/lib/puppet/parser/functions/defined_with_params.rb index 14339f861..0b8b338ee 100644 --- a/lib/puppet/parser/functions/defined_with_params.rb +++ b/lib/puppet/parser/functions/defined_with_params.rb @@ -27,9 +27,7 @@ reference, params = vals raise(ArgumentError, 'Must specify a reference') unless reference - if !params || params == '' - params = {} - end + params = {} if !params || params == '' ret = false if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0 diff --git a/lib/puppet/parser/functions/delete_at.rb b/lib/puppet/parser/functions/delete_at.rb index 12cba08d2..86e7a2960 100644 --- a/lib/puppet/parser/functions/delete_at.rb +++ b/lib/puppet/parser/functions/delete_at.rb @@ -35,24 +35,18 @@ module Puppet::Parser::Functions array = arguments[0] - unless array.is_a?(Array) - raise(Puppet::ParseError, 'delete_at(): Requires array to work with') - end + raise(Puppet::ParseError, 'delete_at(): Requires array to work with') unless array.is_a?(Array) index = arguments[1] - if index.is_a?(String) && !index.match(%r{^\d+$}) - raise(Puppet::ParseError, 'delete_at(): You must provide non-negative numeric index') - end + raise(Puppet::ParseError, 'delete_at(): You must provide non-negative numeric index') if index.is_a?(String) && !index.match(%r{^\d+$}) result = array.clone # Numbers in Puppet are often string-encoded which is troublesome ... index = index.to_i - if index > result.size - 1 # First element is at index 0 is it not? - raise(Puppet::ParseError, 'delete_at(): Given index exceeds size of array given') - end + raise(Puppet::ParseError, 'delete_at(): Given index exceeds size of array given') if index > result.size - 1 # First element is at index 0 is it not? result.delete_at(index) # We ignore the element that got deleted ... diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb index 75ba298e0..6d71e76df 100644 --- a/lib/puppet/parser/functions/delete_undef_values.rb +++ b/lib/puppet/parser/functions/delete_undef_values.rb @@ -28,9 +28,7 @@ module Puppet::Parser::Functions ) do |args| raise(Puppet::ParseError, "delete_undef_values(): Wrong number of arguments given (#{args.size})") if args.empty? - unless args[0].is_a?(Array) || args[0].is_a?(Hash) - raise(Puppet::ParseError, "delete_undef_values(): expected an array or hash, got #{args[0]} type #{args[0].class} ") - end + raise(Puppet::ParseError, "delete_undef_values(): expected an array or hash, got #{args[0]} type #{args[0].class} ") unless args[0].is_a?(Array) || args[0].is_a?(Hash) result = args[0].dup if result.is_a?(Hash) diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb index e7e731fdd..4d2323082 100644 --- a/lib/puppet/parser/functions/delete_values.rb +++ b/lib/puppet/parser/functions/delete_values.rb @@ -26,9 +26,7 @@ module Puppet::Parser::Functions hash, item = arguments - unless hash.is_a?(Hash) - raise(TypeError, "delete_values(): First argument must be a Hash. Given an argument of class #{hash.class}.") - end + raise(TypeError, "delete_values(): First argument must be a Hash. Given an argument of class #{hash.class}.") unless hash.is_a?(Hash) hash.dup.delete_if { |_key, val| item == val } end diff --git a/lib/puppet/parser/functions/deprecation.rb b/lib/puppet/parser/functions/deprecation.rb index 5db654dd1..503edc98d 100644 --- a/lib/puppet/parser/functions/deprecation.rb +++ b/lib/puppet/parser/functions/deprecation.rb @@ -20,8 +20,6 @@ module Puppet::Parser::Functions key = arguments[0] message = arguments[1] - if ENV['STDLIB_LOG_DEPRECATIONS'] == 'true' - warning("deprecation. #{key}. #{message}") - end + warning("deprecation. #{key}. #{message}") if ENV['STDLIB_LOG_DEPRECATIONS'] == 'true' end end diff --git a/lib/puppet/parser/functions/difference.rb b/lib/puppet/parser/functions/difference.rb index 09440be61..e20fc32e2 100644 --- a/lib/puppet/parser/functions/difference.rb +++ b/lib/puppet/parser/functions/difference.rb @@ -32,9 +32,7 @@ module Puppet::Parser::Functions first = arguments[0] second = arguments[1] - unless first.is_a?(Array) && second.is_a?(Array) - raise(Puppet::ParseError, 'difference(): Requires 2 arrays') - end + raise(Puppet::ParseError, 'difference(): Requires 2 arrays') unless first.is_a?(Array) && second.is_a?(Array) result = first - second diff --git a/lib/puppet/parser/functions/dirname.rb b/lib/puppet/parser/functions/dirname.rb index 6fcfa5832..5ea1f0133 100644 --- a/lib/puppet/parser/functions/dirname.rb +++ b/lib/puppet/parser/functions/dirname.rb @@ -11,19 +11,11 @@ module Puppet::Parser::Functions @return [String] the given path's dirname DOC ) do |arguments| - if arguments.empty? - raise(Puppet::ParseError, 'dirname(): No arguments given') - end - if arguments.size > 1 - raise(Puppet::ParseError, "dirname(): Too many arguments given (#{arguments.size})") - end - unless arguments[0].is_a?(String) - raise(Puppet::ParseError, 'dirname(): Requires string as argument') - end + raise(Puppet::ParseError, 'dirname(): No arguments given') if arguments.empty? + raise(Puppet::ParseError, "dirname(): Too many arguments given (#{arguments.size})") if arguments.size > 1 + raise(Puppet::ParseError, 'dirname(): Requires string as argument') unless arguments[0].is_a?(String) # undef is converted to an empty string '' - if arguments[0].empty? - raise(Puppet::ParseError, 'dirname(): Requires a non-empty string as argument') - end + raise(Puppet::ParseError, 'dirname(): Requires a non-empty string as argument') if arguments[0].empty? return File.dirname(arguments[0]) end diff --git a/lib/puppet/parser/functions/dos2unix.rb b/lib/puppet/parser/functions/dos2unix.rb index daa92b6b2..b55ebd4d7 100644 --- a/lib/puppet/parser/functions/dos2unix.rb +++ b/lib/puppet/parser/functions/dos2unix.rb @@ -11,9 +11,7 @@ module Puppet::Parser::Functions @return The retrieved version DOC ) do |arguments| - unless arguments[0].is_a?(String) - raise(Puppet::ParseError, 'dos2unix(): Requires string as argument') - end + raise(Puppet::ParseError, 'dos2unix(): Requires string as argument') unless arguments[0].is_a?(String) arguments[0].gsub(%r{\r\n}, "\n") end diff --git a/lib/puppet/parser/functions/enclose_ipv6.rb b/lib/puppet/parser/functions/enclose_ipv6.rb index cfa174b1c..b23561d81 100644 --- a/lib/puppet/parser/functions/enclose_ipv6.rb +++ b/lib/puppet/parser/functions/enclose_ipv6.rb @@ -16,16 +16,10 @@ module Puppet::Parser::Functions require 'ipaddr' rescuable_exceptions = [ArgumentError] - if defined?(IPAddr::InvalidAddressError) - rescuable_exceptions << IPAddr::InvalidAddressError - end + rescuable_exceptions << IPAddr::InvalidAddressError if defined?(IPAddr::InvalidAddressError) - if arguments.size != 1 - raise(Puppet::ParseError, "enclose_ipv6(): Wrong number of arguments given #{arguments.size} for 1") - end - unless arguments[0].is_a?(String) || arguments[0].is_a?(Array) - raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument type given #{arguments[0].class} expected String or Array") - end + raise(Puppet::ParseError, "enclose_ipv6(): Wrong number of arguments given #{arguments.size} for 1") if arguments.size != 1 + raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument type given #{arguments[0].class} expected String or Array") unless arguments[0].is_a?(String) || arguments[0].is_a?(Array) input = [arguments[0]].flatten.compact result = [] diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb index e6fec1413..dcece1b77 100644 --- a/lib/puppet/parser/functions/fqdn_rotate.rb +++ b/lib/puppet/parser/functions/fqdn_rotate.rb @@ -22,9 +22,7 @@ value = args.shift require 'digest/md5' - unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'fqdn_rotate(): Requires either array or string to work with') - end + raise(Puppet::ParseError, 'fqdn_rotate(): Requires either array or string to work with') unless value.is_a?(Array) || value.is_a?(String) result = value.clone diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb index 9290b9ebd..4ae73c45a 100644 --- a/lib/puppet/parser/functions/grep.rb +++ b/lib/puppet/parser/functions/grep.rb @@ -20,9 +20,7 @@ module Puppet::Parser::Functions ```['aaa', 'bbb', 'ccc', 'aaaddd']. filter |$x| { $x =~ 'aaa' }``` DOC ) do |arguments| - if arguments.size != 2 - raise(Puppet::ParseError, "grep(): Wrong number of arguments given #{arguments.size} for 2") - end + raise(Puppet::ParseError, "grep(): Wrong number of arguments given #{arguments.size} for 2") if arguments.size != 2 a = arguments[0] pattern = Regexp.new(arguments[1]) diff --git a/lib/puppet/parser/functions/has_interface_with.rb b/lib/puppet/parser/functions/has_interface_with.rb index d2295ef94..b78029d08 100644 --- a/lib/puppet/parser/functions/has_interface_with.rb +++ b/lib/puppet/parser/functions/has_interface_with.rb @@ -30,9 +30,7 @@ module Puppet::Parser::Functions interfaces = interfaces.split(',') - if args.size == 1 - return interfaces.member?(args[0]) - end + return interfaces.member?(args[0]) if args.size == 1 kind, value = args @@ -45,9 +43,7 @@ module Puppet::Parser::Functions end rescue Puppet::ParseError end - if factval == value - return true - end + return true if factval == value result = false interfaces.each do |iface| diff --git a/lib/puppet/parser/functions/intersection.rb b/lib/puppet/parser/functions/intersection.rb index 929a1b42a..0f729faca 100644 --- a/lib/puppet/parser/functions/intersection.rb +++ b/lib/puppet/parser/functions/intersection.rb @@ -22,9 +22,7 @@ module Puppet::Parser::Functions first = arguments[0] second = arguments[1] - unless first.is_a?(Array) && second.is_a?(Array) - raise(Puppet::ParseError, "intersection(): Requires 2 arrays, got #{first.class} and #{second.class}") - end + raise(Puppet::ParseError, "intersection(): Requires 2 arrays, got #{first.class} and #{second.class}") unless first.is_a?(Array) && second.is_a?(Array) result = first & second diff --git a/lib/puppet/parser/functions/join_keys_to_values.rb b/lib/puppet/parser/functions/join_keys_to_values.rb index 72116956d..d456a4e4b 100644 --- a/lib/puppet/parser/functions/join_keys_to_values.rb +++ b/lib/puppet/parser/functions/join_keys_to_values.rb @@ -27,21 +27,15 @@ module Puppet::Parser::Functions DOC ) do |arguments| # Validate the number of arguments. - if arguments.size != 2 - raise(Puppet::ParseError, "join_keys_to_values(): Takes exactly two arguments, but #{arguments.size} given.") - end + raise(Puppet::ParseError, "join_keys_to_values(): Takes exactly two arguments, but #{arguments.size} given.") if arguments.size != 2 # Validate the first argument. hash = arguments[0] - unless hash.is_a?(Hash) - raise(TypeError, "join_keys_to_values(): The first argument must be a hash, but a #{hash.class} was given.") - end + raise(TypeError, "join_keys_to_values(): The first argument must be a hash, but a #{hash.class} was given.") unless hash.is_a?(Hash) # Validate the second argument. separator = arguments[1] - unless separator.is_a?(String) - raise(TypeError, "join_keys_to_values(): The second argument must be a string, but a #{separator.class} was given.") - end + raise(TypeError, "join_keys_to_values(): The second argument must be a string, but a #{separator.class} was given.") unless separator.is_a?(String) # Join the keys to their values. hash.map { |k, v| diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index c7372a04f..b9bbeb717 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -41,13 +41,9 @@ module Puppet::Parser::Functions array = arguments[0] - unless array.is_a?(Array) - raise(Puppet::ParseError, 'member(): Requires array to work with') - end + raise(Puppet::ParseError, 'member(): Requires array to work with') unless array.is_a?(Array) - unless arguments[1].is_a?(String) || arguments[1].is_a?(Integer) || arguments[1].is_a?(Array) - raise(Puppet::ParseError, 'member(): Item to search for must be a string, fixnum, or array') - end + raise(Puppet::ParseError, 'member(): Item to search for must be a string, fixnum, or array') unless arguments[1].is_a?(String) || arguments[1].is_a?(Integer) || arguments[1].is_a?(Array) item = if arguments[1].is_a?(String) || arguments[1].is_a?(Integer) [arguments[1]] diff --git a/lib/puppet/parser/functions/merge.rb b/lib/puppet/parser/functions/merge.rb index e867f435b..c257d2c43 100644 --- a/lib/puppet/parser/functions/merge.rb +++ b/lib/puppet/parser/functions/merge.rb @@ -22,18 +22,14 @@ module Puppet::Parser::Functions `$merged_hash = $hash1 + $hash2` DOC - if args.length < 2 - raise Puppet::ParseError, "merge(): wrong number of arguments (#{args.length}; must be at least 2)" - end + raise Puppet::ParseError, "merge(): wrong number of arguments (#{args.length}; must be at least 2)" if args.length < 2 # The hash we accumulate into accumulator = {} # Merge into the accumulator hash args.each do |arg| next if arg.is_a?(String) && arg.empty? # empty string is synonym for puppet's undef - unless arg.is_a?(Hash) - raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments" - end + raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments" unless arg.is_a?(Hash) accumulator.merge!(arg) end diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index c7ad107a1..57b7f7ff6 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -25,16 +25,12 @@ module Puppet::Parser::Functions enumerable = arguments[0] - unless enumerable.is_a?(Array) || enumerable.is_a?(Hash) - raise Puppet::ParseError, "prefix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}" - end + raise Puppet::ParseError, "prefix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}" unless enumerable.is_a?(Array) || enumerable.is_a?(Hash) prefix = arguments[1] if arguments[1] if prefix - unless prefix.is_a?(String) - raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{prefix.inspect}" - end + raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{prefix.inspect}" unless prefix.is_a?(String) end result = if enumerable.is_a?(Array) @@ -44,10 +40,10 @@ module Puppet::Parser::Functions prefix ? prefix + i : i end else - Hash[enumerable.map do |k, v| + enumerable.map { |k, v| k = k.to_s [prefix ? prefix + k : k, v] - end] + }.to_h end return result diff --git a/lib/puppet/parser/functions/pw_hash.rb b/lib/puppet/parser/functions/pw_hash.rb index e0c542e4d..ccc991028 100644 --- a/lib/puppet/parser/functions/pw_hash.rb +++ b/lib/puppet/parser/functions/pw_hash.rb @@ -76,14 +76,14 @@ # handle weak implementations of String#crypt # dup the string to get rid of frozen status for testing - if ('test'.dup).crypt('$1$1') != '$1$1$Bp8CU9Oujr9SSEw53WV6G.' + if ('test'.dup).crypt('$1$1') == '$1$1$Bp8CU9Oujr9SSEw53WV6G.' + password.crypt(salt) + else # JRuby < 1.7.17 # MS Windows and other systems that don't support enhanced salts raise Puppet::ParseError, 'system does not support enhanced salts' unless RUBY_PLATFORM == 'java' # puppetserver bundles Apache Commons Codec org.apache.commons.codec.digest.Crypt.crypt(password.to_java_bytes, salt) - else - password.crypt(salt) end end diff --git a/lib/puppet/parser/functions/regexpescape.rb b/lib/puppet/parser/functions/regexpescape.rb index 0c125525f..a67138a7b 100644 --- a/lib/puppet/parser/functions/regexpescape.rb +++ b/lib/puppet/parser/functions/regexpescape.rb @@ -16,9 +16,7 @@ module Puppet::Parser::Functions value = arguments[0] - unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'regexpescape(): Requires either array or string to work with') - end + raise(Puppet::ParseError, 'regexpescape(): Requires either array or string to work with') unless value.is_a?(Array) || value.is_a?(String) result = if value.is_a?(Array) # Numbers in Puppet are often string-encoded which is troublesome ... diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb index 075bce3f4..0cd62f246 100644 --- a/lib/puppet/parser/functions/reverse.rb +++ b/lib/puppet/parser/functions/reverse.rb @@ -18,9 +18,7 @@ module Puppet::Parser::Functions value = arguments[0] - unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'reverse(): Requires either array or string to work with') - end + raise(Puppet::ParseError, 'reverse(): Requires either array or string to work with') unless value.is_a?(Array) || value.is_a?(String) result = value.reverse diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb index c79ec2e8a..fa7fc8ffa 100644 --- a/lib/puppet/parser/functions/shuffle.rb +++ b/lib/puppet/parser/functions/shuffle.rb @@ -16,9 +16,7 @@ module Puppet::Parser::Functions value = arguments[0] - unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'shuffle(): Requires either array or string to work with') - end + raise(Puppet::ParseError, 'shuffle(): Requires either array or string to work with') unless value.is_a?(Array) || value.is_a?(String) result = value.clone diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index 9fdc3a8d0..97ceaf360 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -12,9 +12,7 @@ module Puppet::Parser::Functions a new string where runs of the same character that occur in this set are replaced by a single character. DOC ) do |arguments| - if (arguments.size != 2) && (arguments.size != 1) - raise(Puppet::ParseError, "squeeze(): Wrong number of arguments given #{arguments.size} for 2 or 1") - end + raise(Puppet::ParseError, "squeeze(): Wrong number of arguments given #{arguments.size} for 2 or 1") if (arguments.size != 2) && (arguments.size != 1) item = arguments[0] squeezeval = arguments[1] diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb index 672306035..f2cb42ba6 100644 --- a/lib/puppet/parser/functions/str2bool.rb +++ b/lib/puppet/parser/functions/str2bool.rb @@ -21,13 +21,9 @@ module Puppet::Parser::Functions string = arguments[0] # If string is already Boolean, return it - if !!string == string # rubocop:disable Style/DoubleNegation : No viable alternative - return string - end + return string if !!string == string # rubocop:disable Style/DoubleNegation : No viable alternative - unless string.is_a?(String) - raise(Puppet::ParseError, 'str2bool(): Requires string to work with') - end + raise(Puppet::ParseError, 'str2bool(): Requires string to work with') unless string.is_a?(String) # We consider all the yes, no, y, n and so on too ... result = case string diff --git a/lib/puppet/parser/functions/str2saltedsha512.rb b/lib/puppet/parser/functions/str2saltedsha512.rb index b8f7b929c..0423b20ca 100644 --- a/lib/puppet/parser/functions/str2saltedsha512.rb +++ b/lib/puppet/parser/functions/str2saltedsha512.rb @@ -24,9 +24,7 @@ module Puppet::Parser::Functions password = arguments[0] - unless password.is_a?(String) - raise(Puppet::ParseError, "str2saltedsha512(): Requires a String argument, you passed: #{password.class}") - end + raise(Puppet::ParseError, "str2saltedsha512(): Requires a String argument, you passed: #{password.class}") unless password.is_a?(String) seedint = rand((2**31) - 1) seedstring = Array(seedint).pack('L') diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb index cae9ade74..e74e50956 100644 --- a/lib/puppet/parser/functions/suffix.rb +++ b/lib/puppet/parser/functions/suffix.rb @@ -29,16 +29,12 @@ module Puppet::Parser::Functions enumerable = arguments[0] - unless enumerable.is_a?(Array) || enumerable.is_a?(Hash) - raise Puppet::ParseError, "suffix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}" - end + raise Puppet::ParseError, "suffix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}" unless enumerable.is_a?(Array) || enumerable.is_a?(Hash) suffix = arguments[1] if arguments[1] if suffix - unless suffix.is_a? String - raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}" - end + raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}" unless suffix.is_a? String end result = if enumerable.is_a?(Array) @@ -48,10 +44,10 @@ module Puppet::Parser::Functions suffix ? i + suffix : i end else - Hash[enumerable.map do |k, v| + enumerable.map { |k, v| k = k.to_s [suffix ? k + suffix : k, v] - end] + }.to_h end return result diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb index 347d709e8..a640f0272 100644 --- a/lib/puppet/parser/functions/swapcase.rb +++ b/lib/puppet/parser/functions/swapcase.rb @@ -22,9 +22,7 @@ module Puppet::Parser::Functions value = arguments[0] - unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'swapcase(): Requires either array or string to work with') - end + raise(Puppet::ParseError, 'swapcase(): Requires either array or string to work with') unless value.is_a?(Array) || value.is_a?(String) result = if value.is_a?(Array) # Numbers in Puppet are often string-encoded which is troublesome ... diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb index 147a9e994..77ef8c724 100644 --- a/lib/puppet/parser/functions/time.rb +++ b/lib/puppet/parser/functions/time.rb @@ -27,9 +27,7 @@ module Puppet::Parser::Functions # The Time Zone argument is optional ... time_zone = arguments[0] if arguments[0] - if !arguments.empty? && (arguments.size != 1) - raise(Puppet::ParseError, "time(): Wrong number of arguments given #{arguments.size} for 0 or 1") - end + raise(Puppet::ParseError, "time(): Wrong number of arguments given #{arguments.size} for 0 or 1") if !arguments.empty? && (arguments.size != 1) time = Time.new diff --git a/lib/puppet/parser/functions/unix2dos.rb b/lib/puppet/parser/functions/unix2dos.rb index dfae1b607..4dd4c5662 100644 --- a/lib/puppet/parser/functions/unix2dos.rb +++ b/lib/puppet/parser/functions/unix2dos.rb @@ -12,9 +12,7 @@ module Puppet::Parser::Functions Takes a single string argument. DOC ) do |arguments| - unless arguments[0].is_a?(String) - raise(Puppet::ParseError, 'unix2dos(): Requires string as argument') - end + raise(Puppet::ParseError, 'unix2dos(): Requires string as argument') unless arguments[0].is_a?(String) arguments[0].gsub(%r{\r*\n}, "\r\n") end diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb index e14070f01..01b615788 100644 --- a/lib/puppet/parser/functions/uriescape.rb +++ b/lib/puppet/parser/functions/uriescape.rb @@ -21,9 +21,7 @@ module Puppet::Parser::Functions value = arguments[0] - unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'uriescape(): Requires either array or string to work with') - end + raise(Puppet::ParseError, 'uriescape(): Requires either array or string to work with') unless value.is_a?(Array) || value.is_a?(String) result = if value.is_a?(Array) # Numbers in Puppet are often string-encoded which is troublesome ... diff --git a/lib/puppet/parser/functions/validate_augeas.rb b/lib/puppet/parser/functions/validate_augeas.rb index cadd3e3c0..8d89b1e7d 100644 --- a/lib/puppet/parser/functions/validate_augeas.rb +++ b/lib/puppet/parser/functions/validate_augeas.rb @@ -47,9 +47,7 @@ module Puppet::Parser::Functions raise Puppet::ParseError, 'validate_augeas(): this function requires the augeas feature. See http://docs.puppetlabs.com/guides/augeas.html#pre-requisites for how to activate it.' end - if (args.length < 2) || (args.length > 4) - raise Puppet::ParseError, "validate_augeas(): wrong number of arguments (#{args.length}; must be 2, 3, or 4)" - end + raise Puppet::ParseError, "validate_augeas(): wrong number of arguments (#{args.length}; must be 2, 3, or 4)" if (args.length < 2) || (args.length > 4) msg = args[3] || "validate_augeas(): Failed to validate content against #{args[1].inspect}" diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 312d7d7f9..c82952a2b 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -34,9 +34,7 @@ module Puppet::Parser::Functions DOC ) do |args| - if (args.length < 2) || (args.length > 3) - raise Puppet::ParseError, "validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)" - end + raise Puppet::ParseError, "validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)" if (args.length < 2) || (args.length > 3) msg = args[2] || "validate_cmd(): failed to validate content with command #{args[1].inspect}" diff --git a/lib/puppet/parser/functions/validate_x509_rsa_key_pair.rb b/lib/puppet/parser/functions/validate_x509_rsa_key_pair.rb index 4261cc09d..48fbfe30a 100644 --- a/lib/puppet/parser/functions/validate_x509_rsa_key_pair.rb +++ b/lib/puppet/parser/functions/validate_x509_rsa_key_pair.rb @@ -29,9 +29,7 @@ module Puppet::Parser::Functions end args.each do |arg| - unless arg.is_a?(String) - raise Puppet::ParseError, "#{arg.inspect} is not a string." - end + raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String) end begin @@ -46,8 +44,6 @@ module Puppet::Parser::Functions raise Puppet::ParseError, "Not a valid RSA key: #{e}" end - unless cert.verify(key) - raise Puppet::ParseError, 'Certificate signature does not match supplied key' - end + raise Puppet::ParseError, 'Certificate signature does not match supplied key' unless cert.verify(key) end end diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb index da5749fb7..ebe734de1 100644 --- a/lib/puppet/parser/functions/values_at.rb +++ b/lib/puppet/parser/functions/values_at.rb @@ -43,15 +43,11 @@ module Puppet::Parser::Functions array = arguments.shift - unless array.is_a?(Array) - raise(Puppet::ParseError, 'values_at(): Requires array to work with') - end + raise(Puppet::ParseError, 'values_at(): Requires array to work with') unless array.is_a?(Array) indices = [arguments.shift].flatten # Get them all ... Pokemon ... - if !indices || indices.empty? - raise(Puppet::ParseError, 'values_at(): You must provide at least one positive index to collect') - end + raise(Puppet::ParseError, 'values_at(): You must provide at least one positive index to collect') if !indices || indices.empty? indices_list = [] @@ -75,16 +71,12 @@ module Puppet::Parser::Functions range.each { |i| indices_list << i.to_i } # rubocop:disable Lint/ShadowingOuterLocalVariable : Value is meant to be shadowed else # Only positive numbers allowed in this case ... - unless %r{^\d+$}.match?(i) - raise(Puppet::ParseError, 'values_at(): Unknown format of given index') - end + raise(Puppet::ParseError, 'values_at(): Unknown format of given index') unless %r{^\d+$}.match?(i) # In Puppet numbers are often string-encoded ... i = i.to_i - if i > array.size - 1 # Same story. First element is at index 0 ... - raise(Puppet::ParseError, 'values_at(): Given index exceeds array size') - end + raise(Puppet::ParseError, 'values_at(): Given index exceeds array size') if i > array.size - 1 # Same story. First element is at index 0 ... indices_list << i end diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb index 47941f626..40323b7de 100644 --- a/lib/puppet/parser/functions/zip.rb +++ b/lib/puppet/parser/functions/zip.rb @@ -22,9 +22,7 @@ module Puppet::Parser::Functions a = arguments[0] b = arguments[1] - unless a.is_a?(Array) && b.is_a?(Array) - raise(Puppet::ParseError, 'zip(): Requires array to work with') - end + raise(Puppet::ParseError, 'zip(): Requires array to work with') unless a.is_a?(Array) && b.is_a?(Array) flatten = function_str2bool([arguments[2]]) if arguments[2] diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index b93de31e8..3b773f0b3 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -15,9 +15,7 @@ def exists? lines_count = 0 lines.each do |line| found = line.chomp == resource[:line] - if found - lines_count += 1 - end + lines_count += 1 if found end return found = lines_count > 0 if resource[:match].nil? @@ -113,9 +111,7 @@ def handle_create_with_match match_regex = new_match_regex match_count = count_matches(new_match_regex) - if match_count > 1 && resource[:multiple].to_s != 'true' - raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" - end + raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" if match_count > 1 && resource[:multiple].to_s != 'true' File.open(resource[:path], 'w') do |fh| lines.each do |line| @@ -128,9 +124,7 @@ def handle_create_with_match end end - if match_count.zero? - fh.puts(resource[:line]) - end + fh.puts(resource[:line]) if match_count.zero? end end @@ -145,23 +139,17 @@ def handle_create_with_after File.open(resource[:path], 'w') do |fh| lines.each do |line| fh.puts(line) - if after_regex.match(line) - fh.puts(resource[:line]) - end + fh.puts(resource[:line]) if after_regex.match(line) end - if after_count.zero? - fh.puts(resource[:line]) - end + fh.puts(resource[:line]) if after_count.zero? end end def handle_destroy_with_match match_regex = new_match_regex match_count = count_matches(match_regex) - if match_count > 1 && resource[:multiple].to_s != 'true' - raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" - end + raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" if match_count > 1 && resource[:multiple].to_s != 'true' local_lines = lines File.write(resource[:path], local_lines.reject { |line| match_regex.match(line) }.join('')) diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 7712f7be0..31da3f5a8 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -152,9 +152,7 @@ def retrieve newparam(:path) do desc 'The file Puppet will ensure contains the line specified by the line parameter.' validate do |value| - unless Puppet::Util.absolute_path?(value) - raise Puppet::Error, "File paths must be fully qualified, not '#{value}'" - end + raise Puppet::Error, "File paths must be fully qualified, not '#{value}'" unless Puppet::Util.absolute_path?(value) end end @@ -187,20 +185,12 @@ def retrieve self[:path] end validate do - if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:multiple].to_s == 'false' - raise(Puppet::Error, 'multiple must be true when replace_all_matches_not_matching_line is true') - end - if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:replace].to_s == 'false' - raise(Puppet::Error, 'replace must be true when replace_all_matches_not_matching_line is true') - end + raise(Puppet::Error, 'multiple must be true when replace_all_matches_not_matching_line is true') if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:multiple].to_s == 'false' + raise(Puppet::Error, 'replace must be true when replace_all_matches_not_matching_line is true') if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:replace].to_s == 'false' unless self[:line] - unless (self[:ensure].to_s == 'absent') && (self[:match_for_absence].to_s == 'true') && self[:match] - raise(Puppet::Error, 'line is a required attribute') - end - end - unless self[:path] - raise(Puppet::Error, 'path is a required attribute') + raise(Puppet::Error, 'line is a required attribute') unless (self[:ensure].to_s == 'absent') && (self[:match_for_absence].to_s == 'true') && self[:match] end + raise(Puppet::Error, 'path is a required attribute') unless self[:path] end end diff --git a/lib/puppet_x/stdlib/toml_dumper.rb b/lib/puppet_x/stdlib/toml_dumper.rb index e71dead39..3259201c1 100644 --- a/lib/puppet_x/stdlib/toml_dumper.rb +++ b/lib/puppet_x/stdlib/toml_dumper.rb @@ -40,9 +40,7 @@ def initialize(hash) def visit(hash, prefix, extra_brackets = false) simple_pairs, nested_pairs, table_array_pairs = sort_pairs hash - if prefix.any? && (simple_pairs.any? || hash.empty?) - print_prefix prefix, extra_brackets - end + print_prefix prefix, extra_brackets if prefix.any? && (simple_pairs.any? || hash.empty?) dump_pairs simple_pairs, nested_pairs, table_array_pairs, prefix end From e2c3f6a14f2a46a7e0de921ca72eeac172860abc Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:42:59 +0100 Subject: [PATCH 24/33] (CONT-801) Autocorrect safe group 7 --- .rubocop_todo.yml | 52 --------------------- lib/puppet/functions/stdlib/crc32.rb | 2 +- lib/puppet/functions/stdlib/sha256.rb | 2 +- lib/puppet/functions/to_toml.rb | 2 +- lib/puppet/parser/functions/any2bool.rb | 2 +- lib/puppet/parser/functions/convert_base.rb | 2 +- lib/puppet/parser/functions/deep_merge.rb | 2 +- lib/puppet/parser/functions/getparam.rb | 2 +- lib/puppet/parser/functions/loadjson.rb | 6 +-- lib/puppet/parser/functions/loadyaml.rb | 6 +-- lib/puppet/parser/functions/merge.rb | 2 +- lib/puppet/parser/functions/pw_hash.rb | 2 +- lib/puppet/parser/functions/range.rb | 2 +- lib/puppet/parser/functions/values_at.rb | 6 +-- lib/puppet/type/file_line.rb | 4 +- spec/functions/load_module_metadata_spec.rb | 10 ++-- spec/functions/loadjson_spec.rb | 6 +-- spec/functions/validate_cmd_spec.rb | 4 +- spec/spec_helper.rb | 2 +- 19 files changed, 32 insertions(+), 84 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1e869a3c5..ec6a1c1ea 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -290,58 +290,6 @@ Style/RedundantArgument: Exclude: - 'lib/puppet/provider/file_line/ruby.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantFileExtensionInRequire: - Exclude: - - 'lib/puppet/functions/to_toml.rb' - -# Offense count: 6 -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantHeredocDelimiterQuotes: - Exclude: - - 'lib/puppet/parser/functions/convert_base.rb' - - 'lib/puppet/parser/functions/deep_merge.rb' - - 'lib/puppet/parser/functions/getparam.rb' - - 'lib/puppet/parser/functions/loadjson.rb' - - 'lib/puppet/parser/functions/loadyaml.rb' - - 'lib/puppet/parser/functions/merge.rb' - -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantParentheses: - Exclude: - - 'lib/puppet/parser/functions/pw_hash.rb' - -# Offense count: 41 -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantRegexpEscape: - Exclude: - - 'lib/puppet/parser/functions/loadjson.rb' - - 'lib/puppet/parser/functions/loadyaml.rb' - - 'lib/puppet/parser/functions/range.rb' - - 'lib/puppet/parser/functions/values_at.rb' - - 'spec/functions/load_module_metadata_spec.rb' - - 'spec/functions/loadjson_spec.rb' - - 'spec/functions/validate_cmd_spec.rb' - -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantStringEscape: - Exclude: - - 'lib/puppet/type/file_line.rb' - -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: implicit, explicit -Style/RescueStandardError: - Exclude: - - 'lib/puppet/functions/stdlib/crc32.rb' - - 'lib/puppet/functions/stdlib/sha256.rb' - - 'lib/puppet/parser/functions/any2bool.rb' - - 'spec/spec_helper.rb' - # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). Style/SelectByRegexp: diff --git a/lib/puppet/functions/stdlib/crc32.rb b/lib/puppet/functions/stdlib/crc32.rb index 36b2a6ca7..73ecfa3b5 100644 --- a/lib/puppet/functions/stdlib/crc32.rb +++ b/lib/puppet/functions/stdlib/crc32.rb @@ -25,7 +25,7 @@ def crc32(my_data) Zlib.crc32(my_data.unwrap.to_s).to_s(16).downcase - rescue + rescue StandardError Zlib.crc32(my_data.to_s).to_s(16).downcase end end diff --git a/lib/puppet/functions/stdlib/sha256.rb b/lib/puppet/functions/stdlib/sha256.rb index 4b9b6c24a..278568ba5 100644 --- a/lib/puppet/functions/stdlib/sha256.rb +++ b/lib/puppet/functions/stdlib/sha256.rb @@ -20,7 +20,7 @@ def sha256(my_data) Digest::SHA256.hexdigest(my_data.unwrap.to_s) - rescue + rescue StandardError Digest::SHA256.hexdigest(my_data.to_s) end end diff --git a/lib/puppet/functions/to_toml.rb b/lib/puppet/functions/to_toml.rb index 2097ae1c5..9eccb07c0 100644 --- a/lib/puppet/functions/to_toml.rb +++ b/lib/puppet/functions/to_toml.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative '../../puppet_x/stdlib/toml_dumper.rb' +require_relative '../../puppet_x/stdlib/toml_dumper' # @summary Convert a data structure and output to TOML. Puppet::Functions.create_function(:to_toml) do diff --git a/lib/puppet/parser/functions/any2bool.rb b/lib/puppet/parser/functions/any2bool.rb index 86d9bdfee..f32e3daea 100644 --- a/lib/puppet/parser/functions/any2bool.rb +++ b/lib/puppet/parser/functions/any2bool.rb @@ -35,7 +35,7 @@ module Puppet::Parser::Functions valid_float = begin !!Float(arg) # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean - rescue + rescue StandardError false end diff --git a/lib/puppet/parser/functions/convert_base.rb b/lib/puppet/parser/functions/convert_base.rb index d58a03edd..80610433d 100644 --- a/lib/puppet/parser/functions/convert_base.rb +++ b/lib/puppet/parser/functions/convert_base.rb @@ -4,7 +4,7 @@ # convert_base.rb # module Puppet::Parser::Functions - newfunction(:convert_base, type: :rvalue, arity: 2, doc: <<-'DOC') do |args| + newfunction(:convert_base, type: :rvalue, arity: 2, doc: <<-DOC) do |args| @summary Converts a given integer or base 10 string representing an integer to a specified base, as a string. diff --git a/lib/puppet/parser/functions/deep_merge.rb b/lib/puppet/parser/functions/deep_merge.rb index 6e6fc837f..3669d4afc 100644 --- a/lib/puppet/parser/functions/deep_merge.rb +++ b/lib/puppet/parser/functions/deep_merge.rb @@ -4,7 +4,7 @@ # deep_merge.rb # module Puppet::Parser::Functions - newfunction(:deep_merge, type: :rvalue, doc: <<-'DOC') do |args| + newfunction(:deep_merge, type: :rvalue, doc: <<-DOC) do |args| @summary Recursively merges two or more hashes together and returns the resulting hash. diff --git a/lib/puppet/parser/functions/getparam.rb b/lib/puppet/parser/functions/getparam.rb index 49e8043a6..44ae4eacf 100644 --- a/lib/puppet/parser/functions/getparam.rb +++ b/lib/puppet/parser/functions/getparam.rb @@ -3,7 +3,7 @@ # Test whether a given class or definition is defined require 'puppet/parser/functions' -Puppet::Parser::Functions.newfunction(:getparam, type: :rvalue, doc: <<-'DOC' +Puppet::Parser::Functions.newfunction(:getparam, type: :rvalue, doc: <<-DOC @summary Returns the value of a resource's parameter. diff --git a/lib/puppet/parser/functions/loadjson.rb b/lib/puppet/parser/functions/loadjson.rb index 1c908799d..5cf2d5ab1 100644 --- a/lib/puppet/parser/functions/loadjson.rb +++ b/lib/puppet/parser/functions/loadjson.rb @@ -5,7 +5,7 @@ # module Puppet::Parser::Functions - newfunction(:loadjson, type: :rvalue, arity: -2, doc: <<-'DOC') do |args| + newfunction(:loadjson, type: :rvalue, arity: -2, doc: <<-DOC) do |args| @summary Load a JSON file containing an array, string, or hash, and return the data in the corresponding native data type. @@ -30,12 +30,12 @@ module Puppet::Parser::Functions begin if args[0].start_with?('http://', 'https://') http_options = {} - if (match = args[0].match(%r{(http\://|https\://)(.*):(.*)@(.*)})) + if (match = args[0].match(%r{(http://|https://)(.*):(.*)@(.*)})) # If URL is in the format of https://username:password@example.local/my_hash.yaml protocol, username, password, path = match.captures url = "#{protocol}#{path}" http_options[:http_basic_authentication] = [username, password] - elsif (match = args[0].match(%r{(http\:\/\/|https\:\/\/)(.*)@(.*)})) + elsif (match = args[0].match(%r{(http://|https://)(.*)@(.*)})) # If URL is in the format of https://username@example.local/my_hash.yaml protocol, username, path = match.captures url = "#{protocol}#{path}" diff --git a/lib/puppet/parser/functions/loadyaml.rb b/lib/puppet/parser/functions/loadyaml.rb index 1055cb830..9bf047a0a 100644 --- a/lib/puppet/parser/functions/loadyaml.rb +++ b/lib/puppet/parser/functions/loadyaml.rb @@ -4,7 +4,7 @@ # loadyaml.rb # module Puppet::Parser::Functions - newfunction(:loadyaml, type: :rvalue, arity: -2, doc: <<-'DOC') do |args| + newfunction(:loadyaml, type: :rvalue, arity: -2, doc: <<-DOC) do |args| @summary Load a YAML file containing an array, string, or hash, and return the data in the corresponding native data type. @@ -31,11 +31,11 @@ module Puppet::Parser::Functions if args[0].start_with?('http://', 'https://') username = '' password = '' - if (match = args[0].match(%r{(http\://|https\://)(.*):(.*)@(.*)})) + if (match = args[0].match(%r{(http://|https://)(.*):(.*)@(.*)})) # If URL is in the format of https://username:password@example.local/my_hash.yaml protocol, username, password, path = match.captures url = "#{protocol}#{path}" - elsif (match = args[0].match(%r{(http\:\/\/|https\:\/\/)(.*)@(.*)})) + elsif (match = args[0].match(%r{(http://|https://)(.*)@(.*)})) # If URL is in the format of https://username@example.local/my_hash.yaml protocol, username, path = match.captures url = "#{protocol}#{path}" diff --git a/lib/puppet/parser/functions/merge.rb b/lib/puppet/parser/functions/merge.rb index c257d2c43..5e9d79d47 100644 --- a/lib/puppet/parser/functions/merge.rb +++ b/lib/puppet/parser/functions/merge.rb @@ -4,7 +4,7 @@ # merge.rb # module Puppet::Parser::Functions - newfunction(:merge, type: :rvalue, doc: <<-'DOC') do |args| + newfunction(:merge, type: :rvalue, doc: <<-DOC) do |args| @summary Merges two or more hashes together and returns the resulting hash. diff --git a/lib/puppet/parser/functions/pw_hash.rb b/lib/puppet/parser/functions/pw_hash.rb index ccc991028..d944bf9a8 100644 --- a/lib/puppet/parser/functions/pw_hash.rb +++ b/lib/puppet/parser/functions/pw_hash.rb @@ -76,7 +76,7 @@ # handle weak implementations of String#crypt # dup the string to get rid of frozen status for testing - if ('test'.dup).crypt('$1$1') == '$1$1$Bp8CU9Oujr9SSEw53WV6G.' + if 'test'.dup.crypt('$1$1') == '$1$1$Bp8CU9Oujr9SSEw53WV6G.' password.crypt(salt) else # JRuby < 1.7.17 diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index a0c2f627f..13f93facd 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -59,7 +59,7 @@ module Puppet::Parser::Functions else # arguments.size == 1 value = arguments[0] - m = value.match(%r{^(\w+)(\.\.\.?|\-)(\w+)$}) + m = value.match(%r{^(\w+)(\.\.\.?|-)(\w+)$}) if m start = m[1] stop = m[3] diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb index ebe734de1..a3e69e065 100644 --- a/lib/puppet/parser/functions/values_at.rb +++ b/lib/puppet/parser/functions/values_at.rb @@ -53,7 +53,7 @@ module Puppet::Parser::Functions indices.each do |i| i = i.to_s - m = i.match(%r{^(\d+)(\.\.\.?|\-)(\d+)$}) + m = i.match(%r{^(\d+)(\.\.\.?|-)(\d+)$}) if m start = m[1].to_i stop = m[3].to_i @@ -64,8 +64,8 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, 'values_at(): Stop index in given indices range exceeds array size') if stop > array.size - 1 # First element is at index 0 is it not? range = case type - when %r{^(\.\.|\-)$} then (start..stop) - when %r{^(\.\.\.)$} then (start...stop) # Exclusive of last element ... + when %r{^(\.\.|-)$} then (start..stop) + when %r{^(\.\.\.)$} then (start...stop) # Exclusive of last element ... end range.each { |i| indices_list << i.to_i } # rubocop:disable Lint/ShadowingOuterLocalVariable : Value is meant to be shadowed diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 31da3f5a8..48cd89d35 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -32,7 +32,7 @@ ensure => present, path => '/etc/bashrc', line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', - match => '^export\ HTTP_PROXY\=', + match => '^export HTTP_PROXY=', } ``` @@ -50,7 +50,7 @@ file_line { 'bashrc_proxy': ensure => absent, path => '/etc/bashrc', - match => '^export\ HTTP_PROXY\=', + match => '^export HTTP_PROXY=', match_for_absence => true, } ``` diff --git a/spec/functions/load_module_metadata_spec.rb b/spec/functions/load_module_metadata_spec.rb index caca5e751..608e19db7 100644 --- a/spec/functions/load_module_metadata_spec.rb +++ b/spec/functions/load_module_metadata_spec.rb @@ -11,17 +11,17 @@ before :each do # In Puppet 7, there are two prior calls to File.read prior to the responses we want to mock allow(File).to receive(:read).with(anything, anything).and_call_original - allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, encoding: 'utf-8').and_return('{"name": "puppetlabs-stdlib"}') - allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}') + allow(File).to receive(:read).with(%r{/(stdlib|test)/metadata.json}, encoding: 'utf-8').and_return('{"name": "puppetlabs-stdlib"}') + allow(File).to receive(:read).with(%r{/(stdlib|test)/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}') # Additional modules used by litmus which are identified while running these dues to being in fixtures - allow(File).to receive(:read).with(%r{\/(provision|puppet_agent|facts)\/metadata.json}, encoding: 'utf-8') + allow(File).to receive(:read).with(%r{/(provision|puppet_agent|facts)/metadata.json}, encoding: 'utf-8') end context 'when calling with valid utf8 and double byte character arguments' do before :each do - allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, encoding: 'utf-8').and_return('{"ĭďèʼnţĩƒіểя": "ċơņťęאּť ỡƒ ţħíš - + allow(File).to receive(:read).with(%r{/(stdlib|test)/metadata.json}, encoding: 'utf-8').and_return('{"ĭďèʼnţĩƒіểя": "ċơņťęאּť ỡƒ ţħíš - この文字"}') - allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"ĭďèʼnţĩƒіểя": "ċơņťęאּť ỡƒ ţħíš - + allow(File).to receive(:read).with(%r{/(stdlib|test)/metadata.json}).and_return('{"ĭďèʼnţĩƒіểя": "ċơņťęאּť ỡƒ ţħíš - この文字"}') end diff --git a/spec/functions/loadjson_spec.rb b/spec/functions/loadjson_spec.rb index 0196bfe69..b8282982c 100644 --- a/spec/functions/loadjson_spec.rb +++ b/spec/functions/loadjson_spec.rb @@ -10,10 +10,10 @@ before :each do # In Puppet 7, there are two prior calls to File.read prior to the responses we want to mock allow(File).to receive(:read).with(anything, anything).and_call_original - allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, encoding: 'utf-8').and_return('{"name": "puppetlabs-stdlib"}') - allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}') + allow(File).to receive(:read).with(%r{/(stdlib|test)/metadata.json}, encoding: 'utf-8').and_return('{"name": "puppetlabs-stdlib"}') + allow(File).to receive(:read).with(%r{/(stdlib|test)/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}') # Additional modules used by litmus which are identified while running these dues to being in fixtures - allow(File).to receive(:read).with(%r{\/(provision|puppet_agent|facts)\/metadata.json}, encoding: 'utf-8') + allow(File).to receive(:read).with(%r{/(provision|puppet_agent|facts)/metadata.json}, encoding: 'utf-8') end context 'when a non-existing file is specified' do diff --git a/spec/functions/validate_cmd_spec.rb b/spec/functions/validate_cmd_spec.rb index 99f9529a3..4c0f17e0c 100644 --- a/spec/functions/validate_cmd_spec.rb +++ b/spec/functions/validate_cmd_spec.rb @@ -31,7 +31,7 @@ context 'with % placeholder' do it { expect(subject).to run - .with_params('', "#{touch} % /no/such/file").and_raise_error(Puppet::ParseError, %r{Execution of '#{touch} \S+ \/no\/such\/file' returned 1:.*(cannot touch|o such file or)}) + .with_params('', "#{touch} % /no/such/file").and_raise_error(Puppet::ParseError, %r{Execution of '#{touch} \S+ /no/such/file' returned 1:.*(cannot touch|o such file or)}) } it { is_expected.to run.with_params('', "#{touch} % /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, %r{custom error}) } @@ -40,7 +40,7 @@ context 'without % placeholder' do it { expect(subject).to run - .with_params('', "#{touch} /no/such/file").and_raise_error(Puppet::ParseError, %r{Execution of '#{touch} \/no\/such\/file \S+' returned 1:.*(cannot touch|o such file or)}) + .with_params('', "#{touch} /no/such/file").and_raise_error(Puppet::ParseError, %r{Execution of '#{touch} /no/such/file \S+' returned 1:.*(cannot touch|o such file or)}) } it { is_expected.to run.with_params('', "#{touch} /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, %r{custom error}) } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 335122063..6820cebee 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,7 +26,7 @@ begin default_facts.merge!(YAML.safe_load(File.read(f), permitted_classes: [], permitted_symbols: [], aliases: true)) - rescue => e + rescue StandardError => e RSpec.configuration.reporter.message "WARNING: Unable to load #{f}: #{e}" end end From e1a809d0939f0c313f8f6e3af12729c0ce39f9b7 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 11:44:18 +0100 Subject: [PATCH 25/33] (CONT-801) Autocorrect safe group 8 --- .rubocop_todo.yml | 44 ------------------- lib/puppet/parser/functions/convert_base.rb | 8 +--- lib/puppet/parser/functions/getparam.rb | 4 +- lib/puppet/parser/functions/prefix.rb | 4 +- lib/puppet/parser/functions/pw_hash.rb | 2 +- .../parser/functions/str2saltedpbkdf2.rb | 6 +-- .../parser/functions/str2saltedsha512.rb | 2 +- lib/puppet/parser/functions/suffix.rb | 4 +- lib/puppet/type/file_line.rb | 4 +- spec/classes/manage_spec.rb | 4 +- spec/functions/has_interface_with_spec.rb | 6 +-- spec/functions/has_ip_address_spec.rb | 2 +- spec/functions/has_ip_network_spec.rb | 2 +- spec/functions/os_version_gte_spec.rb | 6 +-- spec/spec_helper.rb | 2 +- spec/unit/facter/root_home_spec.rb | 2 +- .../puppet/provider/file_line/ruby_spec.rb | 18 ++++---- .../provider/file_line/ruby_spec_alter.rb | 4 +- .../provider/file_line/ruby_spec_use_cases.rb | 14 +++--- 19 files changed, 41 insertions(+), 97 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ec6a1c1ea..9d29b1f61 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -302,17 +302,6 @@ Style/SlicingWithRange: Exclude: - 'spec/functions/validate_x509_rsa_key_pair_spec.rb' -# Offense count: 6 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowModifier. -Style/SoleNestedConditional: - Exclude: - - 'lib/puppet/parser/functions/convert_base.rb' - - 'lib/puppet/parser/functions/getparam.rb' - - 'lib/puppet/parser/functions/prefix.rb' - - 'lib/puppet/parser/functions/suffix.rb' - - 'lib/puppet/type/file_line.rb' - # Offense count: 2 # This cop supports unsafe autocorrection (--autocorrect-all). Style/StringChars: @@ -339,36 +328,3 @@ Style/SymbolProc: - 'lib/puppet/parser/functions/fqdn_uuid.rb' - 'lib/puppet/parser/functions/shell_join.rb' - 'lib/puppet/parser/functions/squeeze.rb' - -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, AllowSafeAssignment. -# SupportedStyles: require_parentheses, require_no_parentheses, require_parentheses_when_complex -Style/TernaryParentheses: - Exclude: - - 'spec/unit/facter/root_home_spec.rb' - -# Offense count: 31 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyleForMultiline. -# SupportedStylesForMultiline: comma, consistent_comma, no_comma -Style/TrailingCommaInHashLiteral: - Exclude: - - 'lib/puppet/parser/functions/pw_hash.rb' - - 'lib/puppet/parser/functions/str2saltedpbkdf2.rb' - - 'spec/classes/manage_spec.rb' - - 'spec/functions/has_interface_with_spec.rb' - - 'spec/functions/has_ip_address_spec.rb' - - 'spec/functions/has_ip_network_spec.rb' - - 'spec/functions/os_version_gte_spec.rb' - - 'spec/spec_helper.rb' - - 'spec/unit/puppet/provider/file_line/ruby_spec.rb' - - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' - - 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb' - -# Offense count: 3 -# This cop supports safe autocorrection (--autocorrect). -Style/UnpackFirst: - Exclude: - - 'lib/puppet/parser/functions/str2saltedpbkdf2.rb' - - 'lib/puppet/parser/functions/str2saltedsha512.rb' diff --git a/lib/puppet/parser/functions/convert_base.rb b/lib/puppet/parser/functions/convert_base.rb index 80610433d..b2bddd36d 100644 --- a/lib/puppet/parser/functions/convert_base.rb +++ b/lib/puppet/parser/functions/convert_base.rb @@ -32,13 +32,9 @@ module Puppet::Parser::Functions raise Puppet::ParseError, 'convert_base(): First argument must be either a string or an integer' unless args[0].is_a?(Integer) || args[0].is_a?(String) raise Puppet::ParseError, 'convert_base(): Second argument must be either a string or an integer' unless args[1].is_a?(Integer) || args[1].is_a?(String) - if args[0].is_a?(String) - raise Puppet::ParseError, 'convert_base(): First argument must be an integer or a string corresponding to an integer in base 10' unless %r{^[0-9]+$}.match?(args[0]) - end + raise Puppet::ParseError, 'convert_base(): First argument must be an integer or a string corresponding to an integer in base 10' if args[0].is_a?(String) && !%r{^[0-9]+$}.match?(args[0]) - if args[1].is_a?(String) - raise Puppet::ParseError, 'convert_base(): First argument must be an integer or a string corresponding to an integer in base 10' unless %r{^[0-9]+$}.match?(args[1]) - end + raise Puppet::ParseError, 'convert_base(): First argument must be an integer or a string corresponding to an integer in base 10' if args[1].is_a?(String) && !%r{^[0-9]+$}.match?(args[1]) number_to_convert = args[0] new_base = args[1] diff --git a/lib/puppet/parser/functions/getparam.rb b/lib/puppet/parser/functions/getparam.rb index 44ae4eacf..6dc24a61c 100644 --- a/lib/puppet/parser/functions/getparam.rb +++ b/lib/puppet/parser/functions/getparam.rb @@ -51,9 +51,7 @@ return '' if param.empty? resource = findresource(reference.to_s) - if resource - return resource[param] unless resource[param].nil? - end + return resource[param] if resource && !resource[param].nil? return '' end diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 57b7f7ff6..900067965 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -29,9 +29,7 @@ module Puppet::Parser::Functions prefix = arguments[1] if arguments[1] - if prefix - raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{prefix.inspect}" unless prefix.is_a?(String) - end + raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{prefix.inspect}" if prefix && !prefix.is_a?(String) result = if enumerable.is_a?(Array) # Turn everything into string same as join would do ... diff --git a/lib/puppet/parser/functions/pw_hash.rb b/lib/puppet/parser/functions/pw_hash.rb index d944bf9a8..55eb709bc 100644 --- a/lib/puppet/parser/functions/pw_hash.rb +++ b/lib/puppet/parser/functions/pw_hash.rb @@ -54,7 +54,7 @@ 'bcrypt' => { prefix: '2b', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, 'bcrypt-a' => { prefix: '2a', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, 'bcrypt-x' => { prefix: '2x', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, - 'bcrypt-y' => { prefix: '2y', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }, + 'bcrypt-y' => { prefix: '2y', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} } } raise ArgumentError, 'pw_hash(): first argument must be a string' unless args[0].is_a?(String) || args[0].nil? diff --git a/lib/puppet/parser/functions/str2saltedpbkdf2.rb b/lib/puppet/parser/functions/str2saltedpbkdf2.rb index bb26eb585..5640832dd 100644 --- a/lib/puppet/parser/functions/str2saltedpbkdf2.rb +++ b/lib/puppet/parser/functions/str2saltedpbkdf2.rb @@ -62,9 +62,9 @@ module Puppet::Parser::Functions hash = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, keylen, digest) { - 'password_hex' => hash.unpack('H*').first, - 'salt_hex' => salt.unpack('H*').first, - 'iterations' => iterations, + 'password_hex' => hash.unpack1('H*'), + 'salt_hex' => salt.unpack1('H*'), + 'iterations' => iterations } end end diff --git a/lib/puppet/parser/functions/str2saltedsha512.rb b/lib/puppet/parser/functions/str2saltedsha512.rb index 0423b20ca..4d4800e8a 100644 --- a/lib/puppet/parser/functions/str2saltedsha512.rb +++ b/lib/puppet/parser/functions/str2saltedsha512.rb @@ -29,7 +29,7 @@ module Puppet::Parser::Functions seedint = rand((2**31) - 1) seedstring = Array(seedint).pack('L') saltedpass = Digest::SHA512.digest(seedstring + password) - (seedstring + saltedpass).unpack('H*')[0] + (seedstring + saltedpass).unpack1('H*') end end diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb index e74e50956..037ab5b34 100644 --- a/lib/puppet/parser/functions/suffix.rb +++ b/lib/puppet/parser/functions/suffix.rb @@ -33,9 +33,7 @@ module Puppet::Parser::Functions suffix = arguments[1] if arguments[1] - if suffix - raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}" unless suffix.is_a? String - end + raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}" if suffix && !(suffix.is_a? String) result = if enumerable.is_a?(Array) # Turn everything into string same as join would do ... diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 48cd89d35..bcb74f3fa 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -188,9 +188,7 @@ def retrieve raise(Puppet::Error, 'multiple must be true when replace_all_matches_not_matching_line is true') if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:multiple].to_s == 'false' raise(Puppet::Error, 'replace must be true when replace_all_matches_not_matching_line is true') if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:replace].to_s == 'false' - unless self[:line] - raise(Puppet::Error, 'line is a required attribute') unless (self[:ensure].to_s == 'absent') && (self[:match_for_absence].to_s == 'true') && self[:match] - end + raise(Puppet::Error, 'line is a required attribute') if !self[:line] && !((self[:ensure].to_s == 'absent') && (self[:match_for_absence].to_s == 'true') && self[:match]) raise(Puppet::Error, 'path is a required attribute') unless self[:path] end end diff --git a/spec/classes/manage_spec.rb b/spec/classes/manage_spec.rb index af41354fc..a34fc7296 100644 --- a/spec/classes/manage_spec.rb +++ b/spec/classes/manage_spec.rb @@ -24,13 +24,13 @@ 'file' => { '/etc/motd.d/hello' => { 'content' => 'I say Hi', - 'notify' => 'Service[sshd]', + 'notify' => 'Service[sshd]' } }, 'package' => { 'example' => { 'ensure' => 'installed', - 'subscribe' => ['Service[sshd]', 'File[/etc/motd.d]'], + 'subscribe' => ['Service[sshd]', 'File[/etc/motd.d]'] } } } diff --git a/spec/functions/has_interface_with_spec.rb b/spec/functions/has_interface_with_spec.rb index ed1a6081d..a2f1a856a 100644 --- a/spec/functions/has_interface_with_spec.rb +++ b/spec/functions/has_interface_with_spec.rb @@ -42,7 +42,7 @@ 'network' => '127.0.0.0', 'network6' => '::1', 'scope6' => 'host' - }, + } } } } @@ -129,8 +129,8 @@ 'network' => '127.0.0.0', 'network6' => '::1' } - }, - }, + } + } } end diff --git a/spec/functions/has_ip_address_spec.rb b/spec/functions/has_ip_address_spec.rb index a30a5a5ab..0c08153da 100644 --- a/spec/functions/has_ip_address_spec.rb +++ b/spec/functions/has_ip_address_spec.rb @@ -13,7 +13,7 @@ interfaces: 'eth0,lo', ipaddress: '10.0.0.1', ipaddress_lo: '127.0.0.1', - ipaddress_eth0: '10.0.0.1', + ipaddress_eth0: '10.0.0.1' } end diff --git a/spec/functions/has_ip_network_spec.rb b/spec/functions/has_ip_network_spec.rb index 3366757a1..d6f01f457 100644 --- a/spec/functions/has_ip_network_spec.rb +++ b/spec/functions/has_ip_network_spec.rb @@ -12,7 +12,7 @@ { interfaces: 'eth0,lo', network_lo: '127.0.0.0', - network_eth0: '10.0.0.0', + network_eth0: '10.0.0.0' } end diff --git a/spec/functions/os_version_gte_spec.rb b/spec/functions/os_version_gte_spec.rb index ed1a067af..86ea54c95 100644 --- a/spec/functions/os_version_gte_spec.rb +++ b/spec/functions/os_version_gte_spec.rb @@ -8,7 +8,7 @@ { os: { name: 'Debian', - release: { major: '9' }, + release: { major: '9' } } } end @@ -26,7 +26,7 @@ { os: { name: 'Ubuntu', - release: { major: '16.04' }, + release: { major: '16.04' } } } end @@ -45,7 +45,7 @@ { os: { name: 'Ubuntu', - release: { major: '16.04' }, + release: { major: '16.04' } } } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6820cebee..23cd51a7a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,7 +13,7 @@ default_facts = { puppetversion: Puppet.version, - facterversion: Facter.version, + facterversion: Facter.version } default_fact_files = [ diff --git a/spec/unit/facter/root_home_spec.rb b/spec/unit/facter/root_home_spec.rb index 194abe361..e1c97a30e 100644 --- a/spec/unit/facter/root_home_spec.rb +++ b/spec/unit/facter/root_home_spec.rb @@ -14,7 +14,7 @@ end context 'when non-Windows', if: Facter.value(:kernel) != 'Windows' do - let(:expected) { Facter.value(:kernel) == 'Darwin' ? '/var/root' : '/root' } + let(:expected) { (Facter.value(:kernel) == 'Darwin') ? '/var/root' : '/root' } it { expect(subject.value).to eq(expected) } end diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index 0871daf20..0577e3017 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -20,7 +20,7 @@ Puppet::Type::File_line.new({ name: 'foo', path: tmpfile, - line: 'foo', + line: 'foo' }.merge(params)) end let :provider do @@ -115,7 +115,7 @@ let(:params) do { append_on_no_match: false, - match: '^foo1$', + match: '^foo1$' } end @@ -146,7 +146,7 @@ let(:params) do { replace_all_matches_not_matching_line: true, - replace: false, + replace: false } end @@ -160,7 +160,7 @@ { replace_all_matches_not_matching_line: true, match: '^foo', - multiple: true, + multiple: true } end let(:content) { "foo\nfoo bar\nbar\nfoo baz" } @@ -180,7 +180,7 @@ { replace_all_matches_not_matching_line: true, match: '^foo', - multiple: true, + multiple: true } end let(:content) { "foo\nfoo\nbar" } @@ -195,7 +195,7 @@ { replace_all_matches_not_matching_line: true, match: '^bar', - multiple: true, + multiple: true } end let(:content) { "foo\nfoo bar\nbar\nbar baz" } @@ -215,7 +215,7 @@ { replace_all_matches_not_matching_line: true, match: '^bar', - multiple: true, + multiple: true } end let(:content) { "foo\nfoo\nbar\nbar baz" } @@ -236,7 +236,7 @@ { replace_all_matches_not_matching_line: true, match: '^bar', - multiple: true, + multiple: true } end let(:content) { "foo\nfoo bar" } @@ -251,7 +251,7 @@ { replace_all_matches_not_matching_line: true, match: '^bar', - multiple: true, + multiple: true } end let(:content) { 'foo bar' } diff --git a/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb b/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb index c3cabed65..268157fa2 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb @@ -20,7 +20,7 @@ Puppet::Type::File_line.new({ name: 'foo', path: tmpfile, - line: 'foo', + line: 'foo' }.merge(params)) end let :provider do @@ -37,7 +37,7 @@ { line: 'foo = bar', match: '^foo\s*=.*$', - replace: false, + replace: false } end let(:content) { "foo1\nfoo=blah\nfoo2\nfoo3" } diff --git a/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb b/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb index fb9cedb46..b3578a679 100644 --- a/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb @@ -20,7 +20,7 @@ Puppet::Type::File_line.new({ name: 'foo', path: tmpfile, - line: 'foo', + line: 'foo' }.merge(params)) end let :provider do @@ -37,7 +37,7 @@ { line: "*\thard\tcore\t0", match: "^[ \t]*\\*[ \t]+hard[ \t]+core[ \t]+.*", - multiple: true, + multiple: true } end let(:content) { "* hard core 90\n* hard core 10\n" } @@ -57,7 +57,7 @@ { line: "*\thard\tcore\t0", match: "^[ \t]*\\*[ \t]+hard[ \t]+core[ \t]+.*", - multiple: true, + multiple: true } end let(:content) { "* hard core 90\n* hard core 0\n" } @@ -72,7 +72,7 @@ { line: "*\thard\tcore\t0", match: "^[ \t]*\\*[ \t]+hard[ \t]+core[ \t]+.*", - multiple: true, + multiple: true }.merge(replace_all_matches_not_matching_line: true) end let(:content) { "* hard core 90\n* hard core 0\n" } @@ -91,7 +91,7 @@ let(:params) do { line: 'LogLevel=notice', - match: '^#LogLevel$', + match: '^#LogLevel$' } end let(:content) { "#LogLevel\nstuff" } @@ -110,7 +110,7 @@ let(:params) do { line: 'LogLevel=notice', - match: '^#LogLevel$', + match: '^#LogLevel$' } end let(:content) { "#Loglevel\nLogLevel=notice\nstuff" } @@ -124,7 +124,7 @@ let(:params) do { line: 'LogLevel=notice', - match: '^#LogLevel$', + match: '^#LogLevel$' } end let(:content) { "LogLevel=notice\nstuff" } From 8840b865b15e0215f3d19c93f4386d08a9593175 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 12:09:27 +0100 Subject: [PATCH 26/33] (CONT-801) Fix cop error --- spec/functions/loadjson_spec.rb | 4 ++-- spec/unit/facter/util/puppet_settings_spec.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/functions/loadjson_spec.rb b/spec/functions/loadjson_spec.rb index b8282982c..41bf2580c 100644 --- a/spec/functions/loadjson_spec.rb +++ b/spec/functions/loadjson_spec.rb @@ -28,9 +28,9 @@ before(:each) do allow(File).to receive(:exists?).with(filename).and_return(false).once if Puppet::PUPPETVERSION[0].to_i < 8 - allow(PSON).not_to receive(:load) + allow(PSON).to receive(:load).never # rubocop:disable RSpec/ReceiveNever Switching to not_to receive breaks testing in this case else - allow(JSON).not_to receive(:parse) + allow(JSON).to receive(:parse).never # rubocop:disable RSpec/ReceiveNever end end diff --git a/spec/unit/facter/util/puppet_settings_spec.rb b/spec/unit/facter/util/puppet_settings_spec.rb index 7bb7da611..13d032f51 100644 --- a/spec/unit/facter/util/puppet_settings_spec.rb +++ b/spec/unit/facter/util/puppet_settings_spec.rb @@ -7,7 +7,7 @@ describe '#with_puppet' do context 'without Puppet loaded' do before(:each) do - allow(Module).to receive(:const_get).with('Puppet').and_raise(NameError) + allow(Module).to receive(:const_get).with(:Puppet).and_raise(NameError) end it 'is nil' do From 18a8332ef5a8f23159d0ff5520dbd4b739792c06 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 14:17:34 +0100 Subject: [PATCH 27/33] (CONT-801) Autocorrect unsafe group 1 --- .rubocop_todo.yml | 37 ---- lib/puppet/parser/functions/assert_private.rb | 2 +- lib/puppet/parser/functions/bool2str.rb | 2 +- lib/puppet/parser/functions/getparam.rb | 2 +- lib/puppet/parser/functions/pw_hash.rb | 2 +- lib/puppet_x/stdlib/toml_dumper.rb | 173 +++++++++--------- 6 files changed, 90 insertions(+), 128 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9d29b1f61..65e6cc203 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -34,15 +34,6 @@ Lint/MissingCopEnableDirective: - 'spec/functions/merge_spec.rb' - 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb' -# Offense count: 2 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: AllowedMethods. -# AllowedMethods: instance_of?, kind_of?, is_a?, eql?, respond_to?, equal? -Lint/RedundantSafeNavigation: - Exclude: - - 'lib/puppet/parser/functions/assert_private.rb' - - 'lib/puppet/parser/functions/getparam.rb' - # Offense count: 3 # Configuration parameters: AllowComments, AllowNil. Lint/SuppressedException: @@ -105,19 +96,6 @@ Performance/CollectionLiteralInLoop: Exclude: - 'lib/puppet/functions/ensure_packages.rb' -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: AllowRegexpMatch. -Performance/RedundantEqualityComparisonBlock: - Exclude: - - 'lib/puppet/parser/functions/bool2str.rb' - -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -Performance/UnfreezeString: - Exclude: - - 'lib/puppet/parser/functions/pw_hash.rb' - # Offense count: 95 # This cop supports unsafe autocorrection (--autocorrect-all). RSpec/BeEq: @@ -202,21 +180,6 @@ RSpec/StubbedMock: - 'spec/functions/reverse_spec.rb' - 'spec/functions/squeeze_spec.rb' -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: MinBranchesCount. -Style/CaseLikeIf: - Exclude: - - 'lib/puppet_x/stdlib/toml_dumper.rb' - -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: nested, compact -Style/ClassAndModuleChildren: - Exclude: - - 'lib/puppet_x/stdlib/toml_dumper.rb' - # Offense count: 2 # This cop supports unsafe autocorrection (--autocorrect-all). Style/CollectionCompact: diff --git a/lib/puppet/parser/functions/assert_private.rb b/lib/puppet/parser/functions/assert_private.rb index 726b88484..a5f420984 100644 --- a/lib/puppet/parser/functions/assert_private.rb +++ b/lib/puppet/parser/functions/assert_private.rb @@ -19,7 +19,7 @@ module Puppet::Parser::Functions scope = self if scope.lookupvar('module_name') != scope.lookupvar('caller_module_name') message = nil - if args[0]&.is_a?(String) + if args[0].is_a?(String) message = args[0] else manifest_name = scope.source.name diff --git a/lib/puppet/parser/functions/bool2str.rb b/lib/puppet/parser/functions/bool2str.rb index 584bb1f0d..f94c37e64 100644 --- a/lib/puppet/parser/functions/bool2str.rb +++ b/lib/puppet/parser/functions/bool2str.rb @@ -49,7 +49,7 @@ module Puppet::Parser::Functions # We can have either true or false, and nothing else raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with') unless [FalseClass, TrueClass].include?(klass) - raise(Puppet::ParseError, 'bool2str(): Requires strings to convert to') unless [true_string, false_string].all? { |x| x.is_a?(String) } + raise(Puppet::ParseError, 'bool2str(): Requires strings to convert to') unless [true_string, false_string].all?(String) return value ? true_string : false_string end diff --git a/lib/puppet/parser/functions/getparam.rb b/lib/puppet/parser/functions/getparam.rb index 6dc24a61c..ed59dcd64 100644 --- a/lib/puppet/parser/functions/getparam.rb +++ b/lib/puppet/parser/functions/getparam.rb @@ -46,7 +46,7 @@ ) do |vals| reference, param = vals raise(ArgumentError, 'Must specify a reference') unless reference - raise(ArgumentError, 'Must specify name of a parameter') unless param&.instance_of?(String) + raise(ArgumentError, 'Must specify name of a parameter') unless param.instance_of?(String) return '' if param.empty? diff --git a/lib/puppet/parser/functions/pw_hash.rb b/lib/puppet/parser/functions/pw_hash.rb index 55eb709bc..8a09647eb 100644 --- a/lib/puppet/parser/functions/pw_hash.rb +++ b/lib/puppet/parser/functions/pw_hash.rb @@ -76,7 +76,7 @@ # handle weak implementations of String#crypt # dup the string to get rid of frozen status for testing - if 'test'.dup.crypt('$1$1') == '$1$1$Bp8CU9Oujr9SSEw53WV6G.' + if (+'test').crypt('$1$1') == '$1$1$Bp8CU9Oujr9SSEw53WV6G.' password.crypt(salt) else # JRuby < 1.7.17 diff --git a/lib/puppet_x/stdlib/toml_dumper.rb b/lib/puppet_x/stdlib/toml_dumper.rb index 3259201c1..84c3bdf7e 100644 --- a/lib/puppet_x/stdlib/toml_dumper.rb +++ b/lib/puppet_x/stdlib/toml_dumper.rb @@ -21,116 +21,115 @@ require_relative '../../puppet_x/stdlib' require 'date' -module PuppetX::Stdlib - # The Dumper class was blindly copied from https://github.com/emancu/toml-rb/blob/v2.0.1/lib/toml-rb/dumper.rb - # This allows us to use the `to_toml` function as a `Deferred` function because the `toml-rb` gem is usually - # installed on the agent and the `Deferred` function gets evaluated before the catalog gets applied. This - # makes it in most scenarios impossible to install the gem before it is used. - class TomlDumper - attr_reader :toml_str - - def initialize(hash) - @toml_str = '' - - visit(hash, []) - end +# The Dumper class was blindly copied from https://github.com/emancu/toml-rb/blob/v2.0.1/lib/toml-rb/dumper.rb +# This allows us to use the `to_toml` function as a `Deferred` function because the `toml-rb` gem is usually +# installed on the agent and the `Deferred` function gets evaluated before the catalog gets applied. This +# makes it in most scenarios impossible to install the gem before it is used. +class PuppetX::Stdlib::TomlDumper + attr_reader :toml_str - private + def initialize(hash) + @toml_str = '' - def visit(hash, prefix, extra_brackets = false) - simple_pairs, nested_pairs, table_array_pairs = sort_pairs hash + visit(hash, []) + end - print_prefix prefix, extra_brackets if prefix.any? && (simple_pairs.any? || hash.empty?) + private - dump_pairs simple_pairs, nested_pairs, table_array_pairs, prefix - end + def visit(hash, prefix, extra_brackets = false) + simple_pairs, nested_pairs, table_array_pairs = sort_pairs hash - def sort_pairs(hash) - nested_pairs = [] - simple_pairs = [] - table_array_pairs = [] - - hash.keys.sort.each do |key| - val = hash[key] - element = [key, val] - - if val.is_a? Hash - nested_pairs << element - elsif val.is_a?(Array) && val.first.is_a?(Hash) - table_array_pairs << element - else - simple_pairs << element - end - end + print_prefix prefix, extra_brackets if prefix.any? && (simple_pairs.any? || hash.empty?) - [simple_pairs, nested_pairs, table_array_pairs] - end - - def dump_pairs(simple, nested, table_array, prefix = []) - # First add simple pairs, under the prefix - dump_simple_pairs simple - dump_nested_pairs nested, prefix - dump_table_array_pairs table_array, prefix - end + dump_pairs simple_pairs, nested_pairs, table_array_pairs, prefix + end - def dump_simple_pairs(simple_pairs) - simple_pairs.each do |key, val| - key = quote_key(key) unless bare_key? key - @toml_str << "#{key} = #{to_toml(val)}\n" - end - end + def sort_pairs(hash) + nested_pairs = [] + simple_pairs = [] + table_array_pairs = [] - def dump_nested_pairs(nested_pairs, prefix) - nested_pairs.each do |key, val| - key = quote_key(key) unless bare_key? key + hash.keys.sort.each do |key| + val = hash[key] + element = [key, val] - visit val, prefix + [key], false + if val.is_a? Hash + nested_pairs << element + elsif val.is_a?(Array) && val.first.is_a?(Hash) + table_array_pairs << element + else + simple_pairs << element end end - def dump_table_array_pairs(table_array_pairs, prefix) - table_array_pairs.each do |key, val| - key = quote_key(key) unless bare_key? key - aux_prefix = prefix + [key] + [simple_pairs, nested_pairs, table_array_pairs] + end - val.each do |child| - print_prefix aux_prefix, true - args = sort_pairs(child) << aux_prefix + def dump_pairs(simple, nested, table_array, prefix = []) + # First add simple pairs, under the prefix + dump_simple_pairs simple + dump_nested_pairs nested, prefix + dump_table_array_pairs table_array, prefix + end - dump_pairs(*args) - end - end + def dump_simple_pairs(simple_pairs) + simple_pairs.each do |key, val| + key = quote_key(key) unless bare_key? key + @toml_str << "#{key} = #{to_toml(val)}\n" end + end - def print_prefix(prefix, extra_brackets = false) - new_prefix = prefix.join('.') - new_prefix = '[' + new_prefix + ']' if extra_brackets + def dump_nested_pairs(nested_pairs, prefix) + nested_pairs.each do |key, val| + key = quote_key(key) unless bare_key? key - @toml_str += "[" + new_prefix + "]\n" # rubocop:disable Style/StringLiterals + visit val, prefix + [key], false end + end - def to_toml(obj) - if obj.is_a?(Time) || obj.is_a?(DateTime) - obj.strftime('%Y-%m-%dT%H:%M:%SZ') - elsif obj.is_a?(Date) - obj.strftime('%Y-%m-%d') - elsif obj.is_a? Regexp - obj.inspect.inspect - elsif obj.is_a? String - obj.inspect.gsub(/\\(#[$@{])/, '\1') # rubocop:disable Style/RegexpLiteral - else - obj.inspect + def dump_table_array_pairs(table_array_pairs, prefix) + table_array_pairs.each do |key, val| + key = quote_key(key) unless bare_key? key + aux_prefix = prefix + [key] + + val.each do |child| + print_prefix aux_prefix, true + args = sort_pairs(child) << aux_prefix + + dump_pairs(*args) end end + end - def bare_key?(key) - # rubocop:disable Style/RegexpLiteral - !!key.to_s.match(/^[a-zA-Z0-9_-]*$/) - # rubocop:enable Style/RegexpLiteral - end + def print_prefix(prefix, extra_brackets = false) + new_prefix = prefix.join('.') + new_prefix = '[' + new_prefix + ']' if extra_brackets - def quote_key(key) - '"' + key.gsub('"', '\\"') + '"' + @toml_str += "[" + new_prefix + "]\n" # rubocop:disable Style/StringLiterals + end + + def to_toml(obj) + case obj + when Time, DateTime + obj.strftime('%Y-%m-%dT%H:%M:%SZ') + when Date + obj.strftime('%Y-%m-%d') + when Regexp + obj.inspect.inspect + when String + obj.inspect.gsub(/\\(#[$@{])/, '\1') # rubocop:disable Style/RegexpLiteral + else + obj.inspect end end + + def bare_key?(key) + # rubocop:disable Style/RegexpLiteral + !!key.to_s.match(/^[a-zA-Z0-9_-]*$/) + # rubocop:enable Style/RegexpLiteral + end + + def quote_key(key) + '"' + key.gsub('"', '\\"') + '"' + end end From 2d788513947f5a8ded6eb403c9e99a15f678b525 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Wed, 17 May 2023 14:26:14 +0100 Subject: [PATCH 28/33] (CONT-801) Unsafe autocorrect RSpec/BeEq --- .rubocop_todo.yml | 5 ----- spec/functions/any2array_spec.rb | 2 +- spec/functions/any2bool_spec.rb | 2 +- spec/functions/base64_spec.rb | 2 +- spec/functions/basename_spec.rb | 2 +- spec/functions/batch_escape_spec.rb | 2 +- spec/functions/bool2num_spec.rb | 2 +- spec/functions/bool2str_spec.rb | 2 +- spec/functions/clamp_spec.rb | 2 +- spec/functions/concat_spec.rb | 2 +- spec/functions/convert_base_spec.rb | 2 +- spec/functions/count_spec.rb | 2 +- spec/functions/crc32_spec.rb | 2 +- spec/functions/delete_at_spec.rb | 2 +- spec/functions/delete_regex_spec.rb | 2 +- spec/functions/delete_spec.rb | 2 +- spec/functions/delete_undef_values_spec.rb | 2 +- spec/functions/delete_values_spec.rb | 2 +- spec/functions/deprecation_spec.rb | 4 ++-- spec/functions/difference_spec.rb | 2 +- spec/functions/dirname_spec.rb | 2 +- spec/functions/dos2unix_spec.rb | 2 +- spec/functions/ensure_packages_spec.rb | 2 +- spec/functions/ensure_resource_spec.rb | 2 +- spec/functions/ensure_resources_spec.rb | 2 +- spec/functions/extname_spec.rb | 2 +- spec/functions/fqdn_rand_string_spec.rb | 2 +- spec/functions/fqdn_rotate_spec.rb | 2 +- spec/functions/get_module_path_spec.rb | 2 +- spec/functions/getparam_spec.rb | 2 +- spec/functions/glob_spec.rb | 2 +- spec/functions/grep_spec.rb | 2 +- spec/functions/has_interface_with_spec.rb | 2 +- spec/functions/has_ip_address_spec.rb | 2 +- spec/functions/has_ip_network_spec.rb | 2 +- spec/functions/intersection_spec.rb | 2 +- spec/functions/ip_in_range_spec.rb | 2 +- spec/functions/is_a_spec.rb | 4 ++-- spec/functions/join_keys_to_values_spec.rb | 2 +- spec/functions/load_module_metadata_spec.rb | 2 +- spec/functions/loadjson_spec.rb | 2 +- spec/functions/loadyaml_spec.rb | 2 +- spec/functions/member_spec.rb | 2 +- spec/functions/merge_spec.rb | 2 +- spec/functions/num2bool_spec.rb | 2 +- spec/functions/parsejson_spec.rb | 2 +- spec/functions/parsepson_spec.rb | 2 +- spec/functions/parseyaml_spec.rb | 2 +- spec/functions/pick_default_spec.rb | 2 +- spec/functions/pick_spec.rb | 2 +- spec/functions/powershell_escape_spec.rb | 2 +- spec/functions/prefix_spec.rb | 2 +- spec/functions/pw_hash_spec.rb | 2 +- spec/functions/range_spec.rb | 2 +- spec/functions/regexpescape_spec.rb | 2 +- spec/functions/reject_spec.rb | 2 +- spec/functions/reverse_spec.rb | 2 +- spec/functions/seeded_rand_spec.rb | 2 +- spec/functions/sha256_spec.rb | 2 +- spec/functions/shell_escape_spec.rb | 2 +- spec/functions/shell_join_spec.rb | 2 +- spec/functions/shell_split_spec.rb | 2 +- spec/functions/shuffle_spec.rb | 2 +- spec/functions/squeeze_spec.rb | 2 +- spec/functions/startswith_spec.rb | 2 +- spec/functions/str2bool_spec.rb | 2 +- spec/functions/str2resource_spec.rb | 2 +- spec/functions/str2saltedpbkdf2_spec.rb | 2 +- spec/functions/str2saltedsha512_spec.rb | 2 +- spec/functions/suffix_spec.rb | 2 +- spec/functions/swapcase_spec.rb | 2 +- spec/functions/time_spec.rb | 2 +- spec/functions/to_bytes_spec.rb | 2 +- spec/functions/to_json_pretty_spec.rb | 2 +- spec/functions/to_json_spec.rb | 2 +- spec/functions/to_python_spec.rb | 2 +- spec/functions/to_ruby_spec.rb | 2 +- spec/functions/to_toml_spec.rb | 2 +- spec/functions/to_yaml_spec.rb | 2 +- spec/functions/type_of_spec.rb | 4 ++-- spec/functions/union_spec.rb | 2 +- spec/functions/unix2dos_spec.rb | 2 +- spec/functions/uriescape_spec.rb | 2 +- spec/functions/validate_augeas_spec.rb | 2 +- spec/functions/validate_cmd_spec.rb | 2 +- spec/functions/validate_domain_name_spec.rb | 2 +- spec/functions/validate_email_address_spec.rb | 2 +- spec/functions/validate_legacy_spec.rb | 2 +- spec/functions/validate_x509_rsa_key_pair_spec.rb | 2 +- spec/functions/values_at_spec.rb | 2 +- spec/functions/zip_spec.rb | 2 +- spec/unit/facter/pe_version_spec.rb | 4 ++-- 92 files changed, 95 insertions(+), 100 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 65e6cc203..24585ea6f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -96,11 +96,6 @@ Performance/CollectionLiteralInLoop: Exclude: - 'lib/puppet/functions/ensure_packages.rb' -# Offense count: 95 -# This cop supports unsafe autocorrection (--autocorrect-all). -RSpec/BeEq: - Enabled: false - # Offense count: 36 # Configuration parameters: Prefixes, AllowedPatterns. # Prefixes: when, with, without diff --git a/spec/functions/any2array_spec.rb b/spec/functions/any2array_spec.rb index 7ddf67b47..679ba9ea0 100644 --- a/spec/functions/any2array_spec.rb +++ b/spec/functions/any2array_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'any2array' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_return([]) } it { is_expected.to run.with_params('').and_return([]) } it { is_expected.to run.with_params(true).and_return([true]) } diff --git a/spec/functions/any2bool_spec.rb b/spec/functions/any2bool_spec.rb index ff06a689d..03fb21906 100644 --- a/spec/functions/any2bool_spec.rb +++ b/spec/functions/any2bool_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'any2bool' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(true).and_return(true) } diff --git a/spec/functions/base64_spec.rb b/spec/functions/base64_spec.rb index b2592be89..111910faf 100644 --- a/spec/functions/base64_spec.rb +++ b/spec/functions/base64_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'base64' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) } it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{first argument must be one of}) } diff --git a/spec/functions/basename_spec.rb b/spec/functions/basename_spec.rb index 3f520602a..e9934ca13 100644 --- a/spec/functions/basename_spec.rb +++ b/spec/functions/basename_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'basename' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{No arguments given}) } it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{Too many arguments given}) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{Requires string as first argument}) } diff --git a/spec/functions/batch_escape_spec.rb b/spec/functions/batch_escape_spec.rb index ab609abd5..7950d6d5e 100644 --- a/spec/functions/batch_escape_spec.rb +++ b/spec/functions/batch_escape_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'batch_escape' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } describe 'signature validation' do it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'batch_escape' expects 1 argument, got none}) } diff --git a/spec/functions/bool2num_spec.rb b/spec/functions/bool2num_spec.rb index 63a664b6b..55e277c0c 100644 --- a/spec/functions/bool2num_spec.rb +++ b/spec/functions/bool2num_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'bool2num' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } [true, 'true', 't', '1', 'y', 'yes', AlsoString.new('true')].each do |truthy| diff --git a/spec/functions/bool2str_spec.rb b/spec/functions/bool2str_spec.rb index 9bf618653..0e955b964 100644 --- a/spec/functions/bool2str_spec.rb +++ b/spec/functions/bool2str_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'bool2str' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } ['true', 'false', nil, :undef, ''].each do |invalid| diff --git a/spec/functions/clamp_spec.rb b/spec/functions/clamp_spec.rb index 37b4935c9..43c877b7c 100644 --- a/spec/functions/clamp_spec.rb +++ b/spec/functions/clamp_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'clamp' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params(12, 88, 71, 190).and_raise_error(Puppet::ParseError, %r{Wrong number of arguments, need three to clamp}) } diff --git a/spec/functions/concat_spec.rb b/spec/functions/concat_spec.rb index d0893e991..504259444 100644 --- a/spec/functions/concat_spec.rb +++ b/spec/functions/concat_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'concat' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params([1]).and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params(1, [2]).and_raise_error(Puppet::ParseError, %r{Requires array}) } diff --git a/spec/functions/convert_base_spec.rb b/spec/functions/convert_base_spec.rb index 88cd93eb2..3ea7d44e2 100644 --- a/spec/functions/convert_base_spec.rb +++ b/spec/functions/convert_base_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'convert_base' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError) } it { is_expected.to run.with_params('asdf').and_raise_error(ArgumentError) } it { is_expected.to run.with_params('asdf', 'moo', 'cow').and_raise_error(ArgumentError) } diff --git a/spec/functions/count_spec.rb b/spec/functions/count_spec.rb index bab41e3c7..2ce409633 100644 --- a/spec/functions/count_spec.rb +++ b/spec/functions/count_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'count' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError) } it { is_expected.to run.with_params('one').and_raise_error(ArgumentError) } it { is_expected.to run.with_params('one', 'two').and_return(1) } diff --git a/spec/functions/crc32_spec.rb b/spec/functions/crc32_spec.rb index ed82ae2fc..5519b6841 100644 --- a/spec/functions/crc32_spec.rb +++ b/spec/functions/crc32_spec.rb @@ -4,7 +4,7 @@ describe 'stdlib::crc32' do context 'when default' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{stdlib::crc32}) } end diff --git a/spec/functions/delete_at_spec.rb b/spec/functions/delete_at_spec.rb index 13e45ebaf..dc0a7578e 100644 --- a/spec/functions/delete_at_spec.rb +++ b/spec/functions/delete_at_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'delete_at' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params('one', 1).and_raise_error(Puppet::ParseError, %r{Requires array}) } it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError, %r{Requires array}) } diff --git a/spec/functions/delete_regex_spec.rb b/spec/functions/delete_regex_spec.rb index 3c2cc39f8..6f4d7048f 100644 --- a/spec/functions/delete_regex_spec.rb +++ b/spec/functions/delete_regex_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'delete_regex' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params([], 'two') } diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb index 82eba5364..37fee322b 100644 --- a/spec/functions/delete_spec.rb +++ b/spec/functions/delete_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'delete' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params([], 'two') } diff --git a/spec/functions/delete_undef_values_spec.rb b/spec/functions/delete_undef_values_spec.rb index d93b1cdc8..4a251cdc2 100644 --- a/spec/functions/delete_undef_values_spec.rb +++ b/spec/functions/delete_undef_values_spec.rb @@ -5,7 +5,7 @@ describe 'delete_undef_values' do let(:is_puppet_6) { Puppet::Util::Package.versioncmp(Puppet.version, '6.0.0') == 0 } - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{expected an array or hash}) } it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{expected an array or hash}) } diff --git a/spec/functions/delete_values_spec.rb b/spec/functions/delete_values_spec.rb index f63af7133..df1752ec5 100644 --- a/spec/functions/delete_values_spec.rb +++ b/spec/functions/delete_values_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'delete_values' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } diff --git a/spec/functions/deprecation_spec.rb b/spec/functions/deprecation_spec.rb index 1d60c9976..a38703a9d 100644 --- a/spec/functions/deprecation_spec.rb +++ b/spec/functions/deprecation_spec.rb @@ -14,7 +14,7 @@ Puppet.settings[:strict] = :warning end - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError) } it 'displays a single warning' do @@ -66,7 +66,7 @@ ENV['STDLIB_LOG_DEPRECATIONS'] = 'true' end - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it 'displays a single warning' do diff --git a/spec/functions/difference_spec.rb b/spec/functions/difference_spec.rb index 292023c19..a66317567 100644 --- a/spec/functions/difference_spec.rb +++ b/spec/functions/difference_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'difference' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{Wrong number of arguments}) } it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{Requires 2 arrays}) } diff --git a/spec/functions/dirname_spec.rb b/spec/functions/dirname_spec.rb index a801f1c1f..ef8c8a05a 100644 --- a/spec/functions/dirname_spec.rb +++ b/spec/functions/dirname_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'dirname' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{No arguments given}) } it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{Too many arguments given}) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{Requires string as argument}) } diff --git a/spec/functions/dos2unix_spec.rb b/spec/functions/dos2unix_spec.rb index e7f588852..26abe1204 100644 --- a/spec/functions/dos2unix_spec.rb +++ b/spec/functions/dos2unix_spec.rb @@ -4,7 +4,7 @@ describe 'dos2unix' do context 'when checking parameter validity' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it do expect(subject).to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) diff --git a/spec/functions/ensure_packages_spec.rb b/spec/functions/ensure_packages_spec.rb index ab4298482..763d842ca 100644 --- a/spec/functions/ensure_packages_spec.rb +++ b/spec/functions/ensure_packages_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'ensure_packages' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params('packagename') } it { is_expected.to run.with_params(['packagename1', 'packagename2']) } diff --git a/spec/functions/ensure_resource_spec.rb b/spec/functions/ensure_resource_spec.rb index 7508eed94..9b04db6b6 100644 --- a/spec/functions/ensure_resource_spec.rb +++ b/spec/functions/ensure_resource_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'ensure_resource' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Must specify a type}) } it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, %r{Must specify a title}) } diff --git a/spec/functions/ensure_resources_spec.rb b/spec/functions/ensure_resources_spec.rb index 55dac3d67..cf4de9e48 100644 --- a/spec/functions/ensure_resources_spec.rb +++ b/spec/functions/ensure_resources_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'ensure_resources' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Must specify a type}) } it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, %r{Must specify a title}) } diff --git a/spec/functions/extname_spec.rb b/spec/functions/extname_spec.rb index 4808e11b4..bca462998 100644 --- a/spec/functions/extname_spec.rb +++ b/spec/functions/extname_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'stdlib::extname' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'stdlib::extname' expects 1 argument, got none}) } it { is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{'stdlib::extname' expects 1 argument, got 2}) } it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, %r{'stdlib::extname' parameter 'filename' expects a String value, got Array}) } diff --git a/spec/functions/fqdn_rand_string_spec.rb b/spec/functions/fqdn_rand_string_spec.rb index 2ba0779cd..46f5d7b45 100644 --- a/spec/functions/fqdn_rand_string_spec.rb +++ b/spec/functions/fqdn_rand_string_spec.rb @@ -5,7 +5,7 @@ describe 'fqdn_rand_string' do let(:default_charset) { %r{\A[a-zA-Z0-9]{100}\z} } - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects at least 1 argument, got none}i) } it { is_expected.to run.with_params(0).and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer\[1\] value, got Integer\[0, 0\]}) } it { is_expected.to run.with_params(1.5).and_raise_error(ArgumentError, %r{parameter 'length' expects an Integer\ value, got Float}) } diff --git a/spec/functions/fqdn_rotate_spec.rb b/spec/functions/fqdn_rotate_spec.rb index 1af0325be..ea0e2ddd5 100644 --- a/spec/functions/fqdn_rotate_spec.rb +++ b/spec/functions/fqdn_rotate_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'fqdn_rotate' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) } it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) } diff --git a/spec/functions/get_module_path_spec.rb b/spec/functions/get_module_path_spec.rb index 364e889d7..ee4060ba6 100644 --- a/spec/functions/get_module_path_spec.rb +++ b/spec/functions/get_module_path_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'get_module_path' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Wrong number of arguments, expects one}) } it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{Wrong number of arguments, expects one}) } it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{Wrong number of arguments, expects one}) } diff --git a/spec/functions/getparam_spec.rb b/spec/functions/getparam_spec.rb index d79493b1d..eee123d9a 100644 --- a/spec/functions/getparam_spec.rb +++ b/spec/functions/getparam_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'getparam' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Must specify a reference}) } it { is_expected.to run.with_params('User[one]').and_raise_error(ArgumentError, %r{Must specify name of a parameter}) } it { is_expected.to run.with_params('User[one]', 2).and_raise_error(ArgumentError, %r{Must specify name of a parameter}) } diff --git a/spec/functions/glob_spec.rb b/spec/functions/glob_spec.rb index 141ee7725..a057122ca 100644 --- a/spec/functions/glob_spec.rb +++ b/spec/functions/glob_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'glob' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) } it { is_expected.to run.with_params('').and_return([]) } diff --git a/spec/functions/grep_spec.rb b/spec/functions/grep_spec.rb index 2fd927fc8..b8e8e2efe 100644 --- a/spec/functions/grep_spec.rb +++ b/spec/functions/grep_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'grep' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/has_interface_with_spec.rb b/spec/functions/has_interface_with_spec.rb index a2f1a856a..16e90447f 100644 --- a/spec/functions/has_interface_with_spec.rb +++ b/spec/functions/has_interface_with_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'has_interface_with' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects between 1 and 2 arguments, got none}) } it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(ArgumentError, %r{expects between 1 and 2 arguments, got 3}) } diff --git a/spec/functions/has_ip_address_spec.rb b/spec/functions/has_ip_address_spec.rb index 0c08153da..5b787d693 100644 --- a/spec/functions/has_ip_address_spec.rb +++ b/spec/functions/has_ip_address_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'has_ip_address' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/has_ip_network_spec.rb b/spec/functions/has_ip_network_spec.rb index d6f01f457..22564bb71 100644 --- a/spec/functions/has_ip_network_spec.rb +++ b/spec/functions/has_ip_network_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'has_ip_network' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/intersection_spec.rb b/spec/functions/intersection_spec.rb index 2be738f77..6e1664609 100644 --- a/spec/functions/intersection_spec.rb +++ b/spec/functions/intersection_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'intersection' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) } it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) } diff --git a/spec/functions/ip_in_range_spec.rb b/spec/functions/ip_in_range_spec.rb index 78130de96..900d59023 100644 --- a/spec/functions/ip_in_range_spec.rb +++ b/spec/functions/ip_in_range_spec.rb @@ -4,7 +4,7 @@ describe 'stdlib::ip_in_range' do describe 'signature validation' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'stdlib::ip_in_range' expects 2 arguments, got none}) } it { is_expected.to run.with_params('one', 'two', '3').and_raise_error(ArgumentError, %r{'stdlib::ip_in_range' expects 2 arguments, got 3}) } it { is_expected.to run.with_params([], []).and_raise_error(ArgumentError, %r{'stdlib::ip_in_range' parameter 'ipaddress' expects a String value, got Array}) } diff --git a/spec/functions/is_a_spec.rb b/spec/functions/is_a_spec.rb index a5796695f..bd689c7ca 100644 --- a/spec/functions/is_a_spec.rb +++ b/spec/functions/is_a_spec.rb @@ -5,14 +5,14 @@ if ENV['FUTURE_PARSER'] == 'yes' describe 'type_of' do pending 'teach rspec-puppet to load future-only functions under 3.7.5' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } end end end if Puppet.version.to_f >= 4.0 describe 'is_a' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError) } it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) } diff --git a/spec/functions/join_keys_to_values_spec.rb b/spec/functions/join_keys_to_values_spec.rb index 591e51085..f23825391 100644 --- a/spec/functions/join_keys_to_values_spec.rb +++ b/spec/functions/join_keys_to_values_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'join_keys_to_values' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Takes exactly two arguments}) } it { is_expected.to run.with_params({}, '', '').and_raise_error(Puppet::ParseError, %r{Takes exactly two arguments}) } it { is_expected.to run.with_params('one', '').and_raise_error(TypeError, %r{The first argument must be a hash}) } diff --git a/spec/functions/load_module_metadata_spec.rb b/spec/functions/load_module_metadata_spec.rb index 608e19db7..f2d857f14 100644 --- a/spec/functions/load_module_metadata_spec.rb +++ b/spec/functions/load_module_metadata_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'load_module_metadata' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/loadjson_spec.rb b/spec/functions/loadjson_spec.rb index 41bf2580c..19e36273d 100644 --- a/spec/functions/loadjson_spec.rb +++ b/spec/functions/loadjson_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'loadjson' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } describe 'when calling with valid arguments' do diff --git a/spec/functions/loadyaml_spec.rb b/spec/functions/loadyaml_spec.rb index 68df64e37..cc08c477a 100644 --- a/spec/functions/loadyaml_spec.rb +++ b/spec/functions/loadyaml_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'loadyaml' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } context 'when a non-existing file is specified' do diff --git a/spec/functions/member_spec.rb b/spec/functions/member_spec.rb index 1a5c00ad0..244c2eea5 100644 --- a/spec/functions/member_spec.rb +++ b/spec/functions/member_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'member' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/merge_spec.rb b/spec/functions/merge_spec.rb index 04472e34d..d729aba45 100644 --- a/spec/functions/merge_spec.rb +++ b/spec/functions/merge_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'merge' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { expect(subject).to run \ diff --git a/spec/functions/num2bool_spec.rb b/spec/functions/num2bool_spec.rb index bedfb64a9..ad87b45f0 100644 --- a/spec/functions/num2bool_spec.rb +++ b/spec/functions/num2bool_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'num2bool' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('abc').and_raise_error(Puppet::ParseError, %r{does not look like a number}) } diff --git a/spec/functions/parsejson_spec.rb b/spec/functions/parsejson_spec.rb index 74c0c701c..6db0f9d95 100644 --- a/spec/functions/parsejson_spec.rb +++ b/spec/functions/parsejson_spec.rb @@ -4,7 +4,7 @@ describe 'parsejson' do it 'exists' do - expect(subject).not_to eq(nil) + expect(subject).not_to be_nil end it 'raises an error if called without any arguments' do diff --git a/spec/functions/parsepson_spec.rb b/spec/functions/parsepson_spec.rb index 26227fb22..9c78ecd2d 100644 --- a/spec/functions/parsepson_spec.rb +++ b/spec/functions/parsepson_spec.rb @@ -5,7 +5,7 @@ describe 'parsepson' do if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? it 'exists' do - expect(subject).not_to eq(nil) + expect(subject).not_to be_nil end it 'raises an error if called without any arguments' do diff --git a/spec/functions/parseyaml_spec.rb b/spec/functions/parseyaml_spec.rb index 5e114c4a9..2096f4758 100644 --- a/spec/functions/parseyaml_spec.rb +++ b/spec/functions/parseyaml_spec.rb @@ -4,7 +4,7 @@ describe 'parseyaml' do it 'exists' do - expect(subject).not_to eq(nil) + expect(subject).not_to be_nil end it 'raises an error if called without any arguments' do diff --git a/spec/functions/pick_default_spec.rb b/spec/functions/pick_default_spec.rb index 6e32c4010..5062596dd 100644 --- a/spec/functions/pick_default_spec.rb +++ b/spec/functions/pick_default_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'pick_default' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(RuntimeError, %r{Must receive at least one argument}) } it { is_expected.to run.with_params('one', 'two').and_return('one') } diff --git a/spec/functions/pick_spec.rb b/spec/functions/pick_spec.rb index 741e0b798..5e6ed1bde 100644 --- a/spec/functions/pick_spec.rb +++ b/spec/functions/pick_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'pick' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{must receive at least one non empty value}) } it { is_expected.to run.with_params('', nil, :undef, :undefined).and_raise_error(Puppet::ParseError, %r{must receive at least one non empty value}) } it { is_expected.to run.with_params('one', 'two').and_return('one') } diff --git a/spec/functions/powershell_escape_spec.rb b/spec/functions/powershell_escape_spec.rb index 804015761..f10940937 100644 --- a/spec/functions/powershell_escape_spec.rb +++ b/spec/functions/powershell_escape_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'powershell_escape' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } describe 'signature validation' do it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'powershell_escape' expects 1 argument, got none}) } diff --git a/spec/functions/prefix_spec.rb b/spec/functions/prefix_spec.rb index 9ede98655..0615a3f11 100644 --- a/spec/functions/prefix_spec.rb +++ b/spec/functions/prefix_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'prefix' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { diff --git a/spec/functions/pw_hash_spec.rb b/spec/functions/pw_hash_spec.rb index a1579870a..5e9af8921 100644 --- a/spec/functions/pw_hash_spec.rb +++ b/spec/functions/pw_hash_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'pw_hash' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } context 'when there are less than 3 arguments' do it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } diff --git a/spec/functions/range_spec.rb b/spec/functions/range_spec.rb index 384a43992..7d266f53a 100644 --- a/spec/functions/range_spec.rb +++ b/spec/functions/range_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'range' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } describe 'signature validation in puppet3', unless: RSpec.configuration.puppet_future do it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/regexpescape_spec.rb b/spec/functions/regexpescape_spec.rb index ecc9c06ca..fea5c8aa5 100644 --- a/spec/functions/regexpescape_spec.rb +++ b/spec/functions/regexpescape_spec.rb @@ -4,7 +4,7 @@ describe 'regexpescape' do describe 'signature validation' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { diff --git a/spec/functions/reject_spec.rb b/spec/functions/reject_spec.rb index 5c52e8f5e..90fc1d190 100644 --- a/spec/functions/reject_spec.rb +++ b/spec/functions/reject_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'reject' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([], 'pattern', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/reverse_spec.rb b/spec/functions/reverse_spec.rb index ac63a03d1..d4740945b 100644 --- a/spec/functions/reverse_spec.rb +++ b/spec/functions/reverse_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'reverse' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { diff --git a/spec/functions/seeded_rand_spec.rb b/spec/functions/seeded_rand_spec.rb index 206765b70..a3c83edbb 100644 --- a/spec/functions/seeded_rand_spec.rb +++ b/spec/functions/seeded_rand_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'seeded_rand' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'seeded_rand' expects 2 arguments, got none}i) } it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{'seeded_rand' expects 2 arguments, got 1}i) } it { is_expected.to run.with_params(0, '').and_raise_error(ArgumentError, %r{parameter 'max' expects an Integer\[1\] value, got Integer\[0, 0\]}) } diff --git a/spec/functions/sha256_spec.rb b/spec/functions/sha256_spec.rb index 423fb0095..5c9e2e3ce 100644 --- a/spec/functions/sha256_spec.rb +++ b/spec/functions/sha256_spec.rb @@ -4,7 +4,7 @@ describe 'stdlib::sha256' do context 'when default' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{stdlib::sha256}) } end diff --git a/spec/functions/shell_escape_spec.rb b/spec/functions/shell_escape_spec.rb index 941a2472b..2fc504056 100644 --- a/spec/functions/shell_escape_spec.rb +++ b/spec/functions/shell_escape_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'shell_escape' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } describe 'signature validation' do it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'shell_escape' expects 1 argument, got none}) } diff --git a/spec/functions/shell_join_spec.rb b/spec/functions/shell_join_spec.rb index 9563efcbe..03144c3ee 100644 --- a/spec/functions/shell_join_spec.rb +++ b/spec/functions/shell_join_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'shell_join' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } describe 'signature validation' do it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/shell_split_spec.rb b/spec/functions/shell_split_spec.rb index 68faece7a..0ad9b0bcd 100644 --- a/spec/functions/shell_split_spec.rb +++ b/spec/functions/shell_split_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'shell_split' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } describe 'signature validation' do it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/shuffle_spec.rb b/spec/functions/shuffle_spec.rb index 938f279f7..97459ab95 100644 --- a/spec/functions/shuffle_spec.rb +++ b/spec/functions/shuffle_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'shuffle' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { diff --git a/spec/functions/squeeze_spec.rb b/spec/functions/squeeze_spec.rb index 337ac85c2..04f45bcfb 100644 --- a/spec/functions/squeeze_spec.rb +++ b/spec/functions/squeeze_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'squeeze' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('', '', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(1).and_raise_error(NoMethodError) } diff --git a/spec/functions/startswith_spec.rb b/spec/functions/startswith_spec.rb index a3478bb39..ebd00553e 100644 --- a/spec/functions/startswith_spec.rb +++ b/spec/functions/startswith_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'stdlib::start_with' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects 2 arguments, got none}i) } it { is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{expects 2 arguments, got 1}) } diff --git a/spec/functions/str2bool_spec.rb b/spec/functions/str2bool_spec.rb index be126bcdd..9259910b1 100644 --- a/spec/functions/str2bool_spec.rb +++ b/spec/functions/str2bool_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'str2bool' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { diff --git a/spec/functions/str2resource_spec.rb b/spec/functions/str2resource_spec.rb index afac627fd..1c8582f0b 100644 --- a/spec/functions/str2resource_spec.rb +++ b/spec/functions/str2resource_spec.rb @@ -4,7 +4,7 @@ describe 'stdlib::str2resource' do context 'when default' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{stdlib::str2resource}) } end diff --git a/spec/functions/str2saltedpbkdf2_spec.rb b/spec/functions/str2saltedpbkdf2_spec.rb index 6da1bad0f..907b18509 100644 --- a/spec/functions/str2saltedpbkdf2_spec.rb +++ b/spec/functions/str2saltedpbkdf2_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'str2saltedpbkdf2' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('Pa55w0rd', 2).and_raise_error(ArgumentError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(1, 'Using s0m3 s@lt', 50_000).and_raise_error(ArgumentError, %r{first argument must be a string}) } diff --git a/spec/functions/str2saltedsha512_spec.rb b/spec/functions/str2saltedsha512_spec.rb index 0ee16fa05..3b468fdb6 100644 --- a/spec/functions/str2saltedsha512_spec.rb +++ b/spec/functions/str2saltedsha512_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'str2saltedsha512' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('password', 2).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires a String argument}) } diff --git a/spec/functions/suffix_spec.rb b/spec/functions/suffix_spec.rb index 682f8b159..04d42739a 100644 --- a/spec/functions/suffix_spec.rb +++ b/spec/functions/suffix_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'suffix' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { diff --git a/spec/functions/swapcase_spec.rb b/spec/functions/swapcase_spec.rb index 1221f6afa..6f3141b41 100644 --- a/spec/functions/swapcase_spec.rb +++ b/spec/functions/swapcase_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'swapcase' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { diff --git a/spec/functions/time_spec.rb b/spec/functions/time_spec.rb index 42024f679..6322d5c55 100644 --- a/spec/functions/time_spec.rb +++ b/spec/functions/time_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'time' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params('a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } context 'when running at a specific time' do diff --git a/spec/functions/to_bytes_spec.rb b/spec/functions/to_bytes_spec.rb index 71ee00fe7..b86be5361 100644 --- a/spec/functions/to_bytes_spec.rb +++ b/spec/functions/to_bytes_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'to_bytes' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('1', 'extras').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([]).and_raise_error(TypeError, %r{(can't convert|no implicit conversion of) Array (in)?to String}) } diff --git a/spec/functions/to_json_pretty_spec.rb b/spec/functions/to_json_pretty_spec.rb index 7158290ed..ed9b14bd9 100644 --- a/spec/functions/to_json_pretty_spec.rb +++ b/spec/functions/to_json_pretty_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'to_json_pretty' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params([]).and_return("[\n\n]\n") } it { is_expected.to run.with_params(['one']).and_return("[\n \"one\"\n]\n") } it { is_expected.to run.with_params(['one', 'two']).and_return("[\n \"one\",\n \"two\"\n]\n") } diff --git a/spec/functions/to_json_spec.rb b/spec/functions/to_json_spec.rb index c27fa709a..4b9b832e3 100644 --- a/spec/functions/to_json_spec.rb +++ b/spec/functions/to_json_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'to_json' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params('').and_return('""') } it { is_expected.to run.with_params(true).and_return('true') } it { is_expected.to run.with_params('one').and_return('"one"') } diff --git a/spec/functions/to_python_spec.rb b/spec/functions/to_python_spec.rb index 8a3899692..f4072ddc7 100644 --- a/spec/functions/to_python_spec.rb +++ b/spec/functions/to_python_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'to_python' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params('').and_return('""') } it { is_expected.to run.with_params(nil).and_return('None') } it { is_expected.to run.with_params(true).and_return('True') } diff --git a/spec/functions/to_ruby_spec.rb b/spec/functions/to_ruby_spec.rb index 94d5de0f0..50cf94af0 100644 --- a/spec/functions/to_ruby_spec.rb +++ b/spec/functions/to_ruby_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'to_ruby' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params('').and_return('""') } it { is_expected.to run.with_params(nil).and_return('nil') } it { is_expected.to run.with_params(true).and_return('true') } diff --git a/spec/functions/to_toml_spec.rb b/spec/functions/to_toml_spec.rb index 17559c0f5..dce0bfb01 100644 --- a/spec/functions/to_toml_spec.rb +++ b/spec/functions/to_toml_spec.rb @@ -4,7 +4,7 @@ describe 'to_toml' do context 'fails on invalid params' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } [ nil, diff --git a/spec/functions/to_yaml_spec.rb b/spec/functions/to_yaml_spec.rb index a014cabca..ae6116afc 100644 --- a/spec/functions/to_yaml_spec.rb +++ b/spec/functions/to_yaml_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'to_yaml' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params('').and_return("--- ''\n") } it { is_expected.to run.with_params(true).and_return(%r{--- true\n}) } it { is_expected.to run.with_params('one').and_return(%r{--- one\n}) } diff --git a/spec/functions/type_of_spec.rb b/spec/functions/type_of_spec.rb index f1131c9fb..e43a0c7bb 100644 --- a/spec/functions/type_of_spec.rb +++ b/spec/functions/type_of_spec.rb @@ -5,14 +5,14 @@ if ENV['FUTURE_PARSER'] == 'yes' describe 'type_of' do pending 'teach rspec-puppet to load future-only functions under 3.7.5' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } end end end if Puppet.version.to_f >= 4.0 describe 'type_of' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError) } it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) } diff --git a/spec/functions/union_spec.rb b/spec/functions/union_spec.rb index d5ab61730..da552b8f9 100644 --- a/spec/functions/union_spec.rb +++ b/spec/functions/union_spec.rb @@ -4,7 +4,7 @@ describe 'union' do describe 'argument checking' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError, %r{Every parameter must be an array}) } diff --git a/spec/functions/unix2dos_spec.rb b/spec/functions/unix2dos_spec.rb index 33c3e4799..9224615ac 100644 --- a/spec/functions/unix2dos_spec.rb +++ b/spec/functions/unix2dos_spec.rb @@ -4,7 +4,7 @@ describe 'unix2dos' do context 'when checking parameter validity' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it do expect(subject).to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) diff --git a/spec/functions/uriescape_spec.rb b/spec/functions/uriescape_spec.rb index 3304a3ccb..d402697ff 100644 --- a/spec/functions/uriescape_spec.rb +++ b/spec/functions/uriescape_spec.rb @@ -6,7 +6,7 @@ # URI.escape has been fully removed as of Ruby 3. Therefore, it will not work as it stand on Puppet 8. if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? describe 'signature validation' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { diff --git a/spec/functions/validate_augeas_spec.rb b/spec/functions/validate_augeas_spec.rb index 04ee0eb3e..d238859bd 100644 --- a/spec/functions/validate_augeas_spec.rb +++ b/spec/functions/validate_augeas_spec.rb @@ -5,7 +5,7 @@ describe 'validate_augeas' do if Puppet.features.augeas? describe 'signature validation' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('', '', [], '', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/validate_cmd_spec.rb b/spec/functions/validate_cmd_spec.rb index 4c0f17e0c..e3e6155cc 100644 --- a/spec/functions/validate_cmd_spec.rb +++ b/spec/functions/validate_cmd_spec.rb @@ -6,7 +6,7 @@ let(:touch) { File.exist?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch' } describe 'signature validation' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params('', '', '', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/validate_domain_name_spec.rb b/spec/functions/validate_domain_name_spec.rb index 3132c64d7..ad4af8b34 100644 --- a/spec/functions/validate_domain_name_spec.rb +++ b/spec/functions/validate_domain_name_spec.rb @@ -4,7 +4,7 @@ describe 'validate_domain_name' do describe 'signature validation' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } end diff --git a/spec/functions/validate_email_address_spec.rb b/spec/functions/validate_email_address_spec.rb index 28d1e9100..c775ba9cd 100644 --- a/spec/functions/validate_email_address_spec.rb +++ b/spec/functions/validate_email_address_spec.rb @@ -4,7 +4,7 @@ describe 'validate_email_address' do describe 'signature validation' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } end diff --git a/spec/functions/validate_legacy_spec.rb b/spec/functions/validate_legacy_spec.rb index b4d66b171..e607b32f5 100644 --- a/spec/functions/validate_legacy_spec.rb +++ b/spec/functions/validate_legacy_spec.rb @@ -4,7 +4,7 @@ if Puppet::Util::Package.versioncmp(Puppet.version, '4.4.0') >= 0 describe 'validate_legacy' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(ArgumentError) } describe 'when passing the type assertion' do diff --git a/spec/functions/validate_x509_rsa_key_pair_spec.rb b/spec/functions/validate_x509_rsa_key_pair_spec.rb index ff7be1f2b..cb23bcded 100644 --- a/spec/functions/validate_x509_rsa_key_pair_spec.rb +++ b/spec/functions/validate_x509_rsa_key_pair_spec.rb @@ -114,7 +114,7 @@ end context 'with function signature validation' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(0, 1, 2, 3).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } end diff --git a/spec/functions/values_at_spec.rb b/spec/functions/values_at_spec.rb index 2d21d5406..14e15e1c5 100644 --- a/spec/functions/values_at_spec.rb +++ b/spec/functions/values_at_spec.rb @@ -4,7 +4,7 @@ describe 'values_at' do describe 'signature validation' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/functions/zip_spec.rb b/spec/functions/zip_spec.rb index 28049a05e..f3e576435 100644 --- a/spec/functions/zip_spec.rb +++ b/spec/functions/zip_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'zip' do - it { is_expected.not_to eq(nil) } + it { is_expected.not_to be_nil } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } diff --git a/spec/unit/facter/pe_version_spec.rb b/spec/unit/facter/pe_version_spec.rb index e152548f7..56b3dafe1 100644 --- a/spec/unit/facter/pe_version_spec.rb +++ b/spec/unit/facter/pe_version_spec.rb @@ -31,7 +31,7 @@ (major, minor, patch) = version.split('.') it 'returns true' do - expect(Facter.fact(:is_pe).value).to eq(true) + expect(Facter.fact(:is_pe).value).to be(true) end it "has a version of #{version}" do @@ -59,7 +59,7 @@ end it 'is_pe is false' do - expect(Facter.fact(:is_pe).value).to eq(false) + expect(Facter.fact(:is_pe).value).to be(false) end it 'pe_version is nil' do From 0e3e5b13cc35fe0de228a047db92f5b91b2e1f31 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Thu, 18 May 2023 15:08:19 +0100 Subject: [PATCH 29/33] (CONT-801) Autocorrect unsafe group 2 --- .rubocop_todo.yml | 54 ++++--------------- lib/puppet/functions/to_json_pretty.rb | 10 +--- lib/puppet/parser/functions/reject.rb | 2 +- lib/puppet/provider/file_line/ruby.rb | 4 +- lib/puppet_x/stdlib.rb | 2 + spec/functions/stdlib_deferrable_epp_spec.rb | 2 + .../validate_x509_rsa_key_pair_spec.rb | 2 +- spec/type_aliases/http__method_spec.rb | 2 + spec/type_aliases/http__status_spec.rb | 2 + 9 files changed, 23 insertions(+), 57 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 24585ea6f..b4273878b 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-05-17 10:40:49 UTC using RuboCop version 1.48.1. +# on 2023-05-18 13:42:01 UTC using RuboCop version 1.48.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -41,28 +41,28 @@ Lint/SuppressedException: - 'lib/puppet/functions/merge.rb' - 'lib/puppet/parser/functions/has_interface_with.rb' -# Offense count: 6 +# Offense count: 5 # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: Max: 44 -# Offense count: 19 +# Offense count: 18 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode. # AllowedMethods: refine Metrics/BlockLength: - Max: 152 + Max: 150 -# Offense count: 5 +# Offense count: 4 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/CyclomaticComplexity: Max: 15 -# Offense count: 9 +# Offense count: 8 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: Max: 35 -# Offense count: 4 +# Offense count: 3 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/PerceivedComplexity: Max: 19 @@ -175,29 +175,13 @@ RSpec/StubbedMock: - 'spec/functions/reverse_spec.rb' - 'spec/functions/squeeze_spec.rb' -# Offense count: 2 -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/CollectionCompact: - Exclude: - - 'lib/puppet/functions/to_json_pretty.rb' - -# Offense count: 5 +# Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. # SupportedStyles: always, always_true, never Style/FrozenStringLiteralComment: Exclude: - - 'lib/puppet_x/stdlib.rb' - 'lib/puppet_x/stdlib/toml_dumper.rb' - - 'spec/functions/stdlib_deferrable_epp_spec.rb' - - 'spec/type_aliases/http__method_spec.rb' - - 'spec/type_aliases/http__status_spec.rb' - -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/HashTransformKeys: - Exclude: - - 'lib/puppet/functions/to_json_pretty.rb' # Offense count: 4 # This cop supports unsafe autocorrection (--autocorrect-all). @@ -210,11 +194,10 @@ Style/IfWithBooleanLiteralBranches: - 'lib/puppet/parser/functions/shuffle.rb' - 'lib/puppet/provider/file_line/ruby.rb' -# Offense count: 3 +# Offense count: 2 # This cop supports unsafe autocorrection (--autocorrect-all). Style/MapToHash: Exclude: - - 'lib/puppet/functions/to_json_pretty.rb' - 'lib/puppet/parser/functions/prefix.rb' - 'lib/puppet/parser/functions/suffix.rb' @@ -241,25 +224,6 @@ Style/OptionalBooleanParameter: - 'lib/puppet/functions/to_json_pretty.rb' - 'lib/puppet_x/stdlib/toml_dumper.rb' -# Offense count: 2 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: Methods. -Style/RedundantArgument: - Exclude: - - 'lib/puppet/provider/file_line/ruby.rb' - -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/SelectByRegexp: - Exclude: - - 'lib/puppet/parser/functions/reject.rb' - -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/SlicingWithRange: - Exclude: - - 'spec/functions/validate_x509_rsa_key_pair_spec.rb' - # Offense count: 2 # This cop supports unsafe autocorrection (--autocorrect-all). Style/StringChars: diff --git a/lib/puppet/functions/to_json_pretty.rb b/lib/puppet/functions/to_json_pretty.rb index 237d0c089..f3fd5fcf2 100644 --- a/lib/puppet/functions/to_json_pretty.rb +++ b/lib/puppet/functions/to_json_pretty.rb @@ -61,18 +61,12 @@ def to_json_pretty(data, skip_undef = false, opts = nil) # It's not possible to make an abstract type that can be either a boolean # false or an integer, so we use -1 as the falsey value if opts - opts = opts.map { |k, v| [k.to_sym, v] }.to_h + opts = opts.transform_keys { |k| k.to_sym } opts[:max_nesting] = false if opts[:max_nesting] == -1 end - if skip_undef - if data.is_a? Array - data = data.reject { |value| value.nil? } - elsif data.is_a? Hash - data = data.reject { |_, value| value.nil? } - end - end + data = data.compact if skip_undef && (data.is_a?(Array) || Hash) # Call ::JSON to ensure it references the JSON library from Ruby's standard library # instead of a random JSON namespace that might be in scope due to user code. JSON.pretty_generate(data, opts) << "\n" diff --git a/lib/puppet/parser/functions/reject.rb b/lib/puppet/parser/functions/reject.rb index ffa1fa8fa..ecda3efed 100644 --- a/lib/puppet/parser/functions/reject.rb +++ b/lib/puppet/parser/functions/reject.rb @@ -31,7 +31,7 @@ module Puppet::Parser::Functions ary = args[0] pattern = Regexp.new(args[1]) - ary.reject { |e| e =~ pattern } + ary.grep_v(pattern) end end diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 3b773f0b3..d1d9a3620 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -152,12 +152,12 @@ def handle_destroy_with_match raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" if match_count > 1 && resource[:multiple].to_s != 'true' local_lines = lines - File.write(resource[:path], local_lines.reject { |line| match_regex.match(line) }.join('')) + File.write(resource[:path], local_lines.reject { |line| match_regex.match(line) }.join) end def handle_destroy_line local_lines = lines - File.write(resource[:path], local_lines.reject { |line| line.chomp == resource[:line] }.join('')) + File.write(resource[:path], local_lines.reject { |line| line.chomp == resource[:line] }.join) end def handle_append_line diff --git a/lib/puppet_x/stdlib.rb b/lib/puppet_x/stdlib.rb index 351dfe2f2..dbbe7cbc1 100644 --- a/lib/puppet_x/stdlib.rb +++ b/lib/puppet_x/stdlib.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puppet_x' # common PuppetX::Stdlib module definition diff --git a/spec/functions/stdlib_deferrable_epp_spec.rb b/spec/functions/stdlib_deferrable_epp_spec.rb index e50301575..022ac031a 100644 --- a/spec/functions/stdlib_deferrable_epp_spec.rb +++ b/spec/functions/stdlib_deferrable_epp_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'stdlib::deferrable_epp' do diff --git a/spec/functions/validate_x509_rsa_key_pair_spec.rb b/spec/functions/validate_x509_rsa_key_pair_spec.rb index cb23bcded..a9e102b7f 100644 --- a/spec/functions/validate_x509_rsa_key_pair_spec.rb +++ b/spec/functions/validate_x509_rsa_key_pair_spec.rb @@ -174,6 +174,6 @@ def truncate_middle(string) start_pos = middle - (chars_to_truncate / 2) end_pos = middle + (chars_to_truncate / 2) - string[0..start_pos] + string[end_pos..-1] + string[0..start_pos] + string[end_pos..] end end diff --git a/spec/type_aliases/http__method_spec.rb b/spec/type_aliases/http__method_spec.rb index e80d02027..838660f01 100644 --- a/spec/type_aliases/http__method_spec.rb +++ b/spec/type_aliases/http__method_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'Stdlib::Http::Method' do diff --git a/spec/type_aliases/http__status_spec.rb b/spec/type_aliases/http__status_spec.rb index 123612fc3..384b284c4 100644 --- a/spec/type_aliases/http__status_spec.rb +++ b/spec/type_aliases/http__status_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'Stdlib::Http::Status' do From 55c4aeaddfbbab21bf08c40ba8f45a3b12e9716a Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Thu, 18 May 2023 15:28:38 +0100 Subject: [PATCH 30/33] (CONT-801) Autocorrect unsafe group 3 --- .rubocop_todo.yml | 66 ++-------------------- lib/facter/pe_version.rb | 6 +- lib/puppet/functions/to_json_pretty.rb | 2 +- lib/puppet/parser/functions/clamp.rb | 2 +- lib/puppet/parser/functions/fqdn_rotate.rb | 4 +- lib/puppet/parser/functions/fqdn_uuid.rb | 2 +- lib/puppet/parser/functions/num2bool.rb | 2 +- lib/puppet/parser/functions/prefix.rb | 4 +- lib/puppet/parser/functions/shell_join.rb | 2 +- lib/puppet/parser/functions/shuffle.rb | 4 +- lib/puppet/parser/functions/squeeze.rb | 2 +- lib/puppet/parser/functions/suffix.rb | 4 +- lib/puppet/provider/file_line/ruby.rb | 8 +-- lib/puppet_x/stdlib/toml_dumper.rb | 6 +- spec/functions/get_module_path_spec.rb | 4 +- 15 files changed, 28 insertions(+), 90 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b4273878b..94cec1faa 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-05-18 13:42:01 UTC using RuboCop version 1.48.1. +# on 2023-05-18 14:27:28 UTC using RuboCop version 1.48.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -20,12 +20,11 @@ Lint/ConstantDefinitionInBlock: - 'spec/functions/get_module_path_spec.rb' - 'spec/unit/facter/util/puppet_settings_spec.rb' -# Offense count: 3 +# Offense count: 2 # Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches. Lint/DuplicateBranch: Exclude: - 'lib/puppet/parser/functions/str2bool.rb' - - 'lib/puppet/provider/file_line/ruby.rb' # Offense count: 2 # Configuration parameters: MaximumRangeSize. @@ -55,17 +54,17 @@ Metrics/BlockLength: # Offense count: 4 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/CyclomaticComplexity: - Max: 15 + Max: 14 # Offense count: 8 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: - Max: 35 + Max: 33 # Offense count: 3 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/PerceivedComplexity: - Max: 19 + Max: 18 # Offense count: 2 # Configuration parameters: ForbiddenDelimiters. @@ -183,39 +182,11 @@ Style/FrozenStringLiteralComment: Exclude: - 'lib/puppet_x/stdlib/toml_dumper.rb' -# Offense count: 4 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: AllowedMethods. -# AllowedMethods: nonzero? -Style/IfWithBooleanLiteralBranches: - Exclude: - - 'lib/facter/pe_version.rb' - - 'lib/puppet/parser/functions/fqdn_rotate.rb' - - 'lib/puppet/parser/functions/shuffle.rb' - - 'lib/puppet/provider/file_line/ruby.rb' - -# Offense count: 2 -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/MapToHash: - Exclude: - - 'lib/puppet/parser/functions/prefix.rb' - - 'lib/puppet/parser/functions/suffix.rb' - # Offense count: 1 Style/MixinUsage: Exclude: - 'spec/spec_helper.rb' -# Offense count: 3 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: EnforcedStyle, AllowedMethods, AllowedPatterns. -# SupportedStyles: predicate, comparison -Style/NumericPredicate: - Exclude: - - 'spec/**/*' - - 'lib/puppet/parser/functions/num2bool.rb' - - 'lib/puppet/provider/file_line/ruby.rb' - # Offense count: 3 # Configuration parameters: AllowedMethods. # AllowedMethods: respond_to_missing? @@ -223,30 +194,3 @@ Style/OptionalBooleanParameter: Exclude: - 'lib/puppet/functions/to_json_pretty.rb' - 'lib/puppet_x/stdlib/toml_dumper.rb' - -# Offense count: 2 -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/StringChars: - Exclude: - - 'lib/puppet/parser/functions/fqdn_rotate.rb' - - 'lib/puppet/parser/functions/shuffle.rb' - -# Offense count: 5 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: Mode. -Style/StringConcatenation: - Exclude: - - 'lib/puppet_x/stdlib/toml_dumper.rb' - - 'spec/functions/get_module_path_spec.rb' - -# Offense count: 5 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: AllowMethodsWithArguments, AllowedMethods, AllowedPatterns, AllowComments. -# AllowedMethods: define_method -Style/SymbolProc: - Exclude: - - 'lib/puppet/functions/to_json_pretty.rb' - - 'lib/puppet/parser/functions/clamp.rb' - - 'lib/puppet/parser/functions/fqdn_uuid.rb' - - 'lib/puppet/parser/functions/shell_join.rb' - - 'lib/puppet/parser/functions/squeeze.rb' diff --git a/lib/facter/pe_version.rb b/lib/facter/pe_version.rb index 2e1cb13f5..716ff7198 100644 --- a/lib/facter/pe_version.rb +++ b/lib/facter/pe_version.rb @@ -30,11 +30,7 @@ # Fact: is_pe Facter.add('is_pe') do setcode do - if Facter.value(:pe_version).to_s.empty? - false - else - true - end + !Facter.value(:pe_version).to_s.empty? end end diff --git a/lib/puppet/functions/to_json_pretty.rb b/lib/puppet/functions/to_json_pretty.rb index f3fd5fcf2..bff40d14e 100644 --- a/lib/puppet/functions/to_json_pretty.rb +++ b/lib/puppet/functions/to_json_pretty.rb @@ -61,7 +61,7 @@ def to_json_pretty(data, skip_undef = false, opts = nil) # It's not possible to make an abstract type that can be either a boolean # false or an integer, so we use -1 as the falsey value if opts - opts = opts.transform_keys { |k| k.to_sym } + opts = opts.transform_keys(&:to_sym) opts[:max_nesting] = false if opts[:max_nesting] == -1 end diff --git a/lib/puppet/parser/functions/clamp.rb b/lib/puppet/parser/functions/clamp.rb index 543a152ad..d95107da4 100644 --- a/lib/puppet/parser/functions/clamp.rb +++ b/lib/puppet/parser/functions/clamp.rb @@ -41,6 +41,6 @@ module Puppet::Parser::Functions # convert to numeric each element # then sort them and get a middle value - args.map { |n| n.to_i }.sort[1] + args.map(&:to_i).sort[1] end end diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb index dcece1b77..1437caa38 100644 --- a/lib/puppet/parser/functions/fqdn_rotate.rb +++ b/lib/puppet/parser/functions/fqdn_rotate.rb @@ -26,13 +26,13 @@ result = value.clone - string = value.is_a?(String) ? true : false + string = value.is_a?(String) # Check whether it makes sense to rotate ... return result if result.size <= 1 # We turn any string value into an array to be able to rotate ... - result = string ? result.split('') : result + result = string ? result.chars : result elements = result.size diff --git a/lib/puppet/parser/functions/fqdn_uuid.rb b/lib/puppet/parser/functions/fqdn_uuid.rb index 83e94a559..cbe8a5652 100644 --- a/lib/puppet/parser/functions/fqdn_uuid.rb +++ b/lib/puppet/parser/functions/fqdn_uuid.rb @@ -45,7 +45,7 @@ module Puppet::Parser::Functions 0x4f, 0xd4, 0x30, - 0xc8].map { |b| b.chr }.join + 0xc8].map(&:chr).join sha1 = Digest::SHA1.new sha1.update(uuid_name_space_dns) diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index f856c1294..50891df3b 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -42,7 +42,7 @@ module Puppet::Parser::Functions number = number.to_i # Return true for any positive number and false otherwise - return number > 0 + return number.positive? end end diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 900067965..4f8f0767a 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -38,10 +38,10 @@ module Puppet::Parser::Functions prefix ? prefix + i : i end else - enumerable.map { |k, v| + enumerable.to_h do |k, v| k = k.to_s [prefix ? prefix + k : k, v] - }.to_h + end end return result diff --git a/lib/puppet/parser/functions/shell_join.rb b/lib/puppet/parser/functions/shell_join.rb index 12d1652af..93f026e1f 100644 --- a/lib/puppet/parser/functions/shell_join.rb +++ b/lib/puppet/parser/functions/shell_join.rb @@ -22,7 +22,7 @@ module Puppet::Parser::Functions raise Puppet::ParseError, "First argument is not an Array: #{array.inspect}" unless array.is_a?(Array) # explicit conversion to string is required for ruby 1.9 - array = array.map { |item| item.to_s } + array = array.map(&:to_s) result = Shellwords.shelljoin(array) return result diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb index fa7fc8ffa..c2c420ebf 100644 --- a/lib/puppet/parser/functions/shuffle.rb +++ b/lib/puppet/parser/functions/shuffle.rb @@ -20,13 +20,13 @@ module Puppet::Parser::Functions result = value.clone - string = value.is_a?(String) ? true : false + string = value.is_a?(String) # Check whether it makes sense to shuffle ... return result if result.size <= 1 # We turn any string value into an array to be able to shuffle ... - result = string ? result.split('') : result + result = string ? result.chars : result elements = result.size diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index 97ceaf360..ee527c4d2 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -21,7 +21,7 @@ module Puppet::Parser::Functions if squeezeval item.map { |i| i.squeeze(squeezeval) } else - item.map { |i| i.squeeze } + item.map(&:squeeze) end elsif squeezeval item.squeeze(squeezeval) diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb index 037ab5b34..34db43833 100644 --- a/lib/puppet/parser/functions/suffix.rb +++ b/lib/puppet/parser/functions/suffix.rb @@ -42,10 +42,10 @@ module Puppet::Parser::Functions suffix ? i + suffix : i end else - enumerable.map { |k, v| + enumerable.to_h do |k, v| k = k.to_s [suffix ? k + suffix : k, v] - }.to_h + end end return result diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index d1d9a3620..4269f16ec 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -17,17 +17,15 @@ def exists? found = line.chomp == resource[:line] lines_count += 1 if found end - return found = lines_count > 0 if resource[:match].nil? + return found = lines_count.positive? if resource[:match].nil? match_count = count_matches(new_match_regex) found = if resource[:ensure] == :present if match_count.zero? if lines_count.zero? && resource[:append_on_no_match].to_s == 'false' true # lies, but gets the job done - elsif lines_count.zero? && resource[:append_on_no_match].to_s != 'false' - false else - true + !(lines_count.zero? && resource[:append_on_no_match].to_s != 'false') end elsif resource[:replace_all_matches_not_matching_line].to_s == 'true' false # maybe lies, but knows there's still work to do @@ -50,7 +48,7 @@ def exists? end def create - return if resource[:replace].to_s != 'true' && count_matches(new_match_regex) > 0 + return if resource[:replace].to_s != 'true' && count_matches(new_match_regex).positive? if resource[:match] handle_create_with_match diff --git a/lib/puppet_x/stdlib/toml_dumper.rb b/lib/puppet_x/stdlib/toml_dumper.rb index 84c3bdf7e..e287bc1cc 100644 --- a/lib/puppet_x/stdlib/toml_dumper.rb +++ b/lib/puppet_x/stdlib/toml_dumper.rb @@ -103,9 +103,9 @@ def dump_table_array_pairs(table_array_pairs, prefix) def print_prefix(prefix, extra_brackets = false) new_prefix = prefix.join('.') - new_prefix = '[' + new_prefix + ']' if extra_brackets + new_prefix = "[#{new_prefix}]" if extra_brackets - @toml_str += "[" + new_prefix + "]\n" # rubocop:disable Style/StringLiterals + @toml_str += "[#{new_prefix}]\n" end def to_toml(obj) @@ -130,6 +130,6 @@ def bare_key?(key) end def quote_key(key) - '"' + key.gsub('"', '\\"') + '"' + "\"#{key.gsub('"', '\\"')}\"" end end diff --git a/spec/functions/get_module_path_spec.rb b/spec/functions/get_module_path_spec.rb index ee4060ba6..25740380a 100644 --- a/spec/functions/get_module_path_spec.rb +++ b/spec/functions/get_module_path_spec.rb @@ -36,7 +36,7 @@ def initialize(path) end it 'when the modulepath is a list' do - Puppet[:modulepath] = modulepath + 'tmp/something_else' + Puppet[:modulepath] = "#{modulepath}tmp/something_else" expect(subject).to run.with_params('foo').and_return(path_of_module_foo.path) end @@ -54,7 +54,7 @@ def initialize(path) end it 'when the modulepath is a list' do - Puppet[:modulepath] = modulepath + 'tmp/something_else' + Puppet[:modulepath] = "#{modulepath}tmp/something_else" expect(subject).to run.with_params('foo').and_return(path_of_module_foo.path) end end From c0c7ccb862ab30a87a7b4e0dbf7aec7e8289a7a4 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Mon, 22 May 2023 09:31:28 +0100 Subject: [PATCH 31/33] (CONT-801) Deprecate parsepson.rb --- lib/puppet/functions/parsepson.rb | 6 +++++- spec/functions/parsepson_spec.rb | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/puppet/functions/parsepson.rb b/lib/puppet/functions/parsepson.rb index 41f56c563..2eedbeee9 100644 --- a/lib/puppet/functions/parsepson.rb +++ b/lib/puppet/functions/parsepson.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true # @summary +# **Deprecated:** Starting Puppet 8, we no longer natively support PSON usage. This function should be removed once we stop supporting Puppet 7. +# # This function accepts PSON, a Puppet variant of JSON, as a string and converts # it into the correct Puppet structure # @@ -20,7 +22,9 @@ end def parsepson(pson_string, default = :no_default_provided) - PSON.load(pson_string) + call_function('deprecation', 'parsepson', 'This method is deprecated. From Puppet 8, PSON is no longer natively supported. Please use JSON.parse().') + + PSON.load(pson_string) if Puppet::Util::Package.versioncmp(Puppet.version, '8').negative? rescue StandardError => e Puppet.debug("Parsing PSON failed with error: #{e.message}") raise e if default == :no_default_provided diff --git a/spec/functions/parsepson_spec.rb b/spec/functions/parsepson_spec.rb index 9c78ecd2d..cff940c84 100644 --- a/spec/functions/parsepson_spec.rb +++ b/spec/functions/parsepson_spec.rb @@ -65,4 +65,9 @@ end end end + if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').positive? + it 'doesnt work on Puppet 8' do + expect(subject).to run.with_params('{"a":"1","b":"2"}').and_return(nil) + end + end end From 799d608939b90effb7cfa7d0054a3985c78db982 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Mon, 22 May 2023 10:07:01 +0100 Subject: [PATCH 32/33] (CONT-801) Deprecate uriescape.rb --- lib/puppet/parser/functions/uriescape.rb | 5 +++++ spec/functions/uriescape_spec.rb | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb index 01b615788..128558163 100644 --- a/lib/puppet/parser/functions/uriescape.rb +++ b/lib/puppet/parser/functions/uriescape.rb @@ -15,8 +15,13 @@ module Puppet::Parser::Functions @return [String] a string that contains the converted value + > **Note:** **Deprecated:** Starting Puppet 8, our Ruby version has upgraded to 3.2. + Therefore, its no longer possible to call URI.escape as it was deprecated by 2.7 and removed completely by 3+. + This function should be removed once Puppet 7 is no longer supported. DOC ) do |arguments| + raise(Puppet::ParseError, 'Puppet: This function is not available in Puppet 8. URI.escape no longer exists as of Ruby 3+.') if Puppet::Util::Package.versioncmp(Puppet.version, '8').positive? + raise(Puppet::ParseError, "uriescape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? value = arguments[0] diff --git a/spec/functions/uriescape_spec.rb b/spec/functions/uriescape_spec.rb index d402697ff..5d7606121 100644 --- a/spec/functions/uriescape_spec.rb +++ b/spec/functions/uriescape_spec.rb @@ -39,5 +39,9 @@ it { is_expected.to run.with_params(['one}', 'two']).and_return(['one%7D', 'two']) } it { is_expected.to run.with_params(['one}', 1, true, {}, 'two']).and_return(['one%7D', 1, true, {}, 'two']) } end + else + describe 'raising errors in Puppet 8' do + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{This function is not available in Puppet 8. URI.escape no longer exists as of Ruby 3+.}) } + end end end From cab7d7e3991e191892b123fe07364df347219f88 Mon Sep 17 00:00:00 2001 From: Lukas Audzevicius Date: Mon, 22 May 2023 11:28:39 +0100 Subject: [PATCH 33/33] Reverting openURI changes --- lib/puppet/parser/functions/loadjson.rb | 21 +++++++-------------- spec/functions/loadjson_spec.rb | 18 +++++------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/lib/puppet/parser/functions/loadjson.rb b/lib/puppet/parser/functions/loadjson.rb index 5cf2d5ab1..6ec9fae1d 100644 --- a/lib/puppet/parser/functions/loadjson.rb +++ b/lib/puppet/parser/functions/loadjson.rb @@ -43,23 +43,16 @@ module Puppet::Parser::Functions else url = args[0] end + begin + contents = OpenURI.open_uri(url, http_options) + rescue OpenURI::HTTPError => e + res = e.io + warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'") + args[1] + end if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative? - begin - contents = OpenURI.open_uri(url, **http_options) - rescue OpenURI::HTTPError => e - res = e.io - warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'") - args[1] - end PSON.load(contents) || args[1] else - begin - contents = URI.open(url, **http_options) # rubocop:disable Security/Open : Temporarily disabling this cop. This is a security risk and must be addressed before release. - rescue URI::Error => e - res = e.io - warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'") - args[1] - end JSON.parse(contents) || args[1] end elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code diff --git a/spec/functions/loadjson_spec.rb b/spec/functions/loadjson_spec.rb index 19e36273d..b9f0df95a 100644 --- a/spec/functions/loadjson_spec.rb +++ b/spec/functions/loadjson_spec.rb @@ -95,11 +95,10 @@ let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } it { + expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) if Puppet::PUPPETVERSION[0].to_i < 8 - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) expect(PSON).to receive(:load).with(json).and_return(data).once else - expect(URI).to receive(:open).with(filename).and_return(json) expect(JSON).to receive(:parse).with(json).and_return(data).once end expect(subject).to run.with_params(filename).and_return(data) @@ -116,11 +115,10 @@ let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } it { + expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) if Puppet::PUPPETVERSION[0].to_i < 8 - expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) expect(PSON).to receive(:load).with(json).and_return(data).once else - expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json) expect(JSON).to receive(:parse).with(json).and_return(data).once end expect(subject).to run.with_params(filename).and_return(data) @@ -137,11 +135,10 @@ let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } it { + expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) if Puppet::PUPPETVERSION[0].to_i < 8 - expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json) expect(PSON).to receive(:load).with(json).and_return(data).once else - expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json) expect(JSON).to receive(:parse).with(json).and_return(data).once end expect(subject).to run.with_params(filename).and_return(data) @@ -155,11 +152,10 @@ let(:json) { ',;{"key":"value"}' } it { + expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) if Puppet::PUPPETVERSION[0].to_i < 8 - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json) expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' else - expect(URI).to receive(:open).with(filename).and_return(json) expect(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!' end expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') @@ -172,11 +168,7 @@ end it { - if Puppet::PUPPETVERSION[0].to_i < 8 - expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found' - else - expect(URI).to receive(:open).with(filename).and_raise URI::Error, '404 File not Found' - end + expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found' expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } end