testing/mozbase/mozinfo/tests/test.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/usr/bin/env python
michael@0 2 #
michael@0 3 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 # You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 6
michael@0 7 import json
michael@0 8 import mock
michael@0 9 import os
michael@0 10 import shutil
michael@0 11 import sys
michael@0 12 import tempfile
michael@0 13 import unittest
michael@0 14 import mozinfo
michael@0 15
michael@0 16 class TestMozinfo(unittest.TestCase):
michael@0 17 def setUp(self):
michael@0 18 reload(mozinfo)
michael@0 19 self.tempdir = os.path.abspath(tempfile.mkdtemp())
michael@0 20
michael@0 21 # When running from an objdir mozinfo will use a build generated json file
michael@0 22 # instead of the ones created for testing. Prevent that from happening.
michael@0 23 # See bug 896038 for details.
michael@0 24 sys.modules['mozbuild'] = None
michael@0 25
michael@0 26 def tearDown(self):
michael@0 27 shutil.rmtree(self.tempdir)
michael@0 28 del sys.modules['mozbuild']
michael@0 29
michael@0 30 def test_basic(self):
michael@0 31 """Test that mozinfo has a few attributes."""
michael@0 32 self.assertNotEqual(mozinfo.os, None)
michael@0 33 # should have isFoo == True where os == "foo"
michael@0 34 self.assertTrue(getattr(mozinfo, "is" + mozinfo.os[0].upper() + mozinfo.os[1:]))
michael@0 35
michael@0 36 def test_update(self):
michael@0 37 """Test that mozinfo.update works."""
michael@0 38 mozinfo.update({"foo": 123})
michael@0 39 self.assertEqual(mozinfo.info["foo"], 123)
michael@0 40
michael@0 41 def test_update_file(self):
michael@0 42 """Test that mozinfo.update can load a JSON file."""
michael@0 43 j = os.path.join(self.tempdir, "mozinfo.json")
michael@0 44 with open(j, "w") as f:
michael@0 45 f.write(json.dumps({"foo": "xyz"}))
michael@0 46 mozinfo.update(j)
michael@0 47 self.assertEqual(mozinfo.info["foo"], "xyz")
michael@0 48
michael@0 49 def test_update_file_invalid_json(self):
michael@0 50 """Test that mozinfo.update handles invalid JSON correctly"""
michael@0 51 j = os.path.join(self.tempdir,'test.json')
michael@0 52 with open(j, 'w') as f:
michael@0 53 f.write('invalid{"json":')
michael@0 54 self.assertRaises(ValueError,mozinfo.update,[j])
michael@0 55
michael@0 56 def test_find_and_update_file(self):
michael@0 57 """Test that mozinfo.find_and_update_from_json can
michael@0 58 find mozinfo.json in a directory passed to it."""
michael@0 59 j = os.path.join(self.tempdir, "mozinfo.json")
michael@0 60 with open(j, "w") as f:
michael@0 61 f.write(json.dumps({"foo": "abcdefg"}))
michael@0 62 self.assertEqual(mozinfo.find_and_update_from_json(self.tempdir), j)
michael@0 63 self.assertEqual(mozinfo.info["foo"], "abcdefg")
michael@0 64
michael@0 65 def test_find_and_update_file_invalid_json(self):
michael@0 66 """Test that mozinfo.find_and_update_from_json can
michael@0 67 handle invalid JSON"""
michael@0 68 j = os.path.join(self.tempdir, "mozinfo.json")
michael@0 69 with open(j, 'w') as f:
michael@0 70 f.write('invalid{"json":')
michael@0 71 self.assertRaises(ValueError, mozinfo.find_and_update_from_json, self.tempdir)
michael@0 72
michael@0 73
michael@0 74 def test_find_and_update_file_mozbuild(self):
michael@0 75 """Test that mozinfo.find_and_update_from_json can
michael@0 76 find mozinfo.json using the mozbuild module."""
michael@0 77 j = os.path.join(self.tempdir, "mozinfo.json")
michael@0 78 with open(j, "w") as f:
michael@0 79 f.write(json.dumps({"foo": "123456"}))
michael@0 80 m = mock.MagicMock()
michael@0 81 # Mock the value of MozbuildObject.from_environment().topobjdir.
michael@0 82 m.MozbuildObject.from_environment.return_value.topobjdir = self.tempdir
michael@0 83 with mock.patch.dict(sys.modules, {"mozbuild": m, "mozbuild.base": m}):
michael@0 84 self.assertEqual(mozinfo.find_and_update_from_json(), j)
michael@0 85 self.assertEqual(mozinfo.info["foo"], "123456")
michael@0 86
michael@0 87 if __name__ == '__main__':
michael@0 88 unittest.main()

mercurial