Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

(FACT-2651) Fix --list-cache-groups when there are multiple arguments before it #545

Merged
merged 5 commits into from
Jun 9, 2020
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/unit_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- run: gem update bundler
- run: bundle install --jobs 3 --retry 3
- run: bundle exec rake spec

Expand All @@ -37,5 +38,6 @@ jobs:
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7
- run: gem update bundler
- run: bundle install --jobs 3 --retry 3
- run: bundle exec rake spec
6 changes: 6 additions & 0 deletions bin/facter
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
require 'pathname'
ROOT_DIR = Pathname.new(File.expand_path('..', __dir__)) unless defined?(ROOT_DIR)
require "#{ROOT_DIR}/lib/framework/cli/cli_launcher.rb"

cli_launcher = CliLauncher.new(ARGV)

cli_launcher.validate_options
cli_launcher.prepare_arguments
cli_launcher.start
6 changes: 6 additions & 0 deletions bin/facter-ng
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
require 'pathname'
ROOT_DIR = Pathname.new(File.expand_path('..', __dir__)) unless defined?(ROOT_DIR)
require "#{ROOT_DIR}/lib/framework/cli/cli_launcher.rb"

cli_launcher = CliLauncher.new(ARGV)

cli_launcher.validate_options
cli_launcher.prepare_arguments
cli_launcher.start
58 changes: 49 additions & 9 deletions lib/framework/cli/cli_launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,53 @@
require "#{ROOT_DIR}/lib/facter"
require "#{ROOT_DIR}/lib/framework/cli/cli"

Facter::OptionsValidator.validate(ARGV)
ARGV.unshift(Facter::Cli.default_task) unless
Facter::Cli.all_tasks.key?(ARGV[0]) ||
Facter::Cli.instance_variable_get(:@map).key?(ARGV[0])

begin
Facter::Cli.start(ARGV, debug: true)
rescue Thor::UnknownArgumentError => e
Facter::OptionsValidator.write_error_and_exit("unrecognised option '#{e.unknown.first}'")
class CliLauncher
def initialize(args)
@args = args
end

def validate_options
Facter::OptionsValidator.validate(@args)
end

def prepare_arguments
@args.unshift(Facter::Cli.default_task) unless
check_if_arguments_is_known(Facter::Cli.all_tasks, @args) ||
check_if_arguments_is_known(Facter::Cli.instance_variable_get(:@map), @args)

@args = reorder_program_arguments(@args)
end

def start
Facter::Cli.start(@args, debug: true)
rescue Thor::UnknownArgumentError => e
Facter::OptionsValidator.write_error_and_exit("unrecognised option '#{e.unknown.first}'")
end

private

def check_if_arguments_is_known(known_arguments, program_arguments)
program_arguments.each do |argument|
return true if known_arguments.key?(argument)
end

false
end

def reorder_program_arguments(program_arguments)
priority_arguments = Facter::Cli.instance_variable_get(:@map)

priority_args = []
normal_args = []

program_arguments.each do |argument|
if priority_arguments.include?(argument)
priority_args << argument
else
normal_args << argument
end
end

priority_args.concat(normal_args)
end
end
96 changes: 96 additions & 0 deletions spec/framework/cli/cli_launcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# frozen_string_literal: true

require "#{ROOT_DIR}/lib/framework/cli/cli_launcher"

describe CliLauncher do
subject(:cli_launcher) { CliLauncher.new(args) }

let(:args) { [] }

describe '#validate_options' do
it 'calls Facter::OptionsValidator.validate' do
allow(Facter::OptionsValidator).to receive(:validate)
cli_launcher.validate_options

expect(Facter::OptionsValidator).to have_received(:validate).with(args)
end
end

describe '#prepare_arguments' do
let(:task_list) do
{ 'help' => Thor::Command.new('help', 'description', 'long_description', 'usage'),
'query' => Thor::Command.new('query', 'description', 'long_description', 'usage'),
'version' => Thor::Command.new('version', 'description', 'long_description', 'usage'),
'list_block_groups' => Thor::Command.new('list_block_groups', 'description', 'long_description', 'usage'),
'list_cache_groups' => Thor::Command.new('list_cache_groups', 'description', 'long_description', 'usage') }
end

let(:map) do
{ '-h' => :help, '--version' => :version, '--list-block-groups' => :list_block_groups,
'--list-cache-groups' => :list_cache_groups }
end

before do
allow(Facter::Cli).to receive(:all_tasks).and_return(task_list)
allow(Facter::Cli).to receive(:instance_variable_get).with(:@map).and_return(map)
end

context 'when arguments should be reordered' do
let(:args) { %w[--debug --list-cache-groups --list-block-groups] }
let(:expected_arguments) { %w[--list-cache-groups --list-block-groups --debug] }

it 'reorders arguments' do
prepare_arguments = cli_launcher.prepare_arguments

expect(prepare_arguments).to eq(expected_arguments)
end
end

context 'when arguments should not be reordered' do
let(:args) { %w[--list-cache-groups --list-block-groups --debug] }

it 'does not reorder arguments' do
prepare_arguments = cli_launcher.prepare_arguments

expect(prepare_arguments).to eq(args)
end
end

context 'when default task should be added' do
let(:args) { %w[fact1 fact2] }
let(:expected_args) { %w[query fact1 fact2] }

it 'adds default (query) task' do
prepare_arguments = cli_launcher.prepare_arguments
expect(prepare_arguments).to eq(expected_args)
end
end
end

describe '#start' do
context 'when no errors' do
before do
allow(Facter::Cli).to receive(:start)
end

it 'calls Facter::Cli.start' do
cli_launcher.start

expect(Facter::Cli).to have_received(:start).with(args, debug: true)
end
end

context 'when errors' do
before do
allow(Facter::OptionsValidator).to receive(:write_error_and_exit)
allow(Facter::Cli).to receive(:start).with(any_args).and_raise(Thor::UnknownArgumentError.new({}, {}))
end

it 'calls Facter::OptionsValidator.write_error_and_exit' do
cli_launcher.start

expect(Facter::OptionsValidator).to have_received(:write_error_and_exit)
end
end
end
end