summaryrefslogtreecommitdiff
path: root/src/zope/i18n/tests/test_compile.py
blob: 1aa7bd9a1eac4c426ddb69652f5158a02fae8909 (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
##############################################################################
#
# Copyright (c) 2017 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

import unittest

from zope.i18n import compile


@unittest.skipUnless(compile.HAS_PYTHON_GETTEXT,
                     "Need python-gettext")
class TestCompile(unittest.TestCase):

    def test_non_existant_path(self):

        self.assertIsNone(compile.compile_mo_file('no_such_domain', ''))

    def test_po_exists_but_invalid(self):
        import tempfile
        import shutil
        import os.path

        td = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, td)

        with open(os.path.join(td, "foo.po"), 'w') as f:
            f.write("this should not compile")

        self.assertEqual(
            0,
            compile.compile_mo_file('foo', td))

    def test_po_exists_cannot_write_mo(self):
        import tempfile
        import shutil
        import os
        import os.path

        td = tempfile.mkdtemp(suffix=".zopei18n_test_compile")
        self.addCleanup(shutil.rmtree, td)

        mofile = os.path.join(td, 'foo.mo')
        with open(mofile, 'w') as f:
            f.write("Touching")

        # Put it in the past, make it not writable
        os.utime(mofile, (1000, 1000))
        os.chmod(mofile, 0)

        with open(os.path.join(td, "foo.po"), 'w') as f:
            f.write("# A comment")

        self.assertIs(
            False,
            compile.compile_mo_file('foo', td))