support local npm package searches (#3105)

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
Dominik Richter 2018-06-01 01:52:46 -07:00 committed by Christoph Hartmann
parent b1d4628221
commit ebd1d36600
6 changed files with 46 additions and 3 deletions

View file

@ -22,6 +22,14 @@ where
* `('npm_package_name')` must specify an NPM package, such as `'bower'` or `'statsd'`
* `be_installed` is a valid matcher for this resource
You can also specify additional options:
describe npm('npm_package_name', path: '/path/to/project') do
it { should be_installed }
end
The `path` specifies a folder, that contains a `node_modules` subdirectory. It emulates running `npm` inside the specified folder. This way you can inspect local NPM installations as well as global ones.
<br>
## Examples

View file

@ -1,5 +1,7 @@
# encoding: utf-8
require 'shellwords'
module Inspec::Resources
class NpmPackage < Inspec.resource(1)
name 'npm'
@ -10,17 +12,28 @@ module Inspec::Resources
describe npm('bower') do
it { should be_installed }
end
describe npm('tar', path: '/path/to/project') do
it { should be_installed }
end
"
def initialize(package_name)
def initialize(package_name, opts = {})
@package_name = package_name
@location = opts[:path]
@cache = nil
end
def info
return @info if defined?(@info)
cmd = inspec.command("npm ls -g --json #{@package_name}")
if @location
npm = "cd #{Shellwords.escape @location} && npm"
else
npm = 'npm -g'
end
cmd = inspec.command("#{npm} ls --json #{@package_name}")
@info = {
name: @package_name,
type: 'npm',

View file

@ -254,7 +254,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 ls -g --json bower' => cmd.call('npm-ls-g--json-bower'),
'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'),
"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'),

View file

@ -0,0 +1,9 @@
{
"dependencies": {
"bower": {
"version": "1.4.2",
"from": "bower@*",
"resolved": "https://registry.npmjs.org/bower/-/bower-1.4.1.tgz"
}
}
}

View file

@ -17,4 +17,16 @@ describe 'Inspec::Resources::Npm' do
_(resource.installed?).must_equal true
_(resource.info).must_equal pkg
end
it 'verify npm package in local folder' do
resource = load_resource('npm', 'bower', path: '/path/to/project')
pkg = {
name: 'bower',
version: '1.4.2',
type: 'npm',
installed: true,
}
_(resource.installed?).must_equal true
_(resource.info).must_equal pkg
end
end