2018-10-26 19:41:14 +01:00
|
|
|
require "rubocops/patches"
|
2017-05-25 22:21:23 +05:30
|
|
|
|
2017-08-01 20:59:17 +05:30
|
|
|
describe RuboCop::Cop::FormulaAudit::Patches do
|
2017-05-25 22:21:23 +05:30
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
|
|
|
context "When auditing legacy patches" do
|
|
|
|
it "When there is no legacy patch" do
|
2017-10-21 03:12:50 +02:00
|
|
|
expect_no_offenses(<<~RUBY)
|
2017-05-25 22:21:23 +05:30
|
|
|
class Foo < Formula
|
2018-11-28 20:51:55 +01:00
|
|
|
url 'https://brew.sh/foo-1.0.tgz'
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
2017-10-21 03:12:50 +02:00
|
|
|
RUBY
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "Formula with `def patches`" do
|
2017-10-21 03:12:50 +02:00
|
|
|
expect_offense(<<~RUBY)
|
2017-05-25 22:21:23 +05:30
|
|
|
class Foo < Formula
|
2018-11-28 20:51:55 +01:00
|
|
|
homepage "ftp://brew.sh/foo"
|
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
2017-05-25 22:21:23 +05:30
|
|
|
def patches
|
2017-10-21 03:12:50 +02:00
|
|
|
^^^^^^^^^^^ Use the patch DSL instead of defining a 'patches' method
|
2017-05-25 22:21:23 +05:30
|
|
|
DATA
|
|
|
|
end
|
|
|
|
end
|
2017-10-21 03:12:50 +02:00
|
|
|
RUBY
|
2017-05-25 22:21:23 +05:30
|
|
|
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",
|
2017-08-01 20:59:17 +05:30
|
|
|
"https://patch-diff.githubusercontent.com/raw/foo/foo-bar/pull/100.patch",
|
2017-08-25 20:07:45 +05:30
|
|
|
"https://github.com/dlang/dub/pull/1221.patch",
|
2017-05-25 22:21:23 +05:30
|
|
|
]
|
|
|
|
patch_urls.each do |patch_url|
|
2017-10-15 02:28:32 +02:00
|
|
|
source = <<~EOS
|
2017-05-25 22:21:23 +05:30
|
|
|
class Foo < Formula
|
2018-11-28 20:51:55 +01:00
|
|
|
homepage "ftp://brew.sh/foo"
|
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
2017-05-25 22:21:23 +05:30
|
|
|
def patches
|
|
|
|
"#{patch_url}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
|
2017-10-07 22:31:23 +02:00
|
|
|
inspect_source(source)
|
2017-08-25 20:07:45 +05:30
|
|
|
expected_offense = if patch_url =~ %r{/raw\.github\.com/}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS.chomp,
|
|
|
|
GitHub/Gist patches should specify a revision:
|
2019-04-01 16:02:13 -04:00
|
|
|
#{patch_url}
|
2018-11-02 17:18:07 +00:00
|
|
|
EOS
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 12,
|
|
|
|
source: source }]
|
2017-05-25 22:21:23 +05:30
|
|
|
elsif patch_url =~ %r{macports/trunk}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS.chomp,
|
|
|
|
MacPorts patches should specify a revision instead of trunk:
|
2019-04-01 16:02:13 -04:00
|
|
|
#{patch_url}
|
2018-11-02 17:18:07 +00:00
|
|
|
EOS
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 33,
|
|
|
|
source: source }]
|
2017-05-25 22:21:23 +05:30
|
|
|
elsif patch_url =~ %r{^http://trac\.macports\.org}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS.chomp,
|
|
|
|
Patches from MacPorts Trac should be https://, not http:
|
2019-04-01 16:02:13 -04:00
|
|
|
#{patch_url}
|
2018-11-02 17:18:07 +00:00
|
|
|
EOS
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 5,
|
|
|
|
source: source }]
|
2017-05-25 22:21:23 +05:30
|
|
|
elsif patch_url =~ %r{^http://bugs\.debian\.org}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS.chomp,
|
|
|
|
Patches from Debian should be https://, not http:
|
2019-04-01 16:02:13 -04:00
|
|
|
#{patch_url}
|
2018-11-02 17:18:07 +00:00
|
|
|
EOS
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 5,
|
|
|
|
source: source }]
|
2017-08-01 20:59:17 +05:30
|
|
|
elsif patch_url =~ %r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS,
|
|
|
|
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
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 5,
|
|
|
|
source: source }]
|
2017-08-25 20:07:45 +05:30
|
|
|
elsif patch_url =~ %r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS,
|
|
|
|
GitHub patches should use the full_index parameter:
|
|
|
|
#{patch_url}?full_index=1
|
|
|
|
EOS
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 5,
|
|
|
|
source: source }]
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
2017-08-25 20:07:45 +05:30
|
|
|
expected_offense.zip([cop.offenses.last]).each do |expected, actual|
|
2017-10-21 03:12:50 +02:00
|
|
|
expect(actual.message).to eq(expected[:message])
|
|
|
|
expect(actual.severity).to eq(expected[:severity])
|
|
|
|
expect(actual.line).to eq(expected[:line])
|
|
|
|
expect(actual.column).to eq(expected[:column])
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "Formula with nested `def patches`" do
|
2018-07-11 15:17:40 +02:00
|
|
|
source = <<~RUBY
|
2017-05-25 22:21:23 +05:30
|
|
|
class Foo < Formula
|
2018-11-28 20:51:55 +01:00
|
|
|
homepage "ftp://brew.sh/foo"
|
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
2017-05-25 22:21:23 +05:30
|
|
|
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
|
2018-07-11 15:17:40 +02:00
|
|
|
RUBY
|
2017-05-25 22:21:23 +05:30
|
|
|
|
2018-11-02 17:18:07 +00:00
|
|
|
expected_offenses = [{ message: "Use the patch DSL instead of defining a 'patches' method",
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 4,
|
|
|
|
column: 2,
|
|
|
|
source: source },
|
2018-09-02 20:14:54 +01:00
|
|
|
{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS.chomp,
|
|
|
|
Patches from MacPorts Trac should be https://, not http:
|
2019-04-01 16:02:13 -04:00
|
|
|
http://trac.macports.org/export/68507/trunk/dports/net/trafshow/files/
|
2018-11-02 17:18:07 +00:00
|
|
|
EOS
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 8,
|
|
|
|
column: 26,
|
|
|
|
source: source }]
|
2017-05-25 22:21:23 +05:30
|
|
|
|
2017-10-07 22:31:23 +02:00
|
|
|
inspect_source(source)
|
2017-05-25 22:21:23 +05:30
|
|
|
|
|
|
|
expected_offenses.zip(cop.offenses).each do |expected, actual|
|
2017-10-21 03:12:50 +02:00
|
|
|
expect(actual.message).to eq(expected[:message])
|
|
|
|
expect(actual.severity).to eq(expected[:severity])
|
|
|
|
expect(actual.line).to eq(expected[:line])
|
|
|
|
expect(actual.column).to eq(expected[:column])
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-01 20:59:17 +05:30
|
|
|
|
|
|
|
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|
|
2018-07-11 15:17:40 +02:00
|
|
|
source = <<~RUBY
|
2017-08-01 20:59:17 +05:30
|
|
|
class Foo < Formula
|
2018-11-28 20:51:55 +01:00
|
|
|
homepage "ftp://brew.sh/foo"
|
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
2017-08-01 20:59:17 +05:30
|
|
|
patch do
|
|
|
|
url "#{patch_url}"
|
|
|
|
sha256 "63376b8fdd6613a91976106d9376069274191860cd58f039b29ff16de1925621"
|
|
|
|
end
|
|
|
|
end
|
2018-07-11 15:17:40 +02:00
|
|
|
RUBY
|
2017-08-01 20:59:17 +05:30
|
|
|
|
2017-10-07 22:31:23 +02:00
|
|
|
inspect_source(source)
|
2017-08-25 20:07:45 +05:30
|
|
|
expected_offense = if patch_url =~ %r{/raw\.github\.com/}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS.chomp,
|
|
|
|
GitHub/Gist patches should specify a revision:
|
2019-04-01 16:02:13 -04:00
|
|
|
#{patch_url}
|
2018-11-02 17:18:07 +00:00
|
|
|
EOS
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 16,
|
|
|
|
source: source }]
|
2017-08-01 20:59:17 +05:30
|
|
|
elsif patch_url =~ %r{macports/trunk}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS.chomp,
|
|
|
|
MacPorts patches should specify a revision instead of trunk:
|
2019-04-01 16:02:13 -04:00
|
|
|
#{patch_url}
|
2018-11-02 17:18:07 +00:00
|
|
|
EOS
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 37,
|
|
|
|
source: source }]
|
2017-08-01 20:59:17 +05:30
|
|
|
elsif patch_url =~ %r{^http://trac\.macports\.org}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS.chomp,
|
|
|
|
Patches from MacPorts Trac should be https://, not http:
|
2019-04-01 16:02:13 -04:00
|
|
|
#{patch_url}
|
2018-11-02 17:18:07 +00:00
|
|
|
EOS
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 9,
|
|
|
|
source: source }]
|
2017-08-01 20:59:17 +05:30
|
|
|
elsif patch_url =~ %r{^http://bugs\.debian\.org}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS.chomp,
|
|
|
|
Patches from Debian should be https://, not http:
|
2019-04-01 16:02:13 -04:00
|
|
|
#{patch_url}
|
2018-11-02 17:18:07 +00:00
|
|
|
EOS
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 9,
|
|
|
|
source: source }]
|
2017-08-01 20:59:17 +05:30
|
|
|
elsif patch_url =~ %r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)}
|
2018-09-02 20:14:54 +01:00
|
|
|
[{ message:
|
2018-11-02 17:18:07 +00:00
|
|
|
<<~EOS,
|
|
|
|
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
|
2017-08-25 20:07:45 +05:30
|
|
|
severity: :convention,
|
2018-11-02 17:18:07 +00:00
|
|
|
line: 5,
|
|
|
|
column: 9,
|
|
|
|
source: source }]
|
2017-08-01 20:59:17 +05:30
|
|
|
end
|
2017-08-25 20:07:45 +05:30
|
|
|
expected_offense.zip([cop.offenses.last]).each do |expected, actual|
|
2017-10-21 03:12:50 +02:00
|
|
|
expect(actual.message).to eq(expected[:message])
|
|
|
|
expect(actual.severity).to eq(expected[:severity])
|
|
|
|
expect(actual.line).to eq(expected[:line])
|
|
|
|
expect(actual.column).to eq(expected[:column])
|
2017-08-01 20:59:17 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-25 22:21:23 +05:30
|
|
|
end
|