Only show test-fixture gem s in search if a speacial flag is passed.

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2018-10-01 15:11:55 -04:00
parent 19c7f55e3a
commit 66e3b578af
3 changed files with 47 additions and 10 deletions

View file

@ -1,5 +1,6 @@
require 'term/ansicolor'
require 'pathname'
require 'inspec/plugin/v2'
require 'inspec/plugin/v2/installer'
module InspecPlugins
@ -35,12 +36,27 @@ module InspecPlugins
# inspec plugin search
#==================================================================#
desc 'search [options] PATTERN', 'Searches rubygems.org for InSpec plugins. Exits 0 on a search hit, exits 2 on a search miss.'
desc 'search [options] PATTERN', 'Searches rubygems.org for plugins.'
long_desc <<~EOLD
Searches rubygems.org for InSpec plugins. Exits 0 on a search hit, 1 on user error,
2 on a search miss. PATTERN is a simple string; a wildcard will be added as
a suffix, unles -e is used.
EOLD
option :all, desc: 'List all available versions, not just the latest one.', type: :boolean, aliases: [:a]
option :exact, desc: 'Assume PATTERN is exact; do not add a wildcard to the end', type: :boolean, aliases: [:e]
# Wish we could hide this
option :'include-test-fixture', type: :boolean, desc: 'Internal use'
# Justification for disabling ABC: currently at 33.51/33
def search(search_term) # rubocop: disable Metrics/AbcSize
search_results = installer.search(search_term, exact: options[:exact])
# The search results have already been filtered by the reject list. But the
# RejectList doesn't filter {inspec, train}-test-fixture because we need those
# for testing. We want to hide those from users, so unless we know we're in
# test mode, remove them.
unless options[:'include-test-fixture']
search_results.delete('inspec-test-fixture')
search_results.delete('train-test-fixture')
end
# TODO: ui object support
puts

View file

@ -143,8 +143,14 @@ class PluginManagerCliSearch < MiniTest::Test
include CorePluginFunctionalHelper
include PluginManagerHelpers
# TODO: Thor can't hide options, but we wish it could.
# def test_search_include_fixture_hidden_option
# result = run_inspec_process_with_this_plugin('plugin help search')
# refute_includes result.stdout, '--include-test-fixture'
# end
def test_search_for_a_real_gem_with_full_name_no_options
result = run_inspec_process('plugin search inspec-test-fixture')
result = run_inspec_process('plugin search --include-test-fixture inspec-test-fixture')
assert_equal 0, result.exit_status, 'Search should exit 0 on a hit'
assert_includes result.stdout, 'inspec-test-fixture', 'Search result should contain the gem name'
assert_includes result.stdout, '1 plugin(s) found', 'Search result should find 1 plugin'
@ -153,7 +159,7 @@ class PluginManagerCliSearch < MiniTest::Test
end
def test_search_for_a_real_gem_with_stub_name_no_options
result = run_inspec_process('plugin search inspec-test-')
result = run_inspec_process('plugin search --include-test-fixture inspec-test-')
assert_equal 0, result.exit_status, 'Search should exit 0 on a hit'
assert_includes result.stdout, 'inspec-test-fixture', 'Search result should contain the gem name'
assert_includes result.stdout, '1 plugin(s) found', 'Search result should find 1 plugin'
@ -163,7 +169,7 @@ class PluginManagerCliSearch < MiniTest::Test
end
def test_search_for_a_real_gem_with_full_name_and_exact_option
result = run_inspec_process('plugin search --exact inspec-test-fixture')
result = run_inspec_process('plugin search --exact --include-test-fixture inspec-test-fixture')
assert_equal 0, result.exit_status, 'Search should exit 0 on a hit'
assert_includes result.stdout, 'inspec-test-fixture', 'Search result should contain the gem name'
assert_includes result.stdout, '1 plugin(s) found', 'Search result should find 1 plugin'
@ -173,7 +179,7 @@ class PluginManagerCliSearch < MiniTest::Test
end
def test_search_for_a_real_gem_with_stub_name_and_exact_option
result = run_inspec_process('plugin search --exact inspec-test-')
result = run_inspec_process('plugin search --exact --include-test-fixture inspec-test-')
assert_equal 2, result.exit_status, 'Search should exit 2 on a miss'
assert_includes result.stdout, '0 plugin(s) found', 'Search result should find 0 plugins'
@ -182,7 +188,7 @@ class PluginManagerCliSearch < MiniTest::Test
end
def test_search_for_a_real_gem_with_full_name_and_all_option
result = run_inspec_process('plugin search --all inspec-test-fixture')
result = run_inspec_process('plugin search --all --include-test-fixture inspec-test-fixture')
assert_equal 0, result.exit_status, 'Search should exit 0 on a hit'
assert_includes result.stdout, 'inspec-test-fixture', 'Search result should contain the gem name'
assert_includes result.stdout, '1 plugin(s) found', 'Search result should find 1 plugin'
@ -195,19 +201,19 @@ class PluginManagerCliSearch < MiniTest::Test
end
def test_search_for_a_gem_with_missing_prefix
result = run_inspec_process('plugin search test-fixture')
result = run_inspec_process('plugin search --include-test-fixture test-fixture')
assert_equal 1, result.exit_status, 'Search should exit 1 on user error'
assert_includes result.stdout, "All inspec plugins must begin with either 'inspec-' or 'train-'"
end
def test_search_for_a_gem_that_does_not_exist
result = run_inspec_process('plugin search inspec-test-fixture-nonesuch')
result = run_inspec_process('plugin search --include-test-fixture inspec-test-fixture-nonesuch')
assert_equal 2, result.exit_status, 'Search should exit 2 on a miss'
assert_includes result.stdout, '0 plugin(s) found', 'Search result should find 0 plugins'
end
def test_search_for_a_real_gem_with_full_name_no_options_and_train_name
result = run_inspec_process('plugin search train-test-fixture')
result = run_inspec_process('plugin search --include-test-fixture train-test-fixture')
assert_equal 0, result.exit_status, 'Search should exit 0 on a hit'
assert_includes result.stdout, 'train-test-fixture', 'Search result should contain the gem name'
assert_includes result.stdout, '1 plugin(s) found', 'Search result should find 1 plugin'
@ -215,6 +221,17 @@ class PluginManagerCliSearch < MiniTest::Test
assert_match(/\s*train-test-fixture\s+\((\d+\.\d+\.\d+){1}\)/,line,'Plugin line should include name and exactly one version')
end
def test_search_for_a_real_gem_with_full_name_no_options_filter_fixtures
result = run_inspec_process('plugin search inspec-test-fixture')
refute_includes result.stdout, 'inspec-test-fixture', 'Search result should not contain the fixture gem name'
end
def test_search_for_a_real_gem_with_full_name_no_options_filter_fixtures_train
result = run_inspec_process('plugin search train-test-fixture')
refute_includes result.stdout, 'train-test-fixture', 'Search result should not contain the fixture gem name'
end
end
#-----------------------------------------------------------------------------------------#

View file

@ -26,7 +26,7 @@ class PluginManagerCliOptions < MiniTest::Test
def test_search_args
arg_config = cli_class.all_commands['search'].options
assert_equal 2, arg_config.count, 'The search command should have 2 options'
assert_equal 3, arg_config.count, 'The search command should have 2 options'
assert_includes arg_config.keys, :all, 'The search command should have an --all option'
assert_equal :boolean, arg_config[:all].type, 'The --all option should be boolean'
@ -40,6 +40,10 @@ class PluginManagerCliOptions < MiniTest::Test
refute_nil arg_config[:exact].description, 'The --exact option should have a description'
refute arg_config[:exact].required, 'The --exact option should not be required'
assert_includes arg_config.keys, :'include-test-fixture', 'The search command should have an --include-test-fixture option'
assert_equal :boolean, arg_config[:'include-test-fixture'].type, 'The --include-test-fixture option should be boolean'
refute arg_config[:'include-test-fixture'].required, 'The --include-test-fixture option should not be required'
assert_equal 1, cli_class.instance_method(:search).arity, 'The search command should take one argument'
end