Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move UnixPlatform#simple_table into Announcer #932

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/aruba/platforms/announcer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "shellwords"
require "aruba/colorizer"
require "aruba/platforms/simple_table"

Aruba::Colorizer.coloring = false if !$stdout.tty? && !ENV.key?("AUTOTEST")

Expand Down Expand Up @@ -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,
Expand All @@ -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)) }
Expand All @@ -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

Expand Down
18 changes: 7 additions & 11 deletions lib/aruba/platforms/simple_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
6 changes: 0 additions & 6 deletions lib/aruba/platforms/unix_platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
57 changes: 35 additions & 22 deletions spec/aruba/platforms/simple_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading