aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/cask/download_strategy_spec.rb
blob: 27f1ad410bf3c75fbde98b796c58a1e37ebd9fd7 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
describe "download strategies", :cask do
  let(:url) { "http://example.com/cask.dmg" }
  let(:url_options) { {} }
  let(:cask) {
    instance_double(Hbc::Cask, token:   "some-cask",
                               url:     Hbc::URL.new(url, url_options),
                               version: "1.2.3.4")
  }

  describe Hbc::CurlDownloadStrategy do
    let(:downloader) { Hbc::CurlDownloadStrategy.new(cask) }

    before do
      allow(downloader.temporary_path).to receive(:rename)
    end

    it "properly assigns a name and uri based on the Cask" do
      expect(downloader.name).to eq("some-cask")
      expect(downloader.url).to eq("http://example.com/cask.dmg")
      expect(downloader.version.to_s).to eq("1.2.3.4")
    end

    it "calls curl with default arguments for a simple Cask" do
      allow(downloader).to receive(:curl)

      shutup do
        downloader.fetch
      end

      expect(downloader).to have_received(:curl).with(
        cask.url.to_s,
        "-C", 0,
        "-o", kind_of(Pathname)
      )
    end

    context "with an explicit user agent" do
      let(:url_options) { { user_agent: "Mozilla/25.0.1" } }

      it "adds the appropriate curl args" do
        curl_args = []
        allow(downloader).to receive(:curl) { |*args| curl_args = args }

        shutup do
          downloader.fetch
        end

        expect(curl_args.each_cons(2)).to include(["-A", "Mozilla/25.0.1"])
      end
    end

    context "with a generalized fake user agent" do
      let(:url_options) { { user_agent: :fake } }

      it "adds the appropriate curl args" do
        curl_args = []
        allow(downloader).to receive(:curl) { |*args| curl_args = args }

        shutup do
          downloader.fetch
        end

        expect(curl_args.each_cons(2)).to include(["-A", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10) https://caskroom.github.io"])
      end
    end

    context "with cookies set" do
      let(:url_options) {
        {
          cookies: {
            coo: "kie",
            mon: "ster",
          },
        }
      }

      it "adds curl args for cookies" do
        curl_args = []
        allow(downloader).to receive(:curl) { |*args| curl_args = args }

        shutup do
          downloader.fetch
        end

        expect(curl_args.each_cons(2)).to include(["-b", "coo=kie;mon=ster"])
      end
    end

    context "with referer set" do
      let(:url_options) { { referer: "http://somehost/also" } }

      it "adds curl args for referer" do
        curl_args = []
        allow(downloader).to receive(:curl) { |*args| curl_args = args }

        shutup do
          downloader.fetch
        end

        expect(curl_args.each_cons(2)).to include(["-e", "http://somehost/also"])
      end
    end
  end

  describe Hbc::CurlPostDownloadStrategy do
    let(:downloader) { Hbc::CurlPostDownloadStrategy.new(cask) }

    before do
      allow(downloader.temporary_path).to receive(:rename)
    end

    context "with :using and :data specified" do
      let(:url_options) {
        {
          using: :post,
          data:  {
            form: "data",
            is:   "good",
          },
        }
      }

      it "adds curl args for post arguments" do
        curl_args = []
        allow(downloader).to receive(:curl) { |*args| curl_args = args }

        shutup do
          downloader.fetch
        end

        expect(curl_args.each_cons(2)).to include(["-d", "form=data"])
        expect(curl_args.each_cons(2)).to include(["-d", "is=good"])
      end
    end

    context "with :using but no :data" do
      let(:url_options) { { using: :post } }

      it "adds curl args for a POST request" do
        curl_args = []
        allow(downloader).to receive(:curl) { |*args| curl_args = args }

        shutup do
          downloader.fetch
        end

        expect(curl_args.each_cons(2)).to include(["-X", "POST"])
      end
    end
  end

  describe Hbc::SubversionDownloadStrategy do
    let(:url_options) { { using: :svn } }
    let(:fake_system_command) { class_double(Hbc::SystemCommand) }
    let(:downloader) { Hbc::SubversionDownloadStrategy.new(cask, fake_system_command) }
    before do
      allow(fake_system_command).to receive(:run!)
    end

    it "returns a tarball path on fetch" do
      allow(downloader).to receive(:compress)
      allow(downloader).to receive(:fetch_repo)

      retval = shutup { downloader.fetch }

      expect(retval).to equal(downloader.tarball_path)
    end

    it "calls fetch_repo with default arguments for a simple Cask" do
      allow(downloader).to receive(:compress)
      allow(downloader).to receive(:fetch_repo)

      shutup do
        downloader.fetch
      end

      expect(downloader).to have_received(:fetch_repo).with(
        downloader.cached_location,
        cask.url.to_s,
      )
    end

    it "calls svn with default arguments for a simple Cask" do
      allow(downloader).to receive(:compress)

      shutup do
        downloader.fetch
      end

      expect(fake_system_command).to have_received(:run!).with(
        "/usr/bin/svn",
        hash_including(args: [
                         "checkout",
                         "--force",
                         "--config-option",
                         "config:miscellany:use-commit-times=yes",
                         cask.url.to_s,
                         downloader.cached_location,
                       ]),
      )
    end

    context "with trust_cert set on the URL" do
      let(:url_options) {
        {
          using:      :svn,
          trust_cert: true,
        }
      }

      it "adds svn arguments for :trust_cert" do
        allow(downloader).to receive(:compress)

        shutup do
          downloader.fetch
        end

        expect(fake_system_command).to have_received(:run!).with(
          "/usr/bin/svn",
          hash_including(args: [
                           "checkout",
                           "--force",
                           "--config-option",
                           "config:miscellany:use-commit-times=yes",
                           "--trust-server-cert",
                           "--non-interactive",
                           cask.url.to_s,
                           downloader.cached_location,
                         ]),
        )
      end
    end

    context "with :revision set on url" do
      let(:url_options) {
        {
          using:    :svn,
          revision: "10",
        }
      }

      it "adds svn arguments for :revision" do
        allow(downloader).to receive(:compress)

        shutup do
          downloader.fetch
        end

        expect(fake_system_command).to have_received(:run!).with(
          "/usr/bin/svn",
          hash_including(args: [
                           "checkout",
                           "--force",
                           "--config-option",
                           "config:miscellany:use-commit-times=yes",
                           cask.url.to_s,
                           downloader.cached_location,
                           "-r",
                           "10",
                         ]),
        )
      end
    end

    it "runs tar to serialize svn downloads" do
      # sneaky stub to remake the directory, since homebrew code removes it
      # before tar is called
      allow(downloader).to receive(:fetch_repo) {
        downloader.cached_location.mkdir
      }

      shutup do
        downloader.fetch
      end

      expect(fake_system_command).to have_received(:run!).with(
        "/usr/bin/tar",
        hash_including(args: [
                         '-s/^\\.//',
                         "--exclude",
                         ".svn",
                         "-cf",
                         downloader.tarball_path,
                         "--",
                         ".",
                       ]),
      )
    end
  end

  # does not work yet, because (for unknown reasons), the tar command
  # returns an error code when running under the test suite
  # it 'creates a tarball matching the expected checksum' do
  #   cask = Hbc::CaskLoader.load('svn-download-check-cask')
  #   downloader = Hbc::SubversionDownloadStrategy.new(cask)
  #   # special mocking required for tar to have something to work with
  #   def downloader.fetch_repo(target, url, revision = nil, ignore_externals=false)
  #     target.mkpath
  #     FileUtils.touch(target.join('empty_file.txt'))
  #     File.utime(1000,1000,target.join('empty_file.txt'))
  #   end
  #   expect(shutup { downloader.fetch }).to equal(downloader.tarball_path)
  #   d = Hbc::Download.new(cask)
  #   d.send(:_check_sums, downloader.tarball_path, cask.sums)
  # end
end