From 3cdbacc46422b8ed6dc6fb8aaec3dfb58451460f Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Tue, 14 Jul 2020 13:08:52 +0200 Subject: [PATCH] [lldb/test] Avoid globbing in log file handling code The glob expression for a test called "test" could match a log file for a the test "test_foo". Instead of globbing, maintain an explicit list of log files relevant to the current test. --- .../Python/lldbsuite/test/lldbtest.py | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lldb/packages/Python/lldbsuite/test/lldbtest.py b/lldb/packages/Python/lldbsuite/test/lldbtest.py index 29561d4794be..ebef896d12b6 100644 --- a/lldb/packages/Python/lldbsuite/test/lldbtest.py +++ b/lldb/packages/Python/lldbsuite/test/lldbtest.py @@ -576,6 +576,7 @@ class Base(unittest2.TestCase): # confirm that the file is writeable host_log_path = "{}-host.log".format(log_basename) open(host_log_path, 'w').close() + self.log_files.append(host_log_path) log_enable = "log enable -Tpn -f {} ".format(host_log_path) for channel_with_categories in lldbtest_config.channels: @@ -602,6 +603,7 @@ class Base(unittest2.TestCase): if lldb.remote_platform is None: server_log_path = "{}-server.log".format(log_basename) open(server_log_path, 'w').close() + self.log_files.append(server_log_path) os.environ["LLDB_DEBUGSERVER_LOG_FILE"] = server_log_path # Communicate channels to lldb-server @@ -623,12 +625,13 @@ class Base(unittest2.TestCase): # Retrieve the server log (if any) from the remote system. It is assumed the server log # is writing to the "server.log" file in the current test directory. This can be # achieved by setting LLDB_DEBUGSERVER_LOG_FILE="server.log" when starting remote - # platform. If the remote logging is not enabled, then just let the Get() command silently - # fail. + # platform. if lldb.remote_platform: - lldb.remote_platform.Get( - lldb.SBFileSpec("server.log"), lldb.SBFileSpec( - self.getLogBasenameForCurrentTest() + "-server.log")) + server_log_path = self.getLogBasenameForCurrentTest() + "-server.log" + if lldb.remote_platform.Get( + lldb.SBFileSpec("server.log"), + lldb.SBFileSpec(server_log_path)).Success(): + self.log_files.append(server_log_path) def setPlatformWorkingDir(self): if not lldb.remote_platform or not configuration.lldb_platform_working_dir: @@ -800,11 +803,12 @@ class Base(unittest2.TestCase): # List of forked process PIDs self.forkedProcessPids = [] - # Create a string buffer to record the session info, to be dumped into a - # test case specific file if test failure is encountered. - self.log_basename = self.getLogBasenameForCurrentTest() + # List of log files produced by the current test. + self.log_files = [] + + session_file = self.getLogBasenameForCurrentTest()+".log" + self.log_files.append(session_file) - session_file = "{}.log".format(self.log_basename) # Python 3 doesn't support unbuffered I/O in text mode. Open buffered. self.session = encoded_file.open(session_file, "utf-8", mode="w") @@ -1218,14 +1222,13 @@ class Base(unittest2.TestCase): del self.session # process the log files - log_files_for_this_test = glob.glob(self.log_basename + "*") - if prefix != 'Success' or lldbtest_config.log_success: # keep all log files, rename them to include prefix + src_log_basename = self.getLogBasenameForCurrentTest(None) dst_log_basename = self.getLogBasenameForCurrentTest(prefix) - for src in log_files_for_this_test: + for src in self.log_files: if os.path.isfile(src): - dst = src.replace(self.log_basename, dst_log_basename) + dst = src.replace(src_log_basename, dst_log_basename) if os.name == "nt" and os.path.isfile(dst): # On Windows, renaming a -> b will throw an exception if # b exists. On non-Windows platforms it silently @@ -1239,8 +1242,9 @@ class Base(unittest2.TestCase): os.rename(src, dst) else: # success! (and we don't want log files) delete log files - for log_file in log_files_for_this_test: - remove_file(log_file) + for log_file in self.log_files: + if os.path.isfile(log_file): + remove_file(log_file) # ==================================================== # Config. methods supported through a plugin interface