Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move 'blurb test' subcommand into test suite #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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