Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/.
7 import json
8 import mock
9 import os
10 import shutil
11 import sys
12 import tempfile
13 import unittest
14 import mozinfo
16 class TestMozinfo(unittest.TestCase):
17 def setUp(self):
18 reload(mozinfo)
19 self.tempdir = os.path.abspath(tempfile.mkdtemp())
21 # When running from an objdir mozinfo will use a build generated json file
22 # instead of the ones created for testing. Prevent that from happening.
23 # See bug 896038 for details.
24 sys.modules['mozbuild'] = None
26 def tearDown(self):
27 shutil.rmtree(self.tempdir)
28 del sys.modules['mozbuild']
30 def test_basic(self):
31 """Test that mozinfo has a few attributes."""
32 self.assertNotEqual(mozinfo.os, None)
33 # should have isFoo == True where os == "foo"
34 self.assertTrue(getattr(mozinfo, "is" + mozinfo.os[0].upper() + mozinfo.os[1:]))
36 def test_update(self):
37 """Test that mozinfo.update works."""
38 mozinfo.update({"foo": 123})
39 self.assertEqual(mozinfo.info["foo"], 123)
41 def test_update_file(self):
42 """Test that mozinfo.update can load a JSON file."""
43 j = os.path.join(self.tempdir, "mozinfo.json")
44 with open(j, "w") as f:
45 f.write(json.dumps({"foo": "xyz"}))
46 mozinfo.update(j)
47 self.assertEqual(mozinfo.info["foo"], "xyz")
49 def test_update_file_invalid_json(self):
50 """Test that mozinfo.update handles invalid JSON correctly"""
51 j = os.path.join(self.tempdir,'test.json')
52 with open(j, 'w') as f:
53 f.write('invalid{"json":')
54 self.assertRaises(ValueError,mozinfo.update,[j])
56 def test_find_and_update_file(self):
57 """Test that mozinfo.find_and_update_from_json can
58 find mozinfo.json in a directory passed to it."""
59 j = os.path.join(self.tempdir, "mozinfo.json")
60 with open(j, "w") as f:
61 f.write(json.dumps({"foo": "abcdefg"}))
62 self.assertEqual(mozinfo.find_and_update_from_json(self.tempdir), j)
63 self.assertEqual(mozinfo.info["foo"], "abcdefg")
65 def test_find_and_update_file_invalid_json(self):
66 """Test that mozinfo.find_and_update_from_json can
67 handle invalid JSON"""
68 j = os.path.join(self.tempdir, "mozinfo.json")
69 with open(j, 'w') as f:
70 f.write('invalid{"json":')
71 self.assertRaises(ValueError, mozinfo.find_and_update_from_json, self.tempdir)
74 def test_find_and_update_file_mozbuild(self):
75 """Test that mozinfo.find_and_update_from_json can
76 find mozinfo.json using the mozbuild module."""
77 j = os.path.join(self.tempdir, "mozinfo.json")
78 with open(j, "w") as f:
79 f.write(json.dumps({"foo": "123456"}))
80 m = mock.MagicMock()
81 # Mock the value of MozbuildObject.from_environment().topobjdir.
82 m.MozbuildObject.from_environment.return_value.topobjdir = self.tempdir
83 with mock.patch.dict(sys.modules, {"mozbuild": m, "mozbuild.base": m}):
84 self.assertEqual(mozinfo.find_and_update_from_json(), j)
85 self.assertEqual(mozinfo.info["foo"], "123456")
87 if __name__ == '__main__':
88 unittest.main()