style: only require tag for git urls with --strict
This commit is contained in:
parent
c84b5f985f
commit
a91730316c
@ -316,23 +316,47 @@ module RuboCop
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# This cop makes sure that git urls have both a `tag` and `revision`.
|
# This cop makes sure that git urls have both a `revision`.
|
||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
class GitUrls < FormulaCop
|
class GitUrls < FormulaCop
|
||||||
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
||||||
|
return unless formula_tap == "homebrew-core"
|
||||||
|
|
||||||
find_method_calls_by_name(body_node, :url).each do |url|
|
find_method_calls_by_name(body_node, :url).each do |url|
|
||||||
next unless string_content(parameters(url).first).match?(/\.git$/)
|
next unless string_content(parameters(url).first).match?(/\.git$/)
|
||||||
|
next if url_has_revision?(parameters(url).last)
|
||||||
next if url_has_tag_and_revision?(parameters(url).last)
|
|
||||||
|
|
||||||
offending_node(url)
|
offending_node(url)
|
||||||
problem "Specify a `tag` and `revision` for git urls"
|
problem "Formulae in homebrew/core should specify a revision for git urls"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def_node_matcher :url_has_tag_and_revision?, <<~EOS
|
def_node_matcher :url_has_revision?, <<~EOS
|
||||||
(hash <(pair (sym :tag) str) (pair (sym :revision) str) ...>)
|
(hash <(pair (sym :revision) str) ...>)
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module FormulaAuditStrict
|
||||||
|
# This cop makes sure that git urls have both a `tag`.
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
class GitUrls < FormulaCop
|
||||||
|
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
||||||
|
return unless formula_tap == "homebrew-core"
|
||||||
|
|
||||||
|
find_method_calls_by_name(body_node, :url).each do |url|
|
||||||
|
next unless string_content(parameters(url).first).match?(/\.git$/)
|
||||||
|
next if url_has_tag?(parameters(url).last)
|
||||||
|
|
||||||
|
offending_node(url)
|
||||||
|
problem "Formulae in homebrew/core should specify a tag for git urls"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def_node_matcher :url_has_tag?, <<~EOS
|
||||||
|
(hash <(pair (sym :tag) str) ...>)
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -282,7 +282,7 @@ describe RuboCop::Cop::FormulaAudit::GitUrls do
|
|||||||
|
|
||||||
context "when a git URL is used" do
|
context "when a git URL is used" do
|
||||||
it "reports no offenses with a non-git url" do
|
it "reports no offenses with a non-git url" do
|
||||||
expect_no_offenses(<<~RUBY)
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
desc "foo"
|
desc "foo"
|
||||||
url "https://foo.com"
|
url "https://foo.com"
|
||||||
@ -291,7 +291,7 @@ describe RuboCop::Cop::FormulaAudit::GitUrls do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "reports no offenses with both a tag and a revision" do
|
it "reports no offenses with both a tag and a revision" do
|
||||||
expect_no_offenses(<<~RUBY)
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
desc "foo"
|
desc "foo"
|
||||||
url "https://github.com/foo/bar.git",
|
url "https://github.com/foo/bar.git",
|
||||||
@ -302,7 +302,7 @@ describe RuboCop::Cop::FormulaAudit::GitUrls do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "reports no offenses with both a tag, revision and `shallow` before" do
|
it "reports no offenses with both a tag, revision and `shallow` before" do
|
||||||
expect_no_offenses(<<~RUBY)
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
desc "foo"
|
desc "foo"
|
||||||
url "https://github.com/foo/bar.git",
|
url "https://github.com/foo/bar.git",
|
||||||
@ -314,7 +314,7 @@ describe RuboCop::Cop::FormulaAudit::GitUrls do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "reports no offenses with both a tag, revision and `shallow` after" do
|
it "reports no offenses with both a tag, revision and `shallow` after" do
|
||||||
expect_no_offenses(<<~RUBY)
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
desc "foo"
|
desc "foo"
|
||||||
url "https://github.com/foo/bar.git",
|
url "https://github.com/foo/bar.git",
|
||||||
@ -326,45 +326,43 @@ describe RuboCop::Cop::FormulaAudit::GitUrls do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "reports an offense with no `revision`" do
|
it "reports an offense with no `revision`" do
|
||||||
expect_offense(<<~RUBY)
|
expect_offense(<<~RUBY, "/homebrew-core/")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
desc "foo"
|
desc "foo"
|
||||||
url "https://github.com/foo/bar.git",
|
url "https://github.com/foo/bar.git",
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Specify a `tag` and `revision` for git urls
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Formulae in homebrew/core should specify a revision for git urls
|
||||||
tag: "v1.0.0"
|
tag: "v1.0.0"
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
it "reports an offense with no `revision` and `shallow`" do
|
it "reports an offense with no `revision` and `shallow`" do
|
||||||
expect_offense(<<~RUBY)
|
expect_offense(<<~RUBY, "/homebrew-core/")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
desc "foo"
|
desc "foo"
|
||||||
url "https://github.com/foo/bar.git",
|
url "https://github.com/foo/bar.git",
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Specify a `tag` and `revision` for git urls
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Formulae in homebrew/core should specify a revision for git urls
|
||||||
shallow: false,
|
shallow: false,
|
||||||
tag: "v1.0.0"
|
tag: "v1.0.0"
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
it "reports an offense with no `tag`" do
|
it "reports no offenses with no `tag`" do
|
||||||
expect_offense(<<~RUBY)
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
desc "foo"
|
desc "foo"
|
||||||
url "https://github.com/foo/bar.git",
|
url "https://github.com/foo/bar.git",
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Specify a `tag` and `revision` for git urls
|
|
||||||
revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
it "reports an offense with no `tag` and `shallow`" do
|
it "reports no offenses with no `tag` and `shallow`" do
|
||||||
expect_offense(<<~RUBY)
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
desc "foo"
|
desc "foo"
|
||||||
url "https://github.com/foo/bar.git",
|
url "https://github.com/foo/bar.git",
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Specify a `tag` and `revision` for git urls
|
|
||||||
revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||||
shallow: false
|
shallow: false
|
||||||
end
|
end
|
||||||
@ -372,7 +370,7 @@ describe RuboCop::Cop::FormulaAudit::GitUrls do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "reports no offenses with missing arguments in `head`" do
|
it "reports no offenses with missing arguments in `head`" do
|
||||||
expect_no_offenses(<<~RUBY)
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
desc "foo"
|
desc "foo"
|
||||||
url "https://foo.com"
|
url "https://foo.com"
|
||||||
@ -384,7 +382,7 @@ describe RuboCop::Cop::FormulaAudit::GitUrls do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "reports no offenses with missing arguments in `devel`" do
|
it "reports no offenses with missing arguments in `devel`" do
|
||||||
expect_no_offenses(<<~RUBY)
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
desc "foo"
|
desc "foo"
|
||||||
url "https://foo.com"
|
url "https://foo.com"
|
||||||
@ -394,5 +392,111 @@ describe RuboCop::Cop::FormulaAudit::GitUrls do
|
|||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "reports no offenses for non-core taps" do
|
||||||
|
expect_no_offenses(<<~RUBY)
|
||||||
|
class Foo < Formula
|
||||||
|
desc "foo"
|
||||||
|
url "https://github.com/foo/bar.git"
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe RuboCop::Cop::FormulaAuditStrict::GitUrls do
|
||||||
|
subject(:cop) { described_class.new }
|
||||||
|
|
||||||
|
context "when a git URL is used" do
|
||||||
|
it "reports no offenses with both a tag and a revision" do
|
||||||
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
|
class Foo < Formula
|
||||||
|
desc "foo"
|
||||||
|
url "https://github.com/foo/bar.git",
|
||||||
|
tag: "v1.0.0",
|
||||||
|
revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports no offenses with both a tag, revision and `shallow` before" do
|
||||||
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
|
class Foo < Formula
|
||||||
|
desc "foo"
|
||||||
|
url "https://github.com/foo/bar.git",
|
||||||
|
shallow: false,
|
||||||
|
tag: "v1.0.0",
|
||||||
|
revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports no offenses with both a tag, revision and `shallow` after" do
|
||||||
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
|
class Foo < Formula
|
||||||
|
desc "foo"
|
||||||
|
url "https://github.com/foo/bar.git",
|
||||||
|
tag: "v1.0.0",
|
||||||
|
revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||||
|
shallow: false
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports an offense with no `tag`" do
|
||||||
|
expect_offense(<<~RUBY, "/homebrew-core/")
|
||||||
|
class Foo < Formula
|
||||||
|
desc "foo"
|
||||||
|
url "https://github.com/foo/bar.git",
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Formulae in homebrew/core should specify a tag for git urls
|
||||||
|
revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports an offense with no `tag` and `shallow`" do
|
||||||
|
expect_offense(<<~RUBY, "/homebrew-core/")
|
||||||
|
class Foo < Formula
|
||||||
|
desc "foo"
|
||||||
|
url "https://github.com/foo/bar.git",
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Formulae in homebrew/core should specify a tag for git urls
|
||||||
|
revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||||
|
shallow: false
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports no offenses with missing arguments in `head`" do
|
||||||
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
|
class Foo < Formula
|
||||||
|
desc "foo"
|
||||||
|
url "https://foo.com"
|
||||||
|
head do
|
||||||
|
url "https://github.com/foo/bar.git"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports no offenses with missing arguments in `devel`" do
|
||||||
|
expect_no_offenses(<<~RUBY, "/homebrew-core/")
|
||||||
|
class Foo < Formula
|
||||||
|
desc "foo"
|
||||||
|
url "https://foo.com"
|
||||||
|
devel do
|
||||||
|
url "https://github.com/foo/bar.git"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports no offenses for non-core taps" do
|
||||||
|
expect_no_offenses(<<~RUBY)
|
||||||
|
class Foo < Formula
|
||||||
|
desc "foo"
|
||||||
|
url "https://github.com/foo/bar.git"
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user