Engine for LaTeX songbooks http://www.patacrep.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.6 KiB

"""Tests for the chordpro parser."""
# pylint: disable=too-few-public-methods
import glob
import os
import sys
import subprocess
import unittest
import logging
from patacrep.encoding import open_read
from .. import dynamic # pylint: disable=unused-import
LOGGER = logging.getLogger(__name__)
class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
"""Test of songbook compilation.
For any given `foo.sb`, it performs several checks:
- the corresponding tex file is generated;
- the generated tex file matches the `foo.tex.control` control file;
- the compilation (tex, pdf, indexes) works without errors.
This class does nothing by itself, but its metaclass populates it with test
methods testing parser and renderers.
"""
maxDiff = None
@classmethod
def _iter_testmethods(cls):
"""Iterate over songbook files to test."""
for songbook in sorted(glob.glob(os.path.join(
os.path.dirname(__file__),
'*.sb',
))):
base = songbook[:-len(".sb")]
control = "{}.tex.control".format(base)
if not os.path.exists(control):
continue
yield (
"test_{}".format(os.path.basename(base)),
[base],
)
@classmethod
def _create_test(cls, base):
"""Return a function testing that `base` compiles."""
def test_compile(self):
"""Test that `base` is correctly compiled."""
if base is None:
return
songbook = "{}.sb".format(base)
# Check tex generation
self.assertEqual(0, self.compile_songbook(songbook, "tex"))
# Check generated tex
control = "{}.tex.control".format(base)
tex = "{}.tex".format(base)
with open_read(control) as expectfile:
with open_read(tex) as latexfile:
expected = expectfile.read().strip()
expected = expected.replace(
"@TEST_FOLDER@",
os.path.dirname(__file__),
)
expected = expected.replace(
"@DATA_FOLDER@",
subprocess.check_output(
[sys.executable, "-c", 'import patacrep, pkg_resources; print(pkg_resources.resource_filename(patacrep.__name__, "data"))'], # pylint: disable=line-too-long
universal_newlines=True,
cwd=os.path.dirname(songbook),
).strip(),
)
self.assertMultiLineEqual(
latexfile.read().strip(),
expected,
)
# Check compilation
if 'TRAVIS' not in os.environ:
# Travis does not support lualatex compilation yet
self.assertEqual(0, self.compile_songbook(songbook))
test_compile.__doc__ = (
"Test that '{base}' is correctly compiled."
).format(base=os.path.basename(base))
return test_compile
@staticmethod
def compile_songbook(songbook, steps=None):
"""Compile songbook, and return the command return code."""
command = [sys.executable, '-m', 'patacrep.songbook', songbook, '-v']
if steps:
command.extend(['--steps', steps])
if os.name == 'nt' and 'APPVEYOR' in os.environ:
# On windows, we need to pass the current env as argument
current_env = os.environ.copy()
# Force the pythonpath for AppVeyor
current_env['PYTHONPATH'] = 'c:\\projects\\patacrep\\.tox\\py34\\lib\\site-packages\\'
else:
current_env = None
print("#####")
try:
cmd = subprocess.check_output(
9 years ago
['dir', 'c:\\projects\\patacrep\\.tox\\py34\\lib\\site-packages\\patacrep\\'],
stderr=subprocess.STDOUT,
universal_newlines=True,
cwd=os.path.dirname(songbook),
env=current_env
)
print(cmd)
except subprocess.CalledProcessError as error:
print(error.output)
print("######")
try:
cmd = subprocess.check_output(
['dir', 'c:\\projects\\patacrep\\.tox\\py34\\lib\\site-packages\\patacrep\\data\\'],
stderr=subprocess.STDOUT,
universal_newlines=True,
cwd=os.path.dirname(songbook),
env=current_env
)
print(cmd)
except subprocess.CalledProcessError as error:
print(error.output)
print("########")
try:
cmd = subprocess.check_output(
['dir', 'c:\\projects\\patacrep\\.tox\\py34\\lib\\site-packages\\patacrep\\data\\templates\\'],
stderr=subprocess.STDOUT,
universal_newlines=True,
cwd=os.path.dirname(songbook),
env=current_env
)
print(cmd)
except subprocess.CalledProcessError as error:
print(error.output)
try:
subprocess.check_output(
command,
stderr=subprocess.STDOUT,
universal_newlines=True,
cwd=os.path.dirname(songbook),
env=current_env
)
return 0
except subprocess.CalledProcessError as error:
LOGGER.warning(error.output)
return error.returncode