tests: Define a MatchFileRegex verifier
Using it as a base class for MatchRegex. While the latter is using a regex to scan the stdout and stderr files, MatchFileRegex is more generically allowing you to specify which file to scan at __init__ time. Signed-off-by: Giacomo Travaglini <giacomo.travaglini@arm.com> Change-Id: I2a64dab9c88a632b0a6b400ba02aafe473b3e62d Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/44445 Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br> Reviewed-by: Jason Lowe-Power <power.jg@gmail.com> Maintainer: Jason Lowe-Power <power.jg@gmail.com> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
@@ -1,3 +1,15 @@
|
||||
# Copyright (c) 2021 Arm Limited
|
||||
# All rights reserved
|
||||
#
|
||||
# The license below extends only to copyright in the software and shall
|
||||
# not be construed as granting a license to any other intellectual
|
||||
# property including but not limited to intellectual property relating
|
||||
# to a hardware implementation of the functionality of the software
|
||||
# licensed hereunder. You may use the software subject to the license
|
||||
# terms below provided that you ensure that this notice is replicated
|
||||
# unmodified and in its entirety in all distributions of the software,
|
||||
# modified or unmodified, in source code or in binary form.
|
||||
#
|
||||
# Copyright (c) 2017 Mark D. Hill and David A. Wood
|
||||
# All rights reserved.
|
||||
#
|
||||
@@ -161,34 +173,47 @@ class MatchConfigJSON(DerivedGoldStandard):
|
||||
re.compile(r'''^\s*"(cwd|input|codefile)":'''),
|
||||
)
|
||||
|
||||
class MatchRegex(Verifier):
|
||||
def __init__(self, regex, match_stderr=True, match_stdout=True):
|
||||
super(MatchRegex, self).__init__()
|
||||
class MatchFileRegex(Verifier):
|
||||
"""
|
||||
Looking for a match between a regex pattern and the content of a list
|
||||
of files. Verifier will pass as long as the pattern is found in at least
|
||||
one of the files.
|
||||
"""
|
||||
def __init__(self, regex, filenames):
|
||||
super(MatchFileRegex, self).__init__()
|
||||
self.regex = _iterable_regex(regex)
|
||||
self.match_stderr = match_stderr
|
||||
self.match_stdout = match_stdout
|
||||
self.filenames = filenames
|
||||
|
||||
def parse_file(self, fname):
|
||||
with open(fname, 'r') as file_:
|
||||
for line in file_:
|
||||
for regex in self.regex:
|
||||
if re.match(regex, line):
|
||||
return True
|
||||
|
||||
def test(self, params):
|
||||
fixtures = params.fixtures
|
||||
# Get the file from the tempdir of the test.
|
||||
tempdir = fixtures[constants.tempdir_fixture_name].path
|
||||
|
||||
def parse_file(fname):
|
||||
with open(fname, 'r') as file_:
|
||||
for line in file_:
|
||||
for regex in self.regex:
|
||||
if re.match(regex, line):
|
||||
return True
|
||||
if self.match_stdout:
|
||||
if parse_file(joinpath(tempdir,
|
||||
constants.gem5_simulation_stdout)):
|
||||
return # Success
|
||||
if self.match_stderr:
|
||||
if parse_file(joinpath(tempdir,
|
||||
constants.gem5_simulation_stderr)):
|
||||
for fname in self.filenames:
|
||||
if self.parse_file(joinpath(tempdir, fname)):
|
||||
return # Success
|
||||
|
||||
test_util.fail('Could not match regex.')
|
||||
|
||||
class MatchRegex(MatchFileRegex):
|
||||
"""
|
||||
Looking for a match between a regex pattern and stdout/stderr.
|
||||
"""
|
||||
def __init__(self, regex, match_stderr=True, match_stdout=True):
|
||||
filenames = list()
|
||||
if match_stdout:
|
||||
filenames.append(constants.gem5_simulation_stdout)
|
||||
if match_stderr:
|
||||
filenames.append(constants.gem5_simulation_stderr)
|
||||
super(MatchRegex, self).__init__(regex, filenames)
|
||||
|
||||
_re_type = type(re.compile(''))
|
||||
def _iterable_regex(regex):
|
||||
if isinstance(regex, _re_type) or isinstance(regex, str):
|
||||
|
||||
Reference in New Issue
Block a user