Skip to content

Commit

Permalink
Simplify mock context creation for multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Oct 1, 2020
1 parent a3dcfde commit e1aaa13
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: Upload Python Package to PyPI
on:
release:
types: [ created ]
push:
tags:
- '*'
jobs:
deploy:
runs-on: ubuntu-latest
Expand Down
83 changes: 45 additions & 38 deletions test/test_requirements.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from collections import OrderedDict

from pkg_resources import parse_requirements

Expand All @@ -13,6 +14,23 @@
builtins_open = '__builtin__.open'


def mock_open_with_name(read_data="", name="file.name"):
"""Mock open call with a specified `name` attribute."""
m = mock.mock_open(read_data=read_data)
m.return_value.name = name
return m


def mock_open_multiple(files=OrderedDict()):
"""Create a mock open object for multiple files."""
m = mock.mock_open()
m.side_effect = [
mock_open_with_name(read_data=content, name=name).return_value
for name, content in files.items()
]
return m


class RequirementsTestCase(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -49,45 +67,44 @@ def test_resolve_requirement_with_file_empty(self):
m.assert_called_once_with("requirements.txt")

def test_resolve_requirement_with_file_content(self):
content = "foo >= 1.0.0\nbar <= 1.0.0\n"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo >= 1.0.0\nbar <= 1.0.0\n"),
)))):
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
["foo", "bar"],
)

def test_resolve_requirement_with_file_content_line_continuation(self):
content = "foo[bar] \\\n>= 1.0.0\n"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo[bar] \\\n>= 1.0.0\n"),
)))):
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
["foo"],
)

def test_resolve_requirement_with_file_content_line_continuation_2(self):
content = "foo \\\n>= 1.0.0 \\\n# comment \\\nbar \\"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo \\\n>= 1.0.0 \\\n# comment \\\nbar \\"),
)))):
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
["foo", "bar"],
)

def test_resolve_requirement_with_file_recursion_beyond_max_depth(self):
content = "-r requirements.txt\n"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "-r requirements.txt\n"),
)))):
with self.assertRaises(RuntimeError):
Flake8Checker.resolve_requirement("-r requirements.txt", 1),

def test_resolve_requirement_with_file_recursion(self):
content = "--requirement inner.txt\nbar <= 1.0.0\n"
inner_content = "# inner\nbaz\n\nqux\n"

with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
mock.mock_open(read_data=content).return_value,
mock.mock_open(read_data=inner_content).return_value,
)

with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "--requirement inner.txt\nbar <= 1.0.0\n"),
("inner.txt", "# inner\nbaz\n\nqux\n"),
)))):
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 2),
["baz", "qux", "bar"],
Expand All @@ -100,9 +117,9 @@ def test_init_with_no_requirements(self):
self.assertEqual(checker.get_requirements_txt(), ())

def test_init_with_simple_requirements(self):
content = "foo >= 1.0.0\nbar <= 1.0.0\n"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):

with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo >= 1.0.0\nbar <= 1.0.0\n"),
)))):
checker = Flake8Checker(None, None)
self.assertEqual(
checker.get_requirements_txt(),
Expand All @@ -113,15 +130,10 @@ def test_init_with_simple_requirements(self):
)

def test_init_with_recursive_requirements_beyond_max_depth(self):
content = "foo >= 1.0.0\n-r inner.txt\nbar <= 1.0.0\n"
inner_content = "# inner\nbaz\n\nqux\n"

with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
mock.mock_open(read_data=content).return_value,
mock.mock_open(read_data=inner_content).return_value,
)

with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo >= 1.0.0\n-r inner.txt\nbar <= 1.0.0\n"),
("inner.txt", "# inner\nbaz\n\nqux\n"),
)))):
with self.assertRaises(RuntimeError):
try:
Flake8Checker.requirements_max_depth = 0
Expand All @@ -131,15 +143,10 @@ def test_init_with_recursive_requirements_beyond_max_depth(self):
Flake8Checker.requirements_max_depth = 1

def test_init_with_recursive_requirements(self):
content = "foo >= 1.0.0\n-r inner.txt\nbar <= 1.0.0\n"
inner_content = "# inner\nbaz\n\nqux\n"

with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
mock.mock_open(read_data=content).return_value,
mock.mock_open(read_data=inner_content).return_value,
)

with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo >= 1.0.0\n-r inner.txt\nbar <= 1.0.0\n"),
("inner.txt", "# inner\nbaz\n\nqux\n"),
)))):
checker = Flake8Checker(None, None)
self.assertEqual(
checker.get_requirements_txt(),
Expand Down

0 comments on commit e1aaa13

Please sign in to comment.