Require long urls for pypi and pythonhosted urls

This commit is contained in:
Rylan Polster 2020-07-18 13:21:09 -04:00
parent 6d22358fe0
commit 5f3f7d010b
2 changed files with 18 additions and 20 deletions

View File

@ -291,20 +291,15 @@ module RuboCop
urls += mirrors urls += mirrors
# Check pypi urls # Check pypi urls
@pypi_pattern = %r{^https?://pypi.python.org/(.*)} pypi_pattern = %r{^https?://pypi.python.org/}
audit_urls(urls, @pypi_pattern) do |match, url| audit_urls(urls, pypi_pattern) do
problem "#{url} should be `https://files.pythonhosted.org/#{match[1]}`" problem "use the `files.pythonhosted.org` url found on the pypi downloads page"
end end
end
def autocorrect(node) # Require long files.pythonhosted.org urls
lambda do |corrector| pythonhosted_pattern = %r{^https?://files.pythonhosted.org/packages/source/}
url_string_node = parameters(node).first audit_urls(urls, pythonhosted_pattern) do
url = string_content(url_string_node) problem "use the url found on the pypi downloads page"
match = regex_match_group(url_string_node, @pypi_pattern)
correction = node.source.sub(url, "https://files.pythonhosted.org/#{match[1]}")
corrector.insert_before(node.source_range, correction)
corrector.remove(node.source_range)
end end
end end
end end

View File

@ -245,29 +245,32 @@ end
describe RuboCop::Cop::FormulaAudit::PyPiUrls do describe RuboCop::Cop::FormulaAudit::PyPiUrls do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
context "when a pypi.python.org URL is used" do context "when a pypi URL is used" do
it "reports an offense" do it "reports an offense for pypi.python.org urls" do
expect_offense(<<~RUBY) expect_offense(<<~RUBY)
class Foo < Formula class Foo < Formula
desc "foo" desc "foo"
url "https://pypi.python.org/packages/source/foo/foo-0.1.tar.gz" url "https://pypi.python.org/packages/source/foo/foo-0.1.tar.gz"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ https://pypi.python.org/packages/source/foo/foo-0.1.tar.gz should be `https://files.pythonhosted.org/packages/source/foo/foo-0.1.tar.gz` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use the `files.pythonhosted.org` url found on the pypi downloads page
end end
RUBY RUBY
end end
it "support auto-correction" do it "reports an offense for short file.pythonhosted.org urls" do
corrected = autocorrect_source(<<~RUBY) expect_offense(<<~RUBY)
class Foo < Formula class Foo < Formula
desc "foo" desc "foo"
url "https://pypi.python.org/packages/source/foo/foo-0.1.tar.gz" url "https://files.pythonhosted.org/packages/source/f/foo/foo-0.1.tar.gz"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use the url found on the pypi downloads page
end end
RUBY RUBY
end
expect(corrected).to eq <<~RUBY it "reports no offenses for long file.pythonhosted.org urls" do
expect_no_offenses(<<~RUBY)
class Foo < Formula class Foo < Formula
desc "foo" desc "foo"
url "https://files.pythonhosted.org/packages/source/foo/foo-0.1.tar.gz" url "https://files.pythonhosted.org/packages/a0/b1/a01b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f/foo-0.1.tar.gz"
end end
RUBY RUBY
end end