From a0141090d3a03d8843bad65f86efacfec0cb4920 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 12 Apr 2022 18:24:02 -0400 Subject: [PATCH] Validate config values for arrays and hashes Signed-off-by: Alexandre Terrasa --- lib/tapioca/helpers/config_helper.rb | 12 ++++++++++++ spec/tapioca/cli/config_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/tapioca/helpers/config_helper.rb b/lib/tapioca/helpers/config_helper.rb index 0cedc1a50..13d0f2bce 100644 --- a/lib/tapioca/helpers/config_helper.rb +++ b/lib/tapioca/helpers/config_helper.rb @@ -126,6 +126,18 @@ def validate_config_options(command_options, config_key, config_options) error_msg = "invalid value for option `#{config_option_key}` for key `#{config_key}` - expected " \ "`#{command_option.type.capitalize}` but found #{config_option_value_type.capitalize}" next build_error(error_msg) unless config_option_value_type == command_option.type + + case config_option_value_type + when :array + error_msg = "invalid value for option `#{config_option_key}` for key `#{config_key}` - expected " \ + "`Array[String]` but found `#{config_option_value}`" + next build_error(error_msg) unless config_option_value.all? { |v| v.is_a?(String) } + when :hash + error_msg = "invalid value for option `#{config_option_key}` for key `#{config_key}` - expected " \ + "`Hash[String, String]` but found `#{config_option_value}`" + all_strings = (config_option_value.keys + config_option_value.values).all? { |v| v.is_a?(String) } + next build_error(error_msg) unless all_strings + end end.compact end diff --git a/spec/tapioca/cli/config_spec.rb b/spec/tapioca/cli/config_spec.rb index f18ee1975..7d7101b9e 100644 --- a/spec/tapioca/cli/config_spec.rb +++ b/spec/tapioca/cli/config_spec.rb @@ -93,6 +93,33 @@ class ConfigTest < SpecWithProject refute_success_status(result) end + it "validates invalid configuration option values inside arrays and hashes" do + @project.write("sorbet/tapioca/config.yml", <<~YAML) + dsl: + only: [1, false] + exclude: [1, false] + gem: + exclude: [1, false] + typed_overrides: + msgpack: false + YAML + + result = @project.tapioca("gem") + + assert_equal(<<~ERR, result.err) + + Configuration file sorbet/tapioca/config.yml has the following errors: + + - invalid value for option only for key dsl - expected Array[String] but found [1, false] + - invalid value for option exclude for key dsl - expected Array[String] but found [1, false] + - invalid value for option exclude for key gem - expected Array[String] but found [1, false] + - invalid value for option typed_overrides for key gem - expected Hash[String, String] but found {\"msgpack\"=>false} + ERR + + assert_empty_stdout(result) + refute_success_status(result) + end + it "validates unknown configuration keys, options, and invalid values" do @project.write("sorbet/tapioca/config.yml", <<~YAML) gem: