aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/rubocops/patches_cop_spec.rb
blob: 4f9ca2df84b9131d204ac6934f112c99010c0691 (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
require "rubocop"
require "rubocop/rspec/support"
require_relative "../../extend/string"
require_relative "../../rubocops/patches_cop"

describe RuboCop::Cop::FormulaAudit::Patches do
  subject(:cop) { described_class.new }

  context "When auditing legacy patches" do
    it "When there is no legacy patch" do
      source = <<-EOS.undent
        class Foo < Formula
          url 'http://example.com/foo-1.0.tgz'
        end
      EOS
      inspect_source(source)
      expect(cop.offenses).to eq([])
    end

    it "Formula with `def patches`" do
      source = <<-EOS.undent
        class Foo < Formula
          homepage "ftp://example.com/foo"
          url "http://example.com/foo-1.0.tgz"
          def patches
            DATA
          end
        end
      EOS

      expected_offenses = [{ message: "Use the patch DSL instead of defining a 'patches' method",
                             severity: :convention,
                             line: 4,
                             column: 2,
                             source: source }]

      inspect_source(source)

      expected_offenses.zip(cop.offenses).each do |expected, actual|
        expect_offense(expected, actual)
      end
    end

    it "Patch URLs" do
      patch_urls = [
        "https://raw.github.com/mogaal/sendemail",
        "https://mirrors.ustc.edu.cn/macports/trunk/",
        "http://trac.macports.org/export/102865/trunk/dports/mail/uudeview/files/inews.c.patch",
        "http://bugs.debian.org/cgi-bin/bugreport.cgi?msg=5;filename=patch-libunac1.txt;att=1;bug=623340",
        "https://patch-diff.githubusercontent.com/raw/foo/foo-bar/pull/100.patch",
        "https://github.com/dlang/dub/pull/1221.patch",
      ]
      patch_urls.each do |patch_url|
        source = <<-EOS.undent
          class Foo < Formula
            homepage "ftp://example.com/foo"
            url "http://example.com/foo-1.0.tgz"
            def patches
              "#{patch_url}"
            end
          end
        EOS

        inspect_source(source)
        expected_offense = if patch_url =~ %r{/raw\.github\.com/}
          [{ message: <<-EOS.undent.chomp,
               GitHub/Gist patches should specify a revision:
               #{patch_url}
             EOS
             severity: :convention,
             line: 5,
             column: 12,
             source: source }]
        elsif patch_url =~ %r{macports/trunk}
          [{ message: <<-EOS.undent.chomp,
               MacPorts patches should specify a revision instead of trunk:
               #{patch_url}
             EOS
             severity: :convention,
             line: 5,
             column: 33,
             source: source }]
        elsif patch_url =~ %r{^http://trac\.macports\.org}
          [{ message: <<-EOS.undent.chomp,
               Patches from MacPorts Trac should be https://, not http:
               #{patch_url}
             EOS
             severity: :convention,
             line: 5,
             column: 5,
             source: source }]
        elsif patch_url =~ %r{^http://bugs\.debian\.org}
          [{ message: <<-EOS.undent.chomp,
               Patches from Debian should be https://, not http:
               #{patch_url}
             EOS
             severity: :convention,
             line: 5,
             column: 5,
             source: source }]
        elsif patch_url =~ %r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)}
          [{ message: <<-EOS.undent,
               use GitHub pull request URLs:
                 https://github.com/foo/foo-bar/pull/100.patch
               Rather than patch-diff:
                 https://patch-diff.githubusercontent.com/raw/foo/foo-bar/pull/100.patch
             EOS
             severity: :convention,
             line: 5,
             column: 5,
             source: source }]
        elsif patch_url =~ %r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)}
          [{ message: <<-EOS.undent,
               GitHub patches should use the full_index parameter:
                 #{patch_url}?full_index=1
             EOS
             severity: :convention,
             line: 5,
             column: 5,
             source: source }]
        end
        expected_offense.zip([cop.offenses.last]).each do |expected, actual|
          expect_offense(expected, actual)
        end
      end
    end

    it "Formula with nested `def patches`" do
      source = <<-EOS.undent
        class Foo < Formula
          homepage "ftp://example.com/foo"
          url "http://example.com/foo-1.0.tgz"
          def patches
            files = %w[patch-domain_resolver.c patch-colormask.c patch-trafshow.c patch-trafshow.1 patch-configure]
            {
              :p0 =>
              files.collect{|p| "http://trac.macports.org/export/68507/trunk/dports/net/trafshow/files/\#{p}"}
            }
          end
        end
      EOS

      expected_offenses = [{ message: "Use the patch DSL instead of defining a 'patches' method",
                             severity: :convention,
                             line: 4,
                             column: 2,
                             source: source },
                           { message: <<-EOS.undent.chomp,
                               Patches from MacPorts Trac should be https://, not http:
                               http://trac.macports.org/export/68507/trunk/dports/net/trafshow/files/
                             EOS
                             severity: :convention,
                             line: 8,
                             column: 26,
                             source: source }]

      inspect_source(source)

      expected_offenses.zip(cop.offenses).each do |expected, actual|
        expect_offense(expected, actual)
      end
    end
  end

  context "When auditing external patches" do
    it "Patch URLs" do
      patch_urls = [
        "https://raw.github.com/mogaal/sendemail",
        "https://mirrors.ustc.edu.cn/macports/trunk/",
        "http://trac.macports.org/export/102865/trunk/dports/mail/uudeview/files/inews.c.patch",
        "http://bugs.debian.org/cgi-bin/bugreport.cgi?msg=5;filename=patch-libunac1.txt;att=1;bug=623340",
        "https://patch-diff.githubusercontent.com/raw/foo/foo-bar/pull/100.patch",
      ]
      patch_urls.each do |patch_url|
        source = <<-EOS.undent
          class Foo < Formula
            homepage "ftp://example.com/foo"
            url "http://example.com/foo-1.0.tgz"
            patch do
              url "#{patch_url}"
              sha256 "63376b8fdd6613a91976106d9376069274191860cd58f039b29ff16de1925621"
            end
          end
        EOS

        inspect_source(source)
        expected_offense = if patch_url =~ %r{/raw\.github\.com/}
          [{ message: <<-EOS.undent.chomp,
               GitHub/Gist patches should specify a revision:
               #{patch_url}
             EOS
             severity: :convention,
             line: 5,
             column: 16,
             source: source }]
        elsif patch_url =~ %r{macports/trunk}
          [{ message: <<-EOS.undent.chomp,
               MacPorts patches should specify a revision instead of trunk:
               #{patch_url}
             EOS
             severity: :convention,
             line: 5,
             column: 37,
             source: source }]
        elsif patch_url =~ %r{^http://trac\.macports\.org}
          [{ message: <<-EOS.undent.chomp,
               Patches from MacPorts Trac should be https://, not http:
               #{patch_url}
             EOS
             severity: :convention,
             line: 5,
             column: 9,
             source: source }]
        elsif patch_url =~ %r{^http://bugs\.debian\.org}
          [{ message: <<-EOS.undent.chomp,
               Patches from Debian should be https://, not http:
               #{patch_url}
             EOS
             severity: :convention,
             line: 5,
             column: 9,
             source: source }]
        elsif patch_url =~ %r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)}
          [{ message: <<-EOS.undent,
               use GitHub pull request URLs:
                 https://github.com/foo/foo-bar/pull/100.patch
               Rather than patch-diff:
                 https://patch-diff.githubusercontent.com/raw/foo/foo-bar/pull/100.patch
             EOS
             severity: :convention,
             line: 5,
             column: 9,
             source: source }]
        end
        expected_offense.zip([cop.offenses.last]).each do |expected, actual|
          expect_offense(expected, actual)
        end
      end
    end
  end
end