summaryrefslogtreecommitdiff
path: root/creole/tests/test_setup_utils.py
blob: 3ef712aeda258fa811cdfdde50f632fd8e83855c (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
    unittest for setup_utils
    ~~~~~~~~~~~~~~~~~~~~~~~~

    https://github.com/jedie/python-creole/wiki/Use-In-Setup

    :copyleft: 2011-2020 by python-creole team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""
import difflib
import filecmp
import os
import shutil
import tempfile
from pathlib import Path

from creole.setup_utils import get_long_description, update_creole_rst_readme
from creole.tests.constants import CREOLE_PACKAGE_ROOT
from creole.tests.utils.base_unittest import BaseCreoleTest
from creole.tests.utils.utils import IsolatedFilesystem

TEST_README_DIR = Path(__file__).parent
TEST_README_FILENAME = "test_README.creole"


class SetupUtilsTests(BaseCreoleTest):
    def test_creole_package_path(self):
        self.assertTrue(
            os.path.isdir(CREOLE_PACKAGE_ROOT),
            f"CREOLE_PACKAGE_ROOT {CREOLE_PACKAGE_ROOT!r} is not a existing direcotry!"
        )
        filepath = os.path.join(CREOLE_PACKAGE_ROOT, "README.creole")
        self.assertTrue(
            os.path.isfile(filepath),
            f"README file {filepath!r} not found!"
        )

    def test_get_long_description_without_raise_errors(self):
        long_description = get_long_description(CREOLE_PACKAGE_ROOT, raise_errors=False)
        self.assertIn("===================\nabout python-creole\n===================\n\n", long_description)
        # Test created ReSt code
        from creole.rest_tools.clean_writer import rest2html
        html = rest2html(long_description)
        self.assertIn("<h1>about python-creole</h1>\n", html)

    def test_get_long_description_with_raise_errors(self):
        long_description = get_long_description(CREOLE_PACKAGE_ROOT, raise_errors=True)
        self.assertIn("===================\nabout python-creole\n===================\n\n", long_description)

    def _tempfile(self, content):
        fd = tempfile.NamedTemporaryFile()
        path, filename = os.path.split(fd.name)

        fd.write(content)
        fd.seek(0)
        return path, filename, fd

    def test_tempfile_without_error(self):
        path, filename, fd = self._tempfile(b"== noerror ==")
        try:
            long_description = get_long_description(path, filename, raise_errors=True)
            self.assertEqual(long_description, "-------\nnoerror\n-------")
        finally:
            fd.close()

    def test_get_long_description_error_handling(self):
        """
        Test if get_long_description will raised a error, if description
        produce a ReSt error.

        We test with this error:
        <string>:102: (ERROR/3) Document or section may not begin with a transition.
        """
        path, filename, fd = self._tempfile(b"----")
        try:
            self.assertRaises(SystemExit, get_long_description, path, filename, raise_errors=True)
        finally:
            fd.close()

    def test_get_long_description_error_handling2(self):
        """
        Test if get_long_description will raised a error, if description
        produce a ReSt error.

        We test with this error:
        SystemExit: ReSt2html error: link scheme not allowed
        """
        path, filename, fd = self._tempfile(b"[[foo://bar]]")
#         print(get_long_description(path, filename, raise_errors=True))
        try:
            self.assertRaises(SystemExit, get_long_description, path, filename, raise_errors=True)
        finally:
            fd.close()

    def test_wrong_path_without_raise_errors(self):
        self.assertEqual(
            get_long_description("wrong/path", raise_errors=False).replace("u'", "'"),
            "[Error: [Errno 2] No such file or directory: 'wrong/path/README.creole']\n"
        )

    def test_wrong_path_with_raise_errors(self):
        self.assertRaises(IOError, get_long_description, "wrong/path", raise_errors=True)

    def test_readme_encoding(self):
        long_description = get_long_description(TEST_README_DIR, filename=TEST_README_FILENAME, raise_errors=True)
        self.assertTrue(isinstance(long_description, str))

        txt = "German Umlaute: ä ö ü ß Ä Ö Ü"
        self.assertIn(txt, long_description)


def test_update_rst_readme():
    with IsolatedFilesystem(prefix="temp_dir_prefix"):
        old_rest_readme_path = Path(Path().cwd(), 'README.rst')
        shutil.copy(
            Path(CREOLE_PACKAGE_ROOT, 'README.rst'),
            old_rest_readme_path
        )
        try:
            rest_readme_path = update_creole_rst_readme()
            assert str(rest_readme_path.relative_to(CREOLE_PACKAGE_ROOT)) == 'README.rst'

            if filecmp.cmp(rest_readme_path, old_rest_readme_path, shallow=False) is True:
                return

            with old_rest_readme_path.open('r') as f:
                from_file = [line.rstrip() for line in f]

            with rest_readme_path.open('r') as f:
                to_tile = [line.rstrip() for line in f]

            diff = '\n'.join(
                line
                for line in difflib.Differ().compare(from_file, to_tile)
                if line[0] != ' '
            )
            raise AssertionError(f'README.rst is not up-to-date:\n{diff}')
        finally:
            # restore the origin file
            shutil.copy(
                old_rest_readme_path,
                Path(CREOLE_PACKAGE_ROOT, 'README.rst'),
            )