testing/mozbase/mozversion/tests/test_sources.py

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:46dd3fb4473e
1 #!/usr/bin/env python
2
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 # You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 import os
8 import tempfile
9 import unittest
10
11 import mozfile
12 from mozversion import get_version
13
14
15 class SourcesTest(unittest.TestCase):
16 """test getting version information from a sources xml"""
17
18 application_ini = """[App]\nName = B2G\n"""
19 sources_xml = """<?xml version="1.0" ?><manifest>
20 <project path="build" revision="build_revision" />
21 <project path="gaia" revision="gaia_revision" />
22 <project path="gecko" revision="gecko_revision" />
23 </manifest>
24 """
25
26 def setUp(self):
27 self.cwd = os.getcwd()
28 self.tempdir = tempfile.mkdtemp()
29
30 self.binary = os.path.join(self.tempdir, 'binary')
31 with open(self.binary, 'w') as f:
32 f.write('foobar')
33
34 def tearDown(self):
35 os.chdir(self.cwd)
36 mozfile.remove(self.tempdir)
37
38 def test_sources(self):
39 with open(os.path.join(self.tempdir, 'application.ini'), 'w') as f:
40 f.writelines(self.application_ini)
41
42 sources = os.path.join(self.tempdir, 'sources.xml')
43 with open(sources, 'w') as f:
44 f.writelines(self.sources_xml)
45
46 os.chdir(self.tempdir)
47 self._check_version(get_version(sources=sources))
48
49 def test_sources_in_current_directory(self):
50 with open(os.path.join(self.tempdir, 'application.ini'), 'w') as f:
51 f.writelines(self.application_ini)
52
53 with open(os.path.join(self.tempdir, 'sources.xml'), 'w') as f:
54 f.writelines(self.sources_xml)
55
56 os.chdir(self.tempdir)
57 self._check_version(get_version())
58
59 def test_invalid_sources_path(self):
60 v = get_version(self.binary, os.path.join(self.tempdir, 'invalid'))
61 self.assertEqual(v, {})
62
63 def test_missing_sources_file(self):
64 v = get_version(self.binary)
65 self.assertEqual(v, {})
66
67 def _check_version(self, version):
68 self.assertEqual(version.get('build_changeset'), 'build_revision')
69 self.assertEqual(version.get('gaia_changeset'), 'gaia_revision')
70 self.assertEqual(version.get('gecko_changeset'), 'gecko_revision')
71 self.assertIsNone(version.get('invalid_key'))
72
73
74 if __name__ == '__main__':
75 unittest.main()

mercurial