Skip to content

Commit 65941da

Browse files
authored
Move 'blurb test' subcommand into test suite (#37)
2 parents 0df5d3b + 0444b48 commit 65941da

File tree

4 files changed

+38
-70
lines changed

4 files changed

+38
-70
lines changed

.coveragerc

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ exclude_also =
1010
[run]
1111
omit =
1212
**/blurb/__main__.py
13+
**/blurb/_version.py

src/blurb/blurb.py

+1-68
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import tempfile
5858
import textwrap
5959
import time
60-
import unittest
6160

6261
from . import __version__
6362

@@ -637,42 +636,6 @@ def save_next(self):
637636
return filename
638637

639638

640-
tests_run = 0
641-
642-
class TestParserPasses(unittest.TestCase):
643-
directory = "tests/pass"
644-
645-
def filename_test(self, filename):
646-
b = Blurbs()
647-
b.load(filename)
648-
self.assertTrue(b)
649-
if os.path.exists(filename + '.res'):
650-
with open(filename + '.res', encoding='utf-8') as file:
651-
expected = file.read()
652-
self.assertEqual(str(b), expected)
653-
654-
def test_files(self):
655-
global tests_run
656-
with pushd(self.directory):
657-
for filename in glob.glob("*"):
658-
if filename[-4:] == '.res':
659-
self.assertTrue(os.path.exists(filename[:-4]), filename)
660-
continue
661-
self.filename_test(filename)
662-
print(".", end="")
663-
sys.stdout.flush()
664-
tests_run += 1
665-
666-
667-
class TestParserFailures(TestParserPasses):
668-
directory = "tests/fail"
669-
670-
def filename_test(self, filename):
671-
b = Blurbs()
672-
with self.assertRaises(Exception):
673-
b.load(filename)
674-
675-
676639
readme_re = re.compile(r"This is \w+ version \d+\.\d+").match
677640

678641
def chdir_to_repo_root():
@@ -836,36 +799,6 @@ def _find_blurb_dir():
836799
return None
837800

838801

839-
@subcommand
840-
def test(*args):
841-
"""
842-
Run unit tests. Only works inside source repo, not when installed.
843-
"""
844-
# unittest.main doesn't work because this isn't a module
845-
# so we'll do it ourselves
846-
847-
while (blurb_dir := _find_blurb_dir()) is None:
848-
old_dir = os.getcwd()
849-
os.chdir("..")
850-
if old_dir == os.getcwd():
851-
# we reached the root and never found it!
852-
sys.exit("Error: Couldn't find the root of your blurb repo!")
853-
os.chdir(blurb_dir)
854-
855-
print("-" * 79)
856-
857-
for clsname, cls in sorted(globals().items()):
858-
if clsname.startswith("Test") and isinstance(cls, type):
859-
o = cls()
860-
for fnname in sorted(dir(o)):
861-
if fnname.startswith("test"):
862-
fn = getattr(o, fnname)
863-
if callable(fn):
864-
fn()
865-
print()
866-
print(tests_run, "tests passed.")
867-
868-
869802
def find_editor():
870803
for var in 'GIT_EDITOR', 'EDITOR':
871804
editor = os.environ.get(var)
@@ -1222,7 +1155,7 @@ def main():
12221155
fn = get_subcommand(subcommand)
12231156

12241157
# hack
1225-
if fn in (help, test, version):
1158+
if fn in (help, version):
12261159
sys.exit(fn(*args))
12271160

12281161
try:

tests/test_parser.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import glob
2+
import os
3+
4+
import pytest
5+
6+
from blurb.blurb import Blurbs, pushd
7+
8+
9+
class TestParserPasses:
10+
directory = "tests/pass"
11+
12+
def filename_test(self, filename):
13+
b = Blurbs()
14+
b.load(filename)
15+
assert b
16+
if os.path.exists(filename + ".res"):
17+
with open(filename + ".res", encoding="utf-8") as file:
18+
expected = file.read()
19+
assert str(b) == expected
20+
21+
def test_files(self):
22+
with pushd(self.directory):
23+
for filename in glob.glob("*"):
24+
if filename.endswith(".res"):
25+
assert os.path.exists(filename[:-4]), filename
26+
continue
27+
self.filename_test(filename)
28+
29+
30+
class TestParserFailures(TestParserPasses):
31+
directory = "tests/fail"
32+
33+
def filename_test(self, filename):
34+
b = Blurbs()
35+
with pytest.raises(Exception):
36+
b.load(filename)

tox.ini

-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ commands =
1717
--cov-report term \
1818
--cov-report xml \
1919
{posargs}
20-
blurb test
2120
blurb help
2221
blurb --version
23-
{envpython} -I -m blurb test
2422
{envpython} -I -m blurb help
2523
{envpython} -I -m blurb version

0 commit comments

Comments
 (0)