diff --git a/.rubocop.yml b/.rubocop.yml index 64372a56..4fb9597f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,17 @@ +# The behavior of RuboCop can be controlled via the .rubocop.yml +# configuration file. It makes it possible to enable/disable +# certain cops (checks) and to alter their behavior if they accept +# any parameters. The file can be placed either in your home +# directory or in some project directory. +# +# RuboCop will start looking for the configuration file in the directory +# where the inspected file is and continue its way up to the root directory. +# +# See https://docs.rubocop.org/rubocop/configuration + +inherit_mode: + merge: + - Exclude inherit_from: .rubocop_todo.yml AllCops: diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b213222f..6826af33 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 100 --no-offense-counts --no-auto-gen-timestamp` -# using RuboCop version 1.81.7. +# using RuboCop version 1.84.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 @@ -18,12 +18,6 @@ Layout/TrailingWhitespace: Exclude: - 'spec/stack_master/commands/status_spec.rb' -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: AllowSafeAssignment. -Lint/AssignmentInCondition: - Exclude: - - 'lib/stack_master/parameter_resolvers/latest_container.rb' - # Configuration parameters: AllowedMethods. # AllowedMethods: enums Lint/ConstantDefinitionInBlock: @@ -41,11 +35,6 @@ Lint/DuplicateMethods: - 'lib/stack_master/stack_definition.rb' - 'lib/stack_master/test_driver/cloud_formation.rb' -Lint/IneffectiveAccessModifier: - Exclude: - - 'lib/stack_master/parameter_loader.rb' - - 'lib/stack_master/template_compilers/sparkle_formation.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. # SupportedStyles: standard_error, runtime_error @@ -99,11 +88,6 @@ Lint/SelfAssignment: Exclude: - 'lib/stack_master/cli.rb' -# This cop supports safe autocorrection (--autocorrect). -Lint/SendWithMixinArgument: - Exclude: - - 'lib/stack_master/sparkle_formation/template_file.rb' - # Configuration parameters: AllowComments, AllowNil. Lint/SuppressedException: Exclude: @@ -111,21 +95,12 @@ Lint/SuppressedException: - 'lib/stack_master/stack_events/streamer.rb' - 'lib/stack_master/stack_status.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments. -Lint/UnusedBlockArgument: - Exclude: - - 'features/step_definitions/parameter_store_steps.rb' - - 'lib/stack_master/stack_differ.rb' - # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods, NotImplementedExceptions. # NotImplementedExceptions: NotImplementedError Lint/UnusedMethodArgument: Exclude: - - 'lib/stack_master/test_driver/cloud_formation.rb' - 'lib/stack_master/test_driver/s3.rb' - - 'spec/stack_master/parameter_resolver_spec.rb' - 'spec/support/aws_stubs.rb' # This cop supports safe autocorrection (--autocorrect). @@ -135,14 +110,6 @@ Lint/UselessAccessModifier: - 'lib/stack_master/parameter_loader.rb' - 'lib/stack_master/template_compilers/sparkle_formation.rb' -# This cop supports safe autocorrection (--autocorrect). -Lint/UselessAssignment: - Exclude: - - 'lib/stack_master/cli.rb' - - 'lib/stack_master/commands/tidy.rb' - - 'lib/stack_master/parameter_resolvers/latest_ami_by_tags.rb' - - 'lib/stack_master/template_compiler.rb' - # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: CheckForMethodsWithNoSideEffects. Lint/Void: @@ -176,7 +143,6 @@ Metrics/AbcSize: # AllowedMethods: refine Metrics/BlockLength: Exclude: - - '**/*.gemspec' - 'example/simple/templates/myapp_vpc.rb' - 'spec/integration/drift_spec.rb' - 'spec/stack_master/aws_driver/s3_spec.rb' @@ -320,7 +286,6 @@ Naming/AccessorMethodName: # AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS Naming/FileName: Exclude: - - 'Rakefile.rb' - 'spec/fixtures/templates/rb/cfndsl/sample-ctp-repeated.rb' - 'spec/fixtures/templates/rb/cfndsl/sample-ctp.rb' @@ -352,7 +317,6 @@ Naming/MethodParameterName: # MethodDefinitionMacros: define_method, define_singleton_method Naming/PredicatePrefix: Exclude: - - 'spec/**/*' - 'lib/stack_master/parameter_resolvers/one_password.rb' - 'lib/stack_master/sparkle_formation/compile_time/definitions_validator.rb' - 'lib/stack_master/sparkle_formation/compile_time/empty_validator.rb' @@ -405,8 +369,6 @@ Style/ClassAndModuleChildren: # Configuration parameters: AllowedConstants. Style/Documentation: Exclude: - - 'spec/**/*' - - 'test/**/*' - 'lib/stack_master.rb' - 'lib/stack_master/aws_driver/cloud_formation.rb' - 'lib/stack_master/aws_driver/s3.rb' @@ -503,6 +465,13 @@ Style/GlobalStdStream: - 'lib/stack_master/cli.rb' - 'spec/stack_master/commands/apply_spec.rb' +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: braces, no_braces +Style/HashAsLastArrayItem: + Exclude: + - 'spec/integration/drift_spec.rb' + # This cop supports unsafe autocorrection (--autocorrect-all). Style/HashTransformValues: Exclude: @@ -541,7 +510,6 @@ Style/MutableConstant: # SupportedStyles: predicate, comparison Style/NumericPredicate: Exclude: - - 'spec/**/*' - 'lib/stack_master/cli.rb' - 'lib/stack_master/commands/events.rb' - 'lib/stack_master/commands/nag.rb' diff --git a/CHANGELOG.md b/CHANGELOG.md index b54ba730..3c799240 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,12 @@ The format is based on [Keep a Changelog], and this project adheres to ## [Unreleased] +### Fixed + +- Resolve lint issues identified by RuboCop. ([#401]) + [Unreleased]: https://github.com/envato/stack_master/compare/v2.18.0...HEAD +[#401]: https://github.com/envato/stack_master/pull/401 ## [2.18.0] - 2026-02-02 diff --git a/features/step_definitions/parameter_store_steps.rb b/features/step_definitions/parameter_store_steps.rb index 384ca190..3fc425e1 100644 --- a/features/step_definitions/parameter_store_steps.rb +++ b/features/step_definitions/parameter_store_steps.rb @@ -1,5 +1,5 @@ Given(/^(?:a|the)\ SSM\ parameter(?:\ named)?\ "([^"]*)" - \ with\ value\ "([^"]*)"\ in\ region\ "([^"]*)"$/x) do |parameter_name, parameter_value, parameter_region| + \ with\ value\ "([^"]*)"\ in\ region\ "([^"]*)"$/x) do |parameter_name, parameter_value, _parameter_region| Aws.config[:ssm] = { stub_responses: { get_parameter: { diff --git a/lib/stack_master/cli.rb b/lib/stack_master/cli.rb index b170c731..81389f9f 100644 --- a/lib/stack_master/cli.rb +++ b/lib/stack_master/cli.rb @@ -263,7 +263,7 @@ def default_config_file def load_config(file) stack_file = file || default_config_file StackMaster::Config.load!(stack_file) - rescue Errno::ENOENT => e + rescue Errno::ENOENT say "Failed to load config file #{stack_file}" @kernel.exit false end @@ -301,7 +301,7 @@ def execute_stacks_command(command, args, options) end def show_other_region_candidates(config, stack_name) - candidates = config.filter(region = '', stack_name = stack_name) + candidates = config.filter('', stack_name = stack_name) return if candidates.empty? StackMaster.stdout.puts "Stack name #{stack_name} exists in regions: #{candidates.map(&:region).join(', ')}" diff --git a/lib/stack_master/commands/tidy.rb b/lib/stack_master/commands/tidy.rb index 49e3be51..0568ac72 100644 --- a/lib/stack_master/commands/tidy.rb +++ b/lib/stack_master/commands/tidy.rb @@ -5,13 +5,10 @@ class Tidy include StackMaster::Commands::TerminalHelper def perform - used_templates = [] - used_parameter_files = [] - templates = Set.new(find_templates) parameter_files = Set.new(find_parameter_files) - status = @config.stacks.each do |stack_definition| + @config.stacks.each do |stack_definition| parameter_files.subtract(stack_definition.parameter_files_from_globs) template = File.absolute_path(stack_definition.template_file_path) diff --git a/lib/stack_master/parameter_loader.rb b/lib/stack_master/parameter_loader.rb index a9baf4e3..598f392b 100644 --- a/lib/stack_master/parameter_loader.rb +++ b/lib/stack_master/parameter_loader.rb @@ -1,46 +1,48 @@ require 'active_support/core_ext/object/deep_dup' module StackMaster - class ParameterLoader + module ParameterLoader COMPILE_TIME_PARAMETERS_KEY = 'compile_time_parameters' - def self.load(parameter_files: [], parameters: {}) - StackMaster.debug 'Searching for parameter files...' - all_parameters = parameter_files.map { |file_name| load_parameters(file_name) } + [parameters] - all_parameters.each_with_object({ template_parameters: {}, compile_time_parameters: {} }) do |parameters, hash| - template_parameters = create_template_parameters(parameters) - compile_time_parameters = create_compile_time_parameters(parameters) - - merge_and_camelize(hash[:template_parameters], template_parameters) - merge_and_camelize(hash[:compile_time_parameters], compile_time_parameters) + class << self + def load(parameter_files: [], parameters: {}) + StackMaster.debug 'Searching for parameter files...' + all_parameters = parameter_files.map { |file_name| load_parameters(file_name) } + [parameters] + all_parameters.each_with_object({ template_parameters: {}, compile_time_parameters: {} }) do |parameters, hash| + template_parameters = create_template_parameters(parameters) + compile_time_parameters = create_compile_time_parameters(parameters) + + merge_and_camelize(hash[:template_parameters], template_parameters) + merge_and_camelize(hash[:compile_time_parameters], compile_time_parameters) + end end - end - private + private - def self.load_parameters(file_name) - file_exists = File.exist?(file_name) - StackMaster.debug file_exists ? " #{file_name} found" : " #{file_name} not found" - file_exists ? load_file(file_name) : {} - end + def load_parameters(file_name) + file_exists = File.exist?(file_name) + StackMaster.debug file_exists ? " #{file_name} found" : " #{file_name} not found" + file_exists ? load_file(file_name) : {} + end - def self.load_file(file_name) - YAML.load(File.read(file_name)) || {} - end + def load_file(file_name) + YAML.load(File.read(file_name)) || {} + end - def self.create_template_parameters(parameters) - parameters.deep_dup.tap do |parameters_clone| - parameters_clone.delete(COMPILE_TIME_PARAMETERS_KEY) || - parameters_clone.delete(COMPILE_TIME_PARAMETERS_KEY.camelize) + def create_template_parameters(parameters) + parameters.deep_dup.tap do |parameters_clone| + parameters_clone.delete(COMPILE_TIME_PARAMETERS_KEY) || + parameters_clone.delete(COMPILE_TIME_PARAMETERS_KEY.camelize) + end end - end - def self.create_compile_time_parameters(parameters) - (parameters[COMPILE_TIME_PARAMETERS_KEY] || parameters[COMPILE_TIME_PARAMETERS_KEY.camelize] || {}).deep_dup - end + def create_compile_time_parameters(parameters) + (parameters[COMPILE_TIME_PARAMETERS_KEY] || parameters[COMPILE_TIME_PARAMETERS_KEY.camelize] || {}).deep_dup + end - def self.merge_and_camelize(hash, parameters) - parameters.each { |key, value| hash[key.camelize] = value } + def merge_and_camelize(hash, parameters) + parameters.each { |key, value| hash[key.camelize] = value } + end end end end diff --git a/lib/stack_master/parameter_resolvers/latest_ami_by_tags.rb b/lib/stack_master/parameter_resolvers/latest_ami_by_tags.rb index af1416fd..2c5feb80 100644 --- a/lib/stack_master/parameter_resolvers/latest_ami_by_tags.rb +++ b/lib/stack_master/parameter_resolvers/latest_ami_by_tags.rb @@ -10,7 +10,7 @@ def initialize(config, stack_definition) end def resolve(value) - filters = @ami_finder.build_filters_from_string(value, prefix = 'tag') + filters = @ami_finder.build_filters_from_string(value, 'tag') @ami_finder.find_latest_ami(filters)&.image_id end end diff --git a/lib/stack_master/parameter_resolvers/latest_container.rb b/lib/stack_master/parameter_resolvers/latest_container.rb index a6d87edb..7dd01e2d 100644 --- a/lib/stack_master/parameter_resolvers/latest_container.rb +++ b/lib/stack_master/parameter_resolvers/latest_container.rb @@ -37,7 +37,7 @@ def resolve(parameters) def fetch_images(repository_name, registry_id, ecr) images = [] next_token = nil - while resp = ecr.describe_images( + while (resp = ecr.describe_images( { repository_name: repository_name, registry_id: registry_id, @@ -46,7 +46,7 @@ def fetch_images(repository_name, registry_id, ecr) tag_status: 'TAGGED' } } - ) + )) images += resp.image_details next_token = resp.next_token diff --git a/lib/stack_master/sparkle_formation/template_file.rb b/lib/stack_master/sparkle_formation/template_file.rb index f6bb232c..78b3997a 100644 --- a/lib/stack_master/sparkle_formation/template_file.rb +++ b/lib/stack_master/sparkle_formation/template_file.rb @@ -61,5 +61,5 @@ def _user_data_file(file_name, vars = {}) end end -SparkleFormation::SparkleAttribute::Aws.send(:include, StackMaster::SparkleFormation::UserDataFile) -SparkleFormation::SparkleAttribute::Aws.send(:include, StackMaster::SparkleFormation::JoinedFile) +SparkleFormation::SparkleAttribute::Aws.include StackMaster::SparkleFormation::UserDataFile +SparkleFormation::SparkleAttribute::Aws.include StackMaster::SparkleFormation::JoinedFile diff --git a/lib/stack_master/stack_differ.rb b/lib/stack_master/stack_differ.rb index a07a69fb..aeae7bd9 100644 --- a/lib/stack_master/stack_differ.rb +++ b/lib/stack_master/stack_differ.rb @@ -96,7 +96,7 @@ def output_diff def noecho_keys if @current_stack - @current_stack.parameters_with_defaults.select do |key, value| + @current_stack.parameters_with_defaults.select do |_key, value| value == '****' end.keys else diff --git a/lib/stack_master/template_compiler.rb b/lib/stack_master/template_compiler.rb index 6fcc5cc1..6394c78e 100644 --- a/lib/stack_master/template_compiler.rb +++ b/lib/stack_master/template_compiler.rb @@ -10,7 +10,7 @@ def self.compile(config, template_compiler, template_dir, template, compile_time end compiler.require_dependencies compiler.compile(template_dir, template, compile_time_parameters, compiler_options) - rescue StandardError => e + rescue StandardError raise TemplateCompilationFailed, "Failed to compile #{template}" end diff --git a/lib/stack_master/template_compilers/sparkle_formation.rb b/lib/stack_master/template_compilers/sparkle_formation.rb index 40da8900..5b66f99f 100644 --- a/lib/stack_master/template_compilers/sparkle_formation.rb +++ b/lib/stack_master/template_compilers/sparkle_formation.rb @@ -3,80 +3,82 @@ require 'stack_master/sparkle_formation/compile_time/state_builder' module StackMaster::TemplateCompilers - class SparkleFormation + module SparkleFormation CompileTime = StackMaster::SparkleFormation::CompileTime - def self.require_dependencies - require 'sparkle_formation' - require 'stack_master/sparkle_formation/template_file' - end + class << self + def require_dependencies + require 'sparkle_formation' + require 'stack_master/sparkle_formation/template_file' + end - def self.compile(template_dir, template, compile_time_parameters, compiler_options = {}) - sparkle_template = compile_sparkle_template(template_dir, template, compiler_options) - definitions = sparkle_template.parameters - validate_definitions(definitions) - validate_parameters(definitions, compile_time_parameters) + def compile(template_dir, template, compile_time_parameters, compiler_options = {}) + sparkle_template = compile_sparkle_template(template_dir, template, compiler_options) + definitions = sparkle_template.parameters + validate_definitions(definitions) + validate_parameters(definitions, compile_time_parameters) - sparkle_template.compile_time_parameter_setter do - sparkle_template.compile_state = create_state(definitions, compile_time_parameters) + sparkle_template.compile_time_parameter_setter do + sparkle_template.compile_state = create_state(definitions, compile_time_parameters) + end + + JSON.pretty_generate(sparkle_template.dump) end - JSON.pretty_generate(sparkle_template.dump) - end + private + + def compile_sparkle_template(template_dir, template, compiler_options) + sparkle_path = + if compiler_options['sparkle_path'] + File.expand_path(compiler_options['sparkle_path']) + else + template_dir + end + + collection = ::SparkleFormation::SparkleCollection.new + root_pack = ::SparkleFormation::Sparkle.new( + root: sparkle_path + ) + collection.set_root(root_pack) + if compiler_options['sparkle_packs'] + compiler_options['sparkle_packs'].each do |pack_name| + require pack_name + pack = ::SparkleFormation::SparklePack.new(name: pack_name) + collection.add_sparkle(pack) + end + end - private + if compiler_options['sparkle_pack_template'] + unless collection.templates['aws'].include? template + raise ArgumentError.new("Template #{template.inspect} not found in any sparkle pack") + end - def self.compile_sparkle_template(template_dir, template, compiler_options) - sparkle_path = - if compiler_options['sparkle_path'] - File.expand_path(compiler_options['sparkle_path']) + template_file_path = collection.templates['aws'][template].top['path'] else - template_dir + template_file_path = File.join(template_dir, template) end - collection = ::SparkleFormation::SparkleCollection.new - root_pack = ::SparkleFormation::Sparkle.new( - root: sparkle_path - ) - collection.set_root(root_pack) - if compiler_options['sparkle_packs'] - compiler_options['sparkle_packs'].each do |pack_name| - require pack_name - pack = ::SparkleFormation::SparklePack.new(name: pack_name) - collection.add_sparkle(pack) - end + sparkle_template = compile_template_with_sparkle_path(template_file_path, sparkle_path) + sparkle_template.sparkle.apply(collection) + sparkle_template end - if compiler_options['sparkle_pack_template'] - unless collection.templates['aws'].include? template - raise ArgumentError.new("Template #{template.inspect} not found in any sparkle pack") - end - - template_file_path = collection.templates['aws'][template].top['path'] - else - template_file_path = File.join(template_dir, template) + def compile_template_with_sparkle_path(template_path, sparkle_path) + ::SparkleFormation.sparkle_path = sparkle_path + ::SparkleFormation.compile(template_path, :sparkle) end - sparkle_template = compile_template_with_sparkle_path(template_file_path, sparkle_path) - sparkle_template.sparkle.apply(collection) - sparkle_template - end - - def self.compile_template_with_sparkle_path(template_path, sparkle_path) - ::SparkleFormation.sparkle_path = sparkle_path - ::SparkleFormation.compile(template_path, :sparkle) - end - - def self.validate_definitions(definitions) - CompileTime::DefinitionsValidator.new(definitions).validate - end + def validate_definitions(definitions) + CompileTime::DefinitionsValidator.new(definitions).validate + end - def self.validate_parameters(definitions, compile_time_parameters) - CompileTime::ParametersValidator.new(definitions, compile_time_parameters).validate - end + def validate_parameters(definitions, compile_time_parameters) + CompileTime::ParametersValidator.new(definitions, compile_time_parameters).validate + end - def self.create_state(definitions, compile_time_parameters) - CompileTime::StateBuilder.new(definitions, compile_time_parameters).build + def create_state(definitions, compile_time_parameters) + CompileTime::StateBuilder.new(definitions, compile_time_parameters).build + end end StackMaster::TemplateCompiler.register(:sparkle_formation, self) diff --git a/lib/stack_master/test_driver/cloud_formation.rb b/lib/stack_master/test_driver/cloud_formation.rb index baca3680..62f8797e 100644 --- a/lib/stack_master/test_driver/cloud_formation.rb +++ b/lib/stack_master/test_driver/cloud_formation.rb @@ -192,7 +192,7 @@ def delete_stack(options) @stacks.delete(stack_name) end - def validate_template(options) + def validate_template(_options) true end diff --git a/spec/stack_master/parameter_resolver_spec.rb b/spec/stack_master/parameter_resolver_spec.rb index fa9cc3e4..f63ebf6d 100644 --- a/spec/stack_master/parameter_resolver_spec.rb +++ b/spec/stack_master/parameter_resolver_spec.rb @@ -20,7 +20,7 @@ def resolve(value) Class.new do def initialize(config, region); end - def resolve(value) + def resolve(_value) raise Aws::CloudFormation::Errors::ValidationError.new(nil, "Can't find stack") end end