Skip to content

Commit 9e0e3ab

Browse files
committed
Fix specs
1 parent a423af8 commit 9e0e3ab

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

lib/puppet/provider/package/gem.rb

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def self.execute_gem_command(command, command_options, custom_environment = {})
8383
custom_environment[:PATH] = windows_path_without_puppet_bin
8484
end
8585

86+
# This uses an unusual form of passing the command and args as [<cmd>, [<arg1>, <arg2>, ...]]
8687
execute(cmd, { :failonfail => true, :combine => true, :custom_environment => custom_environment })
8788
end
8889

spec/unit/provider/package/puppet_gem_spec.rb

+24-16
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,28 @@
2020
let(:provider_gem_cmd) { '/opt/puppetlabs/puppet/bin/gem' }
2121
end
2222

23-
custom_environment = {"HOME"=>ENV["HOME"]}
24-
custom_environment['PKG_CONFIG_PATH'] = '/opt/puppetlabs/puppet/lib/pkgconfig'
25-
26-
let(:execute_options) { {:failonfail => true, :combine => true, :custom_environment => custom_environment} }
23+
let(:execute_options) do
24+
{
25+
failonfail: true,
26+
combine: true,
27+
custom_environment: {
28+
'HOME'=>ENV['HOME'],
29+
'PKG_CONFIG_PATH' => '/opt/puppetlabs/puppet/lib/pkgconfig'
30+
}
31+
}
32+
end
2733

2834
before :each do
2935
resource.provider = provider
30-
allow(described_class).to receive(:command).with(:gemcmd).and_return(provider_gem_cmd)
31-
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
36+
if Puppet::Util::Platform.windows?
37+
# provider is loaded before we can stub, so stub the class we're testing
38+
allow(provider.class).to receive(:command).with(:gemcmd).and_return(provider_gem_cmd)
39+
else
40+
allow(provider.class).to receive(:which).with(provider_gem_cmd).and_return(provider_gem_cmd)
41+
end
42+
allow(File).to receive(:file?).with(provider_gem_cmd).and_return(true)
3243
end
3344

34-
3545
describe '.windows_gemcmd' do
3646
context 'when PUPPET_DIR is not set' do
3747
before do
@@ -64,45 +74,43 @@
6474
end
6575

6676
it "should use the path to the gem command" do
67-
allow(described_class).to receive(:validate_command).with(provider_gem_cmd)
68-
expect(described_class).to receive(:execute).with(be_a(Array), execute_options) { |args| expect(args[0]).to eq(provider_gem_cmd) }.and_return('')
77+
expect(described_class).to receive(:execute).with([provider_gem_cmd, be_an(Array)], be_a(Hash)).and_return('')
6978
provider.install
7079
end
7180

7281
it "should not append install_options by default" do
73-
expect(described_class).to receive(:execute_gem_command).with(provider_gem_cmd, %w{install --no-rdoc --no-ri myresource}).and_return('')
82+
expect(described_class).to receive(:execute).with([provider_gem_cmd, %w{install --no-rdoc --no-ri myresource}], anything).and_return('')
7483
provider.install
7584
end
7685

7786
it "should allow setting an install_options parameter" do
7887
resource[:install_options] = [ '--force', {'--bindir' => '/usr/bin' } ]
79-
expect(described_class).to receive(:execute_gem_command).with(provider_gem_cmd, %w{install --force --bindir=/usr/bin --no-rdoc --no-ri myresource}).and_return('')
88+
expect(described_class).to receive(:execute).with([provider_gem_cmd, %w{install --force --bindir=/usr/bin --no-rdoc --no-ri myresource}], anything).and_return('')
8089
provider.install
8190
end
8291
end
8392

8493
context "when uninstalling" do
8594
it "should use the path to the gem command" do
86-
allow(described_class).to receive(:validate_command).with(provider_gem_cmd)
87-
expect(described_class).to receive(:execute).with(be_a(Array), execute_options) { |args| expect(args[0]).to eq(provider_gem_cmd) }.and_return('')
95+
expect(described_class).to receive(:execute).with([provider_gem_cmd, be_an(Array)], be_a(Hash)).and_return('')
8896
provider.uninstall
8997
end
9098

9199
it "should not append uninstall_options by default" do
92-
expect(described_class).to receive(:execute_gem_command).with(provider_gem_cmd, %w{uninstall --executables --all myresource}).and_return('')
100+
expect(described_class).to receive(:execute).with([provider_gem_cmd, %w{uninstall --executables --all myresource}], anything).and_return('')
93101
provider.uninstall
94102
end
95103

96104
it "should allow setting an uninstall_options parameter" do
97105
resource[:uninstall_options] = [ '--force', {'--bindir' => '/usr/bin' } ]
98-
expect(described_class).to receive(:execute_gem_command).with(provider_gem_cmd, %w{uninstall --executables --all myresource --force --bindir=/usr/bin}).and_return('')
106+
expect(described_class).to receive(:execute).with([provider_gem_cmd, %w{uninstall --executables --all myresource --force --bindir=/usr/bin}], anything).and_return('')
99107
provider.uninstall
100108
end
101109

102110
it 'should invalidate the rubygems cache' do
103111
gem_source = double('gem_source')
104112
allow(Puppet::Util::Autoload).to receive(:gem_source).and_return(gem_source)
105-
expect(described_class).to receive(:execute_gem_command).with(provider_gem_cmd, %w{uninstall --executables --all myresource}).and_return('')
113+
expect(described_class).to receive(:execute).with([provider_gem_cmd, %w{uninstall --executables --all myresource}], anything).and_return('')
106114
expect(gem_source).to receive(:clear_paths)
107115
provider.uninstall
108116
end

spec/unit/provider/package/puppetserver_gem_spec.rb

+19-17
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,57 @@
1616

1717
let(:provider_gem_cmd) { '/opt/puppetlabs/bin/puppetserver' }
1818

19-
custom_environment = { HOME: ENV['HOME'] }
20-
21-
let(:execute_options) { { failonfail: true, combine: true, custom_environment: custom_environment } }
19+
let(:execute_options) do
20+
{ failonfail: true, combine: true, custom_environment: { 'HOME' => ENV['HOME'] } }
21+
end
2222

2323
before :each do
2424
resource.provider = provider
2525
allow(Puppet::Util).to receive(:which).with(provider_gem_cmd).and_return(provider_gem_cmd)
26+
allow(File).to receive(:file?).with(provider_gem_cmd).and_return(true)
2627
end
2728

2829
describe "#install" do
2930
it "uses the path to the gem command" do
30-
allow(described_class).to receive(:validate_command).with(provider_gem_cmd)
31-
expect(Puppet::Util::Execution).to receive(:execute).with(be_a(Array), execute_options) { |args| expect(args[0]).to eq(provider_gem_cmd) }.and_return('')
31+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, be_an(Array)], be_a(Hash)).and_return('')
3232
provider.install
3333
end
3434

3535
it "appends version if given" do
3636
resource[:ensure] = ['1.2.1']
37-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install -v 1.2.1 --no-document myresource}).and_return('')
37+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install -v 1.2.1 --no-document myresource}], anything).and_return('')
3838
provider.install
3939
end
4040

4141
context "with install_options" do
4242
it "does not append the parameter by default" do
43-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install --no-document myresource}).and_return('')
43+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install --no-document myresource}], anything).and_return('')
4444
provider.install
4545
end
4646

4747
it "allows setting the parameter" do
4848
resource[:install_options] = [ '--force', {'--bindir' => '/usr/bin' } ]
49-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install --force --bindir=/usr/bin --no-document myresource}).and_return('')
49+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install --force --bindir=/usr/bin --no-document myresource}], anything).and_return('')
5050
provider.install
5151
end
5252
end
5353

5454
context "with source" do
5555
it "correctly sets http source" do
5656
resource[:source] = 'http://rubygems.com'
57-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install --no-document --source http://rubygems.com myresource}).and_return('')
57+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install --no-document --source http://rubygems.com myresource}], anything).and_return('')
5858
provider.install
5959
end
6060

6161
it "correctly sets local file source" do
6262
resource[:source] = 'paint-2.2.0.gem'
63-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install --no-document paint-2.2.0.gem}).and_return('')
63+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install --no-document paint-2.2.0.gem}], anything).and_return('')
6464
provider.install
6565
end
6666

6767
it "correctly sets local file source with URI scheme" do
6868
resource[:source] = 'file:///root/paint-2.2.0.gem'
69-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install --no-document /root/paint-2.2.0.gem}).and_return('')
69+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install --no-document /root/paint-2.2.0.gem}], anything).and_return('')
7070
provider.install
7171
end
7272

@@ -84,20 +84,19 @@
8484

8585
describe "#uninstall" do
8686
it "uses the path to the gem command" do
87-
allow(described_class).to receive(:validate_command).with(provider_gem_cmd)
88-
expect(Puppet::Util::Execution).to receive(:execute).with(be_a(Array), execute_options) { |args| expect(args[0]).to eq(provider_gem_cmd) }.and_return('')
87+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, be_an(Array)], be_a(Hash)).and_return('')
8988
provider.uninstall
9089
end
9190

9291
context "with uninstall_options" do
9392
it "does not append the parameter by default" do
94-
expect(described_class).to receive(:puppetservercmd).with(%w{gem uninstall --executables --all myresource}).and_return('')
93+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem uninstall --executables --all myresource}], anything).and_return('')
9594
provider.uninstall
9695
end
9796

9897
it "allows setting the parameter" do
9998
resource[:uninstall_options] = [ '--force', {'--bindir' => '/usr/bin' } ]
100-
expect(described_class).to receive(:puppetservercmd).with(%w{gem uninstall --executables --all myresource --force --bindir=/usr/bin}).and_return('')
99+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem uninstall --executables --all myresource --force --bindir=/usr/bin}], anything).and_return('')
101100
provider.uninstall
102101
end
103102
end
@@ -106,14 +105,17 @@
106105
describe ".gemlist" do
107106
context "listing installed packages" do
108107
it "uses the puppet_gem provider_command to list local gems" do
108+
allow(Puppet::Type::Package::ProviderPuppet_gem).to receive(:provider_command).and_return('/opt/puppetlabs/puppet/bin/gem')
109+
allow(described_class).to receive(:validate_command).with('/opt/puppetlabs/puppet/bin/gem')
110+
109111
expected = { name: 'world_airports', provider: :puppetserver_gem, ensure: ['1.1.3'] }
110-
expect(described_class).to receive(:execute_rubygems_list_command).with(['gem', 'list', '--local']).and_return(File.read(my_fixture('gem-list-local-packages')))
112+
expect(Puppet::Util::Execution).to receive(:execute).with(['/opt/puppetlabs/puppet/bin/gem', %w[list --local]], anything).and_return(File.read(my_fixture('gem-list-local-packages')))
111113
expect(described_class.gemlist({ local: true })).to include(expected)
112114
end
113115
end
114116

115117
it "appends the gem source if given" do
116-
expect(described_class).to receive(:puppetservercmd).with(%w{gem list --remote --source https://rubygems.com}).and_return('')
118+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem list --remote --source https://rubygems.com}], anything).and_return('')
117119
described_class.gemlist({ source: 'https://rubygems.com' })
118120
end
119121
end

0 commit comments

Comments
 (0)