From bf284aeae41ed5555089bb384b0485f691540e4f Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 9 Nov 2024 20:47:53 +0200 Subject: [PATCH 1/2] Move 'blurb test' subcommand into test suite --- .coveragerc | 1 + src/blurb/blurb.py | 69 +------------------------------------------- tests/test_parser.py | 35 ++++++++++++++++++++++ tox.ini | 2 -- 4 files changed, 37 insertions(+), 70 deletions(-) create mode 100644 tests/test_parser.py diff --git a/.coveragerc b/.coveragerc index dcd3739..81ba1c7 100644 --- a/.coveragerc +++ b/.coveragerc @@ -10,3 +10,4 @@ exclude_also = [run] omit = **/blurb/__main__.py + **/blurb/_version.py diff --git a/src/blurb/blurb.py b/src/blurb/blurb.py index 0af3ea9..c671d0c 100755 --- a/src/blurb/blurb.py +++ b/src/blurb/blurb.py @@ -57,7 +57,6 @@ import tempfile import textwrap import time -import unittest from . import __version__ @@ -639,42 +638,6 @@ def save_next(self): return filename -tests_run = 0 - -class TestParserPasses(unittest.TestCase): - directory = "tests/pass" - - def filename_test(self, filename): - b = Blurbs() - b.load(filename) - self.assertTrue(b) - if os.path.exists(filename + '.res'): - with open(filename + '.res', encoding='utf-8') as file: - expected = file.read() - self.assertEqual(str(b), expected) - - def test_files(self): - global tests_run - with pushd(self.directory): - for filename in glob.glob("*"): - if filename[-4:] == '.res': - self.assertTrue(os.path.exists(filename[:-4]), filename) - continue - self.filename_test(filename) - print(".", end="") - sys.stdout.flush() - tests_run += 1 - - -class TestParserFailures(TestParserPasses): - directory = "tests/fail" - - def filename_test(self, filename): - b = Blurbs() - with self.assertRaises(Exception): - b.load(filename) - - readme_re = re.compile(r"This is \w+ version \d+\.\d+").match def chdir_to_repo_root(): @@ -838,36 +801,6 @@ def _find_blurb_dir(): return None -@subcommand -def test(*args): - """ -Run unit tests. Only works inside source repo, not when installed. - """ - # unittest.main doesn't work because this isn't a module - # so we'll do it ourselves - - while (blurb_dir := _find_blurb_dir()) is None: - old_dir = os.getcwd() - os.chdir("..") - if old_dir == os.getcwd(): - # we reached the root and never found it! - sys.exit("Error: Couldn't find the root of your blurb repo!") - os.chdir(blurb_dir) - - print("-" * 79) - - for clsname, cls in sorted(globals().items()): - if clsname.startswith("Test") and isinstance(cls, type): - o = cls() - for fnname in sorted(dir(o)): - if fnname.startswith("test"): - fn = getattr(o, fnname) - if callable(fn): - fn() - print() - print(tests_run, "tests passed.") - - def find_editor(): for var in 'GIT_EDITOR', 'EDITOR': editor = os.environ.get(var) @@ -1224,7 +1157,7 @@ def main(): fn = get_subcommand(subcommand) # hack - if fn in (help, test, version): + if fn in (help, version): sys.exit(fn(*args)) try: diff --git a/tests/test_parser.py b/tests/test_parser.py new file mode 100644 index 0000000..4bacc12 --- /dev/null +++ b/tests/test_parser.py @@ -0,0 +1,35 @@ +import glob +import os +import unittest + +from blurb.blurb import Blurbs, pushd + + +class TestParserPasses(unittest.TestCase): + directory = "tests/pass" + + def filename_test(self, filename): + b = Blurbs() + b.load(filename) + self.assertTrue(b) + if os.path.exists(filename + ".res"): + with open(filename + ".res", encoding="utf-8") as file: + expected = file.read() + self.assertEqual(str(b), expected) + + def test_files(self): + with pushd(self.directory): + for filename in glob.glob("*"): + if filename[-4:] == ".res": + self.assertTrue(os.path.exists(filename[:-4]), filename) + continue + self.filename_test(filename) + + +class TestParserFailures(TestParserPasses): + directory = "tests/fail" + + def filename_test(self, filename): + b = Blurbs() + with self.assertRaises(Exception): + b.load(filename) diff --git a/tox.ini b/tox.ini index fa69718..1ed9746 100644 --- a/tox.ini +++ b/tox.ini @@ -17,9 +17,7 @@ commands = --cov-report term \ --cov-report xml \ {posargs} - blurb test blurb help blurb --version - {envpython} -I -m blurb test {envpython} -I -m blurb help {envpython} -I -m blurb version From 0444b4809c1f444001d94c779438b6059317b66d Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 9 Nov 2024 21:15:43 +0200 Subject: [PATCH 2/2] Convert to pytest style --- tests/test_parser.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/test_parser.py b/tests/test_parser.py index 4bacc12..4b5b3f3 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1,27 +1,28 @@ import glob import os -import unittest + +import pytest from blurb.blurb import Blurbs, pushd -class TestParserPasses(unittest.TestCase): +class TestParserPasses: directory = "tests/pass" def filename_test(self, filename): b = Blurbs() b.load(filename) - self.assertTrue(b) + assert b if os.path.exists(filename + ".res"): with open(filename + ".res", encoding="utf-8") as file: expected = file.read() - self.assertEqual(str(b), expected) + assert str(b) == expected def test_files(self): with pushd(self.directory): for filename in glob.glob("*"): - if filename[-4:] == ".res": - self.assertTrue(os.path.exists(filename[:-4]), filename) + if filename.endswith(".res"): + assert os.path.exists(filename[:-4]), filename continue self.filename_test(filename) @@ -31,5 +32,5 @@ class TestParserFailures(TestParserPasses): def filename_test(self, filename): b = Blurbs() - with self.assertRaises(Exception): + with pytest.raises(Exception): b.load(filename)