Skip to content

Commit

Permalink
Fix requirements.txt includes with relative paths
Browse files Browse the repository at this point in the history
Fixes #28 and closes #29
  • Loading branch information
philjnicholls authored and arkq committed Oct 1, 2020
1 parent e1aaa13 commit 279f21e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def discover_project_root_dir(cls):
root_dir = os.path.abspath(os.path.join(root_dir, ".."))

@classmethod
def resolve_requirement(cls, requirement, max_depth=0):
def resolve_requirement(cls, requirement, max_depth=0, path=None):
"""Resolves flags like -r in an individual requirement line."""

option = None
Expand All @@ -377,10 +377,10 @@ def resolve_requirement(cls, requirement, max_depth=0):
raise RuntimeError(msg.format(requirement))
resolved = []
# Error out if requirements file cannot be opened.
with open(os.path.join(cls.root_dir, requirement)) as f:
with open(os.path.join(path or cls.root_dir, requirement)) as f:
for line in joinlines(f.readlines()):
resolved.extend(cls.resolve_requirement(
line, max_depth - 1))
line, max_depth - 1, os.path.dirname(f.name)))
return resolved

if option:
Expand Down
20 changes: 20 additions & 0 deletions test/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ def test_resolve_requirement_with_file_recursion(self):
["baz", "qux", "bar"],
)

def test_resolve_requirement_with_relative_include(self):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "-r requirements/production.txt"),
("requirements/production.txt", "-r node/one.txt\nfoo"),
("requirements/node/one.txt", "-r common.txt\n-r /abs/path.txt"),
("requirements/node/common.txt", "bar"),
("/abs/path.txt", "bis"),
)))) as m:
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 5),
["bar", "bis", "foo"],
)
m.assert_has_calls([
mock.call("requirements.txt"),
mock.call("requirements/production.txt"),
mock.call("requirements/node/one.txt"),
mock.call("requirements/node/common.txt"),
mock.call("/abs/path.txt"),
])

def test_init_with_no_requirements(self):
with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = IOError("No such file or directory"),
Expand Down

0 comments on commit 279f21e

Please sign in to comment.