testing/mozbase/mozinfo/tests/test.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/mozinfo/tests/test.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     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 json
    1.11 +import mock
    1.12 +import os
    1.13 +import shutil
    1.14 +import sys
    1.15 +import tempfile
    1.16 +import unittest
    1.17 +import mozinfo
    1.18 +
    1.19 +class TestMozinfo(unittest.TestCase):
    1.20 +    def setUp(self):
    1.21 +        reload(mozinfo)
    1.22 +        self.tempdir = os.path.abspath(tempfile.mkdtemp())
    1.23 +
    1.24 +        # When running from an objdir mozinfo will use a build generated json file
    1.25 +        # instead of the ones created for testing. Prevent that from happening.
    1.26 +        # See bug 896038 for details.
    1.27 +        sys.modules['mozbuild'] = None
    1.28 +
    1.29 +    def tearDown(self):
    1.30 +        shutil.rmtree(self.tempdir)
    1.31 +        del sys.modules['mozbuild']
    1.32 +
    1.33 +    def test_basic(self):
    1.34 +        """Test that mozinfo has a few attributes."""
    1.35 +        self.assertNotEqual(mozinfo.os, None)
    1.36 +        # should have isFoo == True where os == "foo"
    1.37 +        self.assertTrue(getattr(mozinfo, "is" + mozinfo.os[0].upper() + mozinfo.os[1:]))
    1.38 +
    1.39 +    def test_update(self):
    1.40 +        """Test that mozinfo.update works."""
    1.41 +        mozinfo.update({"foo": 123})
    1.42 +        self.assertEqual(mozinfo.info["foo"], 123)
    1.43 +
    1.44 +    def test_update_file(self):
    1.45 +        """Test that mozinfo.update can load a JSON file."""
    1.46 +        j = os.path.join(self.tempdir, "mozinfo.json")
    1.47 +        with open(j, "w") as f:
    1.48 +            f.write(json.dumps({"foo": "xyz"}))
    1.49 +        mozinfo.update(j)
    1.50 +        self.assertEqual(mozinfo.info["foo"], "xyz")
    1.51 +
    1.52 +    def test_update_file_invalid_json(self):
    1.53 +        """Test that mozinfo.update handles invalid JSON correctly"""
    1.54 +        j = os.path.join(self.tempdir,'test.json')
    1.55 +        with open(j, 'w') as f:
    1.56 +            f.write('invalid{"json":')
    1.57 +        self.assertRaises(ValueError,mozinfo.update,[j])
    1.58 +
    1.59 +    def test_find_and_update_file(self):
    1.60 +        """Test that mozinfo.find_and_update_from_json can
    1.61 +        find mozinfo.json in a directory passed to it."""
    1.62 +        j = os.path.join(self.tempdir, "mozinfo.json")
    1.63 +        with open(j, "w") as f:
    1.64 +            f.write(json.dumps({"foo": "abcdefg"}))
    1.65 +        self.assertEqual(mozinfo.find_and_update_from_json(self.tempdir), j)
    1.66 +        self.assertEqual(mozinfo.info["foo"], "abcdefg")
    1.67 +
    1.68 +    def test_find_and_update_file_invalid_json(self):
    1.69 +        """Test that mozinfo.find_and_update_from_json can
    1.70 +        handle invalid JSON"""
    1.71 +        j = os.path.join(self.tempdir, "mozinfo.json")
    1.72 +        with open(j, 'w') as f:
    1.73 +            f.write('invalid{"json":')
    1.74 +        self.assertRaises(ValueError, mozinfo.find_and_update_from_json, self.tempdir)
    1.75 +
    1.76 +
    1.77 +    def test_find_and_update_file_mozbuild(self):
    1.78 +        """Test that mozinfo.find_and_update_from_json can
    1.79 +        find mozinfo.json using the mozbuild module."""
    1.80 +        j = os.path.join(self.tempdir, "mozinfo.json")
    1.81 +        with open(j, "w") as f:
    1.82 +            f.write(json.dumps({"foo": "123456"}))
    1.83 +        m = mock.MagicMock()
    1.84 +        # Mock the value of MozbuildObject.from_environment().topobjdir.
    1.85 +        m.MozbuildObject.from_environment.return_value.topobjdir = self.tempdir
    1.86 +        with mock.patch.dict(sys.modules, {"mozbuild": m, "mozbuild.base": m}):
    1.87 +            self.assertEqual(mozinfo.find_and_update_from_json(), j)
    1.88 +        self.assertEqual(mozinfo.info["foo"], "123456")
    1.89 +
    1.90 +if __name__ == '__main__':
    1.91 +    unittest.main()

mercurial