Skip to content

Commit

Permalink
feat: Add type annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Drung <[email protected]>
  • Loading branch information
bdrung committed Aug 1, 2024
1 parent ca83a39 commit d34c0e3
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bdebstrap
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ MMDEBSTRAP_OPTS = {
}


class Config(dict):
class Config(dict[str, typing.Any]):
"""YAML configuration for bdebstrap."""

_ENV_PREFIX = "BDEBSTRAP_"
Expand Down
12 changes: 6 additions & 6 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import unittest


def get_path(code_file):
def get_path(code_file: str) -> str:
"""Return relative or absolute path to given code file.
During Debian package build, the current directory is
Expand All @@ -35,7 +35,7 @@ def get_path(code_file):
return code_file


def get_source_files():
def get_source_files() -> list[str]:
"""Return a list of sources files/directories (to check with flake8/pylint)."""
scripts = ["bdebstrap"]
modules = ["tests"]
Expand All @@ -54,7 +54,7 @@ def get_source_files():
return files


def unittest_verbosity():
def unittest_verbosity() -> int:
"""
Return the verbosity setting of the currently running unittest.
Expand All @@ -72,17 +72,17 @@ def unittest_verbosity():
class TestGetPath(unittest.TestCase):
"""Unittests for get_path function."""

def test_get_path_exists(self):
def test_get_path_exists(self) -> None:
"""Test get_path(__file__)."""
path = get_path(__file__)
self.assertEqual(path, __file__)

def test_get_path_missing(self):
def test_get_path_missing(self) -> None:
"""Test non-existing file for get_path()."""
path = get_path(__file__ + "non-existing")
self.assertEqual(path, __file__ + "non-existing")

def test_get_path_pybuild(self):
def test_get_path_pybuild(self) -> None:
"""Test changing current directory before calling get_path()."""
relpath = os.path.relpath(__file__)
oldpwd = os.environ.get("OLDPWD")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BlackTestCase(unittest.TestCase):
"""

@unittest.skipIf(os.environ.get("SKIP_LINTERS"), "requested via SKIP_LINTERS env variable")
def test_black(self):
def test_black(self) -> None:
"""Test: Run black code formatter on Python source code."""
cmd = ["black", "--check", "--diff"] + get_source_files()
if unittest_verbosity() >= 2:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_flake8.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Flake8TestCase(unittest.TestCase):
"""

@unittest.skipIf(os.environ.get("SKIP_LINTERS"), "requested via SKIP_LINTERS env variable")
def test_flake8(self):
def test_flake8(self) -> None:
"""Test: Run flake8 on Python source code."""
cmd = [sys.executable, "-m", "flake8", "--max-line-length=99"] + get_source_files()
if unittest_verbosity() >= 2:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class IsortTestCase(unittest.TestCase):
"""

@unittest.skipIf(os.environ.get("SKIP_LINTERS"), "requested via SKIP_LINTERS env variable")
def test_isort(self):
def test_isort(self) -> None:
"""Test: Run isort on Python source code."""
cmd = ["isort", "--check-only", "--diff"] + get_source_files()
if unittest_verbosity() >= 2:
Expand Down
30 changes: 24 additions & 6 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
EXAMPLE_CONFIG_DIR = os.path.join(os.path.dirname(__file__), "..", "examples")


def default_hooks(output_dir):
def default_hooks(output_dir: str) -> list[str]:
"""Return the list of default hooks."""
return [
f'--essential-hook=mkdir -p "$1{OUTPUT_DIR}"',
Expand All @@ -43,7 +43,12 @@ class TestMain(unittest.TestCase):
@unittest.mock.patch("bdebstrap.Config.save")
@unittest.mock.patch("bdebstrap.prepare_output_dir")
@unittest.mock.patch("bdebstrap.Mmdebstrap.call")
def test_debian_example(self, mmdebstrap_call_mock, prepare_output_dir_mock, config_save_mock):
def test_debian_example(
self,
mmdebstrap_call_mock: unittest.mock.MagicMock,
prepare_output_dir_mock: unittest.mock.MagicMock,
config_save_mock: unittest.mock.MagicMock,
) -> None:
"""Test Debian unstable example."""
args = [
"-c",
Expand All @@ -62,8 +67,11 @@ def test_debian_example(self, mmdebstrap_call_mock, prepare_output_dir_mock, con
@unittest.mock.patch("bdebstrap.prepare_output_dir")
@unittest.mock.patch("bdebstrap.Mmdebstrap.call")
def test_failed_mmdebstrap(
self, mmdebstrap_call_mock, prepare_output_dir_mock, config_save_mock
):
self,
mmdebstrap_call_mock: unittest.mock.MagicMock,
prepare_output_dir_mock: unittest.mock.MagicMock,
config_save_mock: unittest.mock.MagicMock,
) -> None:
"""Test failure of mmdebstrap call."""
mmdebstrap_call_mock.side_effect = subprocess.CalledProcessError(42, "mmdebstrap")
args = ["-c", os.path.join(EXAMPLE_CONFIG_DIR, "Debian-unstable.yaml"), "--name", "foobar"]
Expand All @@ -80,7 +88,12 @@ def test_failed_mmdebstrap(
@unittest.mock.patch("bdebstrap.Config.save")
@unittest.mock.patch("bdebstrap.prepare_output_dir")
@unittest.mock.patch("bdebstrap.Mmdebstrap.call")
def test_empty_target(self, mmdebstrap_call_mock, prepare_output_dir_mock, config_save_mock):
def test_empty_target(
self,
mmdebstrap_call_mock: unittest.mock.MagicMock,
prepare_output_dir_mock: unittest.mock.MagicMock,
config_save_mock: unittest.mock.MagicMock,
) -> None:
"""Test keeping target empty."""
with self.assertLogs("bdebstrap", level="INFO"):
self.assertEqual(main(["--name", "empty-target", "unstable"]), 0)
Expand All @@ -91,7 +104,12 @@ def test_empty_target(self, mmdebstrap_call_mock, prepare_output_dir_mock, confi
@unittest.mock.patch("bdebstrap.Config.save")
@unittest.mock.patch("bdebstrap.prepare_output_dir")
@unittest.mock.patch("subprocess.check_call")
def test_minus_target(self, check_call_mock, prepare_output_dir_mock, config_save_mock):
def test_minus_target(
self,
check_call_mock: unittest.mock.MagicMock,
prepare_output_dir_mock: unittest.mock.MagicMock,
config_save_mock: unittest.mock.MagicMock,
) -> None:
"""Test --target=-."""
with self.assertLogs("bdebstrap", level="INFO"):
self.assertEqual(main(["--target=-", "--name", "minus-target", "unstable"]), 0)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pylint.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PylintTestCase(unittest.TestCase):
a config file.
"""

def test_pylint(self):
def test_pylint(self) -> None:
"""Test: Run pylint on Python source code."""
cmd = ["pylint", "--rcfile=" + CONFIG, "--"] + get_source_files()
if os.environ.get("SKIP_LINTERS"):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_shellcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ShellcheckTestCase(unittest.TestCase):
"""

@unittest.skipIf(os.environ.get("SKIP_LINTERS"), "requested via SKIP_LINTERS env variable")
def test_shellcheck(self):
def test_shellcheck(self) -> None:
"""Test: Run shellcheck on Shell source code."""
cmd = ["shellcheck"] + [get_path(s) for s in SHELL_SCRIPTS]
if unittest_verbosity() >= 2:
Expand Down

0 comments on commit d34c0e3

Please sign in to comment.