Merge pull request #133 from sebastianludwig/duplicate_strings
Duplicate strings
This commit is contained in:
commit
3c1a931404
7 changed files with 150 additions and 17 deletions
|
@ -89,6 +89,12 @@ module Twine
|
|||
end
|
||||
options[:output_encoding] = e
|
||||
end
|
||||
opts.on('--validate', 'Validate the strings file before formatting it') do
|
||||
options[:validate] = true
|
||||
end
|
||||
opts.on('-p', '--pedantic', 'When validating a strings file, perform additional checks that go beyond pure validity (like presence of tags)') do
|
||||
options[:pedantic] = true
|
||||
end
|
||||
opts.on('-h', '--help', 'Show this message.') do |h|
|
||||
puts opts.help
|
||||
exit
|
||||
|
|
|
@ -43,6 +43,8 @@ module Twine
|
|||
end
|
||||
|
||||
def generate_string_file
|
||||
validate_strings_file if @options[:validate]
|
||||
|
||||
lang = nil
|
||||
lang = @options[:languages][0] if @options[:languages]
|
||||
|
||||
|
@ -50,6 +52,8 @@ module Twine
|
|||
end
|
||||
|
||||
def generate_all_string_files
|
||||
validate_strings_file if @options[:validate]
|
||||
|
||||
if !File.directory?(@options[:output_path])
|
||||
if @options[:create_folders]
|
||||
FileUtils.mkdir_p(@options[:output_path])
|
||||
|
@ -99,6 +103,8 @@ module Twine
|
|||
end
|
||||
|
||||
def generate_loc_drop
|
||||
validate_strings_file if @options[:validate]
|
||||
|
||||
require_rubyzip
|
||||
|
||||
if File.file?(@options[:output_path])
|
||||
|
@ -179,10 +185,12 @@ module Twine
|
|||
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
|
||||
errors << "Found strings without tags:\n#{join_keys.call(keys_without_tags)}"
|
||||
if @options[:pedantic]
|
||||
if keys_without_tags.length == total_strings
|
||||
errors << "None of your strings have tags."
|
||||
elsif keys_without_tags.length > 0
|
||||
errors << "Found strings without tags:\n#{join_keys.call(keys_without_tags)}"
|
||||
end
|
||||
end
|
||||
|
||||
unless invalid_keys.empty?
|
||||
|
|
|
@ -21,6 +21,11 @@ class CLITestCase < TwineTestCase
|
|||
assert_equal @strings_file_path, @options[:strings_file]
|
||||
end
|
||||
|
||||
def test_pedantic
|
||||
parse "validate-strings-file #{@strings_file_path} --pedantic"
|
||||
assert @options[:pedantic]
|
||||
end
|
||||
|
||||
def test_missing_parameter
|
||||
assert_raises Twine::Error do
|
||||
parse 'validate-strings-file'
|
||||
|
@ -49,6 +54,11 @@ class CLITestCase < TwineTestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_validate
|
||||
parse "generate-string-file #{@strings_file_path} #{@output_path} --validate"
|
||||
assert @options[:validate]
|
||||
end
|
||||
|
||||
def test_extra_parameter
|
||||
assert_raises Twine::Error do
|
||||
parse 'generate-string-file strings output extra'
|
||||
|
@ -77,6 +87,11 @@ class CLITestCase < TwineTestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_validate
|
||||
parse "generate-all-string-files #{@strings_file_path} #{@output_dir} --validate"
|
||||
assert @options[:validate]
|
||||
end
|
||||
|
||||
def test_extra_parameter
|
||||
assert_raises Twine::Error do
|
||||
parse "generate-all-string-files strings output extra"
|
||||
|
@ -149,6 +164,11 @@ class CLITestCase < TwineTestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_validate
|
||||
parse "generate-loc-drop #{@strings_file_path} #{@output_path} --format apple --validate"
|
||||
assert @options[:validate]
|
||||
end
|
||||
|
||||
def test_extra_parameter
|
||||
assert_raises Twine::Error do
|
||||
parse "generate-loc-drop strings output extra --format apple"
|
||||
|
|
|
@ -8,13 +8,13 @@ class TestGenerateAllStringFiles < CommandTestCase
|
|||
options[:format] = 'apple'
|
||||
options[:create_folders] = create_folders
|
||||
|
||||
@twine_file = build_twine_file 'en', 'es' do
|
||||
twine_file = build_twine_file 'en', 'es' do
|
||||
add_section 'Section' do
|
||||
add_row key: 'value'
|
||||
end
|
||||
end
|
||||
|
||||
Twine::Runner.new(options, @twine_file)
|
||||
Twine::Runner.new(options, twine_file)
|
||||
end
|
||||
|
||||
def test_fails_if_output_folder_does_not_exist
|
||||
|
@ -41,4 +41,36 @@ class TestGenerateAllStringFiles < CommandTestCase
|
|||
assert File.exists?(File.join(@output_dir, 'es.lproj')), "language folder 'es.lproj' should be created"
|
||||
end
|
||||
end
|
||||
|
||||
class TestDeliberate < CommandTestCase
|
||||
def new_runner(validate)
|
||||
Dir.mkdir File.join @output_dir, 'values-en'
|
||||
|
||||
options = {}
|
||||
options[:output_path] = @output_dir
|
||||
options[:format] = 'android'
|
||||
options[:validate] = validate
|
||||
|
||||
twine_file = build_twine_file 'en' do
|
||||
add_section 'Section' do
|
||||
add_row key: 'value'
|
||||
add_row key: 'value'
|
||||
end
|
||||
end
|
||||
|
||||
Twine::Runner.new(options, twine_file)
|
||||
end
|
||||
|
||||
def test_does_not_validate_strings_file
|
||||
prepare_mock_formatter Twine::Formatters::Android
|
||||
|
||||
new_runner(false).generate_all_string_files
|
||||
end
|
||||
|
||||
def test_validates_strings_file_if_validate
|
||||
assert_raises Twine::Error do
|
||||
new_runner(true).generate_all_string_files
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -41,4 +41,34 @@ class TestGenerateLocDrop < CommandTestCase
|
|||
|
||||
@runner.generate_loc_drop
|
||||
end
|
||||
|
||||
class TestDeliberate < CommandTestCase
|
||||
def new_runner(validate)
|
||||
options = {}
|
||||
options[:output_path] = @output_path
|
||||
options[:format] = 'android'
|
||||
options[:validate] = validate
|
||||
|
||||
twine_file = build_twine_file 'en' do
|
||||
add_section 'Section' do
|
||||
add_row key: 'value'
|
||||
add_row key: 'value'
|
||||
end
|
||||
end
|
||||
|
||||
Twine::Runner.new(options, twine_file)
|
||||
end
|
||||
|
||||
def test_does_not_validate_strings_file
|
||||
prepare_mock_formatter Twine::Formatters::Android
|
||||
|
||||
new_runner(false).generate_loc_drop
|
||||
end
|
||||
|
||||
def test_validates_strings_file_if_validate
|
||||
assert_raises Twine::Error do
|
||||
new_runner(true).generate_loc_drop
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,10 +6,10 @@ class TestGenerateStringFile < CommandTestCase
|
|||
options[:output_path] = File.join(@output_dir, file) if file
|
||||
options[:languages] = language if language
|
||||
|
||||
@strings = Twine::StringsFile.new
|
||||
@strings.language_codes.concat KNOWN_LANGUAGES
|
||||
strings = Twine::StringsFile.new
|
||||
strings.language_codes.concat KNOWN_LANGUAGES
|
||||
|
||||
Twine::Runner.new(options, @strings)
|
||||
Twine::Runner.new(options, strings)
|
||||
end
|
||||
|
||||
def prepare_mock_write_file_formatter(formatter_class)
|
||||
|
@ -48,4 +48,35 @@ class TestGenerateStringFile < CommandTestCase
|
|||
|
||||
new_runner(nil, "#{random_language}.xml").generate_string_file
|
||||
end
|
||||
|
||||
class TestDeliberate < CommandTestCase
|
||||
def new_runner(validate)
|
||||
options = {}
|
||||
options[:output_path] = @output_path
|
||||
options[:languages] = ['en']
|
||||
options[:format] = 'android'
|
||||
options[:validate] = validate
|
||||
|
||||
twine_file = build_twine_file 'en' do
|
||||
add_section 'Section' do
|
||||
add_row key: 'value'
|
||||
add_row key: 'value'
|
||||
end
|
||||
end
|
||||
|
||||
Twine::Runner.new(options, twine_file)
|
||||
end
|
||||
|
||||
def test_does_not_validate_strings_file
|
||||
prepare_mock_formatter Twine::Formatters::Android
|
||||
|
||||
new_runner(false).generate_string_file
|
||||
end
|
||||
|
||||
def test_validates_strings_file_if_validate
|
||||
assert_raises Twine::Error do
|
||||
new_runner(true).generate_string_file
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -37,14 +37,6 @@ class TestValidateStringsFile < CommandTestCase
|
|||
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
|
||||
|
||||
def test_reports_invalid_characters_in_keys
|
||||
random_row.key[0] = "!?;:,^`´'\"\\|/(){}[]~-+*=#$%".chars.to_a.sample
|
||||
|
||||
|
@ -52,4 +44,18 @@ class TestValidateStringsFile < CommandTestCase
|
|||
Twine::Runner.new(@options, @twine_file).validate_strings_file
|
||||
end
|
||||
end
|
||||
|
||||
def test_does_not_reports_missing_tags_by_default
|
||||
random_row.tags.clear
|
||||
|
||||
Twine::Runner.new(@options, @twine_file).validate_strings_file
|
||||
end
|
||||
|
||||
def test_reports_missing_tags
|
||||
random_row.tags.clear
|
||||
|
||||
assert_raises Twine::Error do
|
||||
Twine::Runner.new(@options.merge(pedantic: true), @twine_file).validate_strings_file
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Reference in a new issue