From 47270bdbb31935aa74974571ab48cbfac340320b Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sun, 22 Jan 2023 20:07:58 +0100 Subject: [PATCH] Move UnixPlatform#simple_table into Announcer The #simple_table method is only used inside Announcer and is not platform-dependent. This changes moves that method to Announcer and makes it private. Specs for #simple_table are updated to test SimpleTable directly instead. The implementation of SimpleTable is adjusted to only take one option. --- lib/aruba/platforms/announcer.rb | 9 +++- lib/aruba/platforms/simple_table.rb | 18 +++---- lib/aruba/platforms/unix_platform.rb | 6 --- spec/aruba/platforms/simple_table_spec.rb | 57 ++++++++++++++--------- 4 files changed, 49 insertions(+), 41 deletions(-) diff --git a/lib/aruba/platforms/announcer.rb b/lib/aruba/platforms/announcer.rb index 2968263f..52d69faf 100644 --- a/lib/aruba/platforms/announcer.rb +++ b/lib/aruba/platforms/announcer.rb @@ -2,6 +2,7 @@ require "shellwords" require "aruba/colorizer" +require "aruba/platforms/simple_table" Aruba::Colorizer.coloring = false if !$stdout.tty? && !ENV.key?("AUTOTEST") @@ -82,6 +83,10 @@ def initialize private + def simple_table(hash, opts = {}) + SimpleTable.new(hash, opts).to_s + end + def after_init output_format :changed_configuration, proc { |n, v| format("# %s = %s", n, v) } output_format :changed_environment, @@ -93,7 +98,7 @@ def after_init output_format :full_environment, proc { |h| format("<<-ENVIRONMENT\n%s\nENVIRONMENT", - Aruba.platform.simple_table(h)) + simple_table(h)) } output_format :modified_environment, proc { |n, v| format("$ export %s=%s", n, Shellwords.escape(v)) } @@ -109,7 +114,7 @@ def after_init output_format :command_filesystem_status, proc { |status| format("<<-COMMAND FILESYSTEM STATUS\n%s\nCOMMAND FILESYSTEM STATUS", - Aruba.platform.simple_table(status.to_h, sort: false)) + simple_table(status.to_h, sort: false)) } end diff --git a/lib/aruba/platforms/simple_table.rb b/lib/aruba/platforms/simple_table.rb index ffe3c921..698d7e96 100644 --- a/lib/aruba/platforms/simple_table.rb +++ b/lib/aruba/platforms/simple_table.rb @@ -6,21 +6,13 @@ module Aruba module Platforms # Generate simple table class SimpleTable - private - - attr_reader :hash, :opts - - public - # Create # # @param [Hash] hash # Input - def initialize(hash, opts) + def initialize(hash, sort: true) @hash = hash - @opts = { - sort: true - }.merge opts + @sort = sort end # Generate table @@ -37,12 +29,16 @@ def to_s format("# %-*s => %s", name_size, k, v) end - if opts[:sort] == true + if sort rows.sort.join("\n") else rows.join("\n") end end + + private + + attr_reader :hash, :sort end end end diff --git a/lib/aruba/platforms/unix_platform.rb b/lib/aruba/platforms/unix_platform.rb index 5fc5d6fd..dd9c9601 100644 --- a/lib/aruba/platforms/unix_platform.rb +++ b/lib/aruba/platforms/unix_platform.rb @@ -8,7 +8,6 @@ require "aruba/aruba_path" require "aruba/command_monitor" -require "aruba/platforms/simple_table" require "aruba/platforms/unix_command_string" require "aruba/platforms/unix_which" require "aruba/platforms/determine_file_size" @@ -221,11 +220,6 @@ def write_file(path, content) File.write(path, content) end - # Transform hash to a string table which can be output on stderr/stdout - def simple_table(hash, opts = {}) - SimpleTable.new(hash, opts).to_s - end - # Resolve path for command using the PATH-environment variable # # Mostly taken from here: https://github.com/djberg96/ptools diff --git a/spec/aruba/platforms/simple_table_spec.rb b/spec/aruba/platforms/simple_table_spec.rb index e9ca67c7..7b6ba017 100644 --- a/spec/aruba/platforms/simple_table_spec.rb +++ b/spec/aruba/platforms/simple_table_spec.rb @@ -3,35 +3,48 @@ require "spec_helper" require "aruba/platform" -RSpec.describe ".simple_table" do - context "when valid hash" do - let(:hash) do - { - key1: "value", - key2: "value" +RSpec.describe Aruba::Platforms::SimpleTable do + describe "#to_s" do + it "renders a sorted table of the provided hash" do + hash = { + key2: "value", + key1: "value" } - end - let(:rows) { ["# key1 => value", "# key2 => value"] } - it { expect(Aruba.platform.simple_table(hash).to_s).to eq rows.join("\n") } - end + expect(described_class.new(hash).to_s).to eq <<~TABLE.chomp + # key1 => value + # key2 => value + TABLE + end - context "when valid hash with unequal key lengths" do - let(:hash) do - { - key1: "value", - long_key2: "value" + it "renders an unsorted table provided hash unsorted if requested" do + hash = { + key2: "value", + key1: "value" } + + expect(described_class.new(hash, sort: false).to_s).to eq <<~TABLE.chomp + # key2 => value + # key1 => value + TABLE end - let(:rows) { ["# key1 => value", "# long_key2 => value"] } - it { expect(Aruba.platform.simple_table(hash).to_s).to eq rows.join("\n") } - end + it "renders an empty string if the provided hash is empty" do + hash = {} - context "when empty hash" do - let(:hash) { {} } - let(:rows) { [] } + expect(described_class.new(hash).to_s).to eq "" + end - it { expect(Aruba.platform.simple_table(hash).to_s).to eq rows.join("\n") } + it "aligns values when key lengths are unequal" do + hash = { + key1: "value", + long_key2: "value" + } + + expect(described_class.new(hash).to_s).to eq <<~TABLE.chomp + # key1 => value + # long_key2 => value + TABLE + end end end