brew/Library/Homebrew/test/rubocops/patches_spec.rb

253 lines
9.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "rubocops/patches"
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
2017-10-21 03:12:50 +02:00
expect_no_offenses(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
end
2017-10-21 03:12:50 +02:00
RUBY
end
it "Formula with `def patches`" do
2017-10-21 03:12:50 +02:00
expect_offense(<<~RUBY)
class Foo < Formula
homepage "ftp://brew.sh/foo"
url "https://brew.sh/foo-1.0.tgz"
def patches
2017-10-21 03:12:50 +02:00
^^^^^^^^^^^ Use the patch DSL instead of defining a 'patches' method
DATA
end
end
2017-10-21 03:12:50 +02:00
RUBY
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|
2017-10-15 02:28:32 +02:00
source = <<~EOS
class Foo < Formula
homepage "ftp://brew.sh/foo"
url "https://brew.sh/foo-1.0.tgz"
def patches
"#{patch_url}"
end
end
EOS
2017-10-07 22:31:23 +02:00
inspect_source(source)
expected_offense = if patch_url.match?(%r{/raw\.github\.com/})
[{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 12,
source: source }]
elsif patch_url.match?(%r{macports/trunk})
[{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 33,
source: source }]
elsif patch_url.match?(%r{^http://trac\.macports\.org})
[{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 5,
source: source }]
elsif patch_url.match?(%r{^http://bugs\.debian\.org})
[{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 5,
source: source }]
# rubocop:disable Metrics/LineLength
elsif patch_url.match?(%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)})
# rubocop:enable Metrics/LineLength
[{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 5,
source: source }]
elsif patch_url.match?(%r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)})
[{ message:
2018-11-02 17:18:07 +00:00
<<~EOS,
GitHub patches should use the full_index parameter:
#{patch_url}?full_index=1
EOS
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 5,
source: source }]
end
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])
end
end
end
it "Formula with nested `def patches`" do
2018-07-11 15:17:40 +02:00
source = <<~RUBY
class Foo < Formula
homepage "ftp://brew.sh/foo"
url "https://brew.sh/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
2018-07-11 15:17:40 +02:00
RUBY
2018-11-02 17:18:07 +00:00
expected_offenses = [{ message: "Use the patch DSL instead of defining a 'patches' method",
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 4,
column: 2,
source: source },
{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 8,
column: 26,
source: source }]
2017-10-07 22:31:23 +02:00
inspect_source(source)
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])
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|
2018-07-11 15:17:40 +02:00
source = <<~RUBY
class Foo < Formula
homepage "ftp://brew.sh/foo"
url "https://brew.sh/foo-1.0.tgz"
patch do
url "#{patch_url}"
sha256 "63376b8fdd6613a91976106d9376069274191860cd58f039b29ff16de1925621"
end
end
2018-07-11 15:17:40 +02:00
RUBY
2017-10-07 22:31:23 +02:00
inspect_source(source)
expected_offense = if patch_url.match?(%r{/raw\.github\.com/})
[{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 16,
source: source }]
elsif patch_url.match?(%r{macports/trunk})
[{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 37,
source: source }]
elsif patch_url.match?(%r{^http://trac\.macports\.org})
[{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 9,
source: source }]
elsif patch_url.match?(%r{^http://bugs\.debian\.org})
[{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 9,
source: source }]
# rubocop:disable Metrics/LineLength
elsif patch_url.match?(%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)})
# rubocop:enable Metrics/LineLength
[{ 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
severity: :convention,
2018-11-02 17:18:07 +00:00
line: 5,
column: 9,
source: source }]
end
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])
end
end
end
end
end