-
-
Notifications
You must be signed in to change notification settings - Fork 246
/
npm_spec.rb
99 lines (84 loc) · 3.57 KB
/
npm_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true
require 'spec_helper'
describe Puppet::Type.type(:package).provider(:npm) do
let(:resource) do
Puppet::Type.type(:package).new(
name: 'express',
ensure: :present
)
end
let(:provider) do
provider = described_class.new(resource)
provider.resource = resource
provider
end
before do
provider.class.stubs(:command).with(:npm).returns '/usr/local/bin/npm'
resource.provider = provider
end
def self.it_should_respond_to(*actions)
actions.each do |action|
it "responds to :#{action}" do
expect(provider).to respond_to(action)
end
end
end
it_should_respond_to :install, :uninstall, :update, :query, :latest
describe 'when installing npm packages' do
it 'uses package name by default' do
provider.expects(:npm).with('install', '--global', 'express')
provider.install
end
it 'uses the source instead of the package name' do
resource[:source] = '/tmp/express.tar.gz'
provider.expects(:npm).with('install', '--global', '/tmp/express.tar.gz')
provider.install
end
it 'passes the install_options to npm' do
resource[:install_options] = ['--verbose']
provider.expects(:npm).with('install', '--global', '--verbose', 'express')
provider.install
end
end
describe 'and install_options is a hash' do
it 'passes the install_options to npm' do
resource[:install_options] = [{ '--loglevel' => 'error' }]
provider.expects(:npm).with('install', '--global', '--loglevel=error', 'express')
provider.install
end
end
describe 'when npm packages are installed globally' do
before do
provider.class.instance_variable_set(:@npmlist, nil)
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(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(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(Puppet::Util::Execution::ProcessOutput.new('failure!', 0))
Puppet.expects(:debug).with(regexp_matches(%r{npm list.*failure!}))
expect(provider.class.instances).to eq([])
end
end
describe '#latest' do
it 'filters npm registry logging' do
provider.expects(:npm).with('view', 'express', 'version').returns("npm http GET https://registry.npmjs.org/express\nnpm http 200 https://registry.npmjs.org/express\n2.0.0")
expect(provider.latest).to eq('2.0.0')
end
end
end