aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/patch_spec.rb
blob: 56f8f7ea8494dbce04b79f9fbc94a06c28963225 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require "patch"

describe Patch do
  describe "#create" do
    context "simple patch" do
      subject { described_class.create(:p2, nil) }
      it { is_expected.to be_kind_of ExternalPatch }
      it { is_expected.to be_external }
      its(:strip) { is_expected.to eq(:p2) }
    end

    context "string patch" do
      subject { described_class.create(:p0, "foo") }
      it { is_expected.to be_kind_of StringPatch }
      its(:strip) { is_expected.to eq(:p0) }
    end

    context "string patch without strip" do
      subject { described_class.create("foo", nil) }
      it { is_expected.to be_kind_of StringPatch }
      its(:strip) { is_expected.to eq(:p1) }
    end

    context "data patch" do
      subject { described_class.create(:p0, :DATA) }
      it { is_expected.to be_kind_of DATAPatch }
      its(:strip) { is_expected.to eq(:p0) }
    end

    context "data patch without strip" do
      subject { described_class.create(:DATA, nil) }
      it { is_expected.to be_kind_of DATAPatch }
      its(:strip) { is_expected.to eq(:p1) }
    end

    it "raises an error for unknown values" do
      expect {
        described_class.create(Object.new)
      }.to raise_error(ArgumentError)

      expect {
        described_class.create(Object.new, Object.new)
      }.to raise_error(ArgumentError)
    end
  end

  describe "#patch_files" do
    subject { described_class.create(:p2, nil) }

    context "empty patch" do
      its(:resource) { is_expected.to be_kind_of Resource::PatchResource }
      its(:patch_files) { is_expected.to eq(subject.resource.patch_files) }
      its(:patch_files) { is_expected.to eq([]) }
    end

    it "returns applied patch files" do
      subject.resource.apply("patch1.diff")
      expect(subject.patch_files).to eq(["patch1.diff"])

      subject.resource.apply("patch2.diff", "patch3.diff")
      expect(subject.patch_files).to eq(["patch1.diff", "patch2.diff", "patch3.diff"])

      subject.resource.apply(["patch4.diff", "patch5.diff"])
      expect(subject.patch_files.count).to eq(5)

      subject.resource.apply("patch4.diff", ["patch5.diff", "patch6.diff"], "patch7.diff")
      expect(subject.patch_files.count).to eq(7)
    end
  end

  describe "#normalize_legacy_patches" do
    it "can create a patch from a single string" do
      patches = described_class.normalize_legacy_patches("http://example.com/patch.diff")
      expect(patches.length).to eq(1)
      expect(patches.first.strip).to eq(:p1)
    end

    it "can create patches from an array" do
      patches = described_class.normalize_legacy_patches(
        %w[http://example.com/patch1.diff http://example.com/patch2.diff],
      )

      expect(patches.length).to eq(2)
      expect(patches[0].strip).to eq(:p1)
      expect(patches[1].strip).to eq(:p1)
    end

    it "can create patches from a :p0 hash" do
      patches = Patch.normalize_legacy_patches(
        p0: "http://example.com/patch.diff",
      )

      expect(patches.length).to eq(1)
      expect(patches.first.strip).to eq(:p0)
    end

    it "can create patches from a :p1 hash" do
      patches = Patch.normalize_legacy_patches(
        p1: "http://example.com/patch.diff",
      )

      expect(patches.length).to eq(1)
      expect(patches.first.strip).to eq(:p1)
    end

    it "can create patches from a mixed hash" do
      patches = Patch.normalize_legacy_patches(
        p1: "http://example.com/patch1.diff",
        p0: "http://example.com/patch0.diff",
      )

      expect(patches.length).to eq(2)
      expect(patches.count { |p| p.strip == :p0 }).to eq(1)
      expect(patches.count { |p| p.strip == :p1 }).to eq(1)
    end

    it "can create patches from a mixed hash with array" do
      patches = Patch.normalize_legacy_patches(
        p1: [
          "http://example.com/patch10.diff",
          "http://example.com/patch11.diff",
        ],
        p0: [
          "http://example.com/patch00.diff",
          "http://example.com/patch01.diff",
        ],
      )

      expect(patches.length).to eq(4)
      expect(patches.count { |p| p.strip == :p0 }).to eq(2)
      expect(patches.count { |p| p.strip == :p1 }).to eq(2)
    end

    it "returns an empty array if given nil" do
      expect(Patch.normalize_legacy_patches(nil)).to be_empty
    end
  end
end

describe EmbeddedPatch do
  describe "#new" do
    subject { described_class.new(:p1) }
    its(:inspect) { is_expected.to eq("#<EmbeddedPatch: :p1>") }
  end
end

describe ExternalPatch do
  subject { described_class.new(:p1) { url "file:///my.patch" } }

  describe "#url" do
    its(:url) { is_expected.to eq("file:///my.patch") }
  end

  describe "#inspect" do
    its(:inspect) { is_expected.to eq('#<ExternalPatch: :p1 "file:///my.patch">') }
  end

  describe "#cached_download" do
    before(:each) do
      allow(subject.resource).to receive(:cached_download).and_return("/tmp/foo.tar.gz")
    end

    its(:cached_download) { is_expected.to eq("/tmp/foo.tar.gz") }
  end
end