1import unittest
2import os
3import tempfile
4import shutil
5
6from compare_locales import mozpath
7from compare_locales.paths import EnumerateApp, ProjectFiles
8
9MAIL_INI = '''\
10[general]
11depth = ../..
12all = mail/locales/all-locales
13
14[compare]
15dirs = mail
16
17[includes]
18# non-central apps might want to use %(topsrcdir)s here, or other vars
19# RFE: that needs to be supported by compare-locales, too, though
20toolkit = mozilla/toolkit/locales/l10n.ini
21
22[include_toolkit]
23type = hg
24mozilla = mozilla-central
25repo = http://hg.mozilla.org/
26l10n.ini = toolkit/locales/l10n.ini
27'''
28
29
30MAIL_ALL_LOCALES = '''af
31de
32fr
33'''
34
35MAIL_FILTER_PY = '''
36def test(mod, path, entity = None):
37    if mod == 'toolkit' and path == 'ignored_path':
38        return 'ignore'
39    return 'error'
40'''
41
42TOOLKIT_INI = '''[general]
43depth = ../..
44
45[compare]
46dirs = toolkit
47'''
48
49
50class TestApp(unittest.TestCase):
51    def setUp(self):
52        self.stage = tempfile.mkdtemp()
53        mail = mozpath.join(self.stage, 'comm', 'mail', 'locales')
54        toolkit = mozpath.join(
55            self.stage, 'comm', 'mozilla', 'toolkit', 'locales')
56        l10n = mozpath.join(self.stage, 'l10n-central', 'de', 'toolkit')
57        os.makedirs(mozpath.join(mail, 'en-US'))
58        os.makedirs(mozpath.join(toolkit, 'en-US'))
59        os.makedirs(l10n)
60        with open(mozpath.join(mail, 'l10n.ini'), 'w') as f:
61            f.write(MAIL_INI)
62        with open(mozpath.join(mail, 'all-locales'), 'w') as f:
63            f.write(MAIL_ALL_LOCALES)
64        with open(mozpath.join(mail, 'filter.py'), 'w') as f:
65            f.write(MAIL_FILTER_PY)
66        with open(mozpath.join(toolkit, 'l10n.ini'), 'w') as f:
67            f.write(TOOLKIT_INI)
68        with open(mozpath.join(mail, 'en-US', 'mail.ftl'), 'w') as f:
69            f.write('')
70        with open(mozpath.join(toolkit, 'en-US', 'platform.ftl'), 'w') as f:
71            f.write('')
72        with open(mozpath.join(l10n, 'localized.ftl'), 'w') as f:
73            f.write('')
74
75    def tearDown(self):
76        shutil.rmtree(self.stage)
77
78    def test_app(self):
79        'Test parsing a App'
80        app = EnumerateApp(
81            mozpath.join(self.stage, 'comm', 'mail', 'locales', 'l10n.ini'),
82            mozpath.join(self.stage, 'l10n-central'))
83        self.assertListEqual(app.locales, ['af', 'de', 'fr'])
84        self.assertEqual(len(app.config.children), 1)
85        projectconfig = app.asConfig()
86        self.assertListEqual(projectconfig.locales, ['af', 'de', 'fr'])
87        files = ProjectFiles('de', [projectconfig])
88        files = list(files)
89        self.assertEqual(len(files), 3)
90
91        l10nfile, reffile, mergefile, test = files[0]
92        self.assertListEqual(mozpath.split(l10nfile)[-3:],
93                             ['de', 'mail', 'mail.ftl'])
94        self.assertListEqual(mozpath.split(reffile)[-4:],
95                             ['mail', 'locales', 'en-US', 'mail.ftl'])
96        self.assertIsNone(mergefile)
97        self.assertSetEqual(test, set())
98
99        l10nfile, reffile, mergefile, test = files[1]
100        self.assertListEqual(mozpath.split(l10nfile)[-3:],
101                             ['de', 'toolkit', 'localized.ftl'])
102        self.assertListEqual(
103            mozpath.split(reffile)[-6:],
104            ['comm', 'mozilla', 'toolkit',
105             'locales', 'en-US', 'localized.ftl'])
106        self.assertIsNone(mergefile)
107        self.assertSetEqual(test, set())
108
109        l10nfile, reffile, mergefile, test = files[2]
110        self.assertListEqual(mozpath.split(l10nfile)[-3:],
111                             ['de', 'toolkit', 'platform.ftl'])
112        self.assertListEqual(
113            mozpath.split(reffile)[-6:],
114            ['comm', 'mozilla', 'toolkit', 'locales', 'en-US', 'platform.ftl'])
115        self.assertIsNone(mergefile)
116        self.assertSetEqual(test, set())
117