Skip to content

Commit

Permalink
Move 'blurb test' subcommand into test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Nov 9, 2024
1 parent 2a19feb commit bf284ae
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 70 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ exclude_also =
[run]
omit =
**/blurb/__main__.py
**/blurb/_version.py
69 changes: 1 addition & 68 deletions src/blurb/blurb.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import tempfile
import textwrap
import time
import unittest

from . import __version__

Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
35 changes: 35 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bf284ae

Please sign in to comment.