From 0e54d18ab17f99700474ab71a289e643fdc14961 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 28 Jan 2016 15:49:22 +0100 Subject: [PATCH] [WIP] Test of `patatools convert FOO` works - Errors are not tested yet - Pylint may fail --- patacrep/tools/convert/__main__.py | 10 +-- test/test_patatools/test_convert.py | 85 +++++++++++++++++++ .../test_convert_errors/song.tsg | 12 +++ .../test_convert_success/.gitignore | 1 + .../test_convert_success/greensleeves.csg | 45 ++++++++++ .../greensleeves.csg.tsg.control | 62 ++++++++++++++ 6 files changed, 210 insertions(+), 5 deletions(-) create mode 100644 test/test_patatools/test_convert_errors/song.tsg create mode 100644 test/test_patatools/test_convert_success/.gitignore create mode 100644 test/test_patatools/test_convert_success/greensleeves.csg create mode 100644 test/test_patatools/test_convert_success/greensleeves.csg.tsg.control diff --git a/patacrep/tools/convert/__main__.py b/patacrep/tools/convert/__main__.py index 3e84ea1a..f64badc9 100644 --- a/patacrep/tools/convert/__main__.py +++ b/patacrep/tools/convert/__main__.py @@ -25,15 +25,15 @@ def confirm(destname): def main(args=None): """Main function: run from command line.""" if args is None: - args = sys.argv[1:] - if len(args) < 3: + args = sys.argv + if len(args) < 4: LOGGER.error("Invalid number of arguments.") LOGGER.error("Usage: %s", _usage()) sys.exit(1) - source = args[0] - dest = args[1] - song_files = args[2:] + source = args[1] + dest = args[2] + song_files = args[3:] renderers = files.load_plugins( datadirs=DEFAULT_CONFIG.get('datadir', []), diff --git a/test/test_patatools/test_convert.py b/test/test_patatools/test_convert.py index e69de29b..38ee9995 100644 --- a/test/test_patatools/test_convert.py +++ b/test/test_patatools/test_convert.py @@ -0,0 +1,85 @@ +"""Tests of the patatools-convert command.""" + +# pylint: disable=too-few-public-methods + +from pkg_resources import resource_filename +import contextlib +import glob +import os +import shutil +import unittest + +from patacrep import files +from patacrep.tools.__main__ import main as tools_main +from patacrep.encoding import open_read +from patacrep.tools.convert.__main__ import main as convert_main +from patacrep.songbook.__main__ import main as songbook_main + +from .. import dynamic +from .. import logging_reduced + +class TestConvert(unittest.TestCase, metaclass=dynamic.DynamicTest): + """Test of the "patatools convert" subcommand""" + + def _system(self, main, args): + try: + main(args) + except SystemExit as systemexit: + return systemexit.code + return 0 + + def assertConvert(self, basename, in_format, out_format): + """Test of the "patatools convert" subcommand""" + sourcename = "{}.{}".format(basename, in_format) + destname = "{}.{}".format(basename, out_format) + controlname = "{}.{}.control".format(sourcename, out_format) + for main, args in [ + (tools_main, ["patatools", "convert"]), + (convert_main, ["patatools-convert"]), + ]: + with self.subTest(main=main, args=args): + with self.chdir("test_convert_success"): + with open_read(controlname) as controlfile: + with logging_reduced(): + if os.path.exists(destname): + os.remove(destname) + self._system(main, args + [in_format, out_format, sourcename]) + expected = controlfile.read().strip().replace( + "@TEST_FOLDER@", + files.path2posix(resource_filename(__name__, "")), + ) + with open_read(destname) as destfile: + self.assertMultiLineEqual( + destfile.read().strip(), + expected, + ) + + @staticmethod + @contextlib.contextmanager + def chdir(*path): + """Context to temporarry change current directory, relative to this file directory + """ + with files.chdir(resource_filename(__name__, ""), *path): + yield + + @classmethod + def _iter_testmethods(cls): + """Iterate over song files to test.""" + with cls.chdir("test_convert_success"): + for control in sorted(glob.glob('*.*.*.control')): + [*base, in_format, out_format, _] = control.split('.') + base = '.'.join(base) + yield ( + "test_{}_{}_{}".format(base, in_format, out_format), + cls._create_test(base, in_format, out_format), + ) + + @classmethod + def _create_test(cls, base, in_format, out_format): + """Return a function testing that `base` compilation from `in_format` to `out_format` format. + """ + test_parse_render = lambda self: self.assertConvert(base, in_format, out_format) + test_parse_render.__doc__ = ( + "Test that '{base}.{in_format}' is correctly converted to '{out_format}'." + ).format(base=os.path.basename(base), in_format=in_format, out_format=out_format) + return test_parse_render diff --git a/test/test_patatools/test_convert_errors/song.tsg b/test/test_patatools/test_convert_errors/song.tsg new file mode 100644 index 00000000..c57d0f79 --- /dev/null +++ b/test/test_patatools/test_convert_errors/song.tsg @@ -0,0 +1,12 @@ +\beginsong{Wonderful song} + [by={Anonymous}] + + \begin{verse} + \[A]La la la\\ + \end{verse} + + \begin{chorus} + \[C]La la la\\ + \end{chorus} + +\endsong diff --git a/test/test_patatools/test_convert_success/.gitignore b/test/test_patatools/test_convert_success/.gitignore new file mode 100644 index 00000000..9947c79b --- /dev/null +++ b/test/test_patatools/test_convert_success/.gitignore @@ -0,0 +1 @@ +greensleeves.tsg diff --git a/test/test_patatools/test_convert_success/greensleeves.csg b/test/test_patatools/test_convert_success/greensleeves.csg new file mode 100644 index 00000000..54f8911f --- /dev/null +++ b/test/test_patatools/test_convert_success/greensleeves.csg @@ -0,0 +1,45 @@ +{lang: en} +{columns: 2} +{title: Greensleeves} +{title: Un autre sous-titre} +{title: Un sous titre} +{artist: Traditionnel} +{album: Angleterre} + + +A[Am]las, my love, ye [G]do me wrong +To [Am]cast me oft dis[E]curteously +And [Am]I have loved [G]you so long +De[Am]lighting [E]in your [Am]companie + + +{start_of_chorus} + [C]Green[B]sleeves was [G]all my joy + [Am]Greensleeves was [E]my delight + [C]Greensleeves was my [G]heart of gold + And [Am]who but [E]Ladie [Am]Greensleeves +{end_of_chorus} + + +I [Am]have been ready [G]at your hand +To [Am]grant what ever [E]you would crave +I [Am]have both waged [G]life and land +Your [Am]love and [E]good will [Am]for to have + + +I [Am]bought thee kerchers [G]to thy head +That [Am]were wrought fine and [E]gallantly +I [Am]kept thee both at [G]boord and bed +Which [Am]cost my [E]purse well [Am]favouredly + + +I [Am]bought thee peticotes [G]of the best +The [Am]cloth so fine as [E]fine might be +I [Am]gave thee jewels [G]for thy chest +And [Am]all this [E]cost I [Am]spent on thee + + +Thy [Am]smock of silke, both [G]faire and white +With [Am]gold embrodered [E]gorgeously +Thy [Am]peticote of [G]sendall right +And [Am]this I [E]bought thee [Am]gladly diff --git a/test/test_patatools/test_convert_success/greensleeves.csg.tsg.control b/test/test_patatools/test_convert_success/greensleeves.csg.tsg.control new file mode 100644 index 00000000..cba09096 --- /dev/null +++ b/test/test_patatools/test_convert_success/greensleeves.csg.tsg.control @@ -0,0 +1,62 @@ +\selectlanguage{english} +\songcolumns{2} + +\beginsong{Greensleeves\\ +Un autre sous-titre\\ +Un sous titre}[ + by={ + Traditionnel }, + album={Angleterre}, +] + + + + +\begin{verse} + A\[Am]las, my love, ye \[G]do me wrong + To \[Am]cast me oft dis\[E]curteously + And \[Am]I have loved \[G]you so long + De\[Am]lighting \[E]in your \[Am]companie +\end{verse} + + +\begin{chorus} + \[C]Green\[B]sleeves was \[G]all my joy + \[Am]Greensleeves was \[E]my delight + \[C]Greensleeves was my \[G]heart of gold + And \[Am]who but \[E]Ladie \[Am]Greensleeves +\end{chorus} + + +\begin{verse} + I \[Am]have been ready \[G]at your hand + To \[Am]grant what ever \[E]you would crave + I \[Am]have both waged \[G]life and land + Your \[Am]love and \[E]good will \[Am]for to have +\end{verse} + + +\begin{verse} + I \[Am]bought thee kerchers \[G]to thy head + That \[Am]were wrought fine and \[E]gallantly + I \[Am]kept thee both at \[G]boord and bed + Which \[Am]cost my \[E]purse well \[Am]favouredly +\end{verse} + + +\begin{verse} + I \[Am]bought thee peticotes \[G]of the best + The \[Am]cloth so fine as \[E]fine might be + I \[Am]gave thee jewels \[G]for thy chest + And \[Am]all this \[E]cost I \[Am]spent on thee +\end{verse} + + +\begin{verse} + Thy \[Am]smock of silke, both \[G]faire and white + With \[Am]gold embrodered \[E]gorgeously + Thy \[Am]peticote of \[G]sendall right + And \[Am]this I \[E]bought thee \[Am]gladly +\end{verse} + +\endsong