|
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 BinaryTest(unittest.TestCase): |
|
16 """test getting application version information from a binary path""" |
|
17 |
|
18 application_ini = """[App] |
|
19 Name = AppName |
|
20 CodeName = AppCodeName |
|
21 Version = AppVersion |
|
22 BuildID = AppBuildID |
|
23 SourceRepository = AppSourceRepo |
|
24 SourceStamp = AppSourceStamp |
|
25 """ |
|
26 platform_ini = """[Build] |
|
27 BuildID = PlatformBuildID |
|
28 SourceStamp = PlatformSourceStamp |
|
29 SourceRepository = PlatformSourceRepo |
|
30 """ |
|
31 |
|
32 def setUp(self): |
|
33 self.cwd = os.getcwd() |
|
34 self.tempdir = tempfile.mkdtemp() |
|
35 |
|
36 self.binary = os.path.join(self.tempdir, 'binary') |
|
37 with open(self.binary, 'w') as f: |
|
38 f.write('foobar') |
|
39 |
|
40 def tearDown(self): |
|
41 os.chdir(self.cwd) |
|
42 mozfile.remove(self.tempdir) |
|
43 |
|
44 def test_binary(self): |
|
45 with open(os.path.join(self.tempdir, 'application.ini'), 'w') as f: |
|
46 f.writelines(self.application_ini) |
|
47 |
|
48 with open(os.path.join(self.tempdir, 'platform.ini'), 'w') as f: |
|
49 f.writelines(self.platform_ini) |
|
50 |
|
51 self._check_version(get_version(self.binary)) |
|
52 |
|
53 def test_binary_in_current_path(self): |
|
54 with open(os.path.join(self.tempdir, 'application.ini'), 'w') as f: |
|
55 f.writelines(self.application_ini) |
|
56 |
|
57 with open(os.path.join(self.tempdir, 'platform.ini'), 'w') as f: |
|
58 f.writelines(self.platform_ini) |
|
59 os.chdir(self.tempdir) |
|
60 self._check_version(get_version()) |
|
61 |
|
62 def test_invalid_binary_path(self): |
|
63 self.assertRaises(IOError, get_version, |
|
64 os.path.join(self.tempdir, 'invalid')) |
|
65 |
|
66 def test_missing_ini_files(self): |
|
67 v = get_version(self.binary) |
|
68 self.assertEqual(v, {}) |
|
69 |
|
70 def _check_version(self, version): |
|
71 self.assertEqual(version.get('application_name'), 'AppName') |
|
72 self.assertEqual(version.get('application_display_name'), 'AppCodeName') |
|
73 self.assertEqual(version.get('application_version'), 'AppVersion') |
|
74 self.assertEqual(version.get('application_buildid'), 'AppBuildID') |
|
75 self.assertEqual( |
|
76 version.get('application_repository'), 'AppSourceRepo') |
|
77 self.assertEqual( |
|
78 version.get('application_changeset'), 'AppSourceStamp') |
|
79 self.assertIsNone(version.get('platform_name')) |
|
80 self.assertIsNone(version.get('platform_version')) |
|
81 self.assertEqual(version.get('platform_buildid'), 'PlatformBuildID') |
|
82 self.assertEqual( |
|
83 version.get('platform_repository'), 'PlatformSourceRepo') |
|
84 self.assertEqual( |
|
85 version.get('platform_changeset'), 'PlatformSourceStamp') |
|
86 self.assertIsNone(version.get('invalid_key')) |
|
87 |
|
88 |
|
89 if __name__ == '__main__': |
|
90 unittest.main() |