From 1ecb483dbdd893eb6085adb008859bb89ee8c9cc Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Fri, 4 Dec 2015 23:38:45 +0100 Subject: [PATCH 1/4] Added unit test for validate-strings-file command. --- lib/twine/runner.rb | 2 +- test/test_validate_strings_file.rb | 45 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/test_validate_strings_file.rb diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb index 3b91d19..14ef211 100644 --- a/lib/twine/runner.rb +++ b/lib/twine/runner.rb @@ -211,7 +211,7 @@ module Twine errors << "None of your strings have tags." elsif keys_without_tags.length > 0 error_body = keys_without_tags.to_a.join("\n ") - errors << "Found strings(s) without tags:\n #{error_body}" + errors << "Found strings without tags:\n #{error_body}" end if errors.length > 0 diff --git a/test/test_validate_strings_file.rb b/test/test_validate_strings_file.rb new file mode 100644 index 0000000..f6274c0 --- /dev/null +++ b/test/test_validate_strings_file.rb @@ -0,0 +1,45 @@ +require 'command_test_case' + +class TestValidateStringsFile < CommandTestCase + def setup + super + @options = { strings_file: 'input.txt' } + + @twine_file = build_twine_file 'en' do + add_section 'Section 1' do + add_row key1: 'value1', tags: ['tag1'] + add_row key2: 'value2', tags: ['tag1'] + end + + add_section 'Section 2' do + add_row key3: 'value3', tags: ['tag1', 'tag2'] + add_row key4: 'value4', tags: ['tag2'] + end + end + end + + def random_row + @twine_file.strings_map[@twine_file.strings_map.keys.sample] + end + + def test_recognizes_valid_file + Twine::Runner.new(@options, @twine_file).validate_strings_file + assert_equal "input.txt is valid.\n", Twine::stdout.string + end + + def test_reports_duplicate_keys + @twine_file.sections[0].rows << random_row + + assert_raises Twine::Error do + Twine::Runner.new(@options, @twine_file).validate_strings_file + end + end + + def test_reports_missing_tags + random_row.tags.clear + + assert_raises Twine::Error do + Twine::Runner.new(@options, @twine_file).validate_strings_file + end + end +end From 0f9bd0dcba0217d23bbf6705746aaaaccd89afe6 Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Sat, 5 Dec 2015 00:25:29 +0100 Subject: [PATCH 2/4] 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 From 7bcab89373053497cecacac5e732f262e447dc29 Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Sat, 5 Dec 2015 00:32:24 +0100 Subject: [PATCH 3/4] Cleanup of validate_strings_file. --- lib/twine/runner.rb | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb index c40b5ac..3b9ec49 100644 --- a/lib/twine/runner.rb +++ b/lib/twine/runner.rb @@ -191,42 +191,33 @@ module Twine section.rows.each do |row| total_strings += 1 - if all_keys.include? row.key - duplicate_keys.add(row.key) - else - all_keys.add(row.key) - end + duplicate_keys.add(row.key) if all_keys.include? row.key + all_keys.add(row.key) - if row.tags == nil || row.tags.length == 0 - keys_without_tags.add(row.key) - end + keys_without_tags.add(row.key) if row.tags == nil or row.tags.length == 0 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}" + join_keys = lambda { |set| set.map { |k| " " + k }.join("\n") } + + unless duplicate_keys.empty? + errors << "Found duplicate string key(s):\n#{join_keys.call(duplicate_keys)}" end if keys_without_tags.length == total_strings errors << "None of your strings have tags." elsif keys_without_tags.length > 0 - error_body = keys_without_tags.to_a.join("\n ") - errors << "Found strings without tags:\n #{error_body}" + errors << "Found strings without tags:\n#{join_keys.call(keys_without_tags)}" 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 + raise Twine::Error.new errors.join("\n\n") unless errors.empty? Twine::stdout.puts "#{@options[:strings_file]} is valid." end From b73045b33be390267f180a90c291a163aae82341 Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Sat, 5 Dec 2015 00:48:19 +0100 Subject: [PATCH 4/4] Fixed tests for ruby 1.9. --- test/test_validate_strings_file.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test_validate_strings_file.rb b/test/test_validate_strings_file.rb index f1339f2..fcb6a27 100644 --- a/test/test_validate_strings_file.rb +++ b/test/test_validate_strings_file.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + require 'command_test_case' class TestValidateStringsFile < CommandTestCase @@ -44,7 +46,7 @@ class TestValidateStringsFile < CommandTestCase end def test_reports_invalid_characters_in_keys - random_row.key[0] = "!?;:,^`´'\"\\|/(){}[]~-+*=#$%".chars.sample + random_row.key[0] = "!?;:,^`´'\"\\|/(){}[]~-+*=#$%".chars.to_a.sample assert_raises Twine::Error do Twine::Runner.new(@options, @twine_file).validate_strings_file