Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/puppet/provider/package/npm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

def self.npmlist
# Ignore non-zero exit codes as they can be minor, just try and parse JSON
output = execute([command(:npm), 'list', '--json', '--global'], combine: false)
Puppet.debug("Warning: npm list --json exited with code #{$CHILD_STATUS.exitstatus}") unless $CHILD_STATUS.success?
output = execute([command(:npm), 'list', '--json', '--global'], combine: false, failonfail: false)
Puppet.debug("Warning: npm list --json exited with code #{output.exitstatus}") if output.exitstatus != 0
begin
# ignore any npm output lines to be a bit more robust
output = PSON.parse(output.lines.select { |l| l =~ %r{^((?!^npm).*)$} }.join("\n"), max_nesting: 100)
Expand Down
15 changes: 9 additions & 6 deletions spec/unit/puppet/provider/package/npm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,27 @@ def self.it_should_respond_to(*actions)
end

it 'returns a list of npm packages installed globally' do
provider.class.expects(:execute).with(['/usr/local/bin/npm', 'list', '--json', '--global'], anything).returns(my_fixture_read('npm_global'))
provider.class.expects(:execute).
with(['/usr/local/bin/npm', 'list', '--json', '--global'], anything).
returns(Puppet::Util::Execution::ProcessOutput.new(my_fixture_read('npm_global'), 0))
expect(provider.class.instances.map(&:properties).sort_by { |res| res[:name] }).to eq([
{ ensure: '2.5.9', provider: 'npm', name: 'express' },
{ ensure: '1.1.15', provider: 'npm', name: 'npm' }
])
end

it 'logs and continue if the list command has a non-zero exit code' do
provider.class.expects(:execute).with(['/usr/local/bin/npm', 'list', '--json', '--global'], anything).returns(my_fixture_read('npm_global'))
Process::Status.any_instance.expects(:success?).returns(false) # rubocop:disable RSpec/AnyInstance
Process::Status.any_instance.expects(:exitstatus).returns(123) # rubocop:disable RSpec/AnyInstance
provider.class.expects(:execute).
with(['/usr/local/bin/npm', 'list', '--json', '--global'], anything).
returns(Puppet::Util::Execution::ProcessOutput.new(my_fixture_read('npm_global'), 123))
Puppet.expects(:debug).with(regexp_matches(%r{123}))
expect(provider.class.instances.map(&:properties)).not_to eq([])
end

it "logs and return no packages if JSON isn't output" do
provider.class.expects(:execute).with(['/usr/local/bin/npm', 'list', '--json', '--global'], anything).returns('failure!')
Process::Status.any_instance.expects(:success?).returns(true) # rubocop:disable RSpec/AnyInstance
provider.class.expects(:execute).
with(['/usr/local/bin/npm', 'list', '--json', '--global'], anything).
returns(Puppet::Util::Execution::ProcessOutput.new('failure!', 0))
Puppet.expects(:debug).with(regexp_matches(%r{npm list.*failure!}))
expect(provider.class.instances).to eq([])
end
Expand Down