Test cases for plugin listing and search added

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
Nikita Mathur 2022-03-18 17:27:43 +05:30
parent 9ecfad0122
commit bcc71df9dd
3 changed files with 51 additions and 29 deletions

View file

@ -35,8 +35,9 @@ module PluginManagerHelpers
end
end
def parse_plugin_list_lines(stdout)
def parse_plugin_list_lines(stdout, stderr = "")
plugins = []
plugin_data = {}
stdout.force_encoding("UTF-8").lines.each do |line|
next if line.strip.empty?
@ -45,14 +46,32 @@ module PluginManagerHelpers
next if line.include? "plugin(s) total"
parts = line.split(//u).map(&:strip!).compact
if stderr.match(/vertical orientation/)
# logic to parse data from vertical view if the ui breaks in existing width
if line.match(/Plugin/)
plugin_data = {} # reset this again when one row in vertical view is parsed
plugin_data[:name] = line.split(" ")[-2]
elsif line.match(/Version/)
plugin_data[:version] = parts[1]
elsif line.match(/Via/)
plugin_data[:type] = parts[1]
elsif line.match(/ApiVer/)
plugin_data[:generation] = parts[1]
elsif line.match(/Descrip/)
plugin_data[:description] = parts[1]
plugins << plugin_data # assuming this will be end of row
end
else
plugins << {
name: parts[0],
version: parts[1],
type: parts[2],
generation: parts[3],
description: parts[4],
raw: line,
}
end
end
plugins
end

View file

@ -6,22 +6,20 @@ class PluginManagerCliList < Minitest::Test
# Listing all plugins is now default behavior
LIST_CASES = [
{ arg: "-c", name: "inspec-plugin-manager-cli", type: "core" },
{ arg: "-c", name: "inspec-supermarket", type: "core" },
{ arg: "-s", name: "train-aws", type: "gem (system)" },
{ arg: "-c", name: "inspec-plugin-manager-cli", type: "core", description: "CLI plugin for InSpec" },
{ arg: "-c", name: "inspec-supermarket", type: "core", description: "" },
{ arg: "-s", name: "train-aws", type: "gem (system)", description: "AWS API Transport for Train" },
].freeze
def test_list_all_when_no_user_plugins_installed
result = run_inspec_process_with_this_plugin("plugin list --all")
assert_empty result.stderr
plugins_seen = parse_plugin_list_lines(result.stdout)
plugins_seen = parse_plugin_list_lines(result.stdout, result.stderr)
# Look for a specific plugin of each type - core, bundle, and system
LIST_CASES.each do |test_case|
plugin_line = plugins_seen.detect { |plugin| plugin[:name] == test_case[:name] }
refute_nil plugin_line, "#{test_case[:name]} should be detected in plugin list --all output"
assert_equal test_case[:type], plugin_line[:type], "#{test_case[:name]} should be detected as a '#{test_case[:type]}' type in list --all "
assert_equal test_case[:description], plugin_line[:description], "#{test_case[:name]} should have description '#{test_case[:description]}' in list --all "
end
assert_exit_code 0, result
end
@ -29,12 +27,11 @@ class PluginManagerCliList < Minitest::Test
def test_list_selective_when_no_user_plugins_installed
LIST_CASES.each do |test_case|
result = run_inspec_process_with_this_plugin("plugin list #{test_case[:arg]}")
assert_empty result.stderr
plugins_seen = parse_plugin_list_lines(result.stdout)
plugins_seen = parse_plugin_list_lines(result.stdout, result.stderr)
plugin_line = plugins_seen.detect { |plugin| plugin[:name] == test_case[:name] }
refute_nil plugin_line, "#{test_case[:name]} should be detected in plugin list #{test_case[:arg]} output"
assert_equal plugin_line[:type], test_case[:type], "#{test_case[:name]} should be detected as a '#{test_case[:type]}' type in list #{test_case[:arg]} "
assert_equal test_case[:description], plugin_line[:description], "#{test_case[:name]} should have description '#{test_case[:description]}' in list --all "
assert_exit_code 0, result
end
end
@ -48,13 +45,13 @@ class PluginManagerCliList < Minitest::Test
result = run_inspec_process_with_this_plugin("plugin list --user ", pre_run: pre_block)
assert_empty result.stderr
plugins_seen = parse_plugin_list_lines(result.stdout)
plugins_seen = parse_plugin_list_lines(result.stdout, result.stderr)
assert_equal 2, plugins_seen.count
# Plugin Name Version Via ApiVer
# ---------------------------------------------------------
# Plugin Name Version Via ApiVer Description
# -----------------------------------------------------------------------------
# inspec-meaning-of-life src path 2
# inspec-test-fixture 0.1.0 gem (user) 2
# ---------------------------------------------------------
# inspec-test-fixture 0.1.0 gem (user) 2 A simple test plugin gem for InSpec
# -----------------------------------------------------------------------------
# 2 plugin(s) total
meaning = plugins_seen.detect { |p| p[:name] == "inspec-meaning-of-life" }
refute_nil meaning
@ -77,14 +74,14 @@ class PluginManagerCliList < Minitest::Test
result = run_inspec_process_with_this_plugin("plugin list --user ", pre_run: pre_block)
assert_empty result.stderr
plugins_seen = parse_plugin_list_lines(result.stdout)
plugins_seen = parse_plugin_list_lines(result.stdout, result.stderr)
assert_equal 1, plugins_seen.count
assert_includes result.stdout, "1 plugin(s) total", "list train should show one plugins"
# Plugin Name Version Via ApiVer
# -------------------------------------------------------------
# train-test-fixture 0.1.0 gem (user) train-1
# -------------------------------------------------------------
# Plugin Name Version Via ApiVer Description
# -------------------------------------------------------------------------------
# train-test-fixture 0.1.0 gem (user) train-1 Test train plugin. Not intended for use as an example
# -------------------------------------------------------------------------------
# 1 plugin(s) total
train_plugin = plugins_seen.detect { |p| p[:name] == "train-test-fixture" }
refute_nil train_plugin

View file

@ -14,8 +14,8 @@ class PluginManagerCliSearch < Minitest::Test
def test_search_for_a_real_gem_with_full_name_no_options
result = run_inspec_process("plugin search --include-test-fixture inspec-test-fixture")
assert_includes result.stdout, "inspec-test-fixture", "Search result should contain the gem name"
assert_includes result.stdout, "A simple test plugin gem for InSpec", "Search result should contain the gem description"
assert_includes result.stdout, "1 plugin(s) found", "Search result should find 1 plugin"
line = result.stdout.split("\n").grep(/inspec-test-fixture/).first
assert_match(/\s*inspec-test-fixture\s+\((\d+\.\d+\.\d+){1}\)/, line, "Plugin line should include name and exactly one version")
@ -27,6 +27,7 @@ class PluginManagerCliSearch < Minitest::Test
result = run_inspec_process("plugin search --include-test-fixture inspec-test-")
assert_includes result.stdout, "inspec-test-fixture", "Search result should contain the gem name"
assert_includes result.stdout, "A simple test plugin gem for InSpec", "Search result should contain the gem description"
assert_includes result.stdout, "1 plugin(s) found", "Search result should find 1 plugin"
line = result.stdout.split("\n").grep(/inspec-test-fixture/).first
@ -39,6 +40,7 @@ class PluginManagerCliSearch < Minitest::Test
result = run_inspec_process("plugin search --exact --include-test-fixture inspec-test-fixture")
assert_includes result.stdout, "inspec-test-fixture", "Search result should contain the gem name"
assert_includes result.stdout, "A simple test plugin gem for InSpec", "Search result should contain the gem description"
assert_includes result.stdout, "1 plugin(s) found", "Search result should find 1 plugin"
assert_exit_code 0, result
@ -65,6 +67,7 @@ class PluginManagerCliSearch < Minitest::Test
def test_search_for_a_real_gem_with_full_name_and_all_option
result = run_inspec_process("plugin search --all --include-test-fixture inspec-test-fixture")
assert_includes result.stdout, "inspec-test-fixture", "Search result should contain the gem name"
assert_includes result.stdout, "A simple test plugin gem for InSpec", "Search result should contain the gem description"
assert_includes result.stdout, "1 plugin(s) found", "Search result should find 1 plugin"
line = result.stdout.split("\n").grep(/inspec-test-fixture/).first
@ -96,6 +99,7 @@ class PluginManagerCliSearch < Minitest::Test
result = run_inspec_process("plugin search --include-test-fixture train-test-fixture")
assert_includes result.stdout, "train-test-fixture", "Search result should contain the gem name"
assert_includes result.stdout, "Test train plugin. Not intended for use as an example", "Search result should contain the gem description"
assert_includes result.stdout, "1 plugin(s) found", "Search result should find 1 plugin"
line = result.stdout.split("\n").grep(/train-test-fixture/).first
assert_match(/\s*train-test-fixture\s+\((\d+\.\d+\.\d+){1}\)/, line, "Plugin line should include name and exactly one version")
@ -120,10 +124,12 @@ class PluginManagerCliSearch < Minitest::Test
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"
refute_includes result.stdout, "Test train plugin. Not intended for use as an example", "Search result should not contain the gem description"
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"
refute_includes result.stdout, "Test train plugin. Not intended for use as an example", "Search result should not contain the gem description"
end
end