xfstestsdb: Create the root xfstestsdb Command

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-01-31 15:06:34 -05:00
parent bc8b6bdff8
commit c210eff9b9
7 changed files with 105 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*__pycache__*

15
Makefile Normal file
View File

@ -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

3
pytest.ini Normal file
View File

@ -0,0 +1,3 @@
[pytest]
testpaths = xfstestsdb/ tests/
addopts= -rP

2
tests/__init__.py Normal file
View File

@ -0,0 +1,2 @@
# Copyright 2023 (c) Anna Schumaker.
"""Needed to initialize tests correctly."""

50
tests/test_xfstestsdb.py Normal file
View File

@ -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")

6
xfstestsdb.py Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/python
# Copyright 2023 (c) Anna Schumaker.
"""The entrypoint for the xfstestsdb tool."""
import xfstestsdb
xfstestsdb.Command().run()

28
xfstestsdb/__init__.py Normal file
View File

@ -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)