File: test_transifex.py

package info (click to toggle)
sphinx-intl 0.9.11-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 208 kB
  • sloc: python: 986; makefile: 2
file content (133 lines) | stat: -rw-r--r-- 3,965 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
# -*- coding: utf-8 -*-
"""
    test_transifex
    ~~~~~~~~~~~~~~

    Test functions that implements transifex related features.

    :copyright: Copyright 2015 by Takayuki SHIMIZUKAWA.
    :license: BSD, see LICENSE for details.
"""
import re
from textwrap import dedent

import pytest

from sphinx_intl import transifex


def test_create_transifexrc(home_in_temp):
    transifex.create_transifexrc('spam-id', 'egg-pw')


def test_create_txconfig(home_in_temp, temp):
    transifex.create_txconfig()


def test_update_txconfig_resources(home_in_temp, temp):
    transifex.create_txconfig()
    transifex.update_txconfig_resources('ham-project', 'locale', '_build/locale')


def test_update_txconfig_resources_with_config(home_in_temp, temp):
    tx_dir = temp / '.tx'
    tx_dir.makedirs()
    (tx_dir / 'config').write_text(dedent("""\
    [main]
    host = https://www.transifex.com

    [ham-project.domain1]
    """))

    transifex.update_txconfig_resources('ham-project', 'locale', '_build/locale')

    data = (tx_dir / 'config').text().replace('\\', '/')
    assert re.search(r'\[ham-project\.README\]', data)
    assert re.search(r'source_file = _build/locale/README.pot', data)


def test_update_txconfig_resources_with_another_pot_dir(home_in_temp, temp):
    tx_dir = temp / '.tx'
    tx_dir.makedirs()
    (tx_dir / 'config').write_text(dedent("""\
    [main]
    host = https://www.transifex.com

    [ham-project.domain1]
    """))

    (temp / '_build' / 'locale').copytree(temp / 'locale' / 'pot')

    transifex.update_txconfig_resources('ham-project', 'locale', 'locale/pot')

    data = (tx_dir / 'config').text()
    assert re.search(r'\[ham-project\.README\]', data)


def test_update_txconfig_resources_with_project_name_including_dots(home_in_temp, temp):
    tx_dir = temp / '.tx'
    tx_dir.makedirs()
    (tx_dir / 'config').write_text(dedent("""\
    [main]
    host = https://www.transifex.com
    """))

    (temp / '_build' / 'locale').copytree(temp / 'locale' / 'pot')

    transifex.update_txconfig_resources('ham-project.com', 'locale', '_build/locale')

    data = (tx_dir / 'config').text()
    assert re.search(r'\[ham-projectcom\.README\]', data)


def test_update_txconfig_resources_with_project_name_including_spaces(home_in_temp, temp):
    tx_dir = temp / '.tx'
    tx_dir.makedirs()
    (tx_dir / 'config').write_text(dedent("""\
    [main]
    host = https://www.transifex.com
    """))

    (temp / '_build' / 'locale').copytree(temp / 'locale' / 'pot')

    transifex.update_txconfig_resources('ham project com', 'locale', '_build/locale')

    data = (tx_dir / 'config').text()
    assert re.search(r'\[ham-project-com\.README\]', data)


def test_update_txconfig_resources_with_potfile_including_symbols(home_in_temp, temp):
    tx_dir = temp / '.tx'
    tx_dir.makedirs()
    (tx_dir / 'config').write_text(dedent("""\
    [main]
    host = https://www.transifex.com
    """))

    (temp / '_build' / 'locale').copytree(temp / 'locale' / 'pot')

    # copy README.pot to 'example document.pot'
    readme = (temp / '_build' / 'locale' / 'README.pot').text()
    (temp / '_build' / 'locale' / 'example document.pot').write_text(readme)

    # copy README.pot to 'test.document.pot'
    (temp / '_build' / 'locale' / 'test.document.pot').write_text(readme)

    transifex.update_txconfig_resources('ham project com', 'locale', '_build/locale')

    data = (tx_dir / 'config').text()
    assert re.search(r'\[ham-project-com\.example_document\]', data)
    assert re.search(r'\[ham-project-com\.test_document\]', data)


@pytest.mark.parametrize("input,expected", [
    ('spam/ham', 'spam--ham'),
    ('spam\\ham', 'spam--ham'),
    ('ham egg.pot', 'ham_egg_pot'),
    ('spam-ham/egg.pot', 'spam-ham--egg_pot'),
    ('glossary', 'glossary_'),
    ('glossary_', 'glossary_'),
])
def test_normalize_resource_name(input, expected):
    _callSUT = transifex.normalize_resource_name
    assert _callSUT(input) == expected