Exemplo n.º 1
0
    def test_custom_field(self, _, mock_dump):
        """Test inclusion of custom field from other extension."""

        # pylint: disable=abstract-method

        class ExtA(AbstractExtension):
            """Extension that does not write custom fields."""

        class ExtB(AbstractExtension):
            """Extension that writes a custom field."""
            @staticmethod
            def manifest_fields():
                """Provide custom manifest field."""
                return {"otherfield": "foo bar"}

        languages = ("en", "de")
        manifest = Manifest({
            "languages": languages,
            "title": {
                "en": "Title",
                "de": "Titel"
            },
            "min_score": 90,
        })
        ext = WriteManifest(manifest)
        ext.extension_list([ExtA(manifest), ExtB(manifest)])
        self._run(ext, languages=languages, manifest=manifest)
        manifest_dict = mock_dump.call_args[0][0]
        self.assertEqual(manifest_dict["otherfield"], "foo bar")
Exemplo n.º 2
0
 def test_keywords(self):
     """Test a manifest with keywords."""
     manifest = Manifest.from_yaml(KEYWORDS)
     keywords = getattr(manifest, "keywords")
     self.assertIs(len(keywords), 3)
     self.assertIn("foo", keywords)
     self.assertIn("bar", keywords)
     self.assertIn("baz", keywords)
Exemplo n.º 3
0
 def test_with_preamble(self, mock_popen, *_):
     """Test conversion with LaTeX preamble."""
     manifest_data = {
         "languages": ("en",),
         "title": {"en": "Foo"},
         "min_score": 90,
         "tikz_preamble": TIKZ_PREAMBLE,
     }
     manifest = Manifest(manifest_data)
     input_ast = [deepcopy(TIKZ_BLOCK)]
     self._run(Tikz2Svg, input_ast, paths=PATHS, manifest=manifest)
     written = mock_popen.return_value.stdin.write.call_args[0][0].decode()
     self.assertIn(TIKZ_PREAMBLE, written)
Exemplo n.º 4
0
 def test_run_no_pages(self, *_):
     """Ensure pages key can be missing from manifest."""
     manifest = Manifest({
         "title": {
             "en": "Test"
         },
         "languages": ("en", ),
         "min_score": 90
     })
     self.runner = InnoconvRunner("/src", "/out", manifest, [])
     try:
         self.runner.run()
     except RuntimeError:
         self.fail("Exception was raised with missing pages key!")
Exemplo n.º 5
0
 def test_yaml_ext(self, mopen):
     """Ensure a manifest.yaml is accepted."""
     mopen.side_effect = (
         FileNotFoundError,
         mock_open(read_data=MINIMUM).return_value,
     )
     manifest = Manifest.from_directory("/path/to/content")
     self.assertIsInstance(manifest, Manifest)
     self.assertEqual(
         mopen.call_args_list,
         [
             call("/path/to/content/manifest.yml", "r"),
             call("/path/to/content/manifest.yaml", "r"),
         ],
     )
Exemplo n.º 6
0
 def test_minimum(self):
     """Test the minium example."""
     manifest = Manifest.from_yaml(MINIMUM)
     title = getattr(manifest, "title")
     self.assertIs(len(title), 2)
     self.assertEqual(title["de"], "Foo Titel")
     self.assertEqual(title["en"], "Foo Title")
     languages = getattr(manifest, "languages")
     self.assertIs(len(languages), 2)
     self.assertIn("en", languages)
     self.assertIn("de", languages)
     self.assertIs(getattr(manifest, "min_score"), 80)
     with self.assertRaises(AttributeError):
         getattr(manifest, "keywords")
     with self.assertRaises(AttributeError):
         getattr(manifest, "custom_content")
Exemplo n.º 7
0
 def _run(extension,
          ast=None,
          languages=("en", "de"),
          paths=PATHS,
          manifest=None):
     if ast is None:
         ast = get_filler_content()
     title = {}
     for language in languages:
         title[language] = f"Title ({language})"
     if manifest is None:
         manifest = Manifest({
             "languages": languages,
             "title": title,
             "min_score": 90
         })
     try:
         if issubclass(extension, AbstractExtension):
             ext = extension(manifest)
         else:
             raise ValueError(
                 "extension not a sub-class of AbstractExtension!")
     except TypeError:
         ext = extension
     ext.start(DEST, SOURCE)
     asts = []
     for language in languages:
         ext.pre_conversion(language)
         for title, path in paths:
             ext.pre_process_file(join(language, *path))
             file_ast = deepcopy(ast)
             asts.append(file_ast)
             file_title = f"{title} {language}"
             ext.post_process_file(file_ast, file_title, "section", "test")
         ext.post_conversion(language)
     ext.finish()
     return ext, asts
Exemplo n.º 8
0
"""Unit tests for AbstractExtension."""

import unittest

from innoconv.ext.abstract import AbstractExtension
from innoconv.manifest import Manifest

MANIFEST = Manifest({
    "title": {
        "en": "Foo Course Title"
    },
    "languages": ("en", ),
    "min_score": 90
})


class MyCrazyExtension(AbstractExtension):
    """Extension that serves for testing inheriting from AbstractExtension."""

    _helptext = "Foo bar"


class TestAbstractExtension(unittest.TestCase):
    """Test inheriting from AbstractExtension."""
    def test_helptext(self):
        """Test extension help text."""
        self.assertEqual(MyCrazyExtension.helptext(), "Foo bar")

    def test_methods(self):
        """Test extension method interface."""
        test_extension = MyCrazyExtension(MANIFEST)
Exemplo n.º 9
0
"""Unit tests for the innoconv command-line interface."""

import logging
from os.path import join, realpath
import unittest
from unittest.mock import call, patch

from click.testing import CliRunner
from yaml import YAMLError

from innoconv.cli import cli
from innoconv.constants import DEFAULT_EXTENSIONS, LOG_FORMAT
from innoconv.manifest import Manifest


MANIFEST = Manifest(data={"title": "Foo title", "languages": ["en"], "min_score": 80})


@patch("innoconv.cli.Manifest.from_directory", side_effect=(MANIFEST,))
@patch("os.path.exists", return_value=False)
@patch("innoconv.cli.InnoconvRunner.run")
@patch("innoconv.cli.InnoconvRunner.__init__", return_value=None)
@patch("innoconv.cli.coloredlogs.install")
class TestCLI(unittest.TestCase):
    """Test the command-line interface argument parsing."""

    def test_help(self, *_):
        """Test the help argument."""
        runner = CliRunner()
        result = runner.invoke(cli, "--help")
        self.assertIs(result.exit_code, 0)
Exemplo n.º 10
0
from unittest.mock import call, DEFAULT, patch

from innoconv.ext.abstract import AbstractExtension
from innoconv.manifest import Manifest
from innoconv.runner import InnoconvRunner

MANIFEST = Manifest({
    "title": {
        "en": "Foo Course Title",
        "de": "Foo Kurstitel"
    },
    "languages": ("de", "en"),
    "min_score":
    90,
    "pages": [
        {
            "id": "test1",
            "icon": "info-circle",
            "link_in_nav": True,
            "link_in_footer": False,
        },
        {
            "id": "test2"
        },
    ],
})


def walk_side_effect(path):
    """Simulate a recursive traversal in an intact content directory."""
    lang = path[-2:]
    return iter([
Exemplo n.º 11
0
 def test_invalid_yaml(self, mopen):
     """Ensure a YAMLError is raised if manifest could not be parsed."""
     invalid_yaml = '"This is no valid yaml'
     mopen.side_effect = (mock_open(read_data=invalid_yaml).return_value, )
     with self.assertRaises(yaml.YAMLError):
         Manifest.from_directory("/path/to/content")
Exemplo n.º 12
0
 def test_not_found(self, mopen):
     """Ensure a FileNotFoundError is raised if no manifest was found."""
     mopen.side_effect = (FileNotFoundError, FileNotFoundError)
     with self.assertRaises(FileNotFoundError):
         Manifest.from_directory("/path/to/content")
Exemplo n.º 13
0
 def test_yml_ext(self, mopen):
     """Ensure a manifest.yml is accepted."""
     manifest = Manifest.from_directory("/path/to/content")
     self.assertIsInstance(manifest, Manifest)
     self.assertEqual(mopen.call_args,
                      call("/path/to/content/manifest.yml", "r"))
Exemplo n.º 14
0
 def test_missing_required(self):
     """Ensure a RuntimeError is raised for missing data."""
     for data in (MISSING_TITLE, MISSING_LANGUAGES):
         with self.subTest(data):
             with self.assertRaises(RuntimeError):
                 Manifest.from_yaml(data)