Skip to content

Commit

Permalink
Merge pull request #898 from Shopify/at-check-config-values
Browse files Browse the repository at this point in the history
Validate config values for arrays and hashes
  • Loading branch information
Morriar committed Apr 13, 2022
2 parents cadc05f + a014109 commit 3ab0f4e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/tapioca/helpers/config_helper.rb
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions spec/tapioca/cli/config_spec.rb
Expand Up @@ -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:
Expand Down

0 comments on commit 3ab0f4e

Please sign in to comment.