summaryrefslogtreecommitdiff
path: root/tests/test_build_gettext.py
blob: ba2440fd441f4bed9fbee7daaedbd7f0f9fcd173 (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
# -*- coding: utf-8 -*-
"""
    test_build_gettext
    ~~~~~~~~~~~~~~~~~~

    Test the build process with gettext builder with the test root.

    :copyright: Copyright 2010 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import gettext
import os
from subprocess import Popen, PIPE

from util import *


def teardown_module():
    (test_root / '_build').rmtree(True)


@with_app(buildername='gettext')
def test_build(app):
    app.builder.build(['extapi', 'subdir/includes'])
    # documents end up in a message catalog
    assert (app.outdir / 'extapi.pot').isfile()
    # ..and are grouped into sections
    assert (app.outdir / 'subdir.pot').isfile()

@with_app(buildername='gettext')
def test_gettext(app):
    app.builder.build(['markup'])

    (app.outdir / 'en' / 'LC_MESSAGES').makedirs()
    cwd = os.getcwd()
    os.chdir(app.outdir)
    try:
        try:
            p = Popen(['msginit', '--no-translator', '-i', 'markup.pot',
                       '--locale', 'en_US'],
                        stdout=PIPE, stderr=PIPE)
        except OSError:
            return  # most likely msginit was not found
        else:
            stdout, stderr = p.communicate()
            if p.returncode != 0:
                print stdout
                print stderr
                assert False, 'msginit exited with return code %s' % \
                        p.returncode
        assert (app.outdir / 'en_US.po').isfile(), 'msginit failed'
        try:
            p = Popen(['msgfmt', 'en_US.po', '-o',
                os.path.join('en', 'LC_MESSAGES', 'test_root.mo')],
                stdout=PIPE, stderr=PIPE)
        except OSError:
            return  # most likely msgfmt was not found
        else:
            stdout, stderr = p.communicate()
            if p.returncode != 0:
                print stdout
                print stderr
                assert False, 'msgfmt exited with return code %s' % \
                        p.returncode
        assert (app.outdir / 'en' / 'LC_MESSAGES' / 'test_root.mo').isfile(), \
                'msgfmt failed'
    finally:
        os.chdir(cwd)

    _ = gettext.translation('test_root', app.outdir, languages=['en']).gettext
    assert _("Testing various markup") == u"Testing various markup"

@with_app(buildername='gettext')
def test_all(app):
    app.builder.build_all()


def setup_patch():
    (test_root / 'xx' / 'LC_MESSAGES').makedirs()
    try:
        p = Popen(['msgfmt', test_root / 'bom.po', '-o',
            test_root / 'xx' / 'LC_MESSAGES' / 'bom.mo'],
            stdout=PIPE, stderr=PIPE)
    except OSError:
        return  # most likely msgfmt was not found
    else:
        stdout, stderr = p.communicate()
        if p.returncode != 0:
            print stdout
            print stderr
            assert False, 'msgfmt exited with return code %s' % p.returncode
    assert (test_root / 'xx' / 'LC_MESSAGES' / 'bom.mo').isfile(), \
            'msgfmt failed'

def teardown_patch():
    (test_root / 'xx').rmtree()

@with_app(buildername='text',
          confoverrides={'language': 'xx', 'locale_dirs': ['.']})
def test_patch(app):
    app.builder.build(['bom'])
    result = (app.outdir / 'bom.txt').text(encoding='utf-8')
    expect = (u"\nDatei mit UTF-8"
              u"\n***************\n" # underline matches new translation
              u"\nThis file has umlauts: äöü.\n")
    assert result == expect

test_patch.setup = setup_patch
test_patch.teardown = teardown_patch