diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..12e57db --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,5 @@ +--- +inherit_from: .rubocop_todo.yml + +inherit_gem: + voxpupuli-rubocop: rubocop.yml diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..92b6b7a --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,259 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2023-05-30 13:12:42 UTC using RuboCop version 1.50.2. +# 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: 1 +Lint/MissingSuper: + Exclude: + - 'lib/ra10ke.rb' + +# Offense count: 1 +Lint/NoReturnInBeginEndBlocks: + Exclude: + - 'lib/ra10ke/puppetfile_parser.rb' + +# Offense count: 1 +# Configuration parameters: AllowComments, AllowNil. +Lint/SuppressedException: + Exclude: + - 'Rakefile' + +# Offense count: 5 +Lint/UselessAssignment: + Exclude: + - 'lib/ra10ke/dependencies.rb' + - 'lib/ra10ke/solve.rb' + +# Offense count: 1 +Naming/AccessorMethodName: + Exclude: + - 'lib/ra10ke.rb' + +# Offense count: 1 +# Configuration parameters: ForbiddenDelimiters. +# ForbiddenDelimiters: (?i-mx:(^|\s)(EO[A-Z]{1}|END)(\s|$)) +Naming/HeredocDelimiterNaming: + Exclude: + - 'lib/ra10ke/dependencies.rb' + +# Offense count: 1 +# 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: + - 'lib/ra10ke/solve.rb' + +# Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). +Performance/Count: + Exclude: + - 'lib/ra10ke/validate.rb' + - 'spec/ra10ke/validate_spec.rb' + +# Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). +Performance/MapCompact: + Exclude: + - 'lib/ra10ke/dependencies.rb' + - 'lib/ra10ke/puppetfile_parser.rb' + +# Offense count: 4 +RSpec/AnyInstance: + Exclude: + - 'spec/ra10ke/validate_spec.rb' + - 'spec/ra10ke_spec.rb' + +# Offense count: 3 +# This cop supports unsafe autocorrection (--autocorrect-all). +RSpec/BeEq: + Exclude: + - 'spec/ra10ke/deprecation_spec.rb' + +# Offense count: 6 +# Configuration parameters: Prefixes, AllowedPatterns. +# Prefixes: when, with, without +RSpec/ContextWording: + Exclude: + - 'spec/ra10ke/dependencies_spec.rb' + +# Offense count: 5 +# Configuration parameters: CountAsOne. +RSpec/ExampleLength: + Max: 31 + +# Offense count: 10 +RSpec/ExpectInHook: + Exclude: + - 'spec/ra10ke/validate_spec.rb' + +# Offense count: 15 +# Configuration parameters: . +# SupportedStyles: have_received, receive +RSpec/MessageSpies: + EnforcedStyle: receive + +# Offense count: 7 +RSpec/MultipleExpectations: + Max: 3 + +# Offense count: 1 +# Configuration parameters: AllowedPatterns. +# AllowedPatterns: ^expect_, ^assert_ +RSpec/NoExpectationExample: + Exclude: + - 'spec/ra10ke/dependencies_spec.rb' + +# Offense count: 12 +RSpec/StubbedMock: + Exclude: + - 'spec/ra10ke/deprecation_spec.rb' + - 'spec/ra10ke/validate_spec.rb' + +# Offense count: 7 +# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames. +RSpec/VerifiedDoubles: + Exclude: + - 'spec/ra10ke/deprecation_spec.rb' + - 'spec/ra10ke/validate_spec.rb' + +# Offense count: 1 +RSpec/VoidExpect: + Exclude: + - 'spec/ra10ke/git_repo_spec.rb' + +# Offense count: 6 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: nested, compact +Style/ClassAndModuleChildren: + Exclude: + - 'lib/ra10ke/dependencies.rb' + - 'lib/ra10ke/deprecation.rb' + - 'lib/ra10ke/duplicates.rb' + - 'lib/ra10ke/install.rb' + - 'lib/ra10ke/solve.rb' + - 'lib/ra10ke/syntax.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/CollectionCompact: + Exclude: + - 'lib/ra10ke/dependencies.rb' + +# Offense count: 15 +# Configuration parameters: AllowedConstants. +Style/Documentation: + Exclude: + - 'spec/**/*' + - 'test/**/*' + - 'lib/ra10ke.rb' + - 'lib/ra10ke/dependencies.rb' + - 'lib/ra10ke/deprecation.rb' + - 'lib/ra10ke/duplicates.rb' + - 'lib/ra10ke/git_repo.rb' + - 'lib/ra10ke/install.rb' + - 'lib/ra10ke/monkey_patches.rb' + - 'lib/ra10ke/puppetfile_parser.rb' + - 'lib/ra10ke/solve.rb' + - 'lib/ra10ke/syntax.rb' + - 'lib/ra10ke/validate.rb' + +# Offense count: 5 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: MaxUnannotatedPlaceholdersAllowed, AllowedMethods, AllowedPatterns. +# SupportedStyles: annotated, template, unannotated +Style/FormatStringToken: + EnforcedStyle: unannotated + +# Offense count: 11 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, always_true, never +Style/FrozenStringLiteralComment: + Exclude: + - 'Gemfile' + - 'Rakefile' + - 'lib/ra10ke.rb' + - 'lib/ra10ke/dependencies.rb' + - 'lib/ra10ke/install.rb' + - 'lib/ra10ke/puppetfile_parser.rb' + - 'lib/ra10ke/solve.rb' + - 'lib/ra10ke/syntax.rb' + - 'lib/ra10ke/version.rb' + - 'ra10ke.gemspec' + - 'spec/ra10ke/puppetfile_parser_spec.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: InverseMethods, InverseBlocks. +Style/InverseMethods: + Exclude: + - 'lib/ra10ke/dependencies.rb' + +# Offense count: 1 +Style/MultilineBlockChain: + Exclude: + - 'lib/ra10ke/dependencies.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: literals, strict +Style/MutableConstant: + Exclude: + - 'lib/ra10ke/version.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle, AllowedMethods, AllowedPatterns. +# SupportedStyles: predicate, comparison +Style/NumericPredicate: + Exclude: + - 'spec/**/*' + - 'lib/ra10ke/validate.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/RedundantInterpolation: + Exclude: + - 'lib/ra10ke/duplicates.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/RedundantSort: + Exclude: + - 'lib/ra10ke/dependencies.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: RequireEnglish, EnforcedStyle. +# SupportedStyles: use_perl_names, use_english_names, use_builtin_english_names +Style/SpecialGlobalVars: + Exclude: + - 'ra10ke.gemspec' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: Mode. +Style/StringConcatenation: + Exclude: + - 'lib/ra10ke/solve.rb' + +# Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowMethodsWithArguments, AllowedMethods, AllowedPatterns, AllowComments. +# AllowedMethods: define_method +Style/SymbolProc: + Exclude: + - 'lib/ra10ke/dependencies.rb' + +# Offense count: 6 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. +# URISchemes: http, https +Layout/LineLength: + Max: 137 diff --git a/Gemfile b/Gemfile index d63dff1..5afee89 100644 --- a/Gemfile +++ b/Gemfile @@ -4,10 +4,10 @@ source 'https://rubygems.org' gemspec group :release do - gem 'github_changelog_generator', :require => false + gem 'github_changelog_generator', require: false end -group :coverage, optional: ENV['COVERAGE']!='yes' do - gem 'simplecov-console', :require => false - gem 'codecov', :require => false +group :coverage, optional: ENV['COVERAGE'] != 'yes' do + gem 'codecov', require: false + gem 'simplecov-console', require: false end diff --git a/Rakefile b/Rakefile index a390f58..f3a62a5 100644 --- a/Rakefile +++ b/Rakefile @@ -6,24 +6,30 @@ require 'fileutils' require 'rspec/core' require 'rspec/core/rake_task' -CLEAN.include("pkg/", "tmp/") -CLOBBER.include("Gemfile.lock") +CLEAN.include('pkg/', 'tmp/') +CLOBBER.include('Gemfile.lock') -task :default => [:spec] +task default: [:spec] RSpec::Core::RakeTask.new(:spec) do |spec| - spec.pattern = FileList['spec/**/*_spec.rb'] + spec.pattern = FileList['spec/**/*_spec.rb'] end begin require 'github_changelog_generator/task' GitHubChangelogGenerator::RakeTask.new :changelog do |config| version = Ra10ke::VERSION - config.future_release = "v#{version}" if version =~ /^\d+\.\d+.\d+$/ + config.future_release = "v#{version}" if /^\d+\.\d+.\d+$/.match?(version) config.header = "# Changelog\n\nAll notable changes to this project will be documented in this file." - config.exclude_labels = %w{duplicate question invalid wontfix wont-fix skip-changelog} + config.exclude_labels = %w[duplicate question invalid wontfix wont-fix skip-changelog] config.user = 'voxpupuli' config.project = 'ra10ke' end rescue LoadError end + +begin + require 'voxpupuli/rubocop/rake' +rescue LoadError + # the voxpupuli-rubocop gem is optional +end diff --git a/lib/ra10ke.rb b/lib/ra10ke.rb index 76130e1..1a80a2c 100644 --- a/lib/ra10ke.rb +++ b/lib/ra10ke.rb @@ -48,7 +48,8 @@ def initialize(*args) def get_puppetfile R10K::Puppetfile.new(@basedir, @moduledir, @puppetfile_path, @puppetfile_name, @force) rescue ArgumentError # R10k < 2.6.0 - R10K::Puppetfile.new(@basedir, @moduledir, @puppetfile_path || File.join(@basedir, @puppetfile_name || 'Puppetfile')) + R10K::Puppetfile.new(@basedir, @moduledir, + @puppetfile_path || File.join(@basedir, @puppetfile_name || 'Puppetfile')) end end end diff --git a/lib/ra10ke/dependencies.rb b/lib/ra10ke/dependencies.rb index db57ac0..94b9c69 100644 --- a/lib/ra10ke/dependencies.rb +++ b/lib/ra10ke/dependencies.rb @@ -6,7 +6,6 @@ module Ra10ke::Dependencies class Verification - def self.version_formats @version_formats ||= {} end @@ -20,12 +19,10 @@ def self.register_version_format(name, &block) Ra10ke::Dependencies::Verification.register_version_format(:semver) do |tags| latest_tag = tags.map do |tag| - begin - Semverse::Version.new tag[/\Av?(.*)\Z/, 1] - rescue Semverse::InvalidVersionFormat - # ignore tags that do not comply to semver - nil - end + Semverse::Version.new tag[/\Av?(.*)\Z/, 1] + rescue Semverse::InvalidVersionFormat + # ignore tags that do not comply to semver + nil end.select { |tag| !tag.nil? }.sort.last.to_s.downcase latest_ref = tags.detect { |tag| tag[/\Av?(.*)\Z/, 1] == latest_tag } end @@ -33,8 +30,8 @@ def self.register_version_format(name, &block) def initialize(pfile) @puppetfile = pfile - # semver is the default version format. - + # semver is the default version format. + puppetfile.load! end @@ -49,22 +46,22 @@ def get_latest_ref(remote_refs) def ignored_modules # ignore file allows for "don't tell me about this" @ignored_modules ||= begin - File.readlines('.r10kignore').each {|l| l.chomp!} if File.exist?('.r10kignore') + File.readlines('.r10kignore').each { |l| l.chomp! } if File.exist?('.r10kignore') end || [] end - - # @summary creates an array of module hashes with version info - # @param {Object} supplied_puppetfile - the parsed puppetfile object + + # @summary creates an array of module hashes with version info + # @param {Object} supplied_puppetfile - the parsed puppetfile object # @returns {Array} array of version info for each module # @note does not include ignored modules or modules up2date def processed_modules(supplied_puppetfile = puppetfile) threads = [] threads = supplied_puppetfile.modules.map do |puppet_module| - Thread.new do - begin + Thread.new do next if ignored_modules.include? puppet_module.title - if puppet_module.class == ::R10K::Module::Forge - module_name = puppet_module.title.gsub('/', '-') + + if puppet_module.instance_of?(::R10K::Module::Forge) + module_name = puppet_module.title.tr('/', '-') forge_version = ::PuppetForge::Module.find(module_name).current_release.version installed_version = puppet_module.expected_version { @@ -72,10 +69,10 @@ def processed_modules(supplied_puppetfile = puppetfile) installed: installed_version, latest: forge_version, type: 'forge', - message: installed_version != forge_version ? :outdated : :current + message: installed_version == forge_version ? :current : :outdated, } - - elsif puppet_module.class == R10K::Module::Git + + elsif puppet_module.instance_of?(R10K::Module::Git) # use helper; avoid `desired_ref` # we do not want to deal with `:control_branch` ref = puppet_module.version @@ -96,10 +93,10 @@ def processed_modules(supplied_puppetfile = puppetfile) # register own version formats with # Ra10ke::Dependencies.register_version_format(:name, &block) latest_ref = get_latest_ref(remote_refs) - elsif ref.match(/^[a-z0-9]{40}$/) - ref = ref.slice(0,8) + elsif /^[a-z0-9]{40}$/.match?(ref) + ref = ref.slice(0, 8) # for sha just assume head should be tracked - latest_ref = remote_refs['head'][:sha].slice(0,8) + latest_ref = remote_refs['head'][:sha].slice(0, 8) else raise "Unable to determine ref type for #{puppet_module.title}" end @@ -108,26 +105,25 @@ def processed_modules(supplied_puppetfile = puppetfile) installed: ref, latest: latest_ref, type: 'git', - message: ref != latest_ref ? :outdated : :current + message: ref == latest_ref ? :current : :outdated, } end rescue R10K::Util::Subprocess::SubprocessError => e - { - name: puppet_module.title, - installed: nil, - latest: nil, - type: :error, - message: e.message - } - end + { + name: puppet_module.title, + installed: nil, + latest: nil, + type: :error, + message: e.message, + } end end threads.map { |th| th.join.value }.compact end - def outdated(supplied_puppetfile = puppetfile) - processed_modules.find_all do | mod | + def outdated(_supplied_puppetfile = puppetfile) + processed_modules.find_all do |mod| mod[:message] == :outdated end end @@ -139,7 +135,7 @@ def print_table(mods) end def define_task_print_git_conversion(*_args) - desc "Convert and print forge modules to git format" + desc 'Convert and print forge modules to git format' task :print_git_conversion do require 'ra10ke/git_repo' require 'r10k/puppetfile' @@ -152,26 +148,26 @@ def define_task_print_git_conversion(*_args) # ignore file allows for "don't tell me about this" ignore_modules = [] - if File.exist?('.r10kignore') - ignore_modules = File.readlines('.r10kignore').each {|l| l.chomp!} + ignore_modules = File.readlines('.r10kignore').each { |l| l.chomp! } if File.exist?('.r10kignore') + forge_mods = puppetfile.modules.find_all do |mod| + mod.instance_of?(R10K::Module::Forge) && mod.v3_module.homepage_url? end - forge_mods = puppetfile.modules.find_all { |mod| mod.instance_of?(R10K::Module::Forge) && mod.v3_module.homepage_url? } - + threads = forge_mods.map do |mod| - Thread.new do - source_url = mod.v3_module.attributes.dig(:current_release, :metadata, :source) || mod.v3_module.homepage_url + Thread.new do + source_url = mod.v3_module.attributes.dig(:current_release, :metadata, :source) || mod.v3_module.homepage_url # git:// does not work with ls-remote command, convert to https source_url = source_url.gsub('git://', 'https://') - source_url = source_url.gsub(/\Agit\@(.*)\:(.*)/) do - "https://#{$1}/#{$2}" + source_url = source_url.gsub(/\Agit@(.*):(.*)/) do + "https://#{::Regexp.last_match(1)}/#{::Regexp.last_match(2)}" end repo = ::Ra10ke::GitRepo.new(source_url) - ref = repo.get_ref_like(mod.expected_version) + ref = repo.get_ref_like(mod.expected_version) ref_name = ref ? ref[:name] : "bad url or tag #{mod.expected_version} is missing" <<~EOF - mod '#{mod.name}', - :git => '#{source_url}', - :ref => '#{ref_name}' + mod '#{mod.name}', + :git => '#{source_url}', + :ref => '#{ref_name}' EOF end @@ -182,11 +178,11 @@ def define_task_print_git_conversion(*_args) end def define_task_dependencies(*_args) - desc "Print outdated forge modules" + desc 'Print outdated forge modules' task :dependencies do PuppetForge.user_agent = "ra10ke/#{Ra10ke::VERSION}" puppetfile = get_puppetfile - PuppetForge.host = puppetfile.forge if puppetfile.forge =~ /^http/ + PuppetForge.host = puppetfile.forge if /^http/.match?(puppetfile.forge) dependencies = Ra10ke::Dependencies::Verification.new(puppetfile) dependencies.print_table(dependencies.outdated) end diff --git a/lib/ra10ke/duplicates.rb b/lib/ra10ke/duplicates.rb index b6583c8..eb61aa7 100644 --- a/lib/ra10ke/duplicates.rb +++ b/lib/ra10ke/duplicates.rb @@ -5,7 +5,7 @@ module Ra10ke::Duplicates def define_task_duplicates(*_args) - desc "Check Puppetfile for duplicates" + desc 'Check Puppetfile for duplicates' task :duplicates do duplicates = Ra10ke::Duplicates::Verification.new(get_puppetfile.puppetfile_path).duplicates exit_code = 0 diff --git a/lib/ra10ke/git_repo.rb b/lib/ra10ke/git_repo.rb index 18fc7d1..59e7482 100644 --- a/lib/ra10ke/git_repo.rb +++ b/lib/ra10ke/git_repo.rb @@ -82,20 +82,19 @@ def valid_ref?(ref = 'HEAD') # :sha=>"fcc0532bbc5a5b65f3941738339e9cc7e3d767ce", :ref=>"refs/pull/249/head", :type=>:pull, :subtype=>:head, :name=>"249"}, # :sha=>"8d54891fa5df75890ee15d53080c2a81b4960f92", :ref=>"refs/pull/267/head", :type=>:pull, :subtype=>:head, :name=>"267"}] def all_refs - @all_refs ||= begin - remote_refs.each_with_object([]) do |line, refs| - sha, ref = line.split("\t") - next refs if line.include?('redirecting') - next refs if sha.eql?('ref: refs/heads/master') - _, type, name, subtype = ref.chomp.split('/') - next refs unless name - - type = :tag if type.eql?('tags') - type = type.to_sym - subtype = subtype.to_sym if subtype - type = :branch if type.eql?(:heads) - refs << { sha: sha, ref: ref.chomp, type: type, subtype: subtype, name: name } - end + @all_refs ||= remote_refs.each_with_object([]) do |line, refs| + sha, ref = line.split("\t") + next refs if line.include?('redirecting') + next refs if sha.eql?('ref: refs/heads/master') + + _, type, name, subtype = ref.chomp.split('/') + next refs unless name + + type = :tag if type.eql?('tags') + type = type.to_sym + subtype = subtype.to_sym if subtype + type = :branch if type.eql?(:heads) + refs << { sha: sha, ref: ref.chomp, type: type, subtype: subtype, name: name } end end @@ -105,10 +104,10 @@ def all_refs # @return [String] the matching ref_name or nil def get_ref_like(ref_name) return nil unless valid_url? - ref = all_refs.find do |ref| + + all_refs.find do |ref| ref[:name].include?(ref_name) end - ref end # useful for mocking easily diff --git a/lib/ra10ke/install.rb b/lib/ra10ke/install.rb index 9b3e288..76210d8 100644 --- a/lib/ra10ke/install.rb +++ b/lib/ra10ke/install.rb @@ -1,27 +1,27 @@ module Ra10ke::Install def define_task_install(*_args) - desc "Install modules specified in Puppetfile" + desc 'Install modules specified in Puppetfile' task :install do require 'r10k/puppetfile' puppetfile = get_puppetfile puppetfile.load! - puts "Processing Puppetfile for fixtures" + puts 'Processing Puppetfile for fixtures' puppetfile.modules.each do |mod| if mod.status == :insync puts "Skipping #{mod.name} (#{mod.version}) already in sync" else - if mod.status == :absent - msg = "installed #{mod.name}" - else - msg = "updated #{mod.name} from #{mod.version} to" - end + msg = if mod.status == :absent + "installed #{mod.name}" + else + "updated #{mod.name} from #{mod.version} to" + end mod.sync - if mod.status != :insync - puts "Failed to sync #{mod.name}".red - else + if mod.status == :insync puts "Successfully #{msg} #{mod.version}".green + else + puts "Failed to sync #{mod.name}".red end end end diff --git a/lib/ra10ke/monkey_patches.rb b/lib/ra10ke/monkey_patches.rb index 2fff6b1..9b4dd0c 100644 --- a/lib/ra10ke/monkey_patches.rb +++ b/lib/ra10ke/monkey_patches.rb @@ -18,7 +18,7 @@ def yellow colorize(33) end - # removes specified markes from string. + # removes specified markes from string. # @return [String] - the string with markers removed def strip_comment(markers = ['#', "\n"]) re = Regexp.union(markers) diff --git a/lib/ra10ke/puppetfile_parser.rb b/lib/ra10ke/puppetfile_parser.rb index 14883ee..a1ee23a 100644 --- a/lib/ra10ke/puppetfile_parser.rb +++ b/lib/ra10ke/puppetfile_parser.rb @@ -1,90 +1,90 @@ -# it might be desirable to parse the Puppetfile as a string instead of evaling it. +# it might be desirable to parse the Puppetfile as a string instead of evaling it. # this module allows you to do just that. require 'ra10ke/monkey_patches' module Ra10ke - module PuppetfileParser - - # @return [Array] - returns a array of hashes that contain modules with a git source - def git_modules(file = puppetfile) - modules(file).find_all do |mod| - mod[:args].key?(:git) - end + module PuppetfileParser + # @return [Array] - returns a array of hashes that contain modules with a git source + def git_modules(file = puppetfile) + modules(file).find_all do |mod| + mod[:args].key?(:git) end + end - # @return [Array] - returns a array of hashes that contain modules from the Forge - def forge_modules(file = puppetfile) - modules(file).reject do |mod| - mod[:args].key?(:git) - end + # @return [Array] - returns a array of hashes that contain modules from the Forge + def forge_modules(file = puppetfile) + modules(file).reject do |mod| + mod[:args].key?(:git) end + end - # @param puppetfile [String] - the absolute path to the puppetfile - # @return [Array] - returns an array of module hashes that represent the puppetfile - # @example - # [{:namespace=>"puppetlabs", :name=>"stdlib", :args=>[]}, - # {:namespace=>"petems", :name=>"swap_file", :args=>["'4.0.0'"]}] - def modules(puppetfile) - @modules ||= begin - return [] unless File.exist?(puppetfile) - all_lines = File.read(puppetfile).lines.map(&:strip_comment) - # remove comments from all the lines - lines_without_comments = all_lines.reject { |line| line.match(/#.*\n/) || line.empty? }.join("\n") - lines_without_comments.split(/^mod/).map do |line| - - next nil if line =~ /^forge/ - next nil if line.empty? + # @param puppetfile [String] - the absolute path to the puppetfile + # @return [Array] - returns an array of module hashes that represent the puppetfile + # @example + # [{:namespace=>"puppetlabs", :name=>"stdlib", :args=>[]}, + # {:namespace=>"petems", :name=>"swap_file", :args=>["'4.0.0'"]}] + def modules(puppetfile) + @modules ||= begin + return [] unless File.exist?(puppetfile) - parse_module_args(line) - end.compact.uniq - end - end + all_lines = File.read(puppetfile).lines.map(&:strip_comment) + # remove comments from all the lines + lines_without_comments = all_lines.reject { |line| line.match(/#.*\n/) || line.empty? }.join("\n") + lines_without_comments.split(/^mod/).map do |line| + next nil if /^forge/.match?(line) + next nil if line.empty? - # @param data [String] - the string to parse the puppetfile args out of - # @return [Array] - an array of arguments in hash form - # @example - # {:namespace=>"puppetlabs", :name=>"stdlib", :args=>[]} - # {:namespace=>"petems", :name=>"swap_file", :args=>["'4.0.0'"]} - def parse_module_args(data) - return {} if data.empty? - args = data.split(',').map(&:strip) - # we can't guarantee that there will be a namespace when git is used - # remove quotes and dash and slash - namespace, name = args.shift.gsub(/'|"/, '').split(%r{-|/}) - name ||= namespace - namespace = nil if namespace == name - { - namespace: namespace, - name: name, - args: process_args(args) - } + parse_module_args(line) + end.compact.uniq end + end + + # @param data [String] - the string to parse the puppetfile args out of + # @return [Array] - an array of arguments in hash form + # @example + # {:namespace=>"puppetlabs", :name=>"stdlib", :args=>[]} + # {:namespace=>"petems", :name=>"swap_file", :args=>["'4.0.0'"]} + def parse_module_args(data) + return {} if data.empty? + + args = data.split(',').map(&:strip) + # we can't guarantee that there will be a namespace when git is used + # remove quotes and dash and slash + namespace, name = args.shift.gsub(/'|"/, '').split(%r{-|/}) + name ||= namespace + namespace = nil if namespace == name + { + namespace: namespace, + name: name, + args: process_args(args), + } + end - # @return [Array] - returns an array of hashes with the args in key value pairs - # @param [Array] - the arguments processed from each entry in the puppetfile - # @example - # [{:args=>[], :name=>"razor", :namespace=>"puppetlabs"}, - # {:args=>[{:version=>"0.0.3"}], :name=>"ntp", :namespace=>"puppetlabs"}, - # {:args=>[], :name=>"inifile", :namespace=>"puppetlabs"}, - # {:args=> - # [{:git=>"https://github.com/nwops/reportslack.git"}, {:ref=>"1.0.20"}], - # :name=>"reportslack", - # :namespace=>"nwops"}, - # {:args=>{:git=>"git://github.com/puppetlabs/puppetlabs-apt.git"}, - # :name=>"apt", - # :namespace=>nil} - # ] - def process_args(args) - results = {} - args.each do |arg| - a = arg.gsub(/'|"/, '').split(/\A\:|\:\s|\=\>/).map(&:strip).reject(&:empty?) - if a.count < 2 - results[:version] = a.first - else - results[a.first.to_sym] = a.last - end + # @return [Array] - returns an array of hashes with the args in key value pairs + # @param [Array] - the arguments processed from each entry in the puppetfile + # @example + # [{:args=>[], :name=>"razor", :namespace=>"puppetlabs"}, + # {:args=>[{:version=>"0.0.3"}], :name=>"ntp", :namespace=>"puppetlabs"}, + # {:args=>[], :name=>"inifile", :namespace=>"puppetlabs"}, + # {:args=> + # [{:git=>"https://github.com/nwops/reportslack.git"}, {:ref=>"1.0.20"}], + # :name=>"reportslack", + # :namespace=>"nwops"}, + # {:args=>{:git=>"git://github.com/puppetlabs/puppetlabs-apt.git"}, + # :name=>"apt", + # :namespace=>nil} + # ] + def process_args(args) + results = {} + args.each do |arg| + a = arg.gsub(/'|"/, '').split(/\A:|:\s|=>/).map(&:strip).reject(&:empty?) + if a.count < 2 + results[:version] = a.first + else + results[a.first.to_sym] = a.last end - results end + results end + end end diff --git a/lib/ra10ke/solve.rb b/lib/ra10ke/solve.rb index a026e14..47e9fc4 100644 --- a/lib/ra10ke/solve.rb +++ b/lib/ra10ke/solve.rb @@ -27,18 +27,16 @@ def define_task_solve_dependencies(*_args) PuppetForge.user_agent = "ra10ke/#{Ra10ke::VERSION}" puppetfile = get_puppetfile puppetfile.load! - PuppetForge.host = puppetfile.forge if puppetfile.forge =~ /^http/ + PuppetForge.host = puppetfile.forge if /^http/.match?(puppetfile.forge) # ignore file allows for "don't tell me about this" ignore_modules = [] - if File.exist?('.r10kignore') - ignore_modules = File.readlines('.r10kignore').each(&:chomp!) - end + ignore_modules = File.readlines('.r10kignore').each(&:chomp!) if File.exist?('.r10kignore') # Actual new logic begins here: cache = (ENV['XDG_CACHE_DIR'] || File.expand_path('~/.cache')) FileUtils.mkdir_p(cache) - + # Metadata cache, since the Forge is slow: @metadata_cache = YAML::Store.new File.join(cache, 'ra10ke.metadata_cache') # The graph of available module versions @@ -54,7 +52,8 @@ def define_task_solve_dependencies(*_args) puppetfile.modules.each do |puppet_module| next if ignore_modules.include? puppet_module.title - if puppet_module.class == R10K::Module::Forge + + if puppet_module.instance_of?(R10K::Module::Forge) module_name = puppet_module.title.tr('/', '-') installed_version = puppet_module.expected_version puts "Processing Forge module #{module_name}-#{installed_version}" @@ -81,7 +80,8 @@ def define_task_solve_dependencies(*_args) add_reqs_to_graph(mod, meta) end - next unless puppet_module.class == R10K::Module::Git + next unless puppet_module.instance_of?(R10K::Module::Git) + # This downloads the git module to modules/modulename meta = fetch_git_metadata(puppet_module) version = get_key_or_sym(meta, :version) @@ -97,9 +97,7 @@ def define_task_solve_dependencies(*_args) end puts puts 'Resolving dependencies...' - if allow_major_bump - puts 'WARNING: Potentially breaking updates are allowed for this resolution' - end + puts 'WARNING: Potentially breaking updates are allowed for this resolution' if allow_major_bump result = Solve.it!(@graph, @demands, sorted: true) puts print_module_diff(@current_modules, result) @@ -130,7 +128,7 @@ def fetch_git_metadata(puppet_module) return { version: '0.0.0', name: puppet_module.title, - dependencies: [] + dependencies: [], } end metadata = R10K::Module::MetadataFile.new(metadata_path) @@ -138,7 +136,7 @@ def fetch_git_metadata(puppet_module) { version: metadata.version, name: metadata.name, - dependencies: metadata.dependencies + dependencies: metadata.dependencies, } end @@ -150,7 +148,7 @@ def get_key_or_sym(hash, k) # At least puppet-extlib has malformed metadata def get_version_req(dep) req = get_key_or_sym(dep, :version_requirement) - req = get_key_or_sym(dep, :version_range) unless req + req ||= get_key_or_sym(dep, :version_range) req end @@ -186,17 +184,14 @@ def add_reqs_to_graph(artifact, metadata, no_demands = nil) # actually ask the solver for the versioned thing @demands.add(name) unless no_demands ver = get_version_req(dep) - unless ver - # no version specified, so anything will do - ver = '>=0.0.0' - end - ver.split(/(?=[<])/).each do |bound| + ver ||= '>=0.0.0' + ver.split(/(?=<)/).each do |bound| bound.strip! v = begin - Semverse::Constraint.new(bound) - rescue - nil - end + Semverse::Constraint.new(bound) + rescue StandardError + nil + end if v artifact.depends(name, v.to_s) else @@ -206,14 +201,16 @@ def add_reqs_to_graph(artifact, metadata, no_demands = nil) # Find the dependency in the forge, unless it's already been processed # and add its releases to the global graph next unless @processed_modules.add?(name) + puts "Fetching module info for #{name}" mod = begin - PuppetForge::Module.find(name) - rescue - # It's probably a git module - nil - end + PuppetForge::Module.find(name) + rescue StandardError + # It's probably a git module + nil + end next unless mod # Git module, or non-forge dependency. Skip to next for now. + # Fetching metadata for all releases takes ages (which is weird, since it's mostly static info) mod.releases.take(FETCH_LIMIT).each do |rel| meta = get_release_metadata(name, rel) diff --git a/lib/ra10ke/syntax.rb b/lib/ra10ke/syntax.rb index 84dad67..f29738a 100644 --- a/lib/ra10ke/syntax.rb +++ b/lib/ra10ke/syntax.rb @@ -1,18 +1,16 @@ module Ra10ke::Syntax def define_task_syntax(*_args) - desc "Syntax check Puppetfile" + desc 'Syntax check Puppetfile' task :syntax do require 'r10k/action/puppetfile/check' puppetfile = R10K::Action::Puppetfile::Check.new({ - :root => @basedir, - :moduledir => @moduledir, - :puppetfile => @puppetfile_path - }, '') + root: @basedir, + moduledir: @moduledir, + puppetfile: @puppetfile_path, + }, '') - unless puppetfile.call - abort("Puppetfile syntax check failed") - end + abort('Puppetfile syntax check failed') unless puppetfile.call end end end diff --git a/lib/ra10ke/validate.rb b/lib/ra10ke/validate.rb index 4fbb06f..57a94c9 100644 --- a/lib/ra10ke/validate.rb +++ b/lib/ra10ke/validate.rb @@ -8,7 +8,6 @@ module Ra10ke module Validate - GOOD_EMOJI = ENV['GOOD_EMOJI'] || '👍' BAD_EMOJI = ENV['BAD_EMOJI'] || '😨' @@ -64,7 +63,7 @@ def all_modules ref: ref, valid_url?: repo.valid_url?, valid_ref?: valid_ref, - status: valid_ref ? Ra10ke::Validate::GOOD_EMOJI : Ra10ke::Validate::BAD_EMOJI + status: valid_ref ? Ra10ke::Validate::GOOD_EMOJI : Ra10ke::Validate::BAD_EMOJI, } end end diff --git a/lib/ra10ke/version.rb b/lib/ra10ke/version.rb index 9906821..f593b8b 100644 --- a/lib/ra10ke/version.rb +++ b/lib/ra10ke/version.rb @@ -1,3 +1,3 @@ module Ra10ke - VERSION = "2.0.0" + VERSION = '2.0.0' end diff --git a/ra10ke.gemspec b/ra10ke.gemspec index 2cd85e1..d434c7e 100644 --- a/ra10ke.gemspec +++ b/ra10ke.gemspec @@ -1,30 +1,30 @@ -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'ra10ke/version' Gem::Specification.new do |spec| - spec.name = "ra10ke" + spec.name = 'ra10ke' spec.version = Ra10ke::VERSION - spec.authors = ["Theo Chatzimichos", "Vox Pupuli"] - spec.email = ["voxpupuli@groups.io"] - spec.description = %q{R10K and Puppetfile rake tasks} - spec.summary = %q{Syntax check for the Puppetfile, check for outdated installed puppet modules} - spec.homepage = "https://github.com/voxpupuli/ra10ke" - spec.license = "MIT" + spec.authors = ['Theo Chatzimichos', 'Vox Pupuli'] + spec.email = ['voxpupuli@groups.io'] + spec.description = 'R10K and Puppetfile rake tasks' + spec.summary = 'Syntax check for the Puppetfile, check for outdated installed puppet modules' + spec.homepage = 'https://github.com/voxpupuli/ra10ke' + spec.license = 'MIT' spec.files = `git ls-files`.split($/) - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] spec.required_ruby_version = '>= 2.7.0' - spec.add_dependency "rake" - spec.add_dependency "puppet_forge" - spec.add_dependency "r10k", '>= 2.6.5' - spec.add_dependency "git" - spec.add_dependency "solve" + spec.add_dependency 'git' + spec.add_dependency 'puppet_forge' + spec.add_dependency 'r10k', '>= 2.6.5' + spec.add_dependency 'rake' spec.add_dependency 'semverse', '>= 2.0' + spec.add_dependency 'solve' spec.add_dependency 'table_print', '~> 1.5.6' - spec.add_development_dependency 'rspec', '~> 3.6' spec.add_development_dependency 'pry' + spec.add_development_dependency 'rspec', '~> 3.6' spec.add_development_dependency 'simplecov' + spec.add_development_dependency 'voxpupuli-rubocop', '~> 1.1' end - diff --git a/spec/ra10ke/dependencies_spec.rb b/spec/ra10ke/dependencies_spec.rb index f630e14..5fd0e3b 100644 --- a/spec/ra10ke/dependencies_spec.rb +++ b/spec/ra10ke/dependencies_spec.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'r10k/puppetfile' require 'spec_helper' require 'ra10ke/dependencies' @@ -8,7 +9,7 @@ RSpec.describe 'Ra10ke::Dependencies::Verification' do let(:instance) do - pfile = R10K::Puppetfile.new(File.basename(puppetfile), nil,puppetfile, nil, false) + pfile = R10K::Puppetfile.new(File.basename(puppetfile), nil, puppetfile, nil, false) Ra10ke::Dependencies::Verification.new(pfile) end @@ -20,8 +21,9 @@ it 'default contains semver' do expect(Ra10ke::Dependencies::Verification.version_formats).to have_key(:semver) end + it 'add new version format' do - Ra10ke::Dependencies::Verification.register_version_format(:test) do |tags| + Ra10ke::Dependencies::Verification.register_version_format(:test) do |_tags| nil end expect(Ra10ke::Dependencies::Verification.version_formats).to have_key(:test) @@ -30,10 +32,10 @@ context 'show output in table format' do let(:instance) do - pfile = R10K::Puppetfile.new(File.basename(puppetfile), nil,puppetfile, nil, false) + pfile = R10K::Puppetfile.new(File.basename(puppetfile), nil, puppetfile, nil, false) Ra10ke::Dependencies::Verification.new(pfile) end - + let(:puppetfile) do File.join(fixtures_dir, 'Puppetfile') end @@ -49,26 +51,24 @@ it 'show dependencies as table' do instance.print_table(processed_modules) end - end context 'get_latest_ref' do - context 'find latest semver tag' do let(:latest_tag) do - 'v1.1.0' + 'v1.1.0' end let(:test_tags) do { - 'v1.0.0' => nil, + 'v1.0.0' => nil, latest_tag => nil, } end it do expect(instance.get_latest_ref({ - 'tags' => test_tags, - })).to eq(latest_tag) + 'tags' => test_tags, + })).to eq(latest_tag) end end @@ -78,7 +78,7 @@ end let(:test_tags) do { - 'dev' => nil, + 'dev' => nil, latest_tag => nil, } end @@ -88,20 +88,19 @@ tags.detect { |tag| tag == latest_tag } end expect(instance.get_latest_ref({ - 'tags' => test_tags, - })).to eq(latest_tag) + 'tags' => test_tags, + })).to eq(latest_tag) end end context 'convert to ref' do - it 'run rake task' do output_conversion = File.read(File.join(fixtures_dir, 'Puppetfile_git_conversion')) require 'ra10ke' Ra10ke::RakeTask.new do |t| t.basedir = fixtures_dir end - expect{Rake::Task['r10k:print_git_conversion'].invoke}.to output(output_conversion).to_stdout + expect { Rake::Task['r10k:print_git_conversion'].invoke }.to output(output_conversion).to_stdout end end end diff --git a/spec/ra10ke/deprecation_spec.rb b/spec/ra10ke/deprecation_spec.rb index 2ec24d7..1c5843e 100644 --- a/spec/ra10ke/deprecation_spec.rb +++ b/spec/ra10ke/deprecation_spec.rb @@ -14,21 +14,24 @@ end it 'only checks forge modules' do - expect(PuppetForge::Module).to_not receive(:find).with('puppet') + expect(PuppetForge::Module).not_to receive(:find).with('puppet') allow(PuppetForge::Module).to receive(:find).and_raise(Faraday::ResourceNotFound.new(nil)) expect(instance.deprecated_modules.count).to eq(0) end it 'handles deprecated modules' do - expect(PuppetForge::Module).to receive(:find).with('puppetlabs-ruby').and_return(double(slug: 'puppetlabs-ruby', deprecated_at: '2021-04-22 10:29:42 -0700')) + expect(PuppetForge::Module).to receive(:find).with('puppetlabs-ruby').and_return(double(slug: 'puppetlabs-ruby', + deprecated_at: '2021-04-22 10:29:42 -0700')) allow(PuppetForge::Module).to receive(:find).and_return(double(slug: 'module-module', deprecated_at: nil)) expect(instance.bad_mods?).to eq(true) - expect(instance.deprecated_modules.first).to eq(name: 'puppetlabs-ruby', deprecated_at: Time.parse('2021-04-22 10:29:42 -0700')) + expect(instance.deprecated_modules.first).to eq(name: 'puppetlabs-ruby', + deprecated_at: Time.parse('2021-04-22 10:29:42 -0700')) end it 'handles missing modules' do - expect(PuppetForge::Module).to receive(:find).with('choria-choria').and_return(double(slug: 'choria-choria', deprecated_at: nil)) + expect(PuppetForge::Module).to receive(:find).with('choria-choria').and_return(double(slug: 'choria-choria', + deprecated_at: nil)) expect(PuppetForge::Module).to receive(:find).with('puppetlabs-ruby').and_raise(Faraday::ResourceNotFound.new(nil)) allow(PuppetForge::Module).to receive(:find).and_return(double(slug: 'module-module', deprecated_at: nil)) diff --git a/spec/ra10ke/git_repo_spec.rb b/spec/ra10ke/git_repo_spec.rb index 6099484..48497eb 100644 --- a/spec/ra10ke/git_repo_spec.rb +++ b/spec/ra10ke/git_repo_spec.rb @@ -10,7 +10,7 @@ expect(Ra10ke::GitRepo.new(url)).to be_a Ra10ke::GitRepo end - it 'should not run_command more than once' do + it 'does not run_command more than once' do i = Ra10ke::GitRepo.new(url) expect(i).to receive(:run_command).with("git ls-remote --symref #{url}").once i.valid_url? @@ -26,7 +26,7 @@ let(:reflist) { File.read(File.join(fixtures_dir, 'refs', 'gitlab.txt')) } - before(:each) do + before do allow(instance).to receive(:run_command).with("git ls-remote --symref #{url}").and_return([reflist, true]) end @@ -68,22 +68,22 @@ refs = instance.all_refs expect(refs).to be_a Array expect(refs.last).to eq( - { name: 'v5.0.0^{}', ref: 'refs/tags/v5.0.0^{}', sha: '1febd15f90d32e6b3d6c242a70db386b2ef1942c', subtype: nil, type: :tag } + { name: 'v5.0.0^{}', ref: 'refs/tags/v5.0.0^{}', sha: '1febd15f90d32e6b3d6c242a70db386b2ef1942c', subtype: nil, + type: :tag, }, ) end end describe 'bad url' do let(:url) { 'https://github.com/nwops/typo' } - - before(:each) do - allow(instance).to receive(:run_command).with("git ls-remote --symref #{url}").and_return(['', false]) - end - let(:instance) do Ra10ke::GitRepo.new(url) end + before do + allow(instance).to receive(:run_command).with("git ls-remote --symref #{url}").and_return(['', false]) + end + it '#all_refs' do expect(instance.all_refs).to be_a Array expect(instance.all_refs).to eq [] diff --git a/spec/ra10ke/puppetfile_parser_spec.rb b/spec/ra10ke/puppetfile_parser_spec.rb index 2e1aae2..9537846 100644 --- a/spec/ra10ke/puppetfile_parser_spec.rb +++ b/spec/ra10ke/puppetfile_parser_spec.rb @@ -2,110 +2,99 @@ require 'ra10ke/puppetfile_parser' RSpec.describe 'Ra10ke::PuppetfileParser' do - include Ra10ke::PuppetfileParser + include Ra10ke::PuppetfileParser - let(:puppetfile) do - File.join(fixtures_dir, 'Puppetfile') - end + let(:puppetfile) do + File.join(fixtures_dir, 'Puppetfile') + end - let(:puppetfile_modules) do - [{:args=>{:version=>'0.26.2'}, :name=>'choria', :namespace=>nil}, - {:args=>{:version=>"2.2.0"}, :name=>"inifile", :namespace=>"puppetlabs"}, - {:args=>{:version=>"1.0.1"}, :name=>"ruby", :namespace=>"puppetlabs"}, - {:args=>{:version=>"4.24.0"}, :name=>"stdlib", :namespace=>"puppetlabs"}, - {:args=>{:version=>"4.0.0"}, :name=>"concat", :namespace=>"puppetlabs"}, - {:args=>{:version=>"6.4.1"}, :name=>"ntp", :namespace=>"puppetlabs"}, - {:args=>{:version=>"3.1.1"}, :name=>"archive", :namespace=>"puppet"}, - {:args=> - {:git=>"https://github.com/vshn/puppet-gitlab", - :ref=>"00397b86dfb3487d9df768cbd3698d362132b5bf"}, - :name=>"gitlab", - :namespace=>nil}, - {:args=>{:git=>"https://github.com/acidprime/r10k", :tag=>"v3.1.1"}, - :name=>"r10k", - :namespace=>nil}, - {:args=> - {:branch=>"gitlab_disable_ssl_verify_support", - :git=>"https://github.com/npwalker/abrader-gms"}, - :name=>"gms", - :namespace=>nil}, - {:args=> - {:git=>"https://github.com/puppetlabs/pltraining-rbac", - :ref=>"2f60e1789a721ce83f8df061e13f8bf81cd4e4ce"}, - :name=>"rbac", - :namespace=>"pltraining"}, - {:args=> - {:branch=>"master", :git=>"https://github.com/dobbymoodge/puppet-acl.git"}, - :name=>"acl", - :namespace=>"puppet"}, - {:args=>{:branch=>"master", :git=>"https://github.com/cudgel/deploy.git"}, - :name=>"deploy", - :namespace=>nil}, - {:args=> - {:branch=>"master", :git=>"https://github.com/cudgel/puppet-dotfiles.git"}, - :name=>"dotfiles", - :namespace=>nil}, - {:args=>{:branch=>"dev", :git=>"https://github.com/cudgel/splunk.git"}, - :name=>"splunk", - :namespace=>nil}, - {:args=>{:branch=>"master", :git=>"https://github.com/voxpupuli/puppet-module.git"}, - :name=>"puppet", - :namespace=>nil}] - end + let(:puppetfile_modules) do + [{ args: { version: '0.26.2' }, name: 'choria', namespace: nil }, + { args: { version: '2.2.0' }, name: 'inifile', namespace: 'puppetlabs' }, + { args: { version: '1.0.1' }, name: 'ruby', namespace: 'puppetlabs' }, + { args: { version: '4.24.0' }, name: 'stdlib', namespace: 'puppetlabs' }, + { args: { version: '4.0.0' }, name: 'concat', namespace: 'puppetlabs' }, + { args: { version: '6.4.1' }, name: 'ntp', namespace: 'puppetlabs' }, + { args: { version: '3.1.1' }, name: 'archive', namespace: 'puppet' }, + { args: { git: 'https://github.com/vshn/puppet-gitlab', + ref: '00397b86dfb3487d9df768cbd3698d362132b5bf', }, + name: 'gitlab', + namespace: nil, }, + { args: { git: 'https://github.com/acidprime/r10k', tag: 'v3.1.1' }, + name: 'r10k', + namespace: nil, }, + { args: { branch: 'gitlab_disable_ssl_verify_support', + git: 'https://github.com/npwalker/abrader-gms', }, + name: 'gms', + namespace: nil, }, + { args: { git: 'https://github.com/puppetlabs/pltraining-rbac', + ref: '2f60e1789a721ce83f8df061e13f8bf81cd4e4ce', }, + name: 'rbac', + namespace: 'pltraining', }, + { args: { branch: 'master', git: 'https://github.com/dobbymoodge/puppet-acl.git' }, + name: 'acl', + namespace: 'puppet', }, + { args: { branch: 'master', git: 'https://github.com/cudgel/deploy.git' }, + name: 'deploy', + namespace: nil, }, + { args: { branch: 'master', git: 'https://github.com/cudgel/puppet-dotfiles.git' }, + name: 'dotfiles', + namespace: nil, }, + { args: { branch: 'dev', git: 'https://github.com/cudgel/splunk.git' }, + name: 'splunk', + namespace: nil, }, + { args: { branch: 'master', git: 'https://github.com/voxpupuli/puppet-module.git' }, + name: 'puppet', + namespace: nil, },] + end + it '#modules' do + expect(modules(puppetfile)).to eq(puppetfile_modules) + end - it '#modules' do - expect(modules(puppetfile)).to eq(puppetfile_modules) - end + it '#git_modules' do + expected = [{ args: { git: 'https://github.com/vshn/puppet-gitlab', + ref: '00397b86dfb3487d9df768cbd3698d362132b5bf', }, + name: 'gitlab', + namespace: nil, }, + { args: { git: 'https://github.com/acidprime/r10k', tag: 'v3.1.1' }, + name: 'r10k', + namespace: nil, }, + { args: { branch: 'gitlab_disable_ssl_verify_support', + git: 'https://github.com/npwalker/abrader-gms', }, + name: 'gms', + namespace: nil, }, + { args: { git: 'https://github.com/puppetlabs/pltraining-rbac', + ref: '2f60e1789a721ce83f8df061e13f8bf81cd4e4ce', }, + name: 'rbac', + namespace: 'pltraining', }, + { args: { branch: 'master', git: 'https://github.com/dobbymoodge/puppet-acl.git' }, + name: 'acl', + namespace: 'puppet', }, + { args: { branch: 'master', git: 'https://github.com/cudgel/deploy.git' }, + name: 'deploy', + namespace: nil, }, + { args: { branch: 'master', git: 'https://github.com/cudgel/puppet-dotfiles.git' }, + name: 'dotfiles', + namespace: nil, }, + { args: { branch: 'dev', git: 'https://github.com/cudgel/splunk.git' }, + name: 'splunk', + namespace: nil, }, + { args: { branch: 'master', git: 'https://github.com/voxpupuli/puppet-module.git' }, + name: 'puppet', + namespace: nil, },] + expect(git_modules(puppetfile)).to eq(expected) + end - it '#git_modules' do - expected = [{:args=> - {:git=>"https://github.com/vshn/puppet-gitlab", - :ref=>"00397b86dfb3487d9df768cbd3698d362132b5bf"}, - :name=>"gitlab", - :namespace=>nil}, - {:args=>{:git=>"https://github.com/acidprime/r10k", :tag=>"v3.1.1"}, - :name=>"r10k", - :namespace=>nil}, - {:args=> - {:branch=>"gitlab_disable_ssl_verify_support", - :git=>"https://github.com/npwalker/abrader-gms"}, - :name=>"gms", - :namespace=>nil}, - {:args=> - {:git=>"https://github.com/puppetlabs/pltraining-rbac", - :ref=>"2f60e1789a721ce83f8df061e13f8bf81cd4e4ce"}, - :name=>"rbac", - :namespace=>"pltraining"}, - {:args=> - {:branch=>"master", :git=>"https://github.com/dobbymoodge/puppet-acl.git"}, - :name=>"acl", - :namespace=>"puppet"}, - {:args=>{:branch=>"master", :git=>"https://github.com/cudgel/deploy.git"}, - :name=>"deploy", - :namespace=>nil}, - {:args=> - {:branch=>"master", :git=>"https://github.com/cudgel/puppet-dotfiles.git"}, - :name=>"dotfiles", - :namespace=>nil}, - {:args=>{:branch=>"dev", :git=>"https://github.com/cudgel/splunk.git"}, - :name=>"splunk", - :namespace=>nil}, - {:args=>{:branch=>"master", :git=>"https://github.com/voxpupuli/puppet-module.git"}, - :name=>"puppet", - :namespace=>nil}] - expect(git_modules(puppetfile)).to eq(expected) - end + it '#parse_modules_args' do + data = " 'puppet-acl', :git => 'https://github.com/dobbymoodge/puppet-acl.git', :branch => 'master'" + expect(parse_module_args(data)).to eq({ args: { branch: 'master', + git: 'https://github.com/dobbymoodge/puppet-acl.git', }, + name: 'acl', namespace: 'puppet', }) + end - it '#parse_modules_args' do - data = " 'puppet-acl', :git => 'https://github.com/dobbymoodge/puppet-acl.git', :branch => 'master'" - expect(parse_module_args(data)).to eq({:args=>{:branch=>"master", - :git=>"https://github.com/dobbymoodge/puppet-acl.git"}, - :name=>"acl", :namespace=>"puppet"}) - end - - it '#parse_modules_args when empty' do - data = "" - expect(parse_module_args(data)).to eq({}) - end + it '#parse_modules_args when empty' do + data = '' + expect(parse_module_args(data)).to eq({}) + end end diff --git a/spec/ra10ke/validate_spec.rb b/spec/ra10ke/validate_spec.rb index aa59f07..11ef461 100644 --- a/spec/ra10ke/validate_spec.rb +++ b/spec/ra10ke/validate_spec.rb @@ -13,7 +13,7 @@ File.join(fixtures_dir, 'Puppetfile') end - before(:each) do + before do allow_any_instance_of(Ra10ke::GitRepo).to receive(:valid_ref?).and_return(true) allow_any_instance_of(Ra10ke::GitRepo).to receive(:valid_url?).and_return(true) end @@ -25,7 +25,7 @@ it '#data is a hash with values' do keys = instance.all_modules.first.values - expect(keys).to eq(["ntp", "https://github.com/puppetlabs/puppetlabs-ntp", "81b34c6", true, true, "👍"]) + expect(keys).to eq(['ntp', 'https://github.com/puppetlabs/puppetlabs-ntp', '81b34c6', true, true, '👍']) end end @@ -38,7 +38,7 @@ File.join(fixtures_dir, 'Puppetfile_with_bad_refs') end - before(:each) do + before do working = double('Ra1ke::GitRepo', url: 'https://github.com/vshn/puppet-gitlab') expect(working).to receive(:valid_ref?).with('00397b86dfb3487d9df768cbd3698d362132b5bf').and_return(false) expect(working).to receive(:valid_commit?).with('00397b86dfb3487d9df768cbd3698d362132b5bf').and_return(true) @@ -62,7 +62,7 @@ end describe 'control_branch' do - before(:each) do + before do ENV.delete 'CONTROL_BRANCH' ENV.delete 'CONTROL_BRANCH_FALLBACK' end @@ -99,7 +99,9 @@ it 'correctly falls back to fallback if no current branch but default branch' do ENV['CONTROL_BRANCH_FALLBACK'] = 'env-control_branch_fallback' allow(Ra10ke::GitRepo).to receive(:current_branch).and_return(nil) - expect(instance.all_modules.find { |m| m[:name] == 'hiera_controlwithdefault' }[:ref]).to eq('env-control_branch_fallback') + expect(instance.all_modules.find do |m| + m[:name] == 'hiera_controlwithdefault' + end[:ref]).to eq('env-control_branch_fallback') end it 'correctly falls back to default_branch if no current branch with override' do @@ -138,6 +140,6 @@ keys = instance.all_modules.first.values expect(keys).to eq(['gitlab', 'https://github.com/vshn/puppet-gitlab', - '00397b86dfb3487d9df768cbd3698d362132b5bf', true, true, '👍']) + '00397b86dfb3487d9df768cbd3698d362132b5bf', true, true, '👍',]) end end diff --git a/spec/ra10ke_spec.rb b/spec/ra10ke_spec.rb index 6f68739..4833373 100644 --- a/spec/ra10ke_spec.rb +++ b/spec/ra10ke_spec.rb @@ -63,13 +63,13 @@ File.join(fixtures_dir, 'Puppetfile_with_bad_refs') end - # I suspect rake is caching something here and the puppetfile is - # not being sent correctly as it is not using the file I specify. + # I suspect rake is caching something here and the puppetfile is + # not being sent correctly as it is not using the file I specify. # The output should be different. # Testing this by itself works it '#run_validate_task' do task2 = instance.define_task_validate(args) - expect(task2.invoke).to be nil + expect(task2.invoke).to be_nil end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f4ca817..df0a496 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,5 +27,5 @@ require 'rspec/core' def fixtures_dir - File.join(__dir__, 'fixtures') + File.join(__dir__, 'fixtures') end