Closed #84 by checking for invalid characters in keys in validate-strings-file.

This commit is contained in:
Sebastian Ludwig 2015-12-05 00:25:29 +01:00
parent 1ecb483dbd
commit 0f9bd0dcba
2 changed files with 19 additions and 1 deletions

View file

@ -184,7 +184,8 @@ module Twine
all_keys = Set.new
duplicate_keys = Set.new
keys_without_tags = Set.new
errors = []
invalid_keys = Set.new
valid_key_regex = /^[A-Za-z0-9_.]+$/
@strings.sections.each do |section|
section.rows.each do |row|
@ -199,9 +200,12 @@ module Twine
if row.tags == nil || row.tags.length == 0
keys_without_tags.add(row.key)
end
invalid_keys << row.key unless row.key =~ valid_key_regex
end
end
errors = []
if duplicate_keys.length > 0
error_body = duplicate_keys.to_a.join("\n ")
errors << "Found duplicate string key(s):\n #{error_body}"
@ -214,6 +218,12 @@ module Twine
errors << "Found strings without tags:\n #{error_body}"
end
join_keys = lambda { |set| set.map { |k| " " + k }.join("\n") }
unless invalid_keys.empty?
errors << "Found key(s) with invalid characters:\n#{join_keys.call(invalid_keys)}"
end
if errors.length > 0
raise Twine::Error.new errors.join("\n\n")
end

View file

@ -42,4 +42,12 @@ class TestValidateStringsFile < CommandTestCase
Twine::Runner.new(@options, @twine_file).validate_strings_file
end
end
def test_reports_invalid_characters_in_keys
random_row.key[0] = "!?;:,^`´'\"\\|/(){}[]~-+*=#$%".chars.sample
assert_raises Twine::Error do
Twine::Runner.new(@options, @twine_file).validate_strings_file
end
end
end