Skip to content

Commit b1d65a9

Browse files
Merge pull request #31 from r1chardj0n3s/fix-github-ci
Remove flake8 errors without ignoring error codes
2 parents f9d7bcb + 54525d4 commit b1d65a9

File tree

8 files changed

+187
-115
lines changed

8 files changed

+187
-115
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ jobs:
3737
3838
- name: "Run tests"
3939
run: |
40-
pytest -s -vvv --cov-fail-under 100 --cov=pip_check_reqs/ --cov=tests tests/ --cov-report=xml
40+
# We currently require 94% test coverage and not 100% as this is the
41+
# level that coverage was at when the CI restriction was introduced.
42+
pytest -s -vvv --cov-fail-under 94 --cov=pip_check_reqs/ --cov=tests tests/ --cov-report=xml
4143
4244
- name: "Upload coverage to Codecov"
4345
uses: "codecov/codecov-action@v1"

pip_check_reqs/common.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class FoundModule:
1717
def __init__(self, modname, filename, locations=None):
1818
self.modname = modname
1919
self.filename = os.path.realpath(filename)
20-
self.locations = locations or [] # filename, lineno
20+
self.locations = locations or [] # filename, lineno
2121

2222
def __repr__(self):
2323
return 'FoundModule("%s")' % self.modname
@@ -66,7 +66,7 @@ def __addModule(self, modname, lineno):
6666
progress.append(p)
6767

6868
# we might have previously seen a useful path though...
69-
if modpath is None: # pragma: no cover
69+
if modpath is None: # pragma: no cover
7070
# the sys module will hit this code path on py3k - possibly
7171
# others will, but I've not discovered them
7272
modpath = last_modpath
@@ -125,7 +125,7 @@ def find_imported_modules(options):
125125
def find_required_modules(options):
126126
explicit = set()
127127
for requirement in parse_requirements('requirements.txt',
128-
session=PipSession()):
128+
session=PipSession()):
129129
if options.ignore_reqs(requirement):
130130
log.debug('ignoring requirement: %s', requirement.name)
131131
else:
@@ -155,6 +155,7 @@ def f(candidate, ignore_cfg=ignore_cfg):
155155
elif fnmatch.fnmatch(os.path.relpath(candidate), ignore):
156156
return True
157157
return False
158+
158159
return f
159160

160161

pip_check_reqs/find_extra_reqs.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def find_extra_reqs(options):
2222
all_pkgs = (pkg.project_name for pkg in get_installed_distributions())
2323
for package in search_packages_info(all_pkgs):
2424
log.debug('installed package: %s (at %s)', package['name'],
25-
package['location'])
25+
package['location'])
2626
for f in package.get('files', []):
2727
path = os.path.realpath(os.path.join(package['location'], f))
2828
installed_files[path] = package['name']
@@ -40,7 +40,7 @@ def find_extra_reqs(options):
4040
if info.filename in installed_files:
4141
used_name = canonicalize_name(installed_files[info.filename])
4242
log.debug('used module: %s (from package %s)', modname,
43-
installed_files[info.filename])
43+
installed_files[info.filename])
4444
used[used_name].append(info)
4545
else:
4646
log.debug(
@@ -58,21 +58,41 @@ def main():
5858

5959
usage = 'usage: %prog [options] files or directories'
6060
parser = optparse.OptionParser(usage)
61-
parser.add_option("-f", "--ignore-file", dest="ignore_files",
62-
action="append", default=[],
63-
help="file paths globs to ignore")
64-
parser.add_option("-m", "--ignore-module", dest="ignore_mods",
65-
action="append", default=[],
66-
help="used module names (globs are ok) to ignore")
67-
parser.add_option("-r", "--ignore-requirement", dest="ignore_reqs",
68-
action="append", default=[],
69-
help="reqs in requirements.txt to ignore")
70-
parser.add_option("-v", "--verbose", dest="verbose",
71-
action="store_true", default=False, help="be more verbose")
72-
parser.add_option("-d", "--debug", dest="debug",
73-
action="store_true", default=False, help="be *really* verbose")
74-
parser.add_option("--version", dest="version",
75-
action="store_true", default=False, help="display version information")
61+
parser.add_option("-f",
62+
"--ignore-file",
63+
dest="ignore_files",
64+
action="append",
65+
default=[],
66+
help="file paths globs to ignore")
67+
parser.add_option("-m",
68+
"--ignore-module",
69+
dest="ignore_mods",
70+
action="append",
71+
default=[],
72+
help="used module names (globs are ok) to ignore")
73+
parser.add_option("-r",
74+
"--ignore-requirement",
75+
dest="ignore_reqs",
76+
action="append",
77+
default=[],
78+
help="reqs in requirements.txt to ignore")
79+
parser.add_option("-v",
80+
"--verbose",
81+
dest="verbose",
82+
action="store_true",
83+
default=False,
84+
help="be more verbose")
85+
parser.add_option("-d",
86+
"--debug",
87+
dest="debug",
88+
action="store_true",
89+
default=False,
90+
help="be *really* verbose")
91+
parser.add_option("--version",
92+
dest="version",
93+
action="store_true",
94+
default=False,
95+
help="display version information")
7696

7797
(options, args) = parser.parse_args()
7898

pip_check_reqs/find_missing_reqs.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def find_missing_reqs(options):
2525
all_pkgs = (pkg.project_name for pkg in get_installed_distributions())
2626
for package in search_packages_info(all_pkgs):
2727
log.debug('installed package: %s (at %s)', package['name'],
28-
package['location'])
28+
package['location'])
2929
for file in package.get('files', []) or []:
3030
path = os.path.realpath(os.path.join(package['location'], file))
3131
installed_files[path] = package['name']
@@ -43,7 +43,7 @@ def find_missing_reqs(options):
4343
if info.filename in installed_files:
4444
used_name = canonicalize_name(installed_files[info.filename])
4545
log.debug('used module: %s (from package %s)', modname,
46-
installed_files[info.filename])
46+
installed_files[info.filename])
4747
used[used_name].append(info)
4848
else:
4949
log.debug(
@@ -53,31 +53,47 @@ def find_missing_reqs(options):
5353
# 4. compare with requirements.txt
5454
explicit = set()
5555
for requirement in parse_requirements('requirements.txt',
56-
session=PipSession()):
56+
session=PipSession()):
5757
log.debug('found requirement: %s', requirement.name)
5858
explicit.add(canonicalize_name(requirement.name))
5959

60-
return [(name, used[name]) for name in used
61-
if name not in explicit]
60+
return [(name, used[name]) for name in used if name not in explicit]
6261

6362

6463
def main():
6564
from pip_check_reqs import __version__
6665

6766
usage = 'usage: %prog [options] files or directories'
6867
parser = optparse.OptionParser(usage)
69-
parser.add_option("-f", "--ignore-file", dest="ignore_files",
70-
action="append", default=[],
71-
help="file paths globs to ignore")
72-
parser.add_option("-m", "--ignore-module", dest="ignore_mods",
73-
action="append", default=[],
74-
help="used module names (globs are ok) to ignore")
75-
parser.add_option("-v", "--verbose", dest="verbose",
76-
action="store_true", default=False, help="be more verbose")
77-
parser.add_option("-d", "--debug", dest="debug",
78-
action="store_true", default=False, help="be *really* verbose")
79-
parser.add_option("--version", dest="version",
80-
action="store_true", default=False, help="display version information")
68+
parser.add_option("-f",
69+
"--ignore-file",
70+
dest="ignore_files",
71+
action="append",
72+
default=[],
73+
help="file paths globs to ignore")
74+
parser.add_option("-m",
75+
"--ignore-module",
76+
dest="ignore_mods",
77+
action="append",
78+
default=[],
79+
help="used module names (globs are ok) to ignore")
80+
parser.add_option("-v",
81+
"--verbose",
82+
dest="verbose",
83+
action="store_true",
84+
default=False,
85+
help="be more verbose")
86+
parser.add_option("-d",
87+
"--debug",
88+
dest="debug",
89+
action="store_true",
90+
default=False,
91+
help="be *really* verbose")
92+
parser.add_option("--version",
93+
dest="version",
94+
action="store_true",
95+
default=False,
96+
help="display version information")
8197

8298
(options, args) = parser.parse_args()
8399

@@ -111,7 +127,8 @@ def main():
111127
for use in uses:
112128
for filename, lineno in use.locations:
113129
log.warning('%s:%s dist=%s module=%s',
114-
os.path.relpath(filename), lineno, name, use.modname)
130+
os.path.relpath(filename), lineno, name,
131+
use.modname)
115132

116133
if missing:
117134
sys.exit(1)

setup.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
from setuptools import setup
22
from codecs import open
33
from os import path
4+
from pathlib import Path
5+
from typing import List
46

57
from pip_check_reqs import __version__
68

79
here = path.abspath(path.dirname(__file__))
810

11+
12+
def _get_dependencies(requirements_file: Path) -> List[str]:
13+
"""
14+
Return requirements from a requirements file.
15+
This expects a requirements file with no ``--find-links`` lines.
16+
"""
17+
lines = requirements_file.read_text().strip().split('\n')
18+
return [line for line in lines if not line.startswith('#')]
19+
20+
921
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
1022
long_description = f.read()
1123

1224
with open(path.join(here, 'CHANGELOG.rst'), encoding='utf-8') as f:
1325
long_description += f.read()
1426

15-
# This is not usual, but this project needs both install_requires
16-
# and requirements.txt and we'd like to not duplicate them
17-
with open(path.join(here, 'requirements.txt'), encoding='utf-8') as f:
18-
requirements = [s.strip() for s in f.readlines()]
27+
INSTALL_REQUIRES = _get_dependencies(
28+
requirements_file=Path('requirements.txt'),
29+
)
30+
31+
DEV_REQUIRES = _get_dependencies(
32+
requirements_file=Path('test-requirements.txt'),
33+
)
1934

2035
setup(
2136
name='pip_check_reqs',
2237
version=__version__,
2338
description=(
2439
'Find packages that should or should not be in requirements for a '
25-
'project'
26-
),
40+
'project'),
2741
long_description=long_description,
2842
url='https://github.com/r1chardj0n3s/pip-check-reqs',
2943
author='Richard Jones',
@@ -45,5 +59,6 @@
4559
'pip-extra-reqs=pip_check_reqs.find_extra_reqs:main',
4660
],
4761
},
48-
install_requires=requirements,
62+
install_requires=INSTALL_REQUIRES,
63+
extras_require={'dev': DEV_REQUIRES},
4964
)

tests/test_common.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212
from pip_check_reqs import common
1313

1414

15-
@pytest.mark.parametrize(["path", "result"], [
16-
('/', ''),
17-
('__init__.py', ''), # a top-level file like this has no package name
18-
('/__init__.py', ''), # no package name
19-
('spam/__init__.py', 'spam'),
20-
('spam/__init__.pyc', 'spam'),
21-
('spam/__init__.pyo', 'spam'),
22-
('ham/spam/__init__.py', 'ham/spam'),
23-
('/ham/spam/__init__.py', '/ham/spam'),
24-
])
15+
@pytest.mark.parametrize(
16+
["path", "result"],
17+
[
18+
('/', ''),
19+
('__init__.py', ''), # a top-level file like this has no package name
20+
('/__init__.py', ''), # no package name
21+
('spam/__init__.py', 'spam'),
22+
('spam/__init__.pyc', 'spam'),
23+
('spam/__init__.pyo', 'spam'),
24+
('ham/spam/__init__.py', 'ham/spam'),
25+
('/ham/spam/__init__.py', '/ham/spam'),
26+
])
2527
def test_is_package_file(path, result):
2628
assert common.is_package_file(path) == result
2729

@@ -34,18 +36,21 @@ def test_FoundModule():
3436
assert str(fm) == 'FoundModule("spam")'
3537

3638

37-
@pytest.mark.parametrize(["stmt", "result"], [
38-
('import ast', ['ast']),
39-
('import ast, sys', ['ast', 'sys']),
40-
('from sys import version', ['sys']),
41-
('from os import path', ['os']),
42-
('import distutils.command.check', ['distutils']),
43-
('import spam', []), # don't break because bad programmer
44-
])
39+
@pytest.mark.parametrize(
40+
["stmt", "result"],
41+
[
42+
('import ast', ['ast']),
43+
('import ast, sys', ['ast', 'sys']),
44+
('from sys import version', ['sys']),
45+
('from os import path', ['os']),
46+
('import distutils.command.check', ['distutils']),
47+
('import spam', []), # don't break because bad programmer
48+
])
4549
def test_ImportVisitor(stmt, result):
4650
class options:
4751
def ignore_mods(self, modname):
4852
return False
53+
4954
vis = common.ImportVisitor(options())
5055
vis.set_location('spam.py')
5156
vis.visit(ast.parse(stmt))
@@ -55,30 +60,30 @@ def ignore_mods(self, modname):
5560

5661
def test_pyfiles_file(monkeypatch):
5762
monkeypatch.setattr(os.path, 'abspath',
58-
pretend.call_recorder(lambda x: '/spam/ham.py'))
63+
pretend.call_recorder(lambda x: '/spam/ham.py'))
5964

6065
assert list(common.pyfiles('spam')) == ['/spam/ham.py']
6166

6267

6368
def test_pyfiles_file_no_dice(monkeypatch):
6469
monkeypatch.setattr(os.path, 'abspath',
65-
pretend.call_recorder(lambda x: '/spam/ham'))
70+
pretend.call_recorder(lambda x: '/spam/ham'))
6671

6772
with pytest.raises(ValueError):
6873
list(common.pyfiles('spam'))
6974

7075

7176
def test_pyfiles_package(monkeypatch):
7277
monkeypatch.setattr(os.path, 'abspath',
73-
pretend.call_recorder(lambda x: '/spam'))
78+
pretend.call_recorder(lambda x: '/spam'))
7479
monkeypatch.setattr(os.path, 'isdir',
75-
pretend.call_recorder(lambda x: True))
80+
pretend.call_recorder(lambda x: True))
7681
walk_results = [
7782
('spam', [], ['__init__.py', 'spam', 'ham.py']),
7883
('spam/dub', [], ['bass.py', 'dropped']),
7984
]
8085
monkeypatch.setattr(os, 'walk',
81-
pretend.call_recorder(lambda x: walk_results))
86+
pretend.call_recorder(lambda x: walk_results))
8287

8388
assert list(common.pyfiles('spam')) == \
8489
['spam/__init__.py', 'spam/ham.py', 'spam/dub/bass.py']
@@ -91,9 +96,9 @@ def test_pyfiles_package(monkeypatch):
9196
(True, True, ['ast'], [('spam.py', 2)]),
9297
])
9398
def test_find_imported_modules(monkeypatch, caplog, ignore_ham, ignore_hashlib,
94-
expect, locs):
99+
expect, locs):
95100
monkeypatch.setattr(common, 'pyfiles',
96-
pretend.call_recorder(lambda x: ['spam.py', 'ham.py']))
101+
pretend.call_recorder(lambda x: ['spam.py', 'ham.py']))
97102

98103
if sys.version_info[0] == 2:
99104
# py2 will find sys module but py3k won't
@@ -117,6 +122,7 @@ def __enter__(self):
117122

118123
def __exit__(self, *args):
119124
pass
125+
120126
monkeypatch.setattr(common, 'open', FakeFile, raising=False)
121127

122128
caplog.set_level(logging.INFO)
@@ -172,7 +178,8 @@ def ignore_reqs(req):
172178

173179
FakeReq = collections.namedtuple('FakeReq', ['name'])
174180
requirements = [FakeReq('foobar'), FakeReq('barfoo')]
175-
monkeypatch.setattr(common, 'parse_requirements',
181+
monkeypatch.setattr(
182+
common, 'parse_requirements',
176183
pretend.call_recorder(lambda a, session=None: requirements))
177184

178185
reqs = common.find_required_modules(options)

0 commit comments

Comments
 (0)