diff --git a/Library/Homebrew/resource_auditor.rb b/Library/Homebrew/resource_auditor.rb index 69dbbba5dc..85fdb79d0f 100644 --- a/Library/Homebrew/resource_auditor.rb +++ b/Library/Homebrew/resource_auditor.rb @@ -106,6 +106,17 @@ module Homebrew end end + def audit_resource_name_matches_pypi_package_name_in_url + return unless url.match?(%r{^https?://files\.pythonhosted\.org/packages/}) + return if name == owner.name # Skip the top-level package name as we only care about `resource "foo"` blocks. + + url =~ %r{/(?[^/]+)-} + pypi_package_name = Regexp.last_match(:package_name).to_s.gsub(/[_.]/, "-") + return if name.casecmp(pypi_package_name).zero? + + problem "resource name should be `#{pypi_package_name}` to match the PyPI package name" + end + def audit_urls urls = [url] + mirrors diff --git a/Library/Homebrew/test/dev-cmd/audit_spec.rb b/Library/Homebrew/test/dev-cmd/audit_spec.rb index 3e0156aa43..b5b3a93701 100644 --- a/Library/Homebrew/test/dev-cmd/audit_spec.rb +++ b/Library/Homebrew/test/dev-cmd/audit_spec.rb @@ -522,6 +522,27 @@ module Homebrew end end + describe "#audit_resource_name_matches_pypi_package_name_in_url" do + it "reports a problem if the resource name does not match the python package name" do + fa = formula_auditor "foo", <<~RUBY + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + sha256 "abc123" + homepage "https://brew.sh" + + resource "Something" do + url "https://files.pythonhosted.org/packages/FooSomething-1.0.0.tar.gz" + sha256 "def456" + end + end + RUBY + + fa.audit_specs + expect(fa.problems.first[:message]) + .to match("resource name should be `FooSomething` to match the PyPI package name") + end + end + describe "#check_service_command" do specify "Not installed" do fa = formula_auditor "foo", <<~RUBY