Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore exit codes from "npm list --json" as they can be misleading #56

Merged
merged 1 commit into from
Nov 20, 2013
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
6 changes: 4 additions & 2 deletions lib/puppet/provider/package/npm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
optional_commands :npm => 'npm'

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?
begin
output = npm('list', '--json', '--global')
# ignore any npm output lines to be a bit more robust
output = PSON.parse(output.lines.select{ |l| l =~ /^((?!^npm).*)$/}.join("\n"))
@npmlist = output['dependencies'] || {}
rescue Exception => e
rescue PSON::ParserError => e
Puppet.debug("Error: npm list --json command error #{e.message}")
@npmlist = {}
end
Expand Down
23 changes: 21 additions & 2 deletions spec/unit/puppet/provider/pakages/npm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

describe Puppet::Type.type(:package).provider(:npm) do
before :each do
@provider.class.stubs(:optional_commands).with(:npm).returns "/usr/local/bin/npm"
@resource = Puppet::Type.type(:package).new(
:name => 'express',
:ensure => :present
)
@provider = described_class.new(@resource)
@provider.class.stubs(:optional_commands).with(:npm).returns "/usr/local/bin/npm"
@provider.class.stubs(:command).with(:npm).returns "/usr/local/bin/npm"
end

def self.it_should_respond_to(*actions)
Expand Down Expand Up @@ -37,13 +38,31 @@ def self.it_should_respond_to(*actions)
end

describe "when npm packages are installed globally" do
before :each do
@provider.class.instance_variable_set(:@npmlist, nil)
end

it "should return a list of npm packages installed globally" do
@provider.class.stubs(:npm).with('list', '--json', '--global').returns(my_fixture_read('npm_global'))
@provider.class.expects(:execute).with(['/usr/local/bin/npm', 'list', '--json', '--global'], anything).returns(my_fixture_read('npm_global'))
@provider.class.instances.map {|p| p.properties}.sort_by{|res| res[:name]}.should == [
{:ensure => '2.5.9' , :provider => 'npm', :name => 'express'},
{:ensure => '1.1.15', :provider => 'npm', :name => 'npm' },
]
end

it "should log 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)
Process::Status.any_instance.expects(:exitstatus).returns(123)
Puppet.expects(:debug).with(regexp_matches(/123/))
@provider.class.instances.map {|p| p.properties}.should_not == []
end

it "should log 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!")
Puppet.expects(:debug).with(regexp_matches(/npm list.*failure!/))
@provider.class.instances.should == []
end
end

# describe "when no npm packages are installed globally" do
Expand Down