Commit 6d6a5116 authored by Hokein.Wu@gmail.com's avatar Hokein.Wu@gmail.com

Prase IDL files in common/extensions/api PRESUBMIT check.

Review URL: https://chromiumcodereview.appspot.com/23512007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@224709 0039d316-1c4b-4281-b951-d872f2087c98
parent 916770c9
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
def _GetJSONParseError(input_api, contents): def _GetJSONParseError(input_api, filename):
try: try:
contents = input_api.ReadFile(filename)
json_comment_eater = input_api.os_path.join( json_comment_eater = input_api.os_path.join(
input_api.PresubmitLocalPath(), input_api.PresubmitLocalPath(),
'..', '..', '..', '..', 'tools', '..', '..', '..', '..', 'tools',
...@@ -11,7 +12,8 @@ def _GetJSONParseError(input_api, contents): ...@@ -11,7 +12,8 @@ def _GetJSONParseError(input_api, contents):
process = input_api.subprocess.Popen( process = input_api.subprocess.Popen(
[input_api.python_executable, json_comment_eater], [input_api.python_executable, json_comment_eater],
stdin=input_api.subprocess.PIPE, stdin=input_api.subprocess.PIPE,
stdout=input_api.subprocess.PIPE) stdout=input_api.subprocess.PIPE,
universal_newlines=True)
(nommed, _) = process.communicate(input=contents) (nommed, _) = process.communicate(input=contents)
input_api.json.loads(nommed) input_api.json.loads(nommed)
except ValueError as e: except ValueError as e:
...@@ -19,6 +21,20 @@ def _GetJSONParseError(input_api, contents): ...@@ -19,6 +21,20 @@ def _GetJSONParseError(input_api, contents):
return None return None
def _GetIDLParseError(input_api, filename):
idl_schema = input_api.os_path.join(
input_api.PresubmitLocalPath(),
'..', '..', '..', '..', 'tools',
'json_schema_compiler', 'idl_schema.py')
process = input_api.subprocess.Popen(
[input_api.python_executable, idl_schema, filename],
stdout=input_api.subprocess.PIPE,
stderr=input_api.subprocess.PIPE,
universal_newlines=True)
(_, error) = process.communicate()
return error or None
def _GetParseErrors(input_api, output_api): def _GetParseErrors(input_api, output_api):
# Run unit tests. # Run unit tests.
results = [] results = []
...@@ -27,17 +43,24 @@ def _GetParseErrors(input_api, output_api): ...@@ -27,17 +43,24 @@ def _GetParseErrors(input_api, output_api):
results = input_api.canned_checks.RunUnitTestsInDirectory( results = input_api.canned_checks.RunUnitTestsInDirectory(
input_api, output_api, '.', whitelist=[r'^PRESUBMIT_test\.py$']) input_api, output_api, '.', whitelist=[r'^PRESUBMIT_test\.py$'])
actions = {
'.idl': _GetIDLParseError,
'.json': _GetJSONParseError,
}
def get_action(affected_file):
filename = affected_file.LocalPath()
return actions.get(input_api.os_path.splitext(filename)[1])
for affected_file in input_api.AffectedFiles( for affected_file in input_api.AffectedFiles(
file_filter=lambda f: f.LocalPath().endswith('.json'), file_filter=
lambda f: "test_presubmit" not in f.LocalPath() and get_action(f),
include_deletes=False): include_deletes=False):
filename = affected_file.AbsoluteLocalPath() parse_error = get_action(affected_file)(input_api,
contents = input_api.ReadFile(filename) affected_file.AbsoluteLocalPath())
parse_error = _GetJSONParseError(input_api, contents)
if parse_error: if parse_error:
results.append(output_api.PresubmitError( results.append(output_api.PresubmitError('%s could not be parsed: %s' %
'Features file %s could not be parsed: %s' %
(affected_file.LocalPath(), parse_error))) (affected_file.LocalPath(), parse_error)))
# TODO(yoz): Also ensure IDL files are parseable.
return results return results
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import glob
import json import json
import os import os
import subprocess import subprocess
...@@ -22,40 +23,59 @@ class MockInputApi(object): ...@@ -22,40 +23,59 @@ class MockInputApi(object):
def PresubmitLocalPath(self): def PresubmitLocalPath(self):
return os.path.dirname(__file__) return os.path.dirname(__file__)
def ReadFile(self, filename, mode='rU'):
with open(filename, mode=mode) as f:
return f.read()
class JSONParsingTest(unittest.TestCase): class JSONParsingTest(unittest.TestCase):
def testSuccess(self): def testSuccess(self):
input_api = MockInputApi() input_api = MockInputApi()
input_json = ''' filename = 'test_presubmit/valid_json.json'
// This is a comment. self.assertEqual(None,
{ PRESUBMIT._GetJSONParseError(input_api, filename))
"key1": ["value1", "value2"],
"key2": 3 // This is an inline comment. def testFailure(self):
} input_api = MockInputApi()
''' expected_errors = [
'Expecting property name: line 8 column 3 (char 9)',
'Invalid control character at: line 8 column 19 (char 25)',
'Expecting property name: line 8 column 23 (char 29)',
'Expecting , delimiter: line 8 column 12 (char 18)',
]
actual_errors = [
str(PRESUBMIT._GetJSONParseError(input_api, filename))
for filename in sorted(glob.glob('test_presubmit/invalid_*.json'))
]
self.assertEqual(expected_errors, actual_errors)
class IDLParsingTest(unittest.TestCase):
def testSuccess(self):
input_api = MockInputApi()
filename = 'test_presubmit/valid_idl_basics.idl'
self.assertEqual(None, self.assertEqual(None,
PRESUBMIT._GetJSONParseError(input_api, input_json)) PRESUBMIT._GetIDLParseError(input_api, filename))
def testFailure(self): def testFailure(self):
input_api = MockInputApi() input_api = MockInputApi()
input_json = '{ x }' expected_errors = [
self.assertEqual('Expecting property name: line 1 column 2 (char 2)', 'Unexpected "{" after keyword "dictionary".',
str(PRESUBMIT._GetJSONParseError(input_api, input_json))) 'Unexpected symbol DOMString after symbol a.',
'Unexpected symbol name2 after symbol name1.',
input_json = '{ "hello": "world }' 'Trailing comma in block.',
self.assertEqual( 'Unexpected ";" after "(".',
'Unterminated string starting at: line 1 column 11 (char 11)', 'Unexpected ")" after symbol long.',
str(PRESUBMIT._GetJSONParseError(input_api, input_json))) 'Unexpected symbol Events after symbol interace.',
'Did not process Interface Interface(NotEvent)',
input_json = '{ "a": "b", "c": "d", }' 'Interface missing name.',
self.assertEqual( ]
'Expecting property name: line 1 column 22 (char 22)', actual_errors = [
str(PRESUBMIT._GetJSONParseError(input_api, input_json))) PRESUBMIT._GetIDLParseError(input_api, filename)
for filename in sorted(glob.glob('test_presubmit/invalid_*.idl'))
input_json = '{ "a": "b" "c": "d" }' ]
self.assertEqual( for (expected_error, actual_error) in zip(expected_errors, actual_errors):
'Expecting , delimiter: line 1 column 11 (char 11)', self.assertTrue(expected_error in actual_error)
str(PRESUBMIT._GetJSONParseError(input_api, input_json)))
if __name__ == "__main__": if __name__ == "__main__":
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid IDL file.
namespace test {
// Unexpected "{" after keyword "dictionary".
dictionary {
DOMString s;
};
};
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid IDL file.
namespace test {
// Unexpected symbol DOMString after symbol a.
dictionary MissingSemicolon {
DOMString a
DOMString b;
};
};
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid IDL file.
namespace test {
// Unexpected symbol name2 after symbol name1.
enum MissingComma {
name1
name2
};
};
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid IDL file.
namespace test {
// Trailing comma in block.
enum TrailingComma {
name1,
name2,
};
};
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid IDL file.
namespace test {
// Unexpected ";" after "(".
callback Callback1 = void(;
};
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid IDL file.
namespace test {
// Unexpected ")" after symbol long.
callback Callback1 = void(long );
};
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid IDL file.
namespace test {
// Unexpected symbol Events after symbol interace.
interace Events {
static void onFoo1();
};
};
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid IDL file.
namespace test {
// Did not process Interface Interface(NotEvent).
interface NotEvent {
static void onFoo1();
};
};
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid IDL file.
namespace test {
// Interface missing name.
interface {
static void function1();
};
};
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid JSON file.
// Expecting property name: line 8 column 3 (char 9).
{ x }
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid JSON file.
// Invalid control character at: line 8 column 19 (char 25).
{ "hello": "world }
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid JSON file.
// Expecting property name: line 8 column 23 (char 29).
{ "a": "b", "c": "d", }
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests an invalid JSON file.
// Expecting , delimiter: line 8 column 12 (char 18).
{ "a": "b" "c": "d" }
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests a valid IDL file.
namespace idl_basics {
enum EnumType {
name1,
name2
};
dictionary MyType1 {
DOMString a;
};
callback Callback1 = void();
callback Callback2 = void(long x);
callback Callback3 = void(MyType1 arg);
callback Callback4 = void(EnumType type);
interface Functions {
static void function1();
static void function2(long x);
static void function3(MyType1 arg);
static void function4(Callback1 cb);
static void function5(Callback2 cb);
static void function6(Callback3 cb);
static void function7(Callback4 cb);
};
interface Events {
static void onFoo1();
static void onFoo2(long x);
static void onFoo2(MyType1 arg);
static void onFoo3(EnumType type);
};
};
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests a valid IDL file.
// This is a comment.
{
"key1": ["value1", "value2"],
"key2": 3 // This is an inline comment.
}
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