File: test_cmd_pot.py

package info (click to toggle)
sphinx-intl 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 340 kB
  • sloc: python: 1,162; makefile: 13
file content (170 lines) | stat: -rw-r--r-- 5,678 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
    test_cmd_pot
    ~~~~~~~~~~~~~

    Test pot related commands.

    :copyright: Copyright 2019 by Takayuki SHIMIZUKAWA.
    :license: BSD, see LICENSE for details.
"""

import os

from click.testing import CliRunner

from sphinx_intl import commands

runner = CliRunner()


def test_update_pot_notfound(temp):
    r1 = runner.invoke(commands.update, ['-d', 'locale'])
    assert r1.exit_code != 0
    assert 'Please specify pot directory with -p option,' in r1.output


def test_update_no_language(temp):
    r1 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale'])
    assert r1.exit_code != 0
    assert 'No languages are found. Please specify language with -l' in r1.output


def test_update_simple(temp):
    r1 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale', '-l', 'ja'])
    assert r1.exit_code == 0


def test_update_difference_detect(temp):
    r1 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale', '-l', 'ja'])
    assert r1.exit_code == 0
    assert r1.output.count('Create:') == 1
    assert r1.output.count('Update:') == 0
    assert r1.output.count('Not Changed:') == 0

    with open('_build/locale/README.pot', 'a') as f:
        f.write('\nmsgid "test1"\nmsgstr ""\n')

    r2 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale'])
    assert r2.exit_code == 0
    assert r2.output.count('Create:') == 0
    assert r2.output.count('Update:') == 1
    assert r2.output.count('Not Changed:') == 0

    with open('_build/locale/README.pot') as f:
        d = f.read()
        d = d.replace('test1', 'test2')
    with open('_build/locale/README.pot', 'w') as f:
        f.write(d)

    r3 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale'])
    assert r3.exit_code == 0
    assert r3.output.count('Create:') == 0
    assert r3.output.count('Update:') == 1
    assert r3.output.count('Not Changed:') == 0

    r4 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale'])
    assert r4.exit_code == 0
    assert r4.output.count('Create:') == 0
    assert r4.output.count('Update:') == 0
    assert r4.output.count('Not Changed:') == 1


def test_update_line_width(temp):
    with open('_build/locale/README.pot') as f:
        template = f.read()

    with open('_build/locale/README.pot', 'w') as f:
        f.write(template)
        f.write('\nmsgid "foorbar identifier1"\nmsgstr ""\n')

    po_dir = os.path.join('locale', 'ja', 'LC_MESSAGES')
    po_file = os.path.join(po_dir, 'README.po')

    r1 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale', '-l', 'ja'])
    assert r1.exit_code == 0

    with open(po_file) as f:
        contents = f.read()
        assert '"foorbar identifier1"\n' in contents

    # change the identifier to trigger an update and impose a lower line-width count
    with open('_build/locale/README.pot', 'w') as f:
        f.write(template)
        f.write('\nmsgid "foorbar identifier2"\nmsgstr ""\n')

    r2 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale', '-w', '1'])
    assert r2.exit_code == 0

    with open(po_file) as f:
        contents = f.read()
        assert '"foorbar"\n' in contents
        assert '"identifier2"\n' in contents


def test_update_no_obsolete(temp):
    with open('_build/locale/README.pot') as f:
        template = f.read()

    with open('_build/locale/README.pot', 'w') as f:
        f.write(template)
        f.write('\nmsgid "foorbar1"\nmsgstr ""\n')
        f.write('\nmsgid "foorbar2"\nmsgstr ""\n')

    po_dir = os.path.join('locale', 'ja', 'LC_MESSAGES')
    po_file = os.path.join(po_dir, 'README.po')

    r1 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale', '-l', 'ja'])
    assert r1.exit_code == 0

    with open(po_file) as f:
        contents = f.read()
        assert '\nmsgid "foorbar1"\n' in contents
        assert '\nmsgid "foorbar2"\n' in contents

    # remove the foorbar2 and verify we can see the obsolete entry
    with open('_build/locale/README.pot', 'w') as f:
        f.write(template)
        f.write('\nmsgid "foorbar1"\nmsgstr ""\n')

    r2 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale'])
    assert r2.exit_code == 0

    with open(po_file) as f:
        contents = f.read()
        assert '\n#~ msgid "foorbar2"\n' in contents

    # remove the foorbar1 and verify we can no longer see any obsolete entry
    with open('_build/locale/README.pot', 'w') as f:
        f.write(template)

    r3 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale', '--no-obsolete'])
    assert r3.exit_code == 0

    with open(po_file) as f:
        contents = f.read()
        assert 'msgid "foorbar1"' not in contents
        assert 'msgid "foorbar2"' not in contents


def test_stat(temp):
    r1 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale', '-l', 'ja'])
    assert r1.exit_code == 0

    r2 = runner.invoke(commands.stat, ['-d', 'locale'])
    assert r2.exit_code == 0
    assert 'README.po: 0 translated, 0 fuzzy, 1 untranslated.' in r2.output


def test_stat_with_multiple_languages(temp):
    r1 = runner.invoke(commands.update, ['-d', 'locale', '-p', '_build/locale', '-l', 'ja,de,it'])
    assert r1.exit_code == 0

    # r2 = runner.invoke(commands.stat, ['-d', 'locale', '-l', 'ja,de', '-l', 'it'])
    r2 = runner.invoke(commands.stat, ['-d', 'locale', '-l', 'ja'])
    assert r2.exit_code == 0
    assert 'README.po: 0 translated, 0 fuzzy, 1 untranslated.' in r2.output


def test_build(temp):
    result = runner.invoke(commands.build, ['--locale-dir', 'locale'])
    assert result.exit_code == 0