Skip to content
Snippets Groups Projects
Unverified Commit 162ee3c2 authored by Jonathan Chang's avatar Jonathan Chang Committed by GitHub
Browse files

Merge pull request #8031 from Rylan12/long-pythonhosted

style: require long urls for pypi and pythonhosted urls
parents 241e768a 70dfaf3b
No related branches found
No related tags found
No related merge requests found
......@@ -291,22 +291,23 @@ 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
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 |_, url|
problem "use the `Source` url found on PyPI downloads page (`#{get_pypi_url(url)}`)"
end
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
end
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment