Commit 8d626576 authored by Edward Lesmes's avatar Edward Lesmes Committed by Commit Bot

gclient_eval: Add more support when adding new variables.

Now we respect comments before the first variable.

Bug: 760633
Change-Id: Ibe60d719429c033415bfb1c99942c9d04601d967
Reviewed-on: https://chromium-review.googlesource.com/998683
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: 's avatarMichael Moss <mmoss@chromium.org>
Reviewed-by: 's avatarAaron Gable <agable@chromium.org>
parent 0ecf6d6a
...@@ -542,13 +542,15 @@ def AddVar(gclient_dict, var_name, value): ...@@ -542,13 +542,15 @@ def AddVar(gclient_dict, var_name, value):
if not gclient_dict['vars']: if not gclient_dict['vars']:
raise ValueError('vars dict is empty. This is not yet supported.') raise ValueError('vars dict is empty. This is not yet supported.')
# We will attempt to add the var right before the first var. # We will attempt to add the var right after 'vars = {'.
node = gclient_dict.GetNode('vars').keys[0] node = gclient_dict.GetNode('vars')
if node is None: if node is None:
raise ValueError( raise ValueError(
"The vars dict has no formatting information." % var_name) "The vars dict has no formatting information." % var_name)
line = node.lineno line = node.lineno + 1
col = node.col_offset
# We will try to match the new var's indentation to the next variable.
col = node.keys[0].col_offset
# We use a minimal Python dictionary, so that ast can parse it. # We use a minimal Python dictionary, so that ast can parse it.
var_content = '{\n%s"%s": "%s",\n}' % (' ' * col, var_name, value) var_content = '{\n%s"%s": "%s",\n}' % (' ' * col, var_name, value)
......
No-op file. Edit this to kick recipes. No-op file. Edit this to kick recipes. (Did they do something wrong?)
This is a beginning of a story in this silly file. This is a beginning of a story in this silly file.
Once upon a time, a budding web browser dev team needed a CI system. Once upon a time, a budding web browser dev team needed a CI system.
......
...@@ -237,22 +237,25 @@ class EvaluateConditionTest(unittest.TestCase): ...@@ -237,22 +237,25 @@ class EvaluateConditionTest(unittest.TestCase):
class AddVarTest(unittest.TestCase): class AddVarTest(unittest.TestCase):
def assert_adds_var(self, before, after):
local_scope = gclient_eval.Exec('\n'.join(before))
gclient_eval.AddVar(local_scope, 'baz', 'lemur')
results = gclient_eval.RenderDEPSFile(local_scope)
self.assertEqual(results, '\n'.join(after))
def test_adds_var(self): def test_adds_var(self):
local_scope = gclient_eval.Exec('\n'.join([ before = [
'vars = {', 'vars = {',
' "foo": "bar",', ' "foo": "bar",',
'}', '}',
])) ]
after = [
gclient_eval.AddVar(local_scope, 'baz', 'lemur')
result = gclient_eval.RenderDEPSFile(local_scope)
self.assertEqual(result, '\n'.join([
'vars = {', 'vars = {',
' "baz": "lemur",', ' "baz": "lemur",',
' "foo": "bar",', ' "foo": "bar",',
'}', '}',
])) ]
self.assert_adds_var(before, after)
def test_adds_var_twice(self): def test_adds_var_twice(self):
local_scope = gclient_eval.Exec('\n'.join([ local_scope = gclient_eval.Exec('\n'.join([
...@@ -274,39 +277,38 @@ class AddVarTest(unittest.TestCase): ...@@ -274,39 +277,38 @@ class AddVarTest(unittest.TestCase):
])) ]))
def test_preserves_formatting(self): def test_preserves_formatting(self):
local_scope = gclient_eval.Exec('\n'.join([ before = [
'# Copyright stuff', '# Copyright stuff',
'# some initial comments', '# some initial comments',
'', '',
'vars = { ', 'vars = { ',
' # Some comments.',
' "foo": "bar",', ' "foo": "bar",',
' # Some commets.', '',
' # More comments.', ' # More comments.',
' # Even more comments.', ' # Even more comments.',
' "v8_revision": ', ' "v8_revision": ',
' "deadbeef",', ' "deadbeef",',
' # Someone formatted this wrong', ' # Someone formatted this wrong',
'}', '}',
])) ]
after = [
gclient_eval.AddVar(local_scope, 'baz', 'lemur')
result = gclient_eval.RenderDEPSFile(local_scope)
self.assertEqual(result, '\n'.join([
'# Copyright stuff', '# Copyright stuff',
'# some initial comments', '# some initial comments',
'', '',
'vars = { ', 'vars = { ',
' "baz": "lemur",', ' "baz": "lemur",',
' # Some comments.',
' "foo": "bar",', ' "foo": "bar",',
' # Some commets.', '',
' # More comments.', ' # More comments.',
' # Even more comments.', ' # Even more comments.',
' "v8_revision": ', ' "v8_revision": ',
' "deadbeef",', ' "deadbeef",',
' # Someone formatted this wrong', ' # Someone formatted this wrong',
'}', '}',
])) ]
self.assert_adds_var(before, after)
class SetVarTest(unittest.TestCase): class SetVarTest(unittest.TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment