Skip to content

Commit

Permalink
Test error handling in setup.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
devdanzin committed Jul 28, 2024
1 parent 2a7d977 commit 3dc3c3f
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
from typing import List, cast

import pytest
import setuptools

import coverage
import setup
from coverage import env

from tests.coveragetest import CoverageTest
Expand All @@ -27,6 +29,11 @@ def setUp(self) -> None:
# Force the most restrictive interpretation.
self.set_environ('LC_ALL', 'C')

def raise_error(self, error):
def f(*args, **kwargs):
raise error
return f

def test_metadata(self) -> None:
status, output = self.run_command_status(
"python setup.py --description --version --url --author",
Expand Down Expand Up @@ -58,3 +65,44 @@ def test_more_metadata(self) -> None:
assert len(long_description) > 7
assert long_description[0].strip() != ""
assert long_description[-1].strip() != ""

def test_build_extension(self) -> None:
# Do we handle all expected errors?
for ext_error in setup.ext_errors:
with pytest.raises(setup.BuildFailed):
setup.build_ext.build_extension = self.raise_error(ext_error)
ext_builder = setup.ve_build_ext(setuptools.Distribution())
ext_builder.build_extension(1)

# Sanity check: we don't handle unexpected errors
for other_error in (ImportError, ZeroDivisionError, Exception):
with pytest.raises(other_error):
setup.build_ext.build_extension = self.raise_error(other_error)
ext_builder = setup.ve_build_ext(setuptools.Distribution())
ext_builder.build_extension(1)

def test_run(self) -> None:
# `ve_build_ext.run()` only catches `PlatformError` and raises `BuildFailed`
with pytest.raises(setup.BuildFailed):
setup.build_ext.run = self.raise_error(setup.errors.PlatformError)
ext_builder = setup.ve_build_ext(setuptools.Distribution())
ext_builder.run()

# Sanity check: we don't handle unexpected errors
for error in setup.ext_errors:
with pytest.raises(error):
setup.setup = self.raise_error(error)
setup.main()

def test_main(self) -> None:
# `main()` will catch `BuildFailed` once, then it'll be raised again
# when `setup` is called a second time.
with pytest.raises(setup.BuildFailed):
setup.setup = self.raise_error(setup.BuildFailed)
setup.main()

# Sanity check: we don't handle unexpected errors
for error in setup.ext_errors:
with pytest.raises(error):
setup.setup = self.raise_error(error)
setup.main()

0 comments on commit 3dc3c3f

Please sign in to comment.