Skip to content

Commit

Permalink
Rewrite tests to use rspec-mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoppe committed Apr 8, 2020
1 parent 5be3832 commit 6295c99
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
42 changes: 21 additions & 21 deletions spec/unit/provider/package/pear_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
end

before do
described_class.stubs(:command).with(:pear).returns '/fake/pear'
allow(described_class).to receive(:command).with(:pear).and_return '/fake/pear'
resource.provider = provider
end

describe '.instances' do
it 'returns an array of installed packages' do
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('list', '-a').
returns File.read(my_fixture('list_a'))
and_return File.read(my_fixture('list_a'))

expect(described_class.instances.map(&:properties)).to eq [
{ name: 'Archive_Tar', vendor: 'pear.php.net', ensure: '1.4.0', provider: :pear },
Expand All @@ -33,46 +33,46 @@
end

it 'ignores malformed lines' do
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('list', '-a').
returns 'aaa2.1'
Puppet.expects(:warning).with('Could not match aaa2.1')
and_return 'aaa2.1'
allow(Puppet).to receive(:warning).with('Could not match aaa2.1')
expect(described_class.instances).to eq []
end
end

describe '#install' do
it 'installs a package' do
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('-D', 'auto_discover=1', 'upgrade', '--alldeps', 'dummy')
provider.install
end

it 'installs a specific version' do
resource[:ensure] = '0.2'
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('-D', 'auto_discover=1', 'upgrade', '--alldeps', '-f', 'dummy-0.2')
provider.install
end

it 'installs from a specific source' do
resource[:source] = 'pear.php.net/dummy'
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('-D', 'auto_discover=1', 'upgrade', '--alldeps', 'pear.php.net/dummy')
provider.install
end

it 'installs a specific version from a specific source' do
resource[:ensure] = '0.2'
resource[:source] = 'pear.php.net/dummy'
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('-D', 'auto_discover=1', 'upgrade', '--alldeps', '-f', 'pear.php.net/dummy-0.2')
provider.install
end

it 'uses the specified responsefile' do
resource[:responsefile] = '/fake/pearresponse'
Puppet::Util::Execution.expects(:execute).
allow(Puppet::Util::Execution).to receive(:execute).
with(
['/fake/pear', '-D', 'auto_discover=1', 'upgrade', '--alldeps', 'dummy'],
stdinfile: resource[:responsefile]
Expand All @@ -82,17 +82,17 @@

it 'accepts install_options' do
resource[:install_options] = ['--onlyreqdeps']
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('-D', 'auto_discover=1', 'upgrade', '--onlyreqdeps', 'dummy')
provider.install
end
end

describe '#query' do
it 'queries information about one package' do
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('list', '-a').
returns File.read(my_fixture('list_a'))
and_return File.read(my_fixture('list_a'))

resource[:name] = 'pear'
expect(provider.query).to eq(
Expand All @@ -103,9 +103,9 @@

describe '#latest' do
it 'fetches the latest version available' do
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('remote-info', 'Benchmark').
returns File.read(my_fixture('remote-info_benchmark'))
and_return File.read(my_fixture('remote-info_benchmark'))

resource[:name] = 'Benchmark'
expect(provider.latest).to eq '1.2.9'
Expand All @@ -114,16 +114,16 @@

describe '#uninstall' do
it 'uninstalls a package' do
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('uninstall', resource[:name]).
returns('uninstall ok')
and_return('uninstall ok')
provider.uninstall
end

it 'raises an error otherwise' do
described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('uninstall', resource[:name]).
returns('unexpected output')
and_return('unexpected output')
expect { provider.uninstall }.to raise_error(Puppet::Error)
end
end
Expand All @@ -132,7 +132,7 @@
it 'ignores the resource version' do
resource[:ensure] = '2.0'

described_class.expects(:pear).
allow(described_class).to receive(:pear).
with('-D', 'auto_discover=1', 'upgrade', '--alldeps', 'dummy')
provider.update
end
Expand Down
22 changes: 11 additions & 11 deletions spec/unit/provider/package/pecl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
end

before do
parent_class.stubs(:command).with(:pear).returns '/fake/pear'
allow(parent_class).to receive(:command).with(:pear).and_return '/fake/pear'
end

describe '.instances' do
it 'returns pecl installed packages via pear' do
parent_class.expects(:pear).
allow(parent_class).to receive(:pear).
with('list', '-a').
returns File.read(fixtures('unit/provider/package/pear/list_a'))
and_return File.read(fixtures('unit/provider/package/pear/list_a'))

expect(described_class.instances.map(&:properties)).to eq [
{ ensure: '1.13.5', name: 'zip', vendor: 'pecl.php.net', provider: :pecl }
Expand All @@ -31,16 +31,16 @@

describe '#install' do
it 'installs with pear' do
parent_class.expects(:pear)
allow(parent_class).to receive(:pear)
provider.install
end
end

describe '#query' do
it 'queries pecl package info via pear' do
parent_class.expects(:pear).
allow(parent_class).to receive(:pear).
with('list', '-a').
returns File.read(fixtures('unit/provider/package/pear/list_a'))
and_return File.read(fixtures('unit/provider/package/pear/list_a'))

resource[:name] = 'zip'
expect(provider.query).to eq(ensure: '1.13.5', name: 'zip', vendor: 'pecl.php.net', provider: :pecl)
Expand All @@ -49,9 +49,9 @@

describe '#latest' do
it 'fetches the latest version available via pear' do
parent_class.expects(:pear).
allow(parent_class).to receive(:pear).
with('remote-info', 'pecl.php.net/zip').
returns File.read(fixtures('unit/provider/package/pear/remote-info_zip'))
and_return File.read(fixtures('unit/provider/package/pear/remote-info_zip'))

resource[:name] = 'zip'
expect(provider.latest).to eq '1.13.5'
Expand All @@ -60,8 +60,8 @@

describe '#uninstall' do
it 'uninstalls a package via pear' do
parent_class.expects(:pear).
returns('uninstall ok')
allow(parent_class).to receive(:pear).
and_return('uninstall ok')
provider.uninstall
end
end
Expand All @@ -70,7 +70,7 @@
it 'updates to latest version via pear' do
resource[:ensure] = '2.0'

parent_class.expects(:pear)
allow(parent_class).to receive(:pear)
provider.update
end
end
Expand Down

0 comments on commit 6295c99

Please sign in to comment.