michael@0: #!/usr/bin/env python michael@0: # michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: # You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import json michael@0: import mock michael@0: import os michael@0: import shutil michael@0: import sys michael@0: import tempfile michael@0: import unittest michael@0: import mozinfo michael@0: michael@0: class TestMozinfo(unittest.TestCase): michael@0: def setUp(self): michael@0: reload(mozinfo) michael@0: self.tempdir = os.path.abspath(tempfile.mkdtemp()) michael@0: michael@0: # When running from an objdir mozinfo will use a build generated json file michael@0: # instead of the ones created for testing. Prevent that from happening. michael@0: # See bug 896038 for details. michael@0: sys.modules['mozbuild'] = None michael@0: michael@0: def tearDown(self): michael@0: shutil.rmtree(self.tempdir) michael@0: del sys.modules['mozbuild'] michael@0: michael@0: def test_basic(self): michael@0: """Test that mozinfo has a few attributes.""" michael@0: self.assertNotEqual(mozinfo.os, None) michael@0: # should have isFoo == True where os == "foo" michael@0: self.assertTrue(getattr(mozinfo, "is" + mozinfo.os[0].upper() + mozinfo.os[1:])) michael@0: michael@0: def test_update(self): michael@0: """Test that mozinfo.update works.""" michael@0: mozinfo.update({"foo": 123}) michael@0: self.assertEqual(mozinfo.info["foo"], 123) michael@0: michael@0: def test_update_file(self): michael@0: """Test that mozinfo.update can load a JSON file.""" michael@0: j = os.path.join(self.tempdir, "mozinfo.json") michael@0: with open(j, "w") as f: michael@0: f.write(json.dumps({"foo": "xyz"})) michael@0: mozinfo.update(j) michael@0: self.assertEqual(mozinfo.info["foo"], "xyz") michael@0: michael@0: def test_update_file_invalid_json(self): michael@0: """Test that mozinfo.update handles invalid JSON correctly""" michael@0: j = os.path.join(self.tempdir,'test.json') michael@0: with open(j, 'w') as f: michael@0: f.write('invalid{"json":') michael@0: self.assertRaises(ValueError,mozinfo.update,[j]) michael@0: michael@0: def test_find_and_update_file(self): michael@0: """Test that mozinfo.find_and_update_from_json can michael@0: find mozinfo.json in a directory passed to it.""" michael@0: j = os.path.join(self.tempdir, "mozinfo.json") michael@0: with open(j, "w") as f: michael@0: f.write(json.dumps({"foo": "abcdefg"})) michael@0: self.assertEqual(mozinfo.find_and_update_from_json(self.tempdir), j) michael@0: self.assertEqual(mozinfo.info["foo"], "abcdefg") michael@0: michael@0: def test_find_and_update_file_invalid_json(self): michael@0: """Test that mozinfo.find_and_update_from_json can michael@0: handle invalid JSON""" michael@0: j = os.path.join(self.tempdir, "mozinfo.json") michael@0: with open(j, 'w') as f: michael@0: f.write('invalid{"json":') michael@0: self.assertRaises(ValueError, mozinfo.find_and_update_from_json, self.tempdir) michael@0: michael@0: michael@0: def test_find_and_update_file_mozbuild(self): michael@0: """Test that mozinfo.find_and_update_from_json can michael@0: find mozinfo.json using the mozbuild module.""" michael@0: j = os.path.join(self.tempdir, "mozinfo.json") michael@0: with open(j, "w") as f: michael@0: f.write(json.dumps({"foo": "123456"})) michael@0: m = mock.MagicMock() michael@0: # Mock the value of MozbuildObject.from_environment().topobjdir. michael@0: m.MozbuildObject.from_environment.return_value.topobjdir = self.tempdir michael@0: with mock.patch.dict(sys.modules, {"mozbuild": m, "mozbuild.base": m}): michael@0: self.assertEqual(mozinfo.find_and_update_from_json(), j) michael@0: self.assertEqual(mozinfo.info["foo"], "123456") michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()