Fix for invocation when calling npm under sudo (#4548)

Fix for invocation when calling npm under sudo
This commit is contained in:
Clinton Wolfe 2019-11-12 16:01:07 -05:00 committed by GitHub
commit 1db2941a9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 5 deletions

View file

@ -27,12 +27,20 @@ module Inspec::Resources
return @info if defined?(@info)
if @location
npm = "cd #{Shellwords.escape @location} && npm"
command_separator = inspec.os.platform?("windows") ? ";" : "&&"
invocation = "cd #{Shellwords.escape @location} #{command_separator} npm"
else
npm = "npm -g"
invocation = "npm -g"
end
cmd = inspec.command("#{npm} ls --json #{@package_name}")
invocation = "#{invocation} ls --json #{@package_name}"
# If on unix, wrap in sh -c to protect against sudo
unless inspec.os.platform?("windows")
invocation = "sh -c '#{invocation}'"
end
cmd = inspec.command(invocation)
@info = {
name: @package_name,
type: "npm",

View file

@ -228,8 +228,8 @@ class MockLoader
"/opt/chef/embedded/bin/gem list --local -a -q ^chef-sugar$" => cmd.call("gem-list-local-a-q-chef-sugar"),
'c:\opscode\chef\embedded\bin\gem.bat list --local -a -q ^json$' => cmd.call("gem-list-local-a-q-json"),
"/opt/opscode/embedded/bin/gem list --local -a -q ^knife-backup$" => cmd.call("gem-list-local-a-q-knife-backup"),
"npm -g ls --json bower" => cmd.call("npm-g-ls--json-bower"),
"cd /path/to/project && npm ls --json bower" => cmd.call("npm-ls--json-bower"),
"sh -c 'npm -g ls --json bower'" => cmd.call("npm-g-ls--json-bower"),
"sh -c 'cd /path/to/project && npm ls --json bower'" => cmd.call("npm-ls--json-bower"),
"Rscript -e 'packageVersion(\"DBI\")'" => cmd.call("r-print-version"),
"Rscript -e 'packageVersion(\"DoesNotExist\")'" => cmd.call("r-print-version-not-installed"),
"perl -le 'eval \"require $ARGV[0]\" and print $ARGV[0]->VERSION or exit 1' DBD::Pg" => cmd.call("perl-print-version"),