aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/requirement_spec.rb
blob: 2d0d86c86202d2061b0a8a3487f9823005d2764c (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
require "extend/ENV"
require "requirement"

describe Requirement do
  alias_matcher :have_a_default_formula, :be_a_default_formula
  alias_matcher :be_a_build_requirement, :be_a_build

  subject { klass.new }

  let(:klass) { Class.new(described_class) }

  describe "#tags" do
    subject { described_class.new(tags) }

    context "single tag" do
      let(:tags) { ["bar"] }

      its(:tags) { are_expected.to eq(tags) }
    end

    context "multiple tags" do
      let(:tags) { ["bar", "baz"] }

      its(:tags) { are_expected.to eq(tags) }
    end

    context "symbol tags" do
      let(:tags) { [:build] }

      its(:tags) { are_expected.to eq(tags) }
    end

    context "symbol and string tags" do
      let(:tags) { [:build, "bar"] }

      its(:tags) { are_expected.to eq(tags) }
    end
  end

  describe "#fatal?" do
    context "#fatal true is specified" do
      let(:klass) do
        Class.new(described_class) do
          fatal true
        end
      end

      it { is_expected.to be_fatal }
    end

    context "#fatal is omitted" do
      it { is_expected.not_to be_fatal }
    end
  end

  describe "#satisfied?" do
    context "#satisfy with block and build_env returns true" do
      let(:klass) do
        Class.new(described_class) do
          satisfy(build_env: false) do
            true
          end
        end
      end

      it { is_expected.to be_satisfied }
    end

    context "#satisfy with block and build_env returns false" do
      let(:klass) do
        Class.new(described_class) do
          satisfy(build_env: false) do
            false
          end
        end
      end

      it { is_expected.not_to be_satisfied }
    end

    context "#satisfy returns true" do
      let(:klass) do
        Class.new(described_class) do
          satisfy true
        end
      end

      it { is_expected.to be_satisfied }
    end

    context "#satisfy returns false" do
      let(:klass) do
        Class.new(described_class) do
          satisfy false
        end
      end

      it { is_expected.not_to be_satisfied }
    end

    context "#satisfy with block returning true and without :build_env" do
      let(:klass) do
        Class.new(described_class) do
          satisfy do
            true
          end
        end
      end

      it "sets up build environment" do
        expect(ENV).to receive(:with_build_environment).and_call_original
        subject.satisfied?
      end
    end

    context "#satisfy with block returning true and :build_env set to false" do
      let(:klass) do
        Class.new(described_class) do
          satisfy(build_env: false) do
            true
          end
        end
      end

      it "skips setting up build environment" do
        expect(ENV).not_to receive(:with_build_environment)
        subject.satisfied?
      end
    end

    context "#satisfy with block returning path and without :build_env" do
      let(:klass) do
        Class.new(described_class) do
          satisfy do
            Pathname.new("/foo/bar/baz")
          end
        end
      end

      it "infers path from #satisfy result" do
        expect(ENV).to receive(:prepend_path).with("PATH", Pathname.new("/foo/bar"))
        subject.satisfied?
        subject.modify_build_environment
      end
    end
  end

  describe "#build?" do
    context ":build tag is specified" do
      subject { described_class.new([:build]) }

      it { is_expected.to be_a_build_requirement }
    end

    context "#build omitted" do
      it { is_expected.not_to be_a_build_requirement }
    end
  end

  describe "#name and #option_names" do
    let(:const) { :FooRequirement }
    let(:klass) { self.class.const_get(const) }

    before(:each) do
      self.class.const_set(const, Class.new(described_class))
    end

    after(:each) do
      self.class.send(:remove_const, const)
    end

    its(:name) { is_expected.to eq("foo") }
    its(:option_names) { are_expected.to eq(["foo"]) }
  end

  describe "#default_formula?" do
    context "#default_formula specified" do
      let(:klass) do
        Class.new(described_class) do
          default_formula "foo"
        end
      end

      it { is_expected.to have_a_default_formula }
    end

    context "#default_formula omitted" do
      it { is_expected.not_to have_a_default_formula }
    end
  end

  describe "#to_dependency" do
    let(:klass) do
      Class.new(described_class) do
        default_formula "foo"
      end
    end

    it "returns a Dependency for its default Formula" do
      expect(subject.to_dependency).to eq(Dependency.new("foo"))
    end

    context "#modify_build_environment" do
      context "with error" do
        let(:klass) do
          Class.new(described_class) do
            class ModifyBuildEnvironmentError < StandardError; end

            default_formula "foo"

            satisfy do
              true
            end

            env do
              raise ModifyBuildEnvironmentError
            end
          end
        end

        it "raises an error" do
          expect {
            subject.to_dependency.modify_build_environment
          }.to raise_error(klass.const_get(:ModifyBuildEnvironmentError))
        end
      end
    end
  end

  describe "#modify_build_environment" do
    context "without env proc" do
      let(:klass) { Class.new(described_class) }

      it "returns nil" do
        expect(subject.modify_build_environment).to be nil
      end
    end
  end

  describe "#eql? and #==" do
    subject { described_class.new }

    it "returns true if the names and tags are equal" do
      other = described_class.new

      expect(subject).to eql(other)
      expect(subject).to eq(other)
    end

    it "returns false if names differ" do
      other = described_class.new
      allow(other).to receive(:name).and_return("foo")
      expect(subject).not_to eql(other)
      expect(subject).not_to eq(other)
    end

    it "returns false if tags differ" do
      other = described_class.new([:optional])

      expect(subject).not_to eql(other)
      expect(subject).not_to eq(other)
    end
  end

  describe "#hash" do
    subject { described_class.new }

    it "is equal if names and tags are equal" do
      other = described_class.new
      expect(subject.hash).to eq(other.hash)
    end

    it "differs if names differ" do
      other = described_class.new
      allow(other).to receive(:name).and_return("foo")
      expect(subject.hash).not_to eq(other.hash)
    end

    it "differs if tags differ" do
      other = described_class.new([:optional])
      expect(subject.hash).not_to eq(other.hash)
    end
  end
end