testing/mozbase/mozversion/tests/test_binary.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/mozversion/tests/test_binary.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 +# You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 +
    1.10 +import os
    1.11 +import tempfile
    1.12 +import unittest
    1.13 +
    1.14 +import mozfile
    1.15 +from mozversion import get_version
    1.16 +
    1.17 +
    1.18 +class BinaryTest(unittest.TestCase):
    1.19 +    """test getting application version information from a binary path"""
    1.20 +
    1.21 +    application_ini = """[App]
    1.22 +Name = AppName
    1.23 +CodeName = AppCodeName
    1.24 +Version = AppVersion
    1.25 +BuildID = AppBuildID
    1.26 +SourceRepository = AppSourceRepo
    1.27 +SourceStamp = AppSourceStamp
    1.28 +"""
    1.29 +    platform_ini = """[Build]
    1.30 +BuildID = PlatformBuildID
    1.31 +SourceStamp = PlatformSourceStamp
    1.32 +SourceRepository = PlatformSourceRepo
    1.33 +"""
    1.34 +
    1.35 +    def setUp(self):
    1.36 +        self.cwd = os.getcwd()
    1.37 +        self.tempdir = tempfile.mkdtemp()
    1.38 +
    1.39 +        self.binary = os.path.join(self.tempdir, 'binary')
    1.40 +        with open(self.binary, 'w') as f:
    1.41 +            f.write('foobar')
    1.42 +
    1.43 +    def tearDown(self):
    1.44 +        os.chdir(self.cwd)
    1.45 +        mozfile.remove(self.tempdir)
    1.46 +
    1.47 +    def test_binary(self):
    1.48 +        with open(os.path.join(self.tempdir, 'application.ini'), 'w') as f:
    1.49 +            f.writelines(self.application_ini)
    1.50 +
    1.51 +        with open(os.path.join(self.tempdir, 'platform.ini'), 'w') as f:
    1.52 +            f.writelines(self.platform_ini)
    1.53 +
    1.54 +        self._check_version(get_version(self.binary))
    1.55 +
    1.56 +    def test_binary_in_current_path(self):
    1.57 +        with open(os.path.join(self.tempdir, 'application.ini'), 'w') as f:
    1.58 +            f.writelines(self.application_ini)
    1.59 +
    1.60 +        with open(os.path.join(self.tempdir, 'platform.ini'), 'w') as f:
    1.61 +            f.writelines(self.platform_ini)
    1.62 +        os.chdir(self.tempdir)
    1.63 +        self._check_version(get_version())
    1.64 +
    1.65 +    def test_invalid_binary_path(self):
    1.66 +        self.assertRaises(IOError, get_version,
    1.67 +                          os.path.join(self.tempdir, 'invalid'))
    1.68 +
    1.69 +    def test_missing_ini_files(self):
    1.70 +        v = get_version(self.binary)
    1.71 +        self.assertEqual(v, {})
    1.72 +
    1.73 +    def _check_version(self, version):
    1.74 +        self.assertEqual(version.get('application_name'), 'AppName')
    1.75 +        self.assertEqual(version.get('application_display_name'), 'AppCodeName')
    1.76 +        self.assertEqual(version.get('application_version'), 'AppVersion')
    1.77 +        self.assertEqual(version.get('application_buildid'), 'AppBuildID')
    1.78 +        self.assertEqual(
    1.79 +            version.get('application_repository'), 'AppSourceRepo')
    1.80 +        self.assertEqual(
    1.81 +            version.get('application_changeset'), 'AppSourceStamp')
    1.82 +        self.assertIsNone(version.get('platform_name'))
    1.83 +        self.assertIsNone(version.get('platform_version'))
    1.84 +        self.assertEqual(version.get('platform_buildid'), 'PlatformBuildID')
    1.85 +        self.assertEqual(
    1.86 +            version.get('platform_repository'), 'PlatformSourceRepo')
    1.87 +        self.assertEqual(
    1.88 +            version.get('platform_changeset'), 'PlatformSourceStamp')
    1.89 +        self.assertIsNone(version.get('invalid_key'))
    1.90 +
    1.91 +
    1.92 +if __name__ == '__main__':
    1.93 +    unittest.main()

mercurial