Commit b70f8c20 authored by Jérémie Boulic's avatar Jérémie Boulic Committed by Chromium LUCI CQ

[Files app] Fixes in module conversion script and first unittests

Update the modules generation script with the changes below.

Fix reformatting of single-line dependency lists: empty dependency lists
(e.g. `deps = []`) were rewritten on multiple lines (deps = [\n])
incorrectly.

In the `find_dependencies` function, simplify calls to
`add_dependency` by the need for redundant `if is_unittest... else...`.

"//chrome/test/data/webui:chai_assert" can be imported in non-unittest
files, and "//ui/webui/resources/js" can be imported in unittest files.
Remove unittest distinction when importing variabble from these
locations.

Fix function exports: The detection of functions used a variable
(the name of the function the export) that was not defined.

Fix test function exports: when converting the same unittest multiple
times, the "export" keyword was added multiple times in front of each
test function to be exported.

Facilitate testing by adding a `add_js_file_exports` function that
takes `file_lines` as arguments (`convert_js_file` is harder to test
directly, taking a file path as argument).

Fix edge case where, when the only reported closure error is about
extending `EventTarget`, the script doesn't do what's needed to fix it:
Run `find_dependencies` whether or not there is any variable to import.

Add unittests and presubmit script.

Bug: 1133186
Test: ui/file_manager/base/tools/modules_test.py
Change-Id: Ia0ed675576d021b3b952215384e7a491ff7f4430
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2580970
Commit-Queue: Jeremie Boulic <jboulic@chromium.org>
Reviewed-by: default avatarLuciano Pacheco <lucmult@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836525}
parent 6e753ce9
# Copyright 2020 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.
"""Presubmit script for files in ui/file_manager/base/tools/
See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into depot_tools.
"""
def RunTests(input_api, output_api):
presubmit_path = input_api.PresubmitLocalPath()
sources = ['modules_test.py']
tests = [input_api.os_path.join(presubmit_path, s) for s in sources]
return input_api.canned_checks.RunUnitTests(input_api, output_api, tests)
def _CheckChangeOnUploadOrCommit(input_api, output_api):
results = []
affected = input_api.AffectedFiles()
def is_in_current_folder(f):
dirname = input_api.os_path.dirname(f.LocalPath())
return dirname == u'ui/file_manager/base/tools'
# Check if any of the modified files is in 'ui/file_manager/base/tools'.
if any([is_in_current_folder(f) for f in affected]):
results += RunTests(input_api, output_api)
return results
def CheckChangeOnUpload(input_api, output_api):
return _CheckChangeOnUploadOrCommit(input_api, output_api)
def CheckChangeOnCommit(input_api, output_api):
return _CheckChangeOnUploadOrCommit(input_api, output_api)
This diff is collapsed.
#!/usr/bin/env python
# Copyright 2020 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.
import unittest
import modules
import os
_HERE_DIR = os.path.dirname(__file__)
class ModulesConversionTest(unittest.TestCase):
def _get_file_lines(self, file):
path = os.path.join(_HERE_DIR, 'tests', file)
return modules.get_file_lines(path)
def _debug_file_contents(self, file_lines, expected_file_lines):
print '\n------- FILE -------'
print '\n'.join(file_lines)
print '----- EXPECTED -----'
print '\n'.join(expected_file_lines)
print '--------------------\n'
def testExportFunction(self):
'''Tests exporting functions that are not used locally.'''
file_lines = self._get_file_lines('export_function.js')
expected_file_lines = self._get_file_lines(
'export_function_expected.js')
# Check: function is exported correctly.
modules.add_js_file_exports(file_lines)
self.assertEquals(file_lines, expected_file_lines)
# Check: running add_js_file_exports a second time produces the same
# expected output.
modules.add_js_file_exports(file_lines)
self.assertEquals(file_lines, expected_file_lines)
def testExportLocalFunction(self):
'''Tests that functions that are used locally are not be exported.'''
file_lines = self._get_file_lines('export_local_function.js')
expected_file_lines = self._get_file_lines(
'export_local_function_expected.js')
# Check: local function is not exported.
modules.add_js_file_exports(file_lines)
self.assertEquals(file_lines, expected_file_lines)
def testUpdateEmptyDependencyList(self):
'''Tests adding a dependency to an empty dependency list.'''
file_lines = self._get_file_lines('empty_dependency_list.gn')
expected_file_lines = self._get_file_lines(
'empty_dependency_list_expected.gn')
rule_first_line = 'js_unittest("importer_common_unittest.m") {'
list_name = 'deps'
dependency_line = ' "//ui/file_manager/base/js:mock_chrome",'
# Check: dependency list correctly updated with new dependency.
modules.add_dependency(file_lines, rule_first_line, list_name,
dependency_line)
self.assertEquals(file_lines, expected_file_lines)
# Check: running add_dependency a second time produces the same
# expected output (no duplicate dependency).
modules.add_dependency(file_lines, rule_first_line, list_name,
dependency_line)
self.assertEquals(file_lines, expected_file_lines)
def testUpdateSingleLineDependencyList(self):
'''Tests adding a dependency to an single-line dependency list.'''
file_lines = self._get_file_lines('single_line_dependency_list.gn')
expected_file_lines = self._get_file_lines(
'single_line_dependency_list_expected.gn')
rule_first_line = 'js_unittest("importer_common_unittest.m") {'
list_name = 'deps'
dependency_line = ' "//ui/file_manager/base/js:mock_chrome",'
# Check: dependency list correctly formatted and updated with new
# dependency.
modules.add_dependency(file_lines, rule_first_line, list_name,
dependency_line)
self.assertEquals(file_lines, expected_file_lines)
if __name__ == '__main__':
unittest.main()
js_unittest("importer_common_unittest.m") {
deps = []
}
js_unittest("importer_common_unittest.m") {
deps = [
"//ui/file_manager/base/js:mock_chrome",
]
}
/* #export */ function Foo() {}
Foo.types = [];
js_unittest("importer_common_unittest.m") {
deps = [ ":mock_entry" ]
}
js_unittest("importer_common_unittest.m") {
deps = [
"//ui/file_manager/base/js:mock_chrome",
":mock_entry",
]
}
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