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-03 02:48:54 +00:00
1 parent 82d0e84371
commit 7b537c31a6
9 files changed
+61 -57

No files matched your search

+16 -15
View File
@@ -35,8 +35,9 @@ class SkillSeedHelperTestCase(unittest.TestCase):
for name in ("live-info", "document-tools"):
d = os.path.join(src, name)
os.makedirs(d, exist_ok=True)
open(os.path.join(d, "SKILL.md"), "w").write(
"---\nname: %s\ndescription: test\n---\nbody\n" % name)
Path(os.path.join(d, "SKILL.md")).write_text(
"---\nname: %s\ndescription: test\n---\nbody\n" % name,
encoding="utf-8")
return src
def test_copy_idempotent_and_missing_source(self):
@@ -58,10 +59,10 @@ class SkillSeedHelperTestCase(unittest.TestCase):
ensure_codex_home_skills(home, src)
dest = os.path.join(home, "skills",
"live-info", "SKILL.md")
open(dest, "w").write("tenant edited version\n")
Path(dest).write_text("tenant edited version\n", encoding="utf-8")
out = ensure_codex_home_skills(home, src)
self.assertEqual(out["skills_skipped_differ"], ["live-info"])
self.assertEqual(open(dest).read(), "tenant edited version\n")
self.assertEqual(Path(dest).read_text(encoding="utf-8"), "tenant edited version\n")
def test_real_pack_lists_all_skills(self):
names = [n for n, _p in list_skills()]
@@ -78,13 +79,13 @@ class WorkerSkillSeedTestCase(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()
@@ -115,22 +116,22 @@ class WorkerSkillSeedTestCase(unittest.TestCase):
for name in ("live-info", "document-tools", "image-workflow"):
path = self._skill_path(dirs, name)
self.assertTrue(os.path.exists(path), path)
self.assertIn("name: %s" % name, open(path).read())
self.assertIn("name: %s" % name, Path(path).read_text(encoding="utf-8"))
def test_flag_on_seeds_software_engineering_and_user_edit_survives(self):
worker = self._worker(seed_skills=True)
dirs = worker._tenant_dirs("projA")
path = self._skill_path(dirs, "software-engineering")
self.assertTrue(os.path.exists(path), path)
text = open(path).read()
text = Path(path).read_text(encoding="utf-8")
self.assertIn("name: software-engineering", text)
# v0.2 P0-1: the appendix travels inside SKILL.md itself,
# because skill_seed only copies SKILL.md, not templates/.
self.assertIn("附录角色:00-coordinator", text)
self.assertIn("附录角色:08-shared-core", text)
open(path, "w").write("tenant edited version\n")
Path(path).write_text("tenant edited version\n", encoding="utf-8")
worker._tenant_dirs("projA")
self.assertEqual(open(path).read(), "tenant edited version\n")
self.assertEqual(Path(path).read_text(encoding="utf-8"), "tenant edited version\n")
def test_old_tenant_merges_and_user_edit_survives(self):
worker = self._worker()
@@ -139,9 +140,9 @@ class WorkerSkillSeedTestCase(unittest.TestCase):
worker2._tenant_dirs("projA")
path = self._skill_path(dirs, "live-info")
self.assertTrue(os.path.exists(path))
open(path, "w").write("tenant edited version\n")
Path(path).write_text("tenant edited version\n", encoding="utf-8")
worker2._tenant_dirs("projA")
self.assertEqual(open(path).read(), "tenant edited version\n")
self.assertEqual(Path(path).read_text(encoding="utf-8"), "tenant edited version\n")
if __name__ == "__main__":