Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate config values for arrays and hashes #898

Merged
merged 1 commit into from Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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