File: test_example.py

package info (click to toggle)
python-setuptools-gettext 0.1.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 152 kB
  • sloc: python: 462; makefile: 4
file content (87 lines) | stat: -rw-r--r-- 2,270 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
import os
import shutil
from tempfile import TemporaryDirectory
from unittest import SkipTest

from setuptools import Distribution

from setuptools_gettext import (
    build_mo,
    install_mo,
    load_pyproject_config,
    update_pot,
)


def test_example_build():
    with TemporaryDirectory() as td:
        shutil.copytree("example", td + "/example")

        old_cwd = os.getcwd()

        dist = Distribution()

        load_pyproject_config(dist, {})

        os.chdir(td + "/example")
        try:
            cmd = build_mo(dist)
            cmd.initialize_options()
            cmd.finalize_options()
            cmd.run()
        finally:
            os.chdir(old_cwd)


def test_example_install():
    with TemporaryDirectory() as td, TemporaryDirectory() as root:
        shutil.copytree("example", td + "/example")

        dist = Distribution()

        load_pyproject_config(dist, {})

        old_cwd = os.getcwd()

        os.chdir(td + "/example")
        try:
            cmd = install_mo(dist)
            cmd.initialize_options()
            cmd.root = root  # type: ignore
            cmd.finalize_options()
            cmd.run()
        finally:
            os.chdir(old_cwd)


def test_update_pot():
    # Skip this test if xgettext is not available
    if shutil.which("xgettext") is None:
        raise SkipTest("xgettext not available")
    with TemporaryDirectory() as td:
        shutil.copytree("example", td + "/example")
        p = os.path.join(td, "example", "hallowereld", "example.py")
        with open(p, "w") as f:
            f.write('# Use the "_" shorthand for gettext\n')
            f.write("from gettext import gettext as _\n")
            f.write('print(_("Hello Example"))')

        dist = Distribution(
            attrs={
                "name": "hallowereld",
            }
        )

        load_pyproject_config(dist, {})

        old_cwd = os.getcwd()
        os.chdir(os.path.join(td, "example"))
        try:
            cmd = update_pot(dist)
            cmd.initialize_options()
            cmd.finalize_options()
            cmd.run()
        finally:
            os.chdir(old_cwd)
        with open(os.path.join(td, "example", "po", "hallowereld.pot")) as f:
            assert "Hello Example" in f.read()