Bug 1053070 - Output a nicer error when reassigning a variable in moz.build. r=gps

This commit is contained in:
Mike Hommey 2014-08-14 07:15:40 +09:00
parent 7fb9358518
commit 1b7daaf643
4 changed files with 39 additions and 3 deletions

View File

@ -578,6 +578,16 @@ class BuildReaderError(Exception):
verb = 'read'
elif inner.args[1] == 'set_unknown':
verb = 'write'
elif inner.args[1] == 'reassign':
s.write('The underlying problem is an attempt to reassign ')
s.write('a reserved UPPERCASE variable.\n')
s.write('\n')
s.write('The reassigned variable causing the error is:\n')
s.write('\n')
s.write(' %s\n' % inner.args[2])
s.write('\n')
s.write('Maybe you meant "+=" instead of "="?\n')
return
else:
raise AssertionError('Unhandled global_ns: %s' % inner.args[1])

View File

@ -164,7 +164,7 @@ class GlobalNamespace(dict):
if name in self._allow_one_mutation:
self._allow_one_mutation.remove(name)
elif name in self and dict.__getitem__(self, name) is not value:
raise Exception('Reassigning %s is forbidden' % name)
raise KeyError('global_ns', 'reassign', name)
# We don't need to check for name.isupper() here because LocalNamespace
# only sends variables our way if isupper() is True.

View File

@ -81,18 +81,28 @@ class TestGlobalNamespace(unittest.TestCase):
self.assertEqual(ke.exception.args[1], 'set_unknown')
ns['DIRS'] = []
with self.assertRaisesRegexp(Exception, 'Reassigning .* is forbidden') as ke:
with self.assertRaises(KeyError) as ke:
ns['DIRS'] = []
e = ke.exception.args
self.assertEqual(e[0], 'global_ns')
self.assertEqual(e[1], 'reassign')
self.assertEqual(e[2], 'DIRS')
with ns.allow_all_writes() as d:
d['DIST_SUBDIR'] = 'foo'
self.assertEqual(ns['DIST_SUBDIR'], 'foo')
ns['DIST_SUBDIR'] = 'bar'
self.assertEqual(ns['DIST_SUBDIR'], 'bar')
with self.assertRaisesRegexp(Exception, 'Reassigning .* is forbidden') as ke:
with self.assertRaises(KeyError) as ke:
ns['DIST_SUBDIR'] = 'baz'
e = ke.exception.args
self.assertEqual(e[0], 'global_ns')
self.assertEqual(e[1], 'reassign')
self.assertEqual(e[2], 'DIST_SUBDIR')
self.assertTrue(d['foo'])
def test_key_checking(self):

View File

@ -164,6 +164,22 @@ class TestSandbox(unittest.TestCase):
self.assertEqual(e.args[0], 'global_ns')
self.assertEqual(e.args[1], 'set_unknown')
def test_exec_source_reassign(self):
sandbox = self.sandbox()
sandbox.exec_source('DIRS = ["foo"]', 'foo.py')
with self.assertRaises(SandboxExecutionError) as se:
sandbox.exec_source('DIRS = ["bar"]', 'foo.py')
self.assertEqual(sandbox['DIRS'], ['foo'])
e = se.exception
self.assertIsInstance(e.exc_value, KeyError)
e = se.exception.exc_value
self.assertEqual(e.args[0], 'global_ns')
self.assertEqual(e.args[1], 'reassign')
self.assertEqual(e.args[2], 'DIRS')
def test_add_tier_dir_regular_str(self):
sandbox = self.sandbox()