From 8eccb7fa57908aa51dce964f760ca2b9e4718ee0 Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Mon, 21 May 2018 12:33:15 +0200 Subject: [PATCH 1/2] Use stderr strictly for errors and stdout for all other output (#236) --- lib/twine/cli.rb | 2 +- lib/twine/formatters/abstract.rb | 4 ++-- lib/twine/runner.rb | 6 +++--- lib/twine/twine_file.rb | 2 +- test/test_cli.rb | 4 ++-- test/test_generate_all_localization_files.rb | 4 ++-- test/test_generate_localization_archive.rb | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/twine/cli.rb b/lib/twine/cli.rb index 38f7a2f..2ea18eb 100644 --- a/lib/twine/cli.rb +++ b/lib/twine/cli.rb @@ -227,7 +227,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 diff --git a/lib/twine/formatters/abstract.rb b/lib/twine/formatters/abstract.rb index 4bd8c09..f7cb76f 100644 --- a/lib/twine/formatters/abstract.rb +++ b/lib/twine/formatters/abstract.rb @@ -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) diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb index fab6d18..33d5bfe 100644 --- a/lib/twine/runner.rb +++ b/lib/twine/runner.rb @@ -90,7 +90,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 +112,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 +148,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 diff --git a/lib/twine/twine_file.rb b/lib/twine/twine_file.rb index b000180..e875f3c 100644 --- a/lib/twine/twine_file.rb +++ b/lib/twine/twine_file.rb @@ -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 diff --git a/test/test_cli.rb b/test/test_cli.rb index 7b7147e..333db29 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -278,7 +278,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 @@ -401,7 +401,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 diff --git a/test/test_generate_all_localization_files.rb b/test/test_generate_all_localization_files.rb index 42208e1..8c1c3e0 100644 --- a/test/test_generate_all_localization_files.rb +++ b/test/test_generate_all_localization_files.rb @@ -67,7 +67,7 @@ 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 end @@ -92,7 +92,7 @@ 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 end diff --git a/test/test_generate_localization_archive.rb b/test/test_generate_localization_archive.rb index 8591376..bb07efd 100644 --- a/test/test_generate_localization_archive.rb +++ b/test/test_generate_localization_archive.rb @@ -45,7 +45,7 @@ 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 class TestValidate < CommandTest From 937c713b71ddf22678a7f2347d7056e4d6ef282d Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Mon, 21 May 2018 13:05:59 +0200 Subject: [PATCH 2/2] Add --quiet option. Closes #236. --- lib/twine/cli.rb | 13 +++++++++++- lib/twine/runner.rb | 11 ++++++++++ test/test_cli.rb | 14 +++++++++++++ test/test_generate_all_localization_files.rb | 22 ++++++++++++++++---- test/test_generate_localization_archive.rb | 9 ++++++-- 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/lib/twine/cli.rb b/lib/twine/cli.rb index 2ea18eb..89ac3ed 100644 --- a/lib/twine/cli.rb +++ b/lib/twine/cli.rb @@ -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' } diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb index 33d5bfe..fccf099 100644 --- a/lib/twine/runner.rb +++ b/lib/twine/runner.rb @@ -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) diff --git a/test/test_cli.rb b/test/test_cli.rb index 333db29..6244da6 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -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 @@ -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 @@ -432,6 +445,7 @@ class TestValidateTwineFileCLI < CLITest def test_options assert_help assert_option_developer_language + assert_option_quiet end def test_option_pedantic diff --git a/test/test_generate_all_localization_files.rb b/test/test_generate_all_localization_files.rb index 8c1c3e0..61f1c71 100644 --- a/test/test_generate_all_localization_files.rb +++ b/test/test_generate_all_localization_files.rb @@ -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 @@ -69,11 +69,18 @@ class TestGenerateAllLocalizationFiles < CommandTest new_runner(empty_twine_file).generate_all_localization_files 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 @@ -94,6 +101,13 @@ class TestGenerateAllLocalizationFiles < CommandTest 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 class TestValidate < CommandTest diff --git a/test/test_generate_localization_archive.rb b/test/test_generate_localization_archive.rb index bb07efd..075f657 100644 --- a/test/test_generate_localization_archive.rb +++ b/test/test_generate_localization_archive.rb @@ -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' @@ -48,6 +47,12 @@ class TestGenerateLocalizationArchive < CommandTest 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 def new_runner(validate) options = {}