From fb0c4a3b616c6fca50e37c090a15811f58df87d3 Mon Sep 17 00:00:00 2001 From: Mateusz Szklarek Date: Mon, 26 Nov 2018 15:53:58 +0100 Subject: [PATCH 1/7] Extend configuration file with pretty_json parameter --- lib/babelish/commandline.rb | 1 + lib/babelish/csv2base.rb | 1 + lib/babelish/csv2json.rb | 2 +- test/babelish/test_csv2json.rb | 30 ++++++++++++++++++++++++++++++ test/data/test_pretty_json.json | 4 ++++ test/data/test_unpretty_json.json | 1 + 6 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test/data/test_pretty_json.json create mode 100644 test/data/test_unpretty_json.json diff --git a/lib/babelish/commandline.rb b/lib/babelish/commandline.rb index 29ce582..6fe0ebc 100644 --- a/lib/babelish/commandline.rb +++ b/lib/babelish/commandline.rb @@ -31,6 +31,7 @@ class Commandline < Thor method_option :ignore_lang_path, :type => :boolean, :aliases => "-I", :lazy_default => false, :desc => "Ignore the path component of langs" method_option :fetch, :type => :boolean, :desc => "Download file from Google Drive" method_option :sheet, :type => :numeric, :desc => "Index of worksheet to download. First index is 0" + method_option :pretty_json, :type => :boolean, :default => false, :desc => "Prettify your json output files" if klass[:name] == "CSV2Strings" method_option :macros_filename, :type => :boolean, :aliases => "-m", :lazy_default => false, :desc => "Filename containing defines of localized keys" end diff --git a/lib/babelish/csv2base.rb b/lib/babelish/csv2base.rb index 35df277..5419811 100644 --- a/lib/babelish/csv2base.rb +++ b/lib/babelish/csv2base.rb @@ -39,6 +39,7 @@ def initialize(filename, langs, args = {}) @csv_separator = args[:csv_separator] @ignore_lang_path = args[:ignore_lang_path] @stripping = args[:stripping] + @pretty_json = args[:pretty_json] @languages = [] @comments = {} end diff --git a/lib/babelish/csv2json.rb b/lib/babelish/csv2json.rb index 4f10130..87e107d 100644 --- a/lib/babelish/csv2json.rb +++ b/lib/babelish/csv2json.rb @@ -11,7 +11,7 @@ def language_filepaths(language) end def hash_to_output(content = {}) - return content.to_json + return @pretty_json ? JSON.pretty_generate(content) : content.to_json end def extension diff --git a/test/babelish/test_csv2json.rb b/test/babelish/test_csv2json.rb index 6038a4e..5217188 100644 --- a/test/babelish/test_csv2json.rb +++ b/test/babelish/test_csv2json.rb @@ -24,4 +24,34 @@ def test_converting_csv_to_dotstrings_one_output_option # clean up system("rm -rf ./" + single_file) end + + def test_converting_csv_to_json_with_unpretty_json + csv_file = "test/data/test_data.csv" + expected_json_filename = 'test_unpretty_json.json' + given_json_filename = 'output.json' + + expected_json = File.read('test/data/' + expected_json_filename) + converter = Babelish::CSV2JSON.new(csv_file, {'English' => "en"}, :output_basename => 'output', :pretty_json => false) + converter.convert + given_json = File.read(given_json_filename) + assert_equal(expected_json, given_json, "JSON file has incorrect format") + + # clean up + system("rm -rf ./" + given_json_filename) + end + + def test_converting_csv_to_json_with_pretty_json + csv_file = "test/data/test_data.csv" + expected_json_filename = 'test_pretty_json.json' + given_json_filename = 'output.json' + + expected_json = File.read('test/data/' + expected_json_filename) + converter = Babelish::CSV2JSON.new(csv_file, {'English' => "en"}, :output_basename => 'output', :pretty_json => true) + converter.convert + given_json = File.read(given_json_filename) + assert_equal(expected_json, given_json, "JSON file has incorrect format") + + # clean up + system("rm -rf ./" + given_json_filename) + end end diff --git a/test/data/test_pretty_json.json b/test/data/test_pretty_json.json new file mode 100644 index 0000000..81b4904 --- /dev/null +++ b/test/data/test_pretty_json.json @@ -0,0 +1,4 @@ +{ + "ERROR_HANDLER_WARNING_DISMISS": "OK", + "ANOTHER_STRING": "hello" +} \ No newline at end of file diff --git a/test/data/test_unpretty_json.json b/test/data/test_unpretty_json.json new file mode 100644 index 0000000..4bf8460 --- /dev/null +++ b/test/data/test_unpretty_json.json @@ -0,0 +1 @@ +{"ERROR_HANDLER_WARNING_DISMISS":"OK","ANOTHER_STRING":"hello"} \ No newline at end of file From 5ae5c05559aaeabe39b4decb3da4e6c925559d61 Mon Sep 17 00:00:00 2001 From: Mateusz Szklarek Date: Mon, 26 Nov 2018 15:59:05 +0100 Subject: [PATCH 2/7] Update .babelish.sample --- .babelish.sample | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.babelish.sample b/.babelish.sample index 221ea3b..ae3b3af 100644 --- a/.babelish.sample +++ b/.babelish.sample @@ -31,5 +31,6 @@ langs: # Languages to convert. i.e. English:en # ignore_lang_path: true # does not care about lang component path. i.e: en.lproj/ # sheet: 0 # Index of worksheet to download. First index is 0. # macros_filename: Babelish.h # File to output the defines of localized strings -# stripping: false # Strips values inside the spreadsheet -# csv_separator: ',' # CSV column separator character, uses ',' by default \ No newline at end of file +# stripping: false # Strips values inside the spreadsheet +# csv_separator: ',' # CSV column separator character, uses ',' by default +# pretty_json: false # Prettify your json output files From ffb7b1915b230c83edb00fd2add5299b4270d05b Mon Sep 17 00:00:00 2001 From: Mateusz Szklarek Date: Mon, 26 Nov 2018 16:20:36 +0100 Subject: [PATCH 3/7] Add alias for pretty_json option --- lib/babelish/commandline.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/babelish/commandline.rb b/lib/babelish/commandline.rb index 6fe0ebc..237628e 100644 --- a/lib/babelish/commandline.rb +++ b/lib/babelish/commandline.rb @@ -31,7 +31,7 @@ class Commandline < Thor method_option :ignore_lang_path, :type => :boolean, :aliases => "-I", :lazy_default => false, :desc => "Ignore the path component of langs" method_option :fetch, :type => :boolean, :desc => "Download file from Google Drive" method_option :sheet, :type => :numeric, :desc => "Index of worksheet to download. First index is 0" - method_option :pretty_json, :type => :boolean, :default => false, :desc => "Prettify your json output files" + method_option :pretty_json, :type => :boolean, :aliases => "-p", :default => false, :desc => "Prettify your json output files" if klass[:name] == "CSV2Strings" method_option :macros_filename, :type => :boolean, :aliases => "-m", :lazy_default => false, :desc => "Filename containing defines of localized keys" end From e58e5990c0c02e8bb66eeec7d9ee22c5ecd91018 Mon Sep 17 00:00:00 2001 From: Mateusz Szklarek Date: Mon, 26 Nov 2018 16:35:52 +0100 Subject: [PATCH 4/7] Refactor csv2base --- lib/babelish/csv2base.rb | 1 - lib/babelish/csv2json.rb | 5 +++++ test/babelish/test_csv2json.rb | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/babelish/csv2base.rb b/lib/babelish/csv2base.rb index 5419811..35df277 100644 --- a/lib/babelish/csv2base.rb +++ b/lib/babelish/csv2base.rb @@ -39,7 +39,6 @@ def initialize(filename, langs, args = {}) @csv_separator = args[:csv_separator] @ignore_lang_path = args[:ignore_lang_path] @stripping = args[:stripping] - @pretty_json = args[:pretty_json] @languages = [] @comments = {} end diff --git a/lib/babelish/csv2json.rb b/lib/babelish/csv2json.rb index 87e107d..3c434fb 100644 --- a/lib/babelish/csv2json.rb +++ b/lib/babelish/csv2json.rb @@ -2,6 +2,11 @@ module Babelish require 'json' class CSV2JSON < Csv2Base + def initialize(filename, langs, args = {}) + super + @pretty_json = args[:pretty_json] + end + def language_filepaths(language) require 'pathname' filename = @output_basename || language.code diff --git a/test/babelish/test_csv2json.rb b/test/babelish/test_csv2json.rb index 5217188..1f64fff 100644 --- a/test/babelish/test_csv2json.rb +++ b/test/babelish/test_csv2json.rb @@ -31,7 +31,7 @@ def test_converting_csv_to_json_with_unpretty_json given_json_filename = 'output.json' expected_json = File.read('test/data/' + expected_json_filename) - converter = Babelish::CSV2JSON.new(csv_file, {'English' => "en"}, :output_basename => 'output', :pretty_json => false) + converter = Babelish::CSV2JSON.new(csv_file, {'English' => "en"}, :output_basename => 'output', pretty_json: false) converter.convert given_json = File.read(given_json_filename) assert_equal(expected_json, given_json, "JSON file has incorrect format") @@ -46,7 +46,7 @@ def test_converting_csv_to_json_with_pretty_json given_json_filename = 'output.json' expected_json = File.read('test/data/' + expected_json_filename) - converter = Babelish::CSV2JSON.new(csv_file, {'English' => "en"}, :output_basename => 'output', :pretty_json => true) + converter = Babelish::CSV2JSON.new(csv_file, {'English' => "en"}, :output_basename => 'output', pretty_json: true) converter.convert given_json = File.read(given_json_filename) assert_equal(expected_json, given_json, "JSON file has incorrect format") From b8eef7ecc48f30a38d09014e9d62620d112e1341 Mon Sep 17 00:00:00 2001 From: Mateusz Szklarek Date: Mon, 26 Nov 2018 17:10:16 +0100 Subject: [PATCH 5/7] Remove default parameter from commandline.rb --- lib/babelish/commandline.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/babelish/commandline.rb b/lib/babelish/commandline.rb index 237628e..efb3329 100644 --- a/lib/babelish/commandline.rb +++ b/lib/babelish/commandline.rb @@ -31,7 +31,7 @@ class Commandline < Thor method_option :ignore_lang_path, :type => :boolean, :aliases => "-I", :lazy_default => false, :desc => "Ignore the path component of langs" method_option :fetch, :type => :boolean, :desc => "Download file from Google Drive" method_option :sheet, :type => :numeric, :desc => "Index of worksheet to download. First index is 0" - method_option :pretty_json, :type => :boolean, :aliases => "-p", :default => false, :desc => "Prettify your json output files" + method_option :pretty_json, :type => :boolean, :aliases => "-p", :desc => "Prettify your json output files" if klass[:name] == "CSV2Strings" method_option :macros_filename, :type => :boolean, :aliases => "-m", :lazy_default => false, :desc => "Filename containing defines of localized keys" end From aa36473c769c499012f3372f010cd4718f659221 Mon Sep 17 00:00:00 2001 From: Mateusz Szklarek Date: Tue, 27 Nov 2018 15:54:29 +0100 Subject: [PATCH 6/7] Apply fixes according to automatic code review --- lib/babelish/commandline.rb | 2 +- lib/babelish/csv2json.rb | 2 +- test/babelish/test_csv2json.rb | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/babelish/commandline.rb b/lib/babelish/commandline.rb index efb3329..8d8890b 100644 --- a/lib/babelish/commandline.rb +++ b/lib/babelish/commandline.rb @@ -31,7 +31,7 @@ class Commandline < Thor method_option :ignore_lang_path, :type => :boolean, :aliases => "-I", :lazy_default => false, :desc => "Ignore the path component of langs" method_option :fetch, :type => :boolean, :desc => "Download file from Google Drive" method_option :sheet, :type => :numeric, :desc => "Index of worksheet to download. First index is 0" - method_option :pretty_json, :type => :boolean, :aliases => "-p", :desc => "Prettify your json output files" + method_option :pretty_json, :type => :boolean, aliases: "-p", desc: "Prettify your json output files" if klass[:name] == "CSV2Strings" method_option :macros_filename, :type => :boolean, :aliases => "-m", :lazy_default => false, :desc => "Filename containing defines of localized keys" end diff --git a/lib/babelish/csv2json.rb b/lib/babelish/csv2json.rb index 3c434fb..c2ff577 100644 --- a/lib/babelish/csv2json.rb +++ b/lib/babelish/csv2json.rb @@ -16,7 +16,7 @@ def language_filepaths(language) end def hash_to_output(content = {}) - return @pretty_json ? JSON.pretty_generate(content) : content.to_json + @pretty_json ? JSON.pretty_generate(content) : content.to_json end def extension diff --git a/test/babelish/test_csv2json.rb b/test/babelish/test_csv2json.rb index 1f64fff..63dce26 100644 --- a/test/babelish/test_csv2json.rb +++ b/test/babelish/test_csv2json.rb @@ -27,11 +27,11 @@ def test_converting_csv_to_dotstrings_one_output_option def test_converting_csv_to_json_with_unpretty_json csv_file = "test/data/test_data.csv" - expected_json_filename = 'test_unpretty_json.json' - given_json_filename = 'output.json' + expected_json_filename = "test_unpretty_json.json" + given_json_filename = "output.json" - expected_json = File.read('test/data/' + expected_json_filename) - converter = Babelish::CSV2JSON.new(csv_file, {'English' => "en"}, :output_basename => 'output', pretty_json: false) + expected_json = File.read("test/data/" + expected_json_filename) + converter = Babelish::CSV2JSON.new(csv_file, { "English" => "en" }, output_basename: "output", pretty_json: false) converter.convert given_json = File.read(given_json_filename) assert_equal(expected_json, given_json, "JSON file has incorrect format") @@ -42,11 +42,11 @@ def test_converting_csv_to_json_with_unpretty_json def test_converting_csv_to_json_with_pretty_json csv_file = "test/data/test_data.csv" - expected_json_filename = 'test_pretty_json.json' - given_json_filename = 'output.json' + expected_json_filename = "test_pretty_json.json" + given_json_filename = "output.json" - expected_json = File.read('test/data/' + expected_json_filename) - converter = Babelish::CSV2JSON.new(csv_file, {'English' => "en"}, :output_basename => 'output', pretty_json: true) + expected_json = File.read("test/data/" + expected_json_filename) + converter = Babelish::CSV2JSON.new(csv_file, { "English" => "en" }, output_basename: "output", pretty_json: true) converter.convert given_json = File.read(given_json_filename) assert_equal(expected_json, given_json, "JSON file has incorrect format") From d7f7ca961e370f376cb2cfc35cfe9a3d856ecd3d Mon Sep 17 00:00:00 2001 From: Mateusz Szklarek Date: Tue, 27 Nov 2018 16:05:58 +0100 Subject: [PATCH 7/7] Fix formatting in test_csv2json.rb --- test/babelish/test_csv2json.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/babelish/test_csv2json.rb b/test/babelish/test_csv2json.rb index 63dce26..8cd965b 100644 --- a/test/babelish/test_csv2json.rb +++ b/test/babelish/test_csv2json.rb @@ -31,7 +31,10 @@ def test_converting_csv_to_json_with_unpretty_json given_json_filename = "output.json" expected_json = File.read("test/data/" + expected_json_filename) - converter = Babelish::CSV2JSON.new(csv_file, { "English" => "en" }, output_basename: "output", pretty_json: false) + converter = Babelish::CSV2JSON.new(csv_file, + { "English" => "en" }, + output_basename: "output", + pretty_json: false) converter.convert given_json = File.read(given_json_filename) assert_equal(expected_json, given_json, "JSON file has incorrect format") @@ -46,7 +49,10 @@ def test_converting_csv_to_json_with_pretty_json given_json_filename = "output.json" expected_json = File.read("test/data/" + expected_json_filename) - converter = Babelish::CSV2JSON.new(csv_file, { "English" => "en" }, output_basename: "output", pretty_json: true) + converter = Babelish::CSV2JSON.new(csv_file, + { "English" => "en" }, + output_basename: "output", + pretty_json: true) converter.convert given_json = File.read(given_json_filename) assert_equal(expected_json, given_json, "JSON file has incorrect format")