Skip to content

Commit

Permalink
Fix pipchecker with tests (#1291)
Browse files Browse the repository at this point in the history
* Revert "Revert "Merge pull request #1281 from yeojin-dev/patch-pipchecker""

This reverts commit 1e9ee49.

* Add requests to tox

* Add importlib to tox

* Revert "Add importlib to tox"

This reverts commit f7f1b22.

* Update a test package for pipchecker

* Fix code for python 2.7

* Add a comment for skip flake8
  • Loading branch information
yeojin-dev authored and trbs committed Jan 30, 2019
1 parent 0e711d0 commit f10635b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 11 deletions.
23 changes: 12 additions & 11 deletions django_extensions/management/commands/pipchecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ def handle(self, *args, **options):
with PipSession() as session:
for filename in req_files:
for req in parse_requirements(filename, session=session):
name = req.name if req.name else req.link.filename
# url attribute changed to link in pip version 6.1.0 and above
if LooseVersion(pip.__version__) > LooseVersion('6.0.8'):
self.reqs[req.name] = {
self.reqs[name] = {
"pip_req": req,
"url": req.link,
}
else:
self.reqs[req.name] = {
self.reqs[name] = {
"pip_req": req,
"url": req.url,
}
Expand All @@ -109,7 +110,7 @@ def handle(self, *args, **options):
if HAS_REQUESTS:
self.check_github()
else:
print(self.style.ERROR("Cannot check github urls. The requests library is not installed. ( pip install requests )"))
self.stdout.write(self.style.ERROR("Cannot check github urls. The requests library is not installed. ( pip install requests )"))
self.check_other()

def _urlopen_as_json(self, url, headers=None):
Expand Down Expand Up @@ -161,7 +162,7 @@ def check_pypi(self):
else:
msg = "not installed"
pkg_info = name
print("{pkg_info:40} {msg}".format(pkg_info=pkg_info, msg=msg))
self.stdout.write("{pkg_info:40} {msg}".format(pkg_info=pkg_info, msg=msg))
del self.reqs[name]

def check_github(self):
Expand Down Expand Up @@ -243,20 +244,20 @@ def check_github(self):
self.style.ERROR("\nFailed to parse %r\n" % (req_url, ))
continue
except (ValueError, IndexError) as e:
print(self.style.ERROR("\nFailed to parse %r: %s\n" % (req_url, e)))
self.stdout.write(self.style.ERROR("\nFailed to parse %r: %s\n" % (req_url, e)))
continue

try:
test_auth = requests.get("https://api.github.com/django/", headers=headers).json()
except HTTPError as e:
print("\n%s\n" % str(e))
self.stdout.write("\n%s\n" % str(e))
return

if "message" in test_auth and test_auth["message"] == "Bad credentials":
print(self.style.ERROR("\nGithub API: Bad credentials. Aborting!\n"))
self.stdout.write(self.style.ERROR("\nGithub API: Bad credentials. Aborting!\n"))
return
elif "message" in test_auth and test_auth["message"].startswith("API Rate Limit Exceeded"):
print(self.style.ERROR("\nGithub API: Rate Limit Exceeded. Aborting!\n"))
self.stdout.write(self.style.ERROR("\nGithub API: Rate Limit Exceeded. Aborting!\n"))
return

frozen_commit_sha = None
Expand Down Expand Up @@ -292,7 +293,7 @@ def check_github(self):
pkg_info = name
else:
pkg_info = "{0} {1}".format(name, frozen_commit_sha[:10])
print("{pkg_info:40} {msg}".format(pkg_info=pkg_info, msg=msg))
self.stdout.write("{pkg_info:40} {msg}".format(pkg_info=pkg_info, msg=msg))
del self.reqs[name]

def check_other(self):
Expand All @@ -303,12 +304,12 @@ def check_other(self):
support here.
"""
if self.reqs:
print(self.style.ERROR("\nOnly pypi and github based requirements are supported:"))
self.stdout.write(self.style.ERROR("\nOnly pypi and github based requirements are supported:"))
for name, req in self.reqs.items():
if "dist" in req:
pkg_info = "{dist.project_name} {dist.version}".format(dist=req["dist"])
elif "url" in req:
pkg_info = "{url}".format(url=req["url"])
else:
pkg_info = "unknown package"
print(self.style.BOLD("{pkg_info:40} is not a pypi or github requirement".format(pkg_info=pkg_info)))
self.stdout.write(self.style.BOLD("{pkg_info:40} is not a pypi or github requirement".format(pkg_info=pkg_info)))
104 changes: 104 additions & 0 deletions tests/management/commands/test_pipchecker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# -*- coding: utf-8 -*-
import importlib
import os
import subprocess
import sys

import pip
from django.core.management import call_command
from django.test import TestCase
from django.utils.six import StringIO
from pip._internal.exceptions import InstallationError


class PipCheckerTests(TestCase):

def test_pipchecker_when_requirements_file_does_not_exist(self):
with self.assertRaises(InstallationError):
call_command('pipchecker', '-r', 'not_exist.txt')

def test_pipchecker_with_not_installed_requirement(self):
requirements_path = './requirements.txt'
out = StringIO()

f = open(requirements_path, 'wt')
f.write('not-installed==1.0.0')
f.close()

call_command('pipchecker', '-r', requirements_path, stdout=out)

value = out.getvalue()

subprocess.call([sys.executable, '-m', 'pip', 'uninstall', '--yes', '-r', requirements_path])
os.remove(requirements_path)

self.assertTrue(value.endswith('not installed\n'))

def test_pipchecker_with_outdated_requirement(self):
requirements_path = './requirements.txt'
out = StringIO()

f = open(requirements_path, 'wt')
f.write('djangorestframework==3.0.0')
f.close()

subprocess.call([sys.executable, '-m', 'pip', 'install', '-r', requirements_path])
if sys.version_info.major == 3:
pip._vendor.pkg_resources = importlib.reload(pip._vendor.pkg_resources)
else:
# Python 2.7
pip._vendor.pkg_resources = reload(pip._vendor.pkg_resources) # noqa
call_command('pipchecker', '-r', requirements_path, stdout=out)

value = out.getvalue()

subprocess.call([sys.executable, '-m', 'pip', 'uninstall', '--yes', '-r', requirements_path])
os.remove(requirements_path)

self.assertTrue(value.endswith('available\n'))

def test_pipchecker_with_up_to_date_requirement(self):
requirements_path = './requirements.txt'
out = StringIO()

f = open(requirements_path, 'wt')
f.write('djangorestframework')
f.close()

subprocess.call([sys.executable, '-m', 'pip', 'install', '-r', requirements_path])
if sys.version_info.major == 3:
pip._vendor.pkg_resources = importlib.reload(pip._vendor.pkg_resources)
else:
# Python 2.7
pip._vendor.pkg_resources = reload(pip._vendor.pkg_resources) # noqa
call_command('pipchecker', '-r', requirements_path, stdout=out)

value = out.getvalue()

subprocess.call([sys.executable, '-m', 'pip', 'uninstall', '--yes', '-r', requirements_path])
os.remove(requirements_path)

self.assertEqual(value, '')

def test_pipchecker_with_github_url_requirement(self):
requirements_path = './requirements.txt'
out = StringIO()

f = open(requirements_path, 'wt')
f.write('git+https://github.com/jmrivas86/django-json-widget')
f.close()

subprocess.call([sys.executable, '-m', 'pip', 'install', 'django-json-widget'])
if sys.version_info.major == 3:
pip._vendor.pkg_resources = importlib.reload(pip._vendor.pkg_resources)
else:
# Python 2.7
pip._vendor.pkg_resources = reload(pip._vendor.pkg_resources) # noqa
call_command('pipchecker', '-r', requirements_path, stdout=out)

value = out.getvalue()

subprocess.call([sys.executable, '-m', 'pip', 'uninstall', '--yes', '-r', requirements_path])
os.remove(requirements_path)

self.assertTrue(value.endswith('repo is not frozen\n'))
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ deps =
pytest-django
pytest-cov
py27: python-keyczar
requests
mock

[testenv:precommit]
Expand Down

0 comments on commit f10635b

Please sign in to comment.