Fix formatting in Python files

PiperOrigin-RevId: 504890435
Change-Id: Ia9a89d0d7a07fe70c12f7f0202c8256c94d2f118
This commit is contained in:
Tom Hughes 2023-01-26 11:05:38 -08:00 committed by Copybara-Service
parent 0194f90d0e
commit 403a968d3c
5 changed files with 158 additions and 84 deletions

View File

@ -81,6 +81,7 @@ EX_BINARY_OUTPUT = gtest_test_utils.Subprocess(
# The tests.
if SUPPORTS_SEH_EXCEPTIONS:
class CatchSehExceptionsTest(gtest_test_utils.TestCase):
"""Tests exception-catching behavior."""

View File

@ -130,38 +130,50 @@ def CalculateTestLists():
if not ALL_TESTS:
ALL_TESTS.extend(
GetTestsForAllIterations({}, [AlsoRunDisabledTestsFlag()])[0])
GetTestsForAllIterations({}, [AlsoRunDisabledTestsFlag()])[0]
)
if not ACTIVE_TESTS:
ACTIVE_TESTS.extend(GetTestsForAllIterations({}, [])[0])
if not FILTERED_TESTS:
FILTERED_TESTS.extend(
GetTestsForAllIterations({}, [FilterFlag(TEST_FILTER)])[0])
GetTestsForAllIterations({}, [FilterFlag(TEST_FILTER)])[0]
)
if not SHARDED_TESTS:
SHARDED_TESTS.extend(
GetTestsForAllIterations({TOTAL_SHARDS_ENV_VAR: '3',
SHARD_INDEX_ENV_VAR: '1'},
[])[0])
GetTestsForAllIterations(
{TOTAL_SHARDS_ENV_VAR: '3', SHARD_INDEX_ENV_VAR: '1'}, []
)[0]
)
if not SHUFFLED_ALL_TESTS:
SHUFFLED_ALL_TESTS.extend(GetTestsForAllIterations(
{}, [AlsoRunDisabledTestsFlag(), ShuffleFlag(), RandomSeedFlag(1)])[0])
SHUFFLED_ALL_TESTS.extend(
GetTestsForAllIterations(
{}, [AlsoRunDisabledTestsFlag(), ShuffleFlag(), RandomSeedFlag(1)]
)[0]
)
if not SHUFFLED_ACTIVE_TESTS:
SHUFFLED_ACTIVE_TESTS.extend(GetTestsForAllIterations(
{}, [ShuffleFlag(), RandomSeedFlag(1)])[0])
SHUFFLED_ACTIVE_TESTS.extend(
GetTestsForAllIterations({}, [ShuffleFlag(), RandomSeedFlag(1)])[0]
)
if not SHUFFLED_FILTERED_TESTS:
SHUFFLED_FILTERED_TESTS.extend(GetTestsForAllIterations(
{}, [ShuffleFlag(), RandomSeedFlag(1), FilterFlag(TEST_FILTER)])[0])
SHUFFLED_FILTERED_TESTS.extend(
GetTestsForAllIterations(
{}, [ShuffleFlag(), RandomSeedFlag(1), FilterFlag(TEST_FILTER)]
)[0]
)
if not SHUFFLED_SHARDED_TESTS:
SHUFFLED_SHARDED_TESTS.extend(
GetTestsForAllIterations({TOTAL_SHARDS_ENV_VAR: '3',
SHARD_INDEX_ENV_VAR: '1'},
[ShuffleFlag(), RandomSeedFlag(1)])[0])
GetTestsForAllIterations(
{TOTAL_SHARDS_ENV_VAR: '3', SHARD_INDEX_ENV_VAR: '1'},
[ShuffleFlag(), RandomSeedFlag(1)],
)[0]
)
class GTestShuffleUnitTest(gtest_test_utils.TestCase):
@ -208,17 +220,29 @@ class GTestShuffleUnitTest(gtest_test_utils.TestCase):
def testShuffleDoesNotRepeatTest(self):
for test in SHUFFLED_ALL_TESTS:
self.assertEqual(1, SHUFFLED_ALL_TESTS.count(test),
'%s appears more than once' % (test,))
self.assertEqual(
1,
SHUFFLED_ALL_TESTS.count(test),
'%s appears more than once' % (test,),
)
for test in SHUFFLED_ACTIVE_TESTS:
self.assertEqual(1, SHUFFLED_ACTIVE_TESTS.count(test),
'%s appears more than once' % (test,))
self.assertEqual(
1,
SHUFFLED_ACTIVE_TESTS.count(test),
'%s appears more than once' % (test,),
)
for test in SHUFFLED_FILTERED_TESTS:
self.assertEqual(1, SHUFFLED_FILTERED_TESTS.count(test),
'%s appears more than once' % (test,))
self.assertEqual(
1,
SHUFFLED_FILTERED_TESTS.count(test),
'%s appears more than once' % (test,),
)
for test in SHUFFLED_SHARDED_TESTS:
self.assertEqual(1, SHUFFLED_SHARDED_TESTS.count(test),
'%s appears more than once' % (test,))
self.assertEqual(
1,
SHUFFLED_SHARDED_TESTS.count(test),
'%s appears more than once' % (test,),
)
def testShuffleDoesNotCreateNewTest(self):
for test in SHUFFLED_ALL_TESTS:
@ -259,9 +283,11 @@ class GTestShuffleUnitTest(gtest_test_utils.TestCase):
[test_case, _] = test.split('.')
if test_cases and test_cases[-1] != test_case:
test_cases.append(test_case)
self.assertEqual(1, test_cases.count(test_case),
'Test case %s is not grouped together in %s' %
(test_case, tests))
self.assertEqual(
1,
test_cases.count(test_case),
'Test case %s is not grouped together in %s' % (test_case, tests),
)
def testShuffleDoesNotInterleaveTestCases(self):
self._VerifyTestCasesDoNotInterleave(SHUFFLED_ALL_TESTS)
@ -278,7 +304,9 @@ class GTestShuffleUnitTest(gtest_test_utils.TestCase):
# pylint: disable-next=unbalanced-tuple-unpacking
[tests_in_iteration1, tests_in_iteration2, tests_in_iteration3] = (
GetTestsForAllIterations(
{}, [ShuffleFlag(), RandomSeedFlag(1), RepeatFlag(3)]))
{}, [ShuffleFlag(), RandomSeedFlag(1), RepeatFlag(3)]
)
)
# Make sure running the tests with random seed 1 gets the same
# order as in iteration 1 above.
@ -309,7 +337,9 @@ class GTestShuffleUnitTest(gtest_test_utils.TestCase):
# pylint: disable-next=unbalanced-tuple-unpacking
[tests_in_iteration1, tests_in_iteration2, tests_in_iteration3] = (
GetTestsForAllIterations(
{}, [ShuffleFlag(), RandomSeedFlag(1), RepeatFlag(3)]))
{}, [ShuffleFlag(), RandomSeedFlag(1), RepeatFlag(3)]
)
)
self.assertTrue(
tests_in_iteration1 != tests_in_iteration2, tests_in_iteration1
@ -343,5 +373,6 @@ class GTestShuffleUnitTest(gtest_test_utils.TestCase):
sorted_active_tests.sort()
self.assertEqual(sorted_active_tests, sorted_sharded_tests)
if __name__ == '__main__':
gtest_test_utils.Main()

View File

@ -68,8 +68,10 @@ TestCase = _test_module.TestCase
# Initially maps a flag to its default value. After
# _ParseAndStripGTestFlags() is called, maps a flag to its actual value.
_flag_map = {'source_dir': os.path.dirname(sys.argv[0]),
'build_dir': os.path.dirname(sys.argv[0])}
_flag_map = {
'source_dir': os.path.dirname(sys.argv[0]),
'build_dir': os.path.dirname(sys.argv[0]),
}
_gtest_flags_are_parsed = False
@ -91,7 +93,7 @@ def _ParseAndStripGTestFlags(argv):
while i < len(argv):
prefix = '--' + flag + '='
if argv[i].startswith(prefix):
_flag_map[flag] = argv[i][len(prefix):]
_flag_map[flag] = argv[i][len(prefix) :]
del argv[i]
break
else:
@ -147,15 +149,16 @@ def GetTestExecutablePath(executable_name, build_dir=None):
Args:
executable_name: name of the test binary that the test script runs.
build_dir: directory where to look for executables, by default
the result of GetBuildDir().
build_dir: directory where to look for executables, by default the
result of GetBuildDir().
Returns:
The absolute path of the test binary.
"""
path = os.path.abspath(os.path.join(build_dir or GetBuildDir(),
executable_name))
path = os.path.abspath(
os.path.join(build_dir or GetBuildDir(), executable_name)
)
if (IS_WINDOWS or IS_CYGWIN or IS_OS2) and not path.endswith('.exe'):
path += '.exe'
@ -163,7 +166,8 @@ def GetTestExecutablePath(executable_name, build_dir=None):
message = (
'Unable to find the test binary "%s". Please make sure to provide\n'
'a path to the binary via the --build_dir flag or the BUILD_DIR\n'
'environment variable.' % path)
'environment variable.' % path
)
print(message, file=sys.stderr)
sys.exit(1)
@ -191,6 +195,7 @@ def GetExitStatus(exit_code):
class Subprocess:
def __init__(self, command, working_dir=None, capture_stderr=True, env=None):
"""Changes into a specified directory, if provided, and executes a command.
@ -200,7 +205,7 @@ class Subprocess:
command: The command to run, in the form of sys.argv.
working_dir: The directory to change into.
capture_stderr: Determines whether to capture stderr in the output member
or to discard it.
or to discard it.
env: Dictionary with environment to pass to the subprocess.
Returns:
@ -220,9 +225,14 @@ class Subprocess:
else:
stderr = subprocess.PIPE
p = subprocess.Popen(command,
stdout=subprocess.PIPE, stderr=stderr,
cwd=working_dir, universal_newlines=True, env=env)
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=stderr,
cwd=working_dir,
universal_newlines=True,
env=env,
)
# communicate returns a tuple with the file object for the child's
# output.
self.output = p.communicate()[0]

View File

@ -233,18 +233,21 @@ Expected equality of these values:
GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
SUPPORTS_TYPED_TESTS = 'TypedTest' in gtest_test_utils.Subprocess(
[GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False).output
SUPPORTS_TYPED_TESTS = (
'TypedTest'
in gtest_test_utils.Subprocess(
[GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False
).output
)
class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
"""
Unit test for Google Test's XML output functionality.
"""
"""Unit test for Google Test's XML output functionality."""
# This test currently breaks on platforms that do not support typed and
# type-parameterized tests, so we don't run it under them.
if SUPPORTS_TYPED_TESTS:
def testNonEmptyXmlOutput(self):
"""Generates non-empty XML and verifies it matches the expected output.
@ -274,12 +277,16 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
# parse the expected datetime manually.
match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
self.assertTrue(
re.match,
'XML datettime string %s has incorrect format' % date_time_str)
re.match, 'XML datettime string %s has incorrect format' % date_time_str
)
date_time_from_xml = datetime.datetime(
year=int(match.group(1)), month=int(match.group(2)),
day=int(match.group(3)), hour=int(match.group(4)),
minute=int(match.group(5)), second=int(match.group(6)))
year=int(match.group(1)),
month=int(match.group(2)),
day=int(match.group(3)),
hour=int(match.group(4)),
minute=int(match.group(5)),
second=int(match.group(6)),
)
time_delta = abs(datetime.datetime.now() - date_time_from_xml)
# timestamp value should be near the current local time
@ -292,10 +299,12 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
Confirms that Google Test produces an XML output file with the expected
default name if no name is explicitly specified.
"""
output_file = os.path.join(gtest_test_utils.GetTempDir(),
GTEST_DEFAULT_OUTPUT_FILE)
output_file = os.path.join(
gtest_test_utils.GetTempDir(), GTEST_DEFAULT_OUTPUT_FILE
)
gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
'gtest_no_test_unittest')
'gtest_no_test_unittest'
)
try:
os.remove(output_file)
except OSError:
@ -305,7 +314,8 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
p = gtest_test_utils.Subprocess(
[gtest_prog_path, '%s=xml' % GTEST_OUTPUT_FLAG],
working_dir=gtest_test_utils.GetTempDir())
working_dir=gtest_test_utils.GetTempDir(),
)
self.assertTrue(p.exited)
self.assertEqual(0, p.exit_code)
self.assertTrue(os.path.isfile(output_file))
@ -317,20 +327,24 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
shut down before RUN_ALL_TESTS is invoked.
"""
xml_path = os.path.join(gtest_test_utils.GetTempDir(),
GTEST_PROGRAM_NAME + 'out.xml')
xml_path = os.path.join(
gtest_test_utils.GetTempDir(), GTEST_PROGRAM_NAME + 'out.xml'
)
if os.path.isfile(xml_path):
os.remove(xml_path)
command = [GTEST_PROGRAM_PATH,
'%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
'--shut_down_xml']
command = [
GTEST_PROGRAM_PATH,
'%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
'--shut_down_xml',
]
p = gtest_test_utils.Subprocess(command)
if p.terminated_by_signal:
# p.signal is available only if p.terminated_by_signal is True.
self.assertFalse(
p.terminated_by_signal,
'%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
'%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal),
)
else:
self.assertTrue(p.exited)
self.assertEqual(
@ -349,8 +363,12 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
non-selected tests do not show up in the XML output.
"""
self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_FILTERED_TEST_XML, 0,
extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
self._TestXmlOutput(
GTEST_PROGRAM_NAME,
EXPECTED_FILTERED_TEST_XML,
0,
extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG],
)
def testShardedTestXmlOutput(self):
"""Verifies XML output when run using multiple shards.
@ -363,8 +381,8 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
GTEST_PROGRAM_NAME,
EXPECTED_SHARDED_TEST_XML,
0,
extra_env={SHARD_INDEX_ENV_VAR: '0',
TOTAL_SHARDS_ENV_VAR: '10'})
extra_env={SHARD_INDEX_ENV_VAR: '0', TOTAL_SHARDS_ENV_VAR: '10'},
)
def _GetXmlOutput(
self, gtest_prog_name, extra_args, extra_env, expected_exit_code
@ -379,12 +397,15 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
extra_env: Optional environment variables to set.
expected_exit_code: Expected exit code from running gtest_prog_name.
"""
xml_path = os.path.join(gtest_test_utils.GetTempDir(),
gtest_prog_name + 'out.xml')
xml_path = os.path.join(
gtest_test_utils.GetTempDir(), gtest_prog_name + 'out.xml'
)
gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
command = ([gtest_prog_path, '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path)] +
extra_args)
command = [
gtest_prog_path,
'%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
] + extra_args
environ_copy = os.environ.copy()
if extra_env:
environ_copy.update(extra_env)
@ -406,8 +427,14 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
actual = minidom.parse(xml_path)
return actual
def _TestXmlOutput(self, gtest_prog_name, expected_xml,
expected_exit_code, extra_args=None, extra_env=None):
def _TestXmlOutput(
self,
gtest_prog_name,
expected_xml,
expected_exit_code,
extra_args=None,
extra_env=None,
):
"""Asserts that the XML document matches.
Asserts that the XML document generated by running the program
@ -423,12 +450,12 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
extra_env: Optional environment variables to set.
"""
actual = self._GetXmlOutput(gtest_prog_name, extra_args or [],
extra_env or {}, expected_exit_code)
actual = self._GetXmlOutput(
gtest_prog_name, extra_args or [], extra_env or {}, expected_exit_code
)
expected = minidom.parseString(expected_xml)
self.NormalizeXml(actual.documentElement)
self.AssertEquivalentNodes(expected.documentElement,
actual.documentElement)
self.AssertEquivalentNodes(expected.documentElement, actual.documentElement)
expected.unlink()
actual.unlink()

View File

@ -35,10 +35,9 @@ from googletest.test import gtest_test_utils
GTEST_DEFAULT_OUTPUT_FILE = 'test_detail.xml'
class GTestXMLTestCase(gtest_test_utils.TestCase):
"""
Base class for tests of Google Test's XML output functionality.
"""
"""Base class for tests of Google Test's XML output functionality."""
def AssertEquivalentNodes(self, expected_node, actual_node):
"""Asserts that actual_node is equivalent to expected_node.
@ -165,15 +164,19 @@ class GTestXMLTestCase(gtest_test_utils.TestCase):
'Encountered unknown element <%s>' % child.tagName,
)
child_id = child.getAttribute(
self.identifying_attribute[child.tagName])
self.identifying_attribute[child.tagName]
)
self.assertNotIn(child_id, children)
children[child_id] = child
elif child.nodeType in [Node.TEXT_NODE, Node.CDATA_SECTION_NODE]:
if 'detail' not in children:
if (child.nodeType == Node.CDATA_SECTION_NODE or
not child.nodeValue.isspace()):
if (
child.nodeType == Node.CDATA_SECTION_NODE
or not child.nodeValue.isspace()
):
children['detail'] = child.ownerDocument.createCDATASection(
child.nodeValue)
child.nodeValue
)
else:
children['detail'].nodeValue += child.nodeValue
else:
@ -210,8 +213,9 @@ class GTestXMLTestCase(gtest_test_utils.TestCase):
source_file.value = re.sub(r'^.*[/\\](.*)', '\\1', source_file.value)
if element.tagName in ('testsuites', 'testsuite', 'testcase'):
timestamp = element.getAttributeNode('timestamp')
timestamp.value = re.sub(r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d$',
'*', timestamp.value)
timestamp.value = re.sub(
r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d$', '*', timestamp.value
)
if element.tagName in ('testsuites', 'testsuite', 'testcase'):
time = element.getAttributeNode('time')
time.value = re.sub(r'^\d+(\.\d+)?$', '*', time.value)
@ -228,8 +232,9 @@ class GTestXMLTestCase(gtest_test_utils.TestCase):
# Replaces the source line information with a normalized form.
cdata = re.sub(source_line_pat, '\\1*\n', child.nodeValue)
# Removes the actual stack trace.
child.nodeValue = re.sub(r'Stack trace:\n(.|\n)*',
'Stack trace:\n*', cdata)
child.nodeValue = re.sub(
r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', cdata
)
for child in element.childNodes:
if child.nodeType == Node.ELEMENT_NODE:
self.NormalizeXml(child)