Bug 1305743 - Make failure to find mozinfo.json a fatal error r=gbrown,ahal a=reland

- moved placement of the raise_exception computation to be after the initial objdir path computation.
- original implementation was missing detection for cases where user may pass in a False flag to explicitly suppress errors.
- added unit tests to check for scenarios where raise_exception flag is passed in as part of positional argument.
- changed argument to include a **kwargs argument for caller to modify default exception behavior.
- default behavior is to raise exceptions if mozinfo.json cannot be found.
- disabled TreeMetadataEmitter from calling mozinfo.find_and_update_from_json and setting the self.info variable since it was not referenced anywhere else after the initial setup.

Depends on D6859

Differential Revision: https://phabricator.services.mozilla.com/D6860

--HG--
extra : source : 6d25c44c39ea33ebe25829d945b00963dad39ea4
extra : amend_source : 5c7a556b122f887113bae16356ffcf4bd064b620
This commit is contained in:
Edwin Gao 2018-09-26 20:07:25 +00:00
Родитель 5a8eb92385
Коммит 415a950090
3 изменённых файлов: 74 добавлений и 24 удалений

Просмотреть файл

@ -110,18 +110,6 @@ class TreeMetadataEmitter(LoggingMixin):
self.populate_logger()
self.config = config
mozinfo.find_and_update_from_json(config.topobjdir)
# Python 2.6 doesn't allow unicode keys to be used for keyword
# arguments. This gross hack works around the problem until we
# rid ourselves of 2.6.
self.info = {}
for k, v in mozinfo.info.items():
if isinstance(k, unicode):
k = k.encode('ascii')
self.info[k] = v
self._libs = OrderedDefaultDict(list)
self._binaries = OrderedDict()
self._compile_dirs = set()

Просмотреть файл

@ -183,13 +183,14 @@ def sanitize(info):
def update(new_info):
"""
Update the info.
"""Adds information from json file to the global symbol table.
Given the parameter new_info, this method will look for the file.
If found, the file is read into a buffer and assumed to be json.
:param new_info: Either a dict containing the new info or a path/url
to a json file containing the new info.
"""
PY3 = sys.version_info[0] == 3
if PY3:
string_types = str,
@ -215,16 +216,29 @@ def update(new_info):
globals()['isUnix'] = True
def find_and_update_from_json(*dirs):
"""
Find a mozinfo.json file, load it, and update the info with the
contents.
def find_and_update_from_json(*dirs, **kwargs):
"""Find a mozinfo.json file, load it, and update global symbol table.
:param dirs: Directories in which to look for the file. They will be
searched after first looking in the root of the objdir
if the current script is being run from a Mozilla objdir.
This method will first check the relevant objdir directory for the
necessary mozinfo.json file, if the current script is being run from a
Mozilla objdir.
Returns the full path to mozinfo.json if it was found, or None otherwise.
If the objdir directory did not supply the necessary data, this method
will then look for the required mozinfo.json file from the provided
tuple of directories.
If file is found, the global symbols table is updated via a helper method.
If no valid files are found, an exception is raised.
:param tuple dirs: Directories in which to look for the file.
:param dict kwargs: optional values:
raise_exception: if this value is provided, the default
behavior of raising an exception is
overridden.
:returns: EnvironmentError: default behavior.
None: if exception raising is suppressed.
json_path: string representation of path.
"""
# First, see if we're in an objdir
try:
@ -240,6 +254,8 @@ def find_and_update_from_json(*dirs):
except (BuildEnvironmentNotFoundException, MozconfigFindException):
pass
raise_exception = kwargs.get('raise_exception', True)
for d in dirs:
d = _os.path.abspath(d)
json_path = _os.path.join(d, "mozinfo.json")
@ -247,7 +263,10 @@ def find_and_update_from_json(*dirs):
update(json_path)
return json_path
return None
if raise_exception:
raise EnvironmentError('{}: could not find any mozinfo.json.'.format(__name__))
else:
return None
def output_to_file(path):

Просмотреть файл

@ -75,6 +75,13 @@ class TestMozinfo(unittest.TestCase):
self.assertEqual(mozinfo.find_and_update_from_json(self.tempdir), j)
self.assertEqual(mozinfo.info["foo"], "abcdefg")
def test_find_and_update_file_no_argument(self):
"""Test that mozinfo.find_and_update_from_json can
handle not being given any arguments.
"""
with self.assertRaises(EnvironmentError):
self.assertEqual(mozinfo.find_and_update_from_json())
def test_find_and_update_file_invalid_json(self):
"""Test that mozinfo.find_and_update_from_json can
handle invalid JSON"""
@ -83,6 +90,42 @@ class TestMozinfo(unittest.TestCase):
f.write('invalid{"json":')
self.assertRaises(ValueError, mozinfo.find_and_update_from_json, self.tempdir)
def test_find_and_update_file_raise_exception(self):
"""Test that mozinfo.find_and_update_from_json raises
an IOError exception if a True boolean value is
provided as the only argument.
"""
with self.assertRaises(EnvironmentError):
mozinfo.find_and_update_from_json(raise_exception=True)
def test_find_and_update_file_raise_exception_multiple_arguments(self):
"""Test that mozinfo.find_and_update_from_json raises
an IOError exception if a True boolean value is
provided as last positional argument.
"""
with self.assertRaises(EnvironmentError):
mozinfo.find_and_update_from_json(self.tempdir, raise_exception=True)
def test_find_and_update_file_suppress_exception(self):
"""Test that mozinfo.find_and_update_from_json suppresses
an IOError exception if a False boolean value is
provided as the only argument.
"""
self.assertEqual(mozinfo.find_and_update_from_json(
raise_exception=False), None)
def test_find_and_update_file_suppress_exception_multiple_arguments(self):
"""Test that mozinfo.find_and_update_from_json suppresses
an IOError exception if a False boolean value is
provided as last positional argument.
"""
j = os.path.join(self.tempdir, "mozinfo.json")
with open(j, "w") as f:
f.write(json.dumps({"foo": "abcdefg"}))
self.assertEqual(mozinfo.find_and_update_from_json(
self.tempdir, raise_exception=False), j)
self.assertEqual(mozinfo.info["foo"], "abcdefg")
def test_find_and_update_file_mozbuild(self):
"""Test that mozinfo.find_and_update_from_json can
find mozinfo.json using the mozbuild module."""