Merge pull request #245 from sebastianludwig/236-quite

Add quiet mode
This commit is contained in:
Sebastian Celis 2018-05-22 08:55:03 -05:00 committed by GitHub
commit 4354775577
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 19 deletions

View file

@ -78,6 +78,10 @@ module Twine
switch: ['-p', '--[no-]pedantic'],
description: 'When validating a Twine file, perform additional checks that go beyond pure validity (like presence of tags).'
},
quiet: {
switch: ['-q', '--[no-]quiet'],
description: 'Suppress all console output except error messages.'
},
tags: {
switch: ['-t', '--tags TAG1,TAG2,TAG3', Array],
description: <<-DESC,
@ -110,6 +114,7 @@ module Twine
:format,
:include,
:languages,
:quiet,
:tags,
:untagged,
:validate
@ -131,6 +136,7 @@ module Twine
:file_name,
:format,
:include,
:quiet,
:tags,
:untagged,
:validate
@ -147,6 +153,7 @@ module Twine
:developer_language,
:encoding,
:include,
:quiet,
:tags,
:untagged,
:validate
@ -164,6 +171,7 @@ module Twine
:format,
:languages,
:output_path,
:quiet,
:tags
],
option_validation: Proc.new { |options|
@ -183,6 +191,7 @@ module Twine
:encoding,
:format,
:output_path,
:quiet,
:tags
],
example: 'twine consume-all-localization-files twine.txt Resources/Locales/ --developer-language en --tags DefaultTag1,DefaultTag2'
@ -197,6 +206,7 @@ module Twine
:encoding,
:format,
:output_path,
:quiet,
:tags
],
example: 'twine consume-localization-archive twine.txt LocDrop5.zip'
@ -206,7 +216,8 @@ module Twine
arguments: [:twine_file],
optional_options: [
:developer_language,
:pedantic
:pedantic,
:quiet
],
example: 'twine validate-twine-file twine.txt'
}
@ -227,7 +238,7 @@ module Twine
mapped_command = DEPRECATED_COMMAND_MAPPINGS[command]
if mapped_command
Twine::stderr.puts "WARNING: Twine commands names have changed. `#{command}` is now `#{mapped_command}`. The old command is deprecated and will soon stop working. For more information please check the documentation at https://github.com/mobiata/twine"
Twine::stdout.puts "WARNING: Twine commands names have changed. `#{command}` is now `#{mapped_command}`. The old command is deprecated and will soon stop working. For more information please check the documentation at https://github.com/mobiata/twine"
command = mapped_command
end

View file

@ -38,7 +38,7 @@ module Twine
definition.translations[lang] = value
end
elsif @options[:consume_all]
Twine::stderr.puts "Adding new definition '#{key}' to twine file."
Twine::stdout.puts "Adding new definition '#{key}' to twine file."
current_section = @twine_file.sections.find { |s| s.name == 'Uncategorized' }
unless current_section
current_section = TwineSection.new('Uncategorized')
@ -54,7 +54,7 @@ module Twine
@twine_file.definitions_by_key[key] = current_definition
@twine_file.definitions_by_key[key].translations[lang] = value
else
Twine::stderr.puts "Warning: '#{key}' not found in twine file."
Twine::stdout.puts "WARNING: '#{key}' not found in twine file."
end
if !@twine_file.language_codes.include?(lang)
@twine_file.add_language_code(lang)

View file

@ -5,6 +5,14 @@ Twine::Plugin.new # Initialize plugins first in Runner.
module Twine
class Runner
class NullOutput
def puts(message)
end
def string
""
end
end
def self.run(args)
options = CLI.parse(args)
@ -35,6 +43,9 @@ module Twine
def initialize(options = {}, twine_file = TwineFile.new)
@options = options
@twine_file = twine_file
if @options[:quite]
Twine::stdout = NullOutput.new
end
end
def write_twine_data(path)
@ -90,7 +101,7 @@ module Twine
output = formatter.format_file(lang)
unless output
Twine::stderr.puts "Skipping file at path #{file_path} since it would not contain any translations."
Twine::stdout.puts "Skipping file at path #{file_path} since it would not contain any translations."
next
end
@ -112,7 +123,7 @@ module Twine
file_path = File.join(output_path, file_name)
output = formatter.format_file(lang)
unless output
Twine::stderr.puts "Skipping file at path #{file_path} since it would not contain any translations."
Twine::stdout.puts "Skipping file at path #{file_path} since it would not contain any translations."
next
end
@ -148,7 +159,7 @@ module Twine
output = formatter.format_file(lang)
unless output
Twine::stderr.puts "Skipping file #{file_name} since it would not contain any translations."
Twine::stdout.puts "Skipping file #{file_name} since it would not contain any translations."
next
end

View file

@ -190,7 +190,7 @@ module Twine
value = write_value(definition, dev_lang, f)
if !value && !definition.reference_key
puts "Warning: #{definition.key} does not exist in developer language '#{dev_lang}'"
Twine::stdout.puts "WARNING: #{definition.key} does not exist in developer language '#{dev_lang}'"
end
if definition.reference_key

View file

@ -82,6 +82,13 @@ class CLITest < TwineTest
assert_equal @output_path, @options[:output_path]
end
def assert_option_quiet
parse_with '--quiet'
assert @options[:quiet]
parse_with '--no-quiet'
refute @options[:quiet]
end
def assert_option_tags
# single tag
random_tag = "tag#{rand(100)}"
@ -174,6 +181,7 @@ class TestGenerateLocalizationFileCLI < CLITest
assert_option_include
assert_option_single_language
assert_raises(Twine::Error) { assert_option_multiple_languages }
assert_option_quiet
assert_option_tags
assert_option_untagged
assert_option_validate
@ -211,6 +219,7 @@ class TestGenerateAllLocalizationFilesCLI < CLITest
assert_option_encoding
assert_option_format
assert_option_include
assert_option_quiet
assert_option_tags
assert_option_untagged
assert_option_validate
@ -260,6 +269,7 @@ class TestGenerateLocalizationArchiveCLI < CLITest
assert_option_developer_language
assert_option_encoding
assert_option_include
assert_option_quiet
assert_option_tags
assert_option_untagged
assert_option_validate
@ -278,7 +288,7 @@ class TestGenerateLocalizationArchiveCLI < CLITest
def test_deprecated_command_prints_warning
parse "generate-loc-drop #{@twine_file_path} #{@output_path} --format apple"
assert_match "WARNING: Twine commands names have changed.", Twine::stderr.string
assert_match "WARNING: Twine commands names have changed.", Twine::stdout.string
end
end
@ -317,6 +327,7 @@ class TestConsumeLocalizationFileCLI < CLITest
assert_option_single_language
assert_raises(Twine::Error) { assert_option_multiple_languages }
assert_option_output_path
assert_option_quiet
assert_option_tags
end
end
@ -354,6 +365,7 @@ class TestConsumeAllLocalizationFilesCLI < CLITest
assert_option_encoding
assert_option_format
assert_option_output_path
assert_option_quiet
assert_option_tags
end
end
@ -391,6 +403,7 @@ class TestConsumeLocalizationArchiveCLI < CLITest
assert_option_encoding
assert_option_format
assert_option_output_path
assert_option_quiet
assert_option_tags
end
@ -401,7 +414,7 @@ class TestConsumeLocalizationArchiveCLI < CLITest
def test_deprecated_command_prints_warning
parse "consume-loc-drop #{@twine_file_path} #{@input_path}"
assert_match "WARNING: Twine commands names have changed.", Twine::stderr.string
assert_match "WARNING: Twine commands names have changed.", Twine::stdout.string
end
end
@ -432,6 +445,7 @@ class TestValidateTwineFileCLI < CLITest
def test_options
assert_help
assert_option_developer_language
assert_option_quiet
end
def test_option_pedantic

View file

@ -47,8 +47,8 @@ class TestGenerateAllLocalizationFiles < CommandTest
end
class TestDoNotCreateFolders < TestGenerateAllLocalizationFiles
def new_runner(twine_file = nil)
super(false, twine_file)
def new_runner(twine_file = nil, options = {})
super(false, twine_file, options)
end
def test_fails_if_output_folder_does_not_exist
@ -67,13 +67,20 @@ class TestGenerateAllLocalizationFiles < CommandTest
Dir.mkdir File.join @output_dir, 'en.lproj'
empty_twine_file = build_twine_file('en') {}
new_runner(empty_twine_file).generate_all_localization_files
assert_match "Skipping file at path", Twine::stderr.string
assert_match "Skipping file at path", Twine::stdout.string
end
def test_does_not_print_empty_file_warnings_if_quite
Dir.mkdir File.join @output_dir, 'en.lproj'
empty_twine_file = build_twine_file('en') {}
new_runner(empty_twine_file, quite: true).generate_all_localization_files
refute_match "Skipping file at path", Twine::stdout.string
end
end
class TestCreateFolders < TestGenerateAllLocalizationFiles
def new_runner(twine_file = nil)
super(true, twine_file)
def new_runner(twine_file = nil, options = {})
super(true, twine_file, options)
end
def test_creates_output_folder
@ -92,7 +99,14 @@ class TestGenerateAllLocalizationFiles < CommandTest
empty_twine_file = build_twine_file('en') {}
new_runner(empty_twine_file).generate_all_localization_files
assert_match "Skipping file at path", Twine::stderr.string
assert_match "Skipping file at path", Twine::stdout.string
end
def test_does_not_print_empty_file_warnings_if_quite
empty_twine_file = build_twine_file('en') {}
new_runner(empty_twine_file, quite: true).generate_all_localization_files
refute_match "Skipping file at path", Twine::stdout.string
end
end

View file

@ -1,8 +1,7 @@
require 'command_test'
class TestGenerateLocalizationArchive < CommandTest
def new_runner(twine_file = nil)
options = {}
def new_runner(twine_file = nil, options = {})
options[:output_path] = @output_path
options[:format] = 'apple'
@ -45,7 +44,13 @@ class TestGenerateLocalizationArchive < CommandTest
def test_prints_empty_file_warnings
empty_twine_file = build_twine_file('en') {}
new_runner(empty_twine_file).generate_localization_archive
assert_match "Skipping file", Twine::stderr.string
assert_match "Skipping file", Twine::stdout.string
end
def test_does_not_print_empty_file_warnings_if_quite
empty_twine_file = build_twine_file('en') {}
new_runner(empty_twine_file, quite: true).generate_localization_archive
refute_match "Skipping file", Twine::stdout.string
end
class TestValidate < CommandTest