From e1aaa13ad44a1a5bec0b793c8ccd919e5607b88d Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Thu, 1 Oct 2020 13:57:33 +0200 Subject: [PATCH] Simplify mock context creation for multiple files --- .github/workflows/pythonpublish.yml | 5 +- test/test_requirements.py | 83 ++++++++++++++++------------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/.github/workflows/pythonpublish.yml b/.github/workflows/pythonpublish.yml index 352200b..8c85802 100644 --- a/.github/workflows/pythonpublish.yml +++ b/.github/workflows/pythonpublish.yml @@ -1,7 +1,8 @@ name: Upload Python Package to PyPI on: - release: - types: [ created ] + push: + tags: + - '*' jobs: deploy: runs-on: ubuntu-latest diff --git a/test/test_requirements.py b/test/test_requirements.py index 5bcb06e..b582df0 100644 --- a/test/test_requirements.py +++ b/test/test_requirements.py @@ -1,4 +1,5 @@ import unittest +from collections import OrderedDict from pkg_resources import parse_requirements @@ -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): @@ -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"], @@ -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(), @@ -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 @@ -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(),