Added test command caching and refactored inspec helper to run_cmd.

Helps with some of the tests that want to run ruby directly.

Fixes #4416.
This commit is contained in:
Ryan Davis 2019-11-06 14:43:44 -08:00
parent bb7e69d145
commit f8ea33de8f

View file

@ -107,21 +107,47 @@ module FunctionalHelper
_(failed_tests).must_be_empty
end
def inspec(commandline, prefix = nil)
if is_windows?
invocation = "/windows/system32/cmd /C \"#{prefix} #{exec_inspec} #{commandline}\""
result = CMD.run_command(invocation)
result.stdout.encode!(universal_newline: true)
result.stderr.encode!(universal_newline: true)
convert_windows_output(result.stdout)
# remove the CLIXML header trash in windows
result.stderr.gsub!("#< CLIXML\n", "")
else
invocation = "#{prefix} #{exec_inspec} #{commandline}"
result = CMD.run_command(invocation)
end
@inspec_mutex ||= Mutex.new
result
def self.inspec_mutex
@inspec_mutex
end
def self.inspec_cache
@inspec_cache ||= {}
end
def inspec_cache
FunctionalHelper.inspec_cache
end
def inspec_mutex
FunctionalHelper.inspec_mutex
end
def run_cmd(commandline, prefix = nil)
inspec_mutex.synchronize { # rubocop:disable Style/BlockDelimiters
inspec_cache[[commandline, prefix]] ||=
if is_windows?
invocation = "/windows/system32/cmd /C \"#{prefix} #{commandline}\""
# puts
# puts "CMD = #{invocation}"
result = CMD.run_command(invocation)
result.stdout.encode!(universal_newline: true)
result.stderr.encode!(universal_newline: true)
convert_windows_output(result.stdout)
# remove the CLIXML header trash in windows
result.stderr.gsub!("#< CLIXML\n", "")
result
else
invocation = "#{prefix} #{commandline}"
CMD.run_command(invocation)
end
}
end
def inspec(commandline, prefix = nil)
run_cmd "#{exec_inspec} #{commandline}", prefix
end
def inspec_with_env(commandline, env = {})