From a2c378ce042cb67b46c69b72f7d0e8e717cdcf76 Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Thu, 21 Oct 2021 19:27:41 -0400 Subject: [PATCH] test profile CLI commands --- lib/cspace_config_untangler.rb | 13 +++---- .../cli/helpers/profile_getter.rb | 1 + .../cli/profiles_cli.rb | 35 +++++++++---------- .../profile_comparison.rb | 6 +++- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/lib/cspace_config_untangler.rb b/lib/cspace_config_untangler.rb index fb4afb43..4a0b002f 100644 --- a/lib/cspace_config_untangler.rb +++ b/lib/cspace_config_untangler.rb @@ -60,18 +60,19 @@ def app_dir setting :templatedir, default: default_templatedir, reader: true setting :mapperdir, default: default_mapperdir, reader: true - config_file_names = Dir.new(default_configdir).children - .reject{ |e| e['readable'] } - .reject{ |e| e == '.keep' } - .map{ |fn| File.basename(fn).sub('.json', '') } - - setting :profiles, default: config_file_names, reader: true setting :main_profile_name, default: default_main_profile_name, reader: true setting :log, default: logger, reader: true setting :mapper_uri_base, default: default_mapper_uri_base, reader: true + def profiles + Dir.new(CCU.configdir).children + .reject{ |e| e['readable'] } + .reject{ |e| e == '.keep' } + .map{ |fn| File.basename(fn).sub('.json', '') } + end + def main_profile Pathname.new(CCU.configdir) .children(false) diff --git a/lib/cspace_config_untangler/cli/helpers/profile_getter.rb b/lib/cspace_config_untangler/cli/helpers/profile_getter.rb index 55ce793a..4e245cdd 100644 --- a/lib/cspace_config_untangler/cli/helpers/profile_getter.rb +++ b/lib/cspace_config_untangler/cli/helpers/profile_getter.rb @@ -3,6 +3,7 @@ module Cli module Helpers class ProfileGetter def self.call(opt_profiles) + return [CCU.main_profile] unless opt_profiles return [CCU.main_profile] if opt_profiles.empty? return self.all_profiles if opt_profiles == 'all' diff --git a/lib/cspace_config_untangler/cli/profiles_cli.rb b/lib/cspace_config_untangler/cli/profiles_cli.rb index db9abd7c..c4ac4408 100644 --- a/lib/cspace_config_untangler/cli/profiles_cli.rb +++ b/lib/cspace_config_untangler/cli/profiles_cli.rb @@ -4,15 +4,15 @@ module CspaceConfigUntangler module Cli class ProfilesCli < Thor include CCU::Cli::Helpers - desc 'all', 'Print the names of all known profiles' + desc 'all', 'Print the names of all known profiles to screen' def all - puts [CCU.main_profile, CCU.profiles].flatten.uniq.sort + say([CCU.main_profile, CCU.profiles].flatten.uniq.sort.join("\n")) end - desc 'check', 'Print the names of profiles that will be processed' + desc 'check', 'Prints to screen the names of profiles that will be processed' def check profiles = get_profiles - puts profiles + say(profiles.join("\n")) end desc 'compare', 'Outputs a comparison of two profiles in CSV format' @@ -33,20 +33,17 @@ def check LONGDESC option :output, desc: 'Path to directory in which to output file. Name of the file is hardcoded, using the names of the profiles.', default: CCU::datadir, aliases: '-o' def compare - if options[:profiles] == 'all' - profiles = get_profiles - else - profiles = options[:profiles].split(',').map{ |p| p.strip } - end + profiles = get_profiles if profiles.length > 2 - puts "Can only compare two profiles at a time" - exit + say('Can only compare two profiles at a time') elsif profiles.length == 1 - puts "Needs two profiles to compare" - exit + say('Needs two profiles to compare') else - CCU::ProfileComparison.new(profiles, options[:output]).write_csv + comparer = CCU::ProfileComparison.new(profiles, options[:output]) + comparer.write_csv + message = "#{comparer.summary}\n\nWrote detailed report to: #{comparer.output}" + say(message) end end @@ -70,18 +67,20 @@ def by_extension desc 'main', 'Print the name of the main profile' def main - puts CCU.main_profile + say(CCU.main_profile) end desc 'readable', 'Reformats (in place) JSON profile configs so that they are not one very long line. Non-destructive if run over JSON multiple times.' def readable + message = [] get_profiles.each{ |p| - puts "Reformatting #{p} config" - profile = CCU::Profile.new(profile: p).config + message << "Reformatting #{p} config" + oldprofile = JSON.parse(File.read("#{CCU.configdir}/#{p}.json")) File.open("#{CCU.configdir}/#{p}.json", 'w'){ |f| - f.puts JSON.pretty_generate(profile) + f.puts JSON.pretty_generate(oldprofile) } } + say(message.join("\n")) end end end diff --git a/lib/cspace_config_untangler/profile_comparison.rb b/lib/cspace_config_untangler/profile_comparison.rb index 488e4837..d60ffdcd 100644 --- a/lib/cspace_config_untangler/profile_comparison.rb +++ b/lib/cspace_config_untangler/profile_comparison.rb @@ -1,5 +1,6 @@ module CspaceConfigUntangler class ProfileComparison + attr_reader :output def initialize(profilearray, outputdir) profiles = profilearray.map{ |p| CCU::Profile.new(profile: p) } @profiles = profiles.map{ |p| p.name } @@ -7,7 +8,6 @@ def initialize(profilearray, outputdir) @fields = profiles.map{ |p| p.fields.map{ |f| f.clean} }.map{ |p| by_path(p) } @combined = combined_fields @diff = diff_combined - @diff.each{ |k, arr| puts "#{k}: #{arr.size}" } end def write_csv @@ -19,6 +19,10 @@ def write_csv fields.each{ |f| csv << f.to_csv } } end + + def summary + @diff.map{ |k, arr| "#{k}: #{arr.size}" }.join("\n") + end private