Skip to content

Commit

Permalink
Skip invalid TOML and improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Oct 15, 2022
1 parent 88b39d3 commit a8a44cc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ def setup(**kw):
'__file__': os.path.join(cwd, "setup.py"),
'__f8r_setup': setup,
})
except Exception as e:
except BaseException as e:
# XXX: Exception during setup.py evaluation might not necessary
# mean "fatal error". This exception might occur if e.g.
# we have hijacked local setup() function (due to matching
# heuristic for function arguments). Anyway, we shall not
# break flake8 execution due to our eval() usage.
LOG.exception("Couldn't evaluate setup.py: %s", e)
LOG.error("Couldn't evaluate setup.py: %r", e)
self.redirected = False

# Restore import search path.
Expand Down Expand Up @@ -548,7 +548,7 @@ def get_pyproject_toml(cls):
try:
with open(os.path.join(cls.root_dir, "pyproject.toml")) as f:
return toml.loads(f.read())
except IOError as e:
except (IOError, toml.TomlDecodeError) as e:
LOG.debug("Couldn't load project setup: %s", e)
return {}

Expand Down
23 changes: 19 additions & 4 deletions test/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def processing_setup_py(self):
return self.filename == "setup.py"


class Flake8OptionManagerMock(dict):

def add_option(self, name, **kw):
self[name] = kw


class Flake8Options:
known_modules = ""
requirements_file = None
Expand All @@ -49,6 +55,15 @@ def check(code, filename="<unknown>", options=None):

class Flake8CheckerTestCase(unittest.TestCase):

def test_add_options(self):
manager = Flake8OptionManagerMock()
Flake8Checker.add_options(manager)
self.assertEqual(
sorted(manager.keys()),
['--known-modules', '--requirements-file',
'--requirements-max-depth', '--scan-host-site-packages'],
)

def test_stdlib(self):
errors = check("import os\nfrom unittest import TestCase")
self.assertEqual(len(errors), 0)
Expand Down Expand Up @@ -121,10 +136,10 @@ class Options(Flake8Options):
type(Flake8Checker.known_host_3rd_parties),
dict,
)
# Since flake8-requirements (this package) is a plugin for flake8,
# it is very likely that onc will have flake8 installed in the host
# site-packages. However, that is not the case for all our GitHub
# Actions runners, so we can not enforce this assertion.
# Since flake8-requirements (this package) is a plugin for flake8, it
# is very likely that one will have flake8 installed in the host
# site-packages. However, that is not the case for our GitHub Actions
# runners, so we can not enforce this assertion.
if 'flake8' in Flake8Checker.known_host_3rd_parties:
self.assertEqual(
Flake8Checker.known_host_3rd_parties['flake8'],
Expand Down
5 changes: 5 additions & 0 deletions test/test_pep621.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def test_get_pyproject_toml_pep621(self):
}
self.assertDictEqual(pep621, expected)

def test_get_pyproject_toml_invalid(self):
content = self.content + "invalid"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
self.assertDictEqual(Flake8Checker.get_pyproject_toml_pep621(), {})

def test_1st_party(self):
with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
Expand Down
21 changes: 21 additions & 0 deletions test/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ def test_detect_setup(self):
setup = SetupVisitor(ast.parse(code), "")
self.assertEqual(setup.redirected, False)

def test_detect_setup_wrong_num_of_args(self):
setup = SetupVisitor(ast.parse("setup(name='A')"), "")
self.assertEqual(setup.redirected, False)

def test_detect_setup_wrong_function(self):
setup = SetupVisitor(ast.parse("setup(1, name='A')"), "")
self.assertEqual(setup.redirected, False)

def test_detect_setup_oops(self):
setup = SetupVisitor(ast.parse("\n".join((
"from .myModule import setup",
"setup({})".format(",".join((
"name='A'",
"version='1'",
"author='A'",
"packages=['']",
"url='URL'",
))),
))), "")
self.assertEqual(setup.redirected, False)

def test_get_requirements(self):
setup = SetupVisitor(ast.parse("setup(**{})".format(str({
'name': 'A',
Expand Down

0 comments on commit a8a44cc

Please sign in to comment.