File: archive_spec.rb

package info (click to toggle)
ruby-mixlib-archive 0.2.0-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 156 kB
  • ctags: 22
  • sloc: ruby: 233; makefile: 3
file content (93 lines) | stat: -rw-r--r-- 2,968 bytes parent folder | download
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
require "spec_helper"

describe Mixlib::Archive do
  let(:target) { "../fixtures/foo.tar" }
  let (:destination) { Dir.mktmpdir }

  let (:archive) { described_class.new(target) }
  let (:extractor) { double(Mixlib::Archive::Tar, extract: true) }

  before do
    allow(File).to receive(:expand_path).and_call_original
    allow(Mixlib::Archive::Tar).to receive(:new).with(any_args).and_return(extractor)
  end

  after do
    FileUtils.rmdir destination
  end

  describe "#initialize" do
    it "accepts a path" do
      expect { described_class.new("../foo") }.not_to raise_error
    end
    it "allows the target to be emptied" do
      expect { described_class.new("../foo", empty: true) }.not_to raise_error
    end

    context "creates an extractor" do
      let(:expanded_target) { "/my/expanded/target/fixtures/foo.tar" }
      before do
      end

      it "with the correct extractor" do
        expect(File).to receive(:expand_path).with(target).and_return(expanded_target)
        expect(Mixlib::Archive::Tar).to receive(:new).with(expanded_target)
        described_class.new(target)
      end
    end
  end

  describe "#extract" do
    it "creates the target directory" do
      expect(archive).to receive(:create_and_empty).with(destination).and_return(true)
      archive.extract(destination)
    end

    it "runs the extractor" do
      expect(extractor).to receive(:extract).with(destination, { perms: true, ignore: [/^\.$/, /\.{2}/] })
      archive.extract(destination)
    end

    it "passes options to the extractor" do
      expect(extractor).to receive(:extract).with(destination, { perms: false, ignore: [/^\.$/, /\.{2}/] })
      archive.extract(destination, perms: false)
    end
  end

  describe "#create_and_empty" do
    it "creates the target directory" do
      expect(FileUtils).to receive(:mkdir_p).with(destination)
      archive.send(:create_and_empty, destination)
    end

    it "doesn't empty the target directory" do
      expect(Dir).not_to receive(:foreach)
      archive.send(:create_and_empty, destination)
    end

    context "optionally empties the target directory" do
      before do
        archive.instance_variable_set(:@empty, true)
      end

      it "walks the directory" do
        expect(Dir).to receive(:foreach).with(destination)
        archive.send(:create_and_empty, destination)
      end

      it "ignores . and .." do
        expect(Dir).to receive(:foreach).with(destination).and_yield(".").and_yield("..")
        expect(FileUtils).not_to receive(:remove_entry_secure)
        archive.send(:create_and_empty, destination)
      end

      it "deletes the entry" do
        deleted = File.join(destination, "foo")

        expect(Dir).to receive(:foreach).with(destination).and_yield(".").and_yield("..").and_yield("foo")
        expect(FileUtils).to receive(:remove_entry_secure).with(deleted).and_return(true)
        archive.send(:create_and_empty, destination)
      end
    end
  end
end