test: close unclosed file handles in tests (Path read/write)

Replace bare open().read()/write() in tests with Path.read_text/
write_text (or json.loads over read_text); the five remaining
open(...).close() empty-file creations become Path.write_text('').
No assertion, predicate, or production code changed; no behaviour
change beyond closing the handle deterministically.

Long-function splits and server.py module split intentionally not
included: the longest functions (_dispatch, _create_task,
_dispatch_project, ~375-395 lines in a 10430-line server.py) are
central routing/creation paths, and splitting them is not low-risk;
see PR body.
This commit is contained in:
openqj authored and host committed 2026-11-04 20:53:46 +00:00
1 parent fa4588a119
commit d779a1b426
9 files changed
+61 -57

No files matched your search

+10 -10
View File
@@ -28,13 +28,13 @@ class WorkerImagegenTestCase(unittest.TestCase):
init_db(self.db_path)
self.template_home = os.path.join(self.tmp.name, "tpl-home")
os.makedirs(self.template_home)
open(os.path.join(self.template_home, "config.toml"),
"w").write('[model_provider.xcmapi]\n'
'base_url="https://xcmapi.org/v1"\n')
Path(os.path.join(self.template_home, "config.toml")).write_text(
'[model_provider.xcmapi]\n'
'base_url="https://xcmapi.org/v1"\n', encoding="utf-8")
self.template_repo = os.path.join(self.tmp.name, "tpl-repo")
os.makedirs(os.path.join(self.template_repo, ".git"))
open(os.path.join(self.template_repo, "sample.py"),
"w").write("def add(a, b):\n return a + b\n")
Path(os.path.join(self.template_repo, "sample.py")).write_text(
"def add(a, b):\n return a + b\n", encoding="utf-8")
def tearDown(self):
self.tmp.cleanup()
@@ -52,8 +52,8 @@ class WorkerImagegenTestCase(unittest.TestCase):
worker = self._worker()
self.assertFalse(worker.imagegen_mcp)
dirs = worker._tenant_dirs("projA")
config = open(os.path.join(dirs["codex_home"],
"config.toml")).read()
config = Path(os.path.join(dirs["codex_home"],
"config.toml")).read_text(encoding="utf-8")
self.assertNotIn("[mcp_servers.imagegen]", config)
self.assertFalse(os.path.exists(
os.path.join(dirs["codex_home"], "imagegen_mcp.py")))
@@ -62,7 +62,7 @@ class WorkerImagegenTestCase(unittest.TestCase):
worker = self._worker(imagegen_mcp=True)
dirs = worker._tenant_dirs("projA")
home = dirs["codex_home"]
config = open(os.path.join(home, "config.toml")).read()
config = Path(os.path.join(home, "config.toml")).read_text(encoding="utf-8")
self.assertIn("[mcp_servers.imagegen]", config)
self.assertIn("/var/lib/codex-app-server/codex-home/"
"imagegen_mcp.py", config)
@@ -71,7 +71,7 @@ class WorkerImagegenTestCase(unittest.TestCase):
os.path.join(home, "imagegen.py")))
self.assertTrue(os.path.exists(
os.path.join(home, "imagegen_mcp.py")))
script = open(os.path.join(home, "imagegen_mcp.py")).read()
script = Path(os.path.join(home, "imagegen_mcp.py")).read_text(encoding="utf-8")
self.assertIn("image_generate", script)
def test_env_helper_writes_imagegen_section_only(self):
@@ -82,7 +82,7 @@ class WorkerImagegenTestCase(unittest.TestCase):
home, {"IMAGE_GEN_API_KEY": "k",
"IMAGE_GEN_MODEL": "gpt-image-2.5-flare",
"NO_PROXY": "localhost,127.0.0.1"}))
config = open(os.path.join(home, "config.toml")).read()
config = Path(os.path.join(home, "config.toml")).read_text(encoding="utf-8")
self.assertIn("[mcp_servers.imagegen.env]", config)
self.assertIn("gpt-image-2.5-flare", config)
# Idempotent: same values again is a no-op.