Merge pull request #8031 from Rylan12/long-pythonhosted

style: require long urls for pypi and pythonhosted urls
This commit is contained in:
Jonathan Chang 2020-07-19 15:27:08 +10:00 committed by GitHub
commit 162ee3c2d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 20 deletions

View File

@ -291,21 +291,22 @@ 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 |_, url|
problem "use the `Source` url found on PyPI downloads page (`#{get_pypi_url(url)}`)"
end
# Require long files.pythonhosted.org urls
pythonhosted_pattern = %r{^https?://files.pythonhosted.org/packages/source/}
audit_urls(urls, pythonhosted_pattern) do |_, url|
problem "use the `Source` url found on PyPI downloads page (`#{get_pypi_url(url)}`)"
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)
end
def get_pypi_url(url)
package_file = File.basename(url)
package_name = package_file.match(/^(.+)-[a-z0-9.]+$/)[1]
"https://pypi.org/project/#{package_name}/#files"
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 `Source` url found on PyPI downloads page (`https://pypi.org/project/foo/#files`)
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 `Source` url found on PyPI downloads page (`https://pypi.org/project/foo/#files`)
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