diff --git a/lib/inspec/describe.rb b/lib/inspec/describe_base.rb similarity index 100% rename from lib/inspec/describe.rb rename to lib/inspec/describe_base.rb diff --git a/lib/inspec/plugin/v1/plugin_types/resource.rb b/lib/inspec/plugin/v1/plugin_types/resource.rb index 7201ad197..ede076f09 100644 --- a/lib/inspec/plugin/v1/plugin_types/resource.rb +++ b/lib/inspec/plugin/v1/plugin_types/resource.rb @@ -16,7 +16,7 @@ module Inspec module ResourceDSL def name(name = nil) - return if name.nil? + return @name if name.nil? @name = name __register(name, self) diff --git a/lib/inspec/plugin/v2/loader.rb b/lib/inspec/plugin/v2/loader.rb index b88cbc3c9..8feb19bfa 100644 --- a/lib/inspec/plugin/v2/loader.rb +++ b/lib/inspec/plugin/v2/loader.rb @@ -3,12 +3,6 @@ require "inspec/version" require "inspec/plugin/v2/config_file" require "inspec/plugin/v2/filter" -# Add the current directory of the process to the load path -$LOAD_PATH.unshift(".") unless $LOAD_PATH.include?(".") -# Add the InSpec source root directory to the load path -folder = File.expand_path(File.join("..", "..", "..", ".."), __dir__) -$LOAD_PATH.unshift(folder) unless $LOAD_PATH.include?("folder") - module Inspec::Plugin::V2 class Loader attr_reader :conf_file, :registry, :options diff --git a/lib/inspec/profile.rb b/lib/inspec/profile.rb index a5c06d175..6d330be48 100644 --- a/lib/inspec/profile.rb +++ b/lib/inspec/profile.rb @@ -332,6 +332,7 @@ module Inspec # convert legacy os-* supports to their platform counterpart if res[:supports] && !res[:supports].empty? res[:supports].each do |support| + # TODO: deprecate support[:"platform-family"] = support.delete(:"os-family") if support.key?(:"os-family") support[:"platform-name"] = support.delete(:"os-name") if support.key?(:"os-name") end diff --git a/lib/inspec/resource.rb b/lib/inspec/resource.rb index 0b51fea32..b30fcff5a 100644 --- a/lib/inspec/resource.rb +++ b/lib/inspec/resource.rb @@ -10,10 +10,12 @@ module Inspec @default_registry ||= {} end + # TODO: these are keyed off of strings def self.registry @registry ||= default_registry end + # TODO: these are keyed off of symbols def self.supports @supports ||= {} end @@ -22,6 +24,29 @@ module Inspec default_registry.dup end + def self.backfill_supports! + reg = registry.keys.map(&:to_sym).sort + sup = supports.keys.map(&:to_sym).sort + + missings = reg - sup + + supports[:platform] = [{ platform: "os" }] # patch the circular dep + + missings.each do |missing| + klass = registry[missing.to_s].superclass + sklas = klass.superclass.name&.to_sym # might be resource = no name + + klass = klass.name.to_sym + + case + when klass != missing # an alias + supports[missing] = supports[klass] + when sklas + supports[klass] = supports[sklas] + end + end + end + # Creates the inner DSL which includes all resources for # creating tests. It is always connected to one target, # which is specified via the backend argument. diff --git a/lib/inspec/resources/json.rb b/lib/inspec/resources/json.rb index bebff8507..ae7487e65 100644 --- a/lib/inspec/resources/json.rb +++ b/lib/inspec/resources/json.rb @@ -5,6 +5,7 @@ require "inspec/utils/file_reader" module Inspec::Resources class JsonConfig < Inspec.resource(1) name "json" + supports platform: "os" desc "Use the json InSpec audit resource to test data in a JSON file." example <<~EXAMPLE describe json('policyfile.lock.json') do diff --git a/lib/inspec/resources/mssql_session.rb b/lib/inspec/resources/mssql_session.rb index a5b561d6f..eada73d7e 100644 --- a/lib/inspec/resources/mssql_session.rb +++ b/lib/inspec/resources/mssql_session.rb @@ -11,6 +11,7 @@ module Inspec::Resources # @see https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-connect-and-query-sqlcmd class MssqlSession < Inspec.resource(1) name "mssql_session" + supports platform: "windows" desc "Use the mssql_session InSpec audit resource to test SQL commands run against a MS Sql Server database." example <<~EXAMPLE # Using SQL authentication diff --git a/lib/inspec/rule.rb b/lib/inspec/rule.rb index cd1e9ef31..e80e282ff 100644 --- a/lib/inspec/rule.rb +++ b/lib/inspec/rule.rb @@ -2,7 +2,7 @@ require "method_source" require "date" -require "inspec/describe" +require "inspec/describe_base" require "inspec/expect" require "inspec/resource" require "inspec/resources/os" diff --git a/test/functional/helper.rb b/test/functional/helper.rb index 563b1c003..427e393be 100644 --- a/test/functional/helper.rb +++ b/test/functional/helper.rb @@ -202,7 +202,7 @@ module FunctionalHelper run_result.stdout.sub!("\n1 deprecation warning total\n", "") end - if opts[:json] + if opts[:json] && !run_result.stdout.empty? begin payload = JSON.parse(run_result.stdout) @@ -261,14 +261,14 @@ module PluginFunctionalHelper File.write(File.join(tmp_dir, "plugins.json"), content) end - opts.merge!({ + opts = { pre_run: pre, tmpdir: true, json: true, env: { "INSPEC_CONFIG_DIR" => ".", # We're in tmpdir }, - }) + }.merge(opts) run_inspec_process(command, opts) end diff --git a/test/functional/inspec_exec_test.rb b/test/functional/inspec_exec_test.rb index 74038c53b..e580b4eab 100644 --- a/test/functional/inspec_exec_test.rb +++ b/test/functional/inspec_exec_test.rb @@ -761,6 +761,7 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n" let(:json_path) { File.join(config_dir_path, "json-config", "good.json") } let(:cli_args) { "--config -" } let(:opts) { { prefix: "cat " + json_path + " | ", json: true, env: env } } + let(:njopts) { opts.merge(json: false) } # DO NOT use the `let`-defined run_result through here # If you do, it will execute twice, and cause STDIN to read empty on the second time @@ -772,7 +773,7 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n" end it "detect should exit 0" do - result = run_inspec_process( "detect " + cli_args + " ", opts ) + result = run_inspec_process( "detect " + cli_args + " ", njopts ) _(result.stderr).must_be_empty @@ -780,7 +781,7 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n" end it "shell should exit 0" do - result = run_inspec_process( 'shell -c "platform.family" ' + cli_args + " ", opts ) + result = run_inspec_process( 'shell -c "platform.family" ' + cli_args + " ", njopts ) _(result.stderr).must_be_empty diff --git a/test/functional/ui_test.rb b/test/functional/ui_test.rb index f52138c1e..c5c2fb325 100644 --- a/test/functional/ui_test.rb +++ b/test/functional/ui_test.rb @@ -9,8 +9,10 @@ require "functional/helper" module VisibleSpaces def show_spaces(str) - str.tr!(" ", "S") - str.tr!("\n", "N") + str + .tr(" ", "S") + .tr("\n", "N") + .b end end @@ -21,7 +23,7 @@ describe "InSpec UI behavior" do parallelize_me! let(:plugin_path) { File.join(mock_path, "plugins", "inspec-test-ui", "lib", "inspec-test-ui") } - let(:run_result) { run_inspec_with_plugin("#{pre_opts} testui #{feature} #{post_opts}", plugin_path: plugin_path) } + let(:run_result) { run_inspec_with_plugin("#{pre_opts} testui #{feature} #{post_opts}", plugin_path: plugin_path, json: false) } let(:pre_opts) { "" } let(:post_opts) { "" }