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
# Check pypi urls
@pypi_pattern = %r{^https?://pypi.python.org/(.*)}
audit_urls(urls, @pypi_pattern) do |match, url|
problem "#{url} should be `https://files.pythonhosted.org/#{match[1]}`"
pypi_pattern = %r{^https?://pypi.python.org/}
audit_urls(urls, pypi_pattern) do
problem "use the `files.pythonhosted.org` url found on the pypi downloads page"
end
end
def autocorrect(node)
lambda do |corrector|
url_string_node = parameters(node).first
url = string_content(url_string_node)
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)
# Require long files.pythonhosted.org urls
pythonhosted_pattern = %r{^https?://files.pythonhosted.org/packages/source/}
audit_urls(urls, pythonhosted_pattern) do
problem "use the url found on the pypi downloads page"
end
end
end

View File

@ -245,29 +245,32 @@ end
describe RuboCop::Cop::FormulaAudit::PyPiUrls do
subject(:cop) { described_class.new }
context "when a pypi.python.org URL is used" do
it "reports an offense" do
context "when a pypi URL is used" do
it "reports an offense for pypi.python.org urls" do
expect_offense(<<~RUBY)
class Foo < Formula
desc "foo"
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
RUBY
end
it "support auto-correction" do
corrected = autocorrect_source(<<~RUBY)
it "reports an offense for short file.pythonhosted.org urls" do
expect_offense(<<~RUBY)
class Foo < Formula
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
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
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
RUBY
end