summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozversion/tests/test_b2g.py
blob: 09e0eb09e95497d71fd332d5f0c192f219bbf6b4 (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
#!/usr/bin/env python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import tempfile
import unittest
import zipfile

import mozfile
from mozversion import get_version, errors


class SourcesTest(unittest.TestCase):
    """test getting version information from a sources xml"""

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()

        self.binary = os.path.join(self.tempdir, 'binary')
        with open(self.binary, 'w') as f:
            f.write('foobar')

        with open(os.path.join(self.tempdir, 'application.ini'), 'w') as f:
            f.writelines("""[App]\nName = B2G\n""")

        with open(os.path.join(self.tempdir, 'platform.ini'), 'w') as f:
            f.write('[Build]\nBuildID = PlatformBuildID\n')

    def tearDown(self):
        mozfile.remove(self.tempdir)

    def _create_zip(self, revision=None, date=None):
        zip_path = os.path.join(
            self.tempdir, 'gaia', 'profile', 'webapps',
            'settings.gaiamobile.org', 'application.zip')
        os.makedirs(os.path.dirname(zip_path))
        app_zip = zipfile.ZipFile(zip_path, 'w')
        if revision or date:
            app_zip.writestr('resources/gaia_commit.txt',
                             '%s\n%s' % (revision, date))
        app_zip.close()

    def test_gaia_commit(self):
        revision, date = ('a' * 40, 'date')
        self._create_zip(revision, date)
        v = get_version(self.binary)
        self.assertEqual(v.get('gaia_changeset'), revision)
        self.assertEqual(v.get('gaia_date'), date)

    def test_invalid_gaia_commit(self):
        revision, date = ('a' * 41, 'date')
        self._create_zip(revision, date)
        v = get_version(self.binary)
        self.assertIsNone(v.get('gaia_changeset'))
        self.assertEqual(v.get('gaia_date'), date)

    def test_missing_zip_file(self):
        v = get_version(self.binary)
        self.assertIsNone(v.get('gaia_changeset'))
        self.assertIsNone(v.get('gaia_date'))

    def test_missing_gaia_commit(self):
        self._create_zip()
        v = get_version(self.binary)
        self.assertIsNone(v.get('gaia_changeset'))
        self.assertIsNone(v.get('gaia_date'))

    def test_b2g_fallback_when_no_binary(self):
        self.assertRaises(errors.RemoteAppNotFoundError, get_version)

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