From d32650eaf99e69023953c1c8b0eae22deb92a559 Mon Sep 17 00:00:00 2001 From: Mads Navntoft Date: Thu, 13 Aug 2026 14:57:50 +0200 Subject: [PATCH] Use expected_files.add as context manager where applicable --- .../integration-tests/kotlin/all-platforms/logs/test.py | 3 +-- misc/pytest/lib/query_suites.py | 8 ++++---- .../integration-tests/posix/frontend-invocations/test.py | 3 +-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/java/ql/integration-tests/kotlin/all-platforms/logs/test.py b/java/ql/integration-tests/kotlin/all-platforms/logs/test.py index 38a385f0e7d3..44965f0f2dcc 100644 --- a/java/ql/integration-tests/kotlin/all-platforms/logs/test.py +++ b/java/ql/integration-tests/kotlin/all-platforms/logs/test.py @@ -6,9 +6,8 @@ def test(codeql, java_full, cwd: pathlib.Path, expected_files): codeql.database.create(command=["kotlinc test.kt"]) - expected_files.add("logs.actual") - with open("logs.actual", "w") as f_out: + with expected_files.add("logs.actual") as f_out: log_dir = cwd / "test-db" / "log" for file_index, log_file in enumerate(log_dir.glob("kotlin-extractor*.log"), 1): f_out.write(f"Log file {file_index}\n") diff --git a/misc/pytest/lib/query_suites.py b/misc/pytest/lib/query_suites.py index fcbb1f16bde0..1e70008f65bc 100644 --- a/misc/pytest/lib/query_suites.py +++ b/misc/pytest/lib/query_suites.py @@ -9,8 +9,8 @@ def ret(query_suite): actual = sorted(actual.splitlines()) actual = [os.path.relpath(q, semmle_code_dir) for q in actual] actual_file_name = query_suite + '.actual' - expected_files.add(actual_file_name) - (cwd / actual_file_name).write_text('\n'.join(actual) + '\n') + with expected_files.add(actual_file_name) as f: + f.write('\n'.join(actual) + '\n') return ret @pytest.fixture @@ -25,6 +25,6 @@ def ret(lang_folder_name, query_suites): not_included = sorted(set(all_queries) - included_in_qls) not_included = [os.path.relpath(q, semmle_code_dir) for q in not_included] not_included_file_name = 'not_included_in_qls.actual' - expected_files.add(not_included_file_name) - (cwd / not_included_file_name).write_text('\n'.join(not_included) + '\n') + with expected_files.add(not_included_file_name) as f: + f.write('\n'.join(not_included) + '\n') return ret diff --git a/swift/ql/integration-tests/posix/frontend-invocations/test.py b/swift/ql/integration-tests/posix/frontend-invocations/test.py index da5b231883e7..1ec0ed83eb87 100644 --- a/swift/ql/integration-tests/posix/frontend-invocations/test.py +++ b/swift/ql/integration-tests/posix/frontend-invocations/test.py @@ -13,7 +13,7 @@ def test(codeql, swift, expected_files): with open(f, "rb") as module: print(f.name, sha256(module.read()).hexdigest(), file=expected) - with open("hashes.actual", "w") as actual: + with expected_files.add("hashes.actual") as actual: hashes = [ (s.name, s.resolve().name) for s in Path("test-db/working/swift-extraction-artifacts/store").iterdir() @@ -21,4 +21,3 @@ def test(codeql, swift, expected_files): hashes.sort() for module, hash in hashes: print(module, hash, file=actual) - expected_files.add("hashes.actual")