summaryrefslogtreecommitdiff
path: root/tests/run_tests.py
blob: c14a26d067a71ab0361e2657643ce0af4bd08a04 (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
import os
import shutil
from subprocess import Popen, PIPE, call
import unittest

TEST_DIR    = os.path.dirname(os.path.abspath(__file__))
ITSTOOL_DIR = os.path.dirname(TEST_DIR)

class ItstoolTests(unittest.TestCase):
    def tearDown(self):
        for test_file in ('test.pot', 'test.mo', 'test.xml'):
            path = os.path.join(TEST_DIR, test_file)
            if os.path.exists(path):
                os.remove(path)

    def run_command(self, cmd):
        """ Helper method to run a shell command """
        pipe = Popen(cmd, shell=True, env=os.environ, stdin=None, stdout=PIPE, stderr=PIPE)
        (output, errout) = pipe.communicate()
        status = pipe.returncode
        self.assertEqual(status, 0, errout or output)
        return (status, output, errout)

    def assertFilesEqual(self, f1, f2):
        options = ""
        if f1.endswith(".po") or f1.endswith(".pot"):
            options = "--ignore-matching-lines=^\\\"POT-Creation-Date"
        result = self.run_command("diff -u %s %s %s" % (options, f1, f2))
        return self.assertEqual(result[1], "", result[1])

    def _test_translation_process(self, start_file):
        start_file_base = os.path.splitext(start_file)[0]
        result = self.run_command("cd %(dir)s && python itstool_test -o %(out)s %(in)s" % {
            'dir' : ITSTOOL_DIR,
            'out' : os.path.join('tests', "test.pot"),
            'in'  : os.path.join('tests', start_file),
        })
        # If a reference pot file is present, test the output with this file
        reference_pot = start_file_base + ".pot"
        if os.path.exists(reference_pot):
            self.assertFilesEqual(os.path.join(TEST_DIR, "test.pot"), os.path.join(TEST_DIR, reference_pot))

        # Compile mo and merge
        self.run_command("cd %(dir)s && msgfmt -o test.mo %(base)s.ll.po" % {'dir': TEST_DIR, 'base': start_file_base}) 
        res = self.run_command("cd %(dir)s && python itstool_test -m %(mo)s -o %(res)s %(src)s" % {
            'dir': ITSTOOL_DIR,
            'mo' : os.path.join(TEST_DIR, "test.mo"),
            'res': os.path.join(TEST_DIR, "test.xml"),
            'src': os.path.join(TEST_DIR, start_file),
        })
        self.assertFilesEqual(os.path.join(TEST_DIR, "test.xml"), os.path.join(TEST_DIR, "%s.ll.xml" % start_file_base))


    def test_unicode_markup(self):
        self._test_translation_process('Translate1.xml')

    def test_external_rules(self):
        self._test_translation_process('Translate2.xml')

    def test_embedded_its(self):
        self._test_translation_process('Translate3.xml')

    def test_notranslate_to_simpletag(self):
        self._test_translation_process('Translate4.xml')

    def test_selector_translate_rule(self):
        self._test_translation_process('Translate5.xml')

    def test_root_selector(self):
        self._test_translation_process('Translate6.xml')

    def test_last_rule_win(self):
        self._test_translation_process('Translate7.xml')

    # FIXME: currently deactivated until attributes are translatable
    def xx_test_attribute_selectable(self):
        self._test_translation_process('TranslateGlobal.xml')

class ITSTestRunner(unittest.TextTestRunner):
    def run(self, test):
        # Global setup
        test_binary_path = os.path.join(ITSTOOL_DIR, "itstool_test")
        shutil.copy(os.path.join(ITSTOOL_DIR, "itstool.in"), test_binary_path)
        data_dir = os.path.join(ITSTOOL_DIR, "its")
        call("sed -i -e 's/@DATADIR@/%s/' %s" % (data_dir.replace('/', '\/'), test_binary_path), shell=True)

        result = super(ITSTestRunner, self).run(test)

        os.remove(test_binary_path)
        return result


if __name__ == '__main__':
    unittest.main(testRunner=ITSTestRunner)