1"""Test Pandoc module"""
2#-----------------------------------------------------------------------------
3#  Copyright (C) 2014 The IPython Development Team
4#
5#  Distributed under the terms of the BSD License.  The full license is in
6#  the file COPYING, distributed as part of this software.
7#-----------------------------------------------------------------------------
8
9#-----------------------------------------------------------------------------
10# Imports
11#-----------------------------------------------------------------------------
12import os
13import warnings
14
15from ...tests.utils import onlyif_cmds_exist
16
17from nbconvert.tests.base import TestsBase
18from .. import pandoc
19
20#-----------------------------------------------------------------------------
21# Classes and functions
22#-----------------------------------------------------------------------------
23
24class TestPandoc(TestsBase):
25    """Collection of Pandoc tests"""
26
27    def __init__(self, *args, **kwargs):
28        super().__init__(*args, **kwargs)
29        self.original_env = os.environ.copy()
30
31    def setUp(self):
32        super().setUp()
33        pandoc.check_pandoc_version._cached = None
34
35    @onlyif_cmds_exist('pandoc')
36    def test_pandoc_available(self):
37        """ Test behaviour that pandoc functions raise PandocMissing as documented """
38        pandoc.clean_cache()
39
40        os.environ["PATH"] = ""
41        with self.assertRaises(pandoc.PandocMissing):
42            pandoc.get_pandoc_version()
43        with self.assertRaises(pandoc.PandocMissing):
44            pandoc.check_pandoc_version()
45        with self.assertRaises(pandoc.PandocMissing):
46            pandoc.pandoc("", "markdown", "html")
47
48        # original_env["PATH"] should contain pandoc
49        os.environ["PATH"] = self.original_env["PATH"]
50        with warnings.catch_warnings(record=True) as w:
51            pandoc.get_pandoc_version()
52            pandoc.check_pandoc_version()
53            pandoc.pandoc("", "markdown", "html")
54        self.assertEqual(w, [])
55
56    @onlyif_cmds_exist('pandoc')
57    def test_minimal_version(self):
58        original_minversion = pandoc._minimal_version
59
60        pandoc._minimal_version = "120.0"
61        with warnings.catch_warnings(record=True) as w:
62            # call it twice to verify the cached value is used
63            assert not pandoc.check_pandoc_version()
64            assert not pandoc.check_pandoc_version()
65        # only one warning after two calls, due to cache
66        self.assertEqual(len(w), 1)
67        # clear cache
68        pandoc.check_pandoc_version._cached = None
69        pandoc._minimal_version = pandoc.get_pandoc_version()
70        assert pandoc.check_pandoc_version()
71
72
73def pandoc_function_raised_missing(f, *args, **kwargs):
74    try:
75        f(*args, **kwargs)
76    except pandoc.PandocMissing:
77        return True
78    else:
79        return False
80