Files
chromium_depot_tools/tests/gn_helper_test.py
Fumitoshi Ukai fd5161870a gn_helper: find correct directory for //
gn finds // by searching .gn in parent directories.

To support out dir is not 2 directories from root (i.e. out/Default),
search .gn in parent directory for '//' location.

tools/licenses/licenses.py runs 'gn gen' in out/Default/$tmp.
for such case, '//' should be '../../..', or fail to import '//path'

Change-Id: I6e5ccbe93cb5e51704f31d4eb558c03560286865
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6051636
Reviewed-by: Philipp Wollermann <philwo@google.com>
Commit-Queue: Fumitoshi Ukai <ukai@google.com>
Reviewed-by: Takuto Ikuta <tikuta@chromium.org>
2024-11-27 04:20:40 +00:00

91 lines
2.9 KiB
Python

#!/usr/bin/env vpython3
# Copyright (c) 2024 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 os
import sys
import unittest
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT_DIR)
import gn_helper
from testing_support import trial_dir
def write(filename, content):
"""Writes the content of a file and create the directories as needed."""
filename = os.path.abspath(filename)
dirname = os.path.dirname(filename)
if not os.path.isdir(dirname):
os.makedirs(dirname)
with open(filename, 'w') as f:
f.write(content)
class GnHelperTest(trial_dir.TestCase):
def setUp(self):
super().setUp()
self.previous_dir = os.getcwd()
os.chdir(self.root_dir)
def tearDown(self):
os.chdir(self.previous_dir)
super().tearDown()
def test_lines(self):
write('.gn', '')
out_dir = os.path.join('out', 'dir')
# Make sure nested import directives work. This is based on the
# reclient test.
write(os.path.join(out_dir, 'args.gn'), 'import("//out/common.gni")')
write(os.path.join('out', 'common.gni'), 'import("common_2.gni")')
write(os.path.join('out', 'common_2.gni'), 'use_remoteexec=true')
lines = list(gn_helper.lines(out_dir))
# The test will only pass if both imports work and
# 'use_remoteexec=true' is seen.
self.assertListEqual(lines, [
'use_remoteexec=true',
])
def test_args(self):
write('.gn', '')
out_dir = os.path.join('out', 'dir')
# Make sure nested import directives work. This is based on the
# reclient test.
write(os.path.join(out_dir, 'args.gn'), 'import("//out/common.gni")')
write(os.path.join('out', 'common.gni'), 'import("common_2.gni")')
write(os.path.join('out', 'common_2.gni'), 'use_remoteexec=true')
lines = list(gn_helper.args(out_dir))
# The test will only pass if both imports work and
# 'use_remoteexec=true' is seen.
self.assertListEqual(lines, [
('use_remoteexec', 'true'),
])
def test_args_spaces(self):
write('.gn', '')
out_dir = os.path.join('out', 'dir')
# Make sure nested import directives work. This is based on the
# reclient test.
write(os.path.join(out_dir, 'args.gn'), 'import("//out/common.gni")')
write(os.path.join('out', 'common.gni'), 'import("common_2.gni")')
write(os.path.join('out', 'common_2.gni'), ' use_remoteexec = true ')
lines = list(gn_helper.args(out_dir))
# The test will only pass if both imports work and
# 'use_remoteexec=true' is seen.
self.assertListEqual(lines, [
('use_remoteexec', 'true'),
])
if __name__ == '__main__':
unittest.main()