File: test_pandoc.py

package info (click to toggle)
nbconvert 7.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,056 kB
  • sloc: python: 8,449; makefile: 199; javascript: 2
file content (78 lines) | stat: -rw-r--r-- 2,867 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Test Pandoc module"""
# -----------------------------------------------------------------------------
#  Copyright (C) 2014 The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
import os
import warnings

from nbconvert.utils import pandoc
from tests.base import TestsBase
from tests.testutils import onlyif_cmds_exist

# -----------------------------------------------------------------------------
# Classes and functions
# -----------------------------------------------------------------------------


class TestPandoc(TestsBase):
    """Collection of Pandoc tests"""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.original_env = os.environ.copy()

    def setUp(self):
        super().setUp()
        pandoc.check_pandoc_version._cached = None  # type:ignore

    @onlyif_cmds_exist("pandoc")
    def test_pandoc_available(self):
        """Test behaviour that pandoc functions raise PandocMissing as documented"""
        pandoc.clean_cache()

        os.environ["PATH"] = ""
        with self.assertRaises(pandoc.PandocMissing):
            pandoc.get_pandoc_version()
        with self.assertRaises(pandoc.PandocMissing):
            pandoc.check_pandoc_version()
        with self.assertRaises(pandoc.PandocMissing):
            pandoc.pandoc("", "markdown", "html")

        # original_env["PATH"] should contain pandoc
        os.environ["PATH"] = self.original_env["PATH"]
        with warnings.catch_warnings(record=True) as w:
            pandoc.get_pandoc_version()
            pandoc.check_pandoc_version()
            pandoc.pandoc("", "markdown", "html")
        self.assertEqual(w, [])

    @onlyif_cmds_exist("pandoc")
    def test_minimal_version(self):
        pandoc._minimal_version = "120.0"
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            # call it twice to verify the cached value is used
            assert not pandoc.check_pandoc_version()
            assert not pandoc.check_pandoc_version()
        # only one warning after two calls, due to cache
        self.assertEqual(len(w), 1)
        # clear cache
        pandoc.check_pandoc_version._cached = None  # type:ignore
        pandoc._minimal_version = pandoc.get_pandoc_version()
        assert pandoc.check_pandoc_version()


def pandoc_function_raised_missing(f, *args, **kwargs):
    try:
        f(*args, **kwargs)
    except pandoc.PandocMissing:
        return True
    else:
        return False