14862536720b2ca04017856d3642ec8d2325c061
[mirror/dsa-puppet.git] / 3rdparty / modules / archive / spec / unit / puppet_x / bodeco / archive_spec.rb
1 # rubocop:disable RSpec/MultipleExpectations
2 require 'spec_helper'
3 require 'puppet_x/bodeco/archive'
4
5 describe PuppetX::Bodeco::Archive do
6   let(:zipfile) do
7     File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'files', 'test.zip'))
8   end
9
10   it '#checksum' do
11     Dir.mktmpdir do |dir|
12       tempfile = File.join(dir, 'test.zip')
13       FileUtils.cp(zipfile, tempfile)
14
15       archive = described_class.new(tempfile)
16       expect(archive.checksum(:none)).to be nil
17       expect(archive.checksum(:md5)).to eq '557e2ebb67b35d1fddff18090b6bc26b'
18       expect(archive.checksum(:sha1)).to eq '377ec712d7fdb7266221db3441e3af2055448ead'
19     end
20   end
21
22   it '#parse_flags' do
23     archive = described_class.new('test.tar.gz')
24     expect(archive.send(:parse_flags, 'xf', :undef, 'tar')).to eq 'xf'
25     expect(archive.send(:parse_flags, 'xf', 'xvf', 'tar')).to eq 'xvf'
26     expect(archive.send(:parse_flags, 'xf', { 'tar' => 'xzf', '7z' => '-y x' }, 'tar')).to eq 'xzf'
27   end
28
29   it '#command on RedHat' do
30     Facter.stubs(:value).with(:osfamily).returns 'RedHat'
31
32     tar = described_class.new('test.tar.gz')
33     expect(tar.send(:command, :undef)).to eq 'tar xzf test.tar.gz'
34     expect(tar.send(:command, 'xvf')).to eq 'tar xvf test.tar.gz'
35     tar = described_class.new('test.tar.bz2')
36     expect(tar.send(:command, :undef)).to eq 'tar xjf test.tar.bz2'
37     expect(tar.send(:command, 'xjf')).to eq 'tar xjf test.tar.bz2'
38     tar = described_class.new('test.tar.xz')
39     expect(tar.send(:command, :undef)).to eq 'unxz -dc test.tar.xz | tar xf -'
40     gunzip = described_class.new('test.gz')
41     expect(gunzip.send(:command, :undef)).to eq 'gunzip -d test.gz'
42     zip = described_class.new('test.zip')
43     expect(zip.send(:command, :undef)).to eq 'unzip -o test.zip'
44     expect(zip.send(:command, '-a')).to eq 'unzip -a test.zip'
45
46     zip = described_class.new('/tmp/fun folder/test.zip')
47     expect(zip.send(:command, :undef)).to eq 'unzip -o /tmp/fun\ folder/test.zip'
48     expect(zip.send(:command, '-a')).to eq 'unzip -a /tmp/fun\ folder/test.zip'
49   end
50
51   system_v = %w[Solaris AIX]
52   system_v.each do |os|
53     it "#command on #{os}" do
54       Facter.stubs(:value).with(:osfamily).returns os
55
56       tar = described_class.new('test.tar.gz')
57       expect(tar.send(:command, :undef)).to eq 'gunzip -dc test.tar.gz | tar xf -'
58       expect(tar.send(:command, 'gunzip' => '-dc', 'tar' => 'xvf')).to eq 'gunzip -dc test.tar.gz | tar xvf -'
59       tar = described_class.new('test.tar.bz2')
60       expect(tar.send(:command, :undef)).to eq 'bunzip2 -dc test.tar.bz2 | tar xf -'
61       expect(tar.send(:command, 'bunzip' => '-dc', 'tar' => 'xvf')).to eq 'bunzip2 -dc test.tar.bz2 | tar xvf -'
62       tar = described_class.new('test.tar.xz')
63       expect(tar.send(:command, :undef)).to eq 'unxz -dc test.tar.xz | tar xf -'
64       gunzip = described_class.new('test.gz')
65       expect(gunzip.send(:command, :undef)).to eq 'gunzip -d test.gz'
66       zip = described_class.new('test.zip')
67       expect(zip.send(:command, :undef)).to eq 'unzip -o test.zip'
68       expect(zip.send(:command, '-a')).to eq 'unzip -a test.zip'
69
70       zip = described_class.new('/tmp/fun folder/test.zip')
71       expect(zip.send(:command, :undef)).to eq 'unzip -o /tmp/fun\ folder/test.zip'
72       expect(zip.send(:command, '-a')).to eq 'unzip -a /tmp/fun\ folder/test.zip'
73     end
74   end
75
76   it '#command on Windows' do
77     Facter.stubs(:value).with(:osfamily).returns 'windows'
78
79     tar = described_class.new('test.tar.gz')
80     tar.stubs(:win_7zip).returns('7z.exe')
81     expect(tar.send(:command, :undef)).to eq '7z.exe x -aoa "test.tar.gz"'
82     expect(tar.send(:command, 'x -aot')).to eq '7z.exe x -aot "test.tar.gz"'
83
84     zip = described_class.new('test.zip')
85     zip.stubs(:win_7zip).returns('7z.exe')
86     expect(zip.send(:command, :undef)).to eq '7z.exe x -aoa "test.zip"'
87
88     zip = described_class.new('C:/Program Files/test.zip')
89     zip.stubs(:win_7zip).returns('7z.exe')
90     expect(zip.send(:command, :undef)).to eq '7z.exe x -aoa "C:/Program Files/test.zip"'
91
92     zip = described_class.new('C:/Program Files/test.zip')
93     zip.stubs(:win_7zip).returns('powershell')
94     expect(zip.send(:command, :undef)).to eq 'powershell'
95   end
96 end