From c210eff9b9fc21a633f72403d77010febb4c50cf Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 31 Jan 2023 15:06:34 -0500 Subject: [PATCH] xfstestsdb: Create the root xfstestsdb Command Signed-off-by: Anna Schumaker --- .gitignore | 1 + Makefile | 15 ++++++++++++ pytest.ini | 3 +++ tests/__init__.py | 2 ++ tests/test_xfstestsdb.py | 50 ++++++++++++++++++++++++++++++++++++++++ xfstestsdb.py | 6 +++++ xfstestsdb/__init__.py | 28 ++++++++++++++++++++++ 7 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 pytest.ini create mode 100644 tests/__init__.py create mode 100644 tests/test_xfstestsdb.py create mode 100755 xfstestsdb.py create mode 100644 xfstestsdb/__init__.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd4c22c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*__pycache__* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..151b6c6 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +# Copyright 2023 (c) Anna Schumaker. + +clean: + find . -type d -name __pycache__ -exec rm -r {} \+ + +.PHONY: flake8 +flake8: + flake8 + +.PHONY: pytest +pytest: + pytest + +.PHONY: tests +tests: pytest flake8 diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..f91e9c9 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +testpaths = xfstestsdb/ tests/ +addopts= -rP diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..5a9ca89 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,2 @@ +# Copyright 2023 (c) Anna Schumaker. +"""Needed to initialize tests correctly.""" diff --git a/tests/test_xfstestsdb.py b/tests/test_xfstestsdb.py new file mode 100644 index 0000000..5352079 --- /dev/null +++ b/tests/test_xfstestsdb.py @@ -0,0 +1,50 @@ +# Copyright 2023 (c) Anna Schumaker. +"""Tests the `xfstestsdb` command.""" +import io +import argparse +import unittest +import unittest.mock +import xfstestsdb + + +class TestXfstestsdb(unittest.TestCase): + """Tests the xfstestsdb command operation.""" + + def setUp(self): + """Set up common variables.""" + self.xfstestsdb = xfstestsdb.Command() + + def test_init(self): + """Test that the xfstestsdb instance is set up properly.""" + self.assertIsInstance(self.xfstestsdb.parser, argparse.ArgumentParser) + self.assertIsInstance(self.xfstestsdb.subparser, argparse.Action) + + @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) + def test_run_empty(self, mock_stdout: io.StringIO): + """Test calling xfstestdb.run() with an empty argument list.""" + self.xfstestsdb.run([]) + self.assertRegex(mock_stdout.getvalue(), + r"^usage: pytest \[\-h\] \[\-\-version\]" + r"\s+\{.*?} ...$") + + def test_run(self): + """Test running the xfstestsdb.""" + parser = self.xfstestsdb.subparser.add_parser("test-run", help="help") + test_passed = False + + def test_func(args: argparse.Namespace) -> None: + nonlocal test_passed + test_passed = True + parser.set_defaults(function=test_func) + + self.xfstestsdb.run(["test-run"]) + self.assertTrue(test_passed) + + @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) + def test_version(self, mock_stdout: io.StringIO): + """Test printing version information.""" + self.assertEqual(xfstestsdb.MAJOR, 1) + self.assertEqual(xfstestsdb.MINOR, 0) + + self.xfstestsdb.run(["--version"]) + self.assertEqual(mock_stdout.getvalue(), "xfstestsdb v1.0-debug\n") diff --git a/xfstestsdb.py b/xfstestsdb.py new file mode 100755 index 0000000..1690c01 --- /dev/null +++ b/xfstestsdb.py @@ -0,0 +1,6 @@ +#!/usr/bin/python +# Copyright 2023 (c) Anna Schumaker. +"""The entrypoint for the xfstestsdb tool.""" +import xfstestsdb + +xfstestsdb.Command().run() diff --git a/xfstestsdb/__init__.py b/xfstestsdb/__init__.py new file mode 100644 index 0000000..6406c1f --- /dev/null +++ b/xfstestsdb/__init__.py @@ -0,0 +1,28 @@ +# Copyright 2023 (c) Anna Schumaker +"""Implements the toplevel xfstestsdb command.""" +import argparse + +MAJOR = 1 +MINOR = 0 + + +class Command: + """The root xfstestsdb command.""" + + def __init__(self) -> None: + """Initialize the xfstestsdb command.""" + self.parser = argparse.ArgumentParser() + self.parser.set_defaults(function=lambda x: self.parser.print_usage()) + self.parser.add_argument("--version", action="store_true", + help="show version number and exit") + self.subparser = self.parser.add_subparsers(title="commands") + self.commands = {} + + def run(self, args: list = None) -> None: + """Run the xfstestsdb command.""" + parsed = self.parser.parse_args(args) + if parsed.version: + print(f"xfstestsdb v{MAJOR}.{MINOR}" + f"{'-debug' if __debug__ else ''}") + else: + parsed.function(parsed)