Skip to content

Commit

Permalink
Move 'blurb test' subcommand into test suite (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Jan 6, 2025
2 parents 0df5d3b + 0444b48 commit 65941da
Show file tree
Hide file tree
Showing 4 changed files with 38 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 @@ -637,42 +636,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 @@ -836,36 +799,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 @@ -1222,7 +1155,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
36 changes: 36 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import glob
import os

import pytest

from blurb.blurb import Blurbs, pushd


class TestParserPasses:
directory = "tests/pass"

def filename_test(self, filename):
b = Blurbs()
b.load(filename)
assert b
if os.path.exists(filename + ".res"):
with open(filename + ".res", encoding="utf-8") as file:
expected = file.read()
assert str(b) == expected

def test_files(self):
with pushd(self.directory):
for filename in glob.glob("*"):
if filename.endswith(".res"):
assert 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 pytest.raises(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 65941da

Please sign in to comment.