From 0f9bd0dcba0217d23bbf6705746aaaaccd89afe6 Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Sat, 5 Dec 2015 00:25:29 +0100 Subject: [PATCH] Closed #84 by checking for invalid characters in keys in validate-strings-file. --- lib/twine/runner.rb | 12 +++++++++++- test/test_validate_strings_file.rb | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb index 14ef211..c40b5ac 100644 --- a/lib/twine/runner.rb +++ b/lib/twine/runner.rb @@ -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 diff --git a/test/test_validate_strings_file.rb b/test/test_validate_strings_file.rb index f6274c0..f1339f2 100644 --- a/test/test_validate_strings_file.rb +++ b/test/test_validate_strings_file.rb @@ -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