From ed31386e3e4edb7e3f5abfd0f86a58d5e6ec2528 Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Mon, 21 Oct 2024 13:51:22 +0300 Subject: [PATCH] Fix errors --- lib/command/terraform/generate.rb | 8 +++----- lib/core/terraform_config/generator.rb | 4 +++- spec/command/terraform/generate_spec.rb | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/command/terraform/generate.rb b/lib/command/terraform/generate.rb index f3e9dfcf..6f650fb5 100644 --- a/lib/command/terraform/generate.rb +++ b/lib/command/terraform/generate.rb @@ -34,7 +34,7 @@ def generate_common_configs def generate_app_configs Array(config.app || config.apps.keys).each do |app| - config.instance_variable_set(:@app, app) + config.instance_variable_set(:@app, app.to_s) generate_app_config end end @@ -43,12 +43,10 @@ def generate_app_config terraform_app_dir = recreate_terraform_app_dir templates.each do |template| - # TODO: Raise error i/o ignoring invalid template kind after all template kinds are supported - next unless TerraformConfig::Generator::SUPPORTED_TEMPLATE_KINDS.include?(template["kind"]) - generator = TerraformConfig::Generator.new(config: config, template: template) - File.write(terraform_app_dir.join(generator.filename), generator.tf_config.to_tf, mode: "a+") + rescue TerraformConfig::Generator::InvalidTemplateError => e + Shell.warn(e.message) end end diff --git a/lib/core/terraform_config/generator.rb b/lib/core/terraform_config/generator.rb index 7357ba74..25b3ebc0 100644 --- a/lib/core/terraform_config/generator.rb +++ b/lib/core/terraform_config/generator.rb @@ -4,6 +4,8 @@ module TerraformConfig class Generator SUPPORTED_TEMPLATE_KINDS = %w[gvc secret identity policy volumeset].freeze + InvalidTemplateError = Class.new(ArgumentError) + attr_reader :config, :template def initialize(config:, template:) @@ -27,7 +29,7 @@ def tf_config def validate_template_kind! return if SUPPORTED_TEMPLATE_KINDS.include?(kind) - raise ArgumentError, "Unsupported template kind: #{kind}" + raise InvalidTemplateError, "Unsupported template kind: #{kind}" end def config_class diff --git a/spec/command/terraform/generate_spec.rb b/spec/command/terraform/generate_spec.rb index bcd58509..b5116d23 100644 --- a/spec/command/terraform/generate_spec.rb +++ b/spec/command/terraform/generate_spec.rb @@ -86,6 +86,24 @@ end end + context "when InvalidTemplateError is raised" do + before do + allow_any_instance_of(TerraformConfig::Generator).to receive(:tf_config).and_raise( # rubocop:disable RSpec/AnyInstance + TerraformConfig::Generator::InvalidTemplateError, "Invalid template: error message" + ) + end + + it "generates common config files and warns about invalid template" do + config_file_paths.each { |config_file_path| expect(config_file_path).not_to exist } + + expect(result[:status]).to eq(0) + expect(result[:stderr]).to include("Invalid template: error message") + + expect(common_config_files).to all(exist) + app_config_files.each { |config_file_path| expect(config_file_path).not_to exist } + end + end + def config_file_paths common_config_files + app_config_files end