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

+12 -11
View File
@@ -41,16 +41,17 @@ class TenantDirsTestCase(unittest.TestCase):
# 与假密钥文件;模板 repo:sample.py + .git。
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]\nbase_url="https://xcmapi.org/v1"\n')
open(os.path.join(self.template_home, "thread_history_1.sqlite"),
"w").write("OTHER TENANT HISTORY")
open(os.path.join(self.template_home, "auth.json"),
"w").write("SECRET")
Path(os.path.join(self.template_home, "config.toml")).write_text(
'[model_provider.xcmapi]\nbase_url="https://xcmapi.org/v1"\n',
encoding="utf-8")
Path(os.path.join(self.template_home, "thread_history_1.sqlite")).write_text(
"OTHER TENANT HISTORY", encoding="utf-8")
Path(os.path.join(self.template_home, "auth.json")).write_text(
"SECRET", 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()
@@ -77,7 +78,7 @@ class TenantDirsTestCase(unittest.TestCase):
os.path.join(dirs["test_repo"], "sample.py")))
# 第二次调用不覆盖租户自己的改动。
marker = os.path.join(dirs["test_repo"], "mine.txt")
open(marker, "w").write("mine")
Path(marker).write_text("mine", encoding="utf-8")
worker._tenant_dirs("projA")
self.assertTrue(os.path.exists(marker))
@@ -95,8 +96,8 @@ class TenantDirsTestCase(unittest.TestCase):
hosts_b = {m.split(":")[0] for m in mounts_b}
self.assertFalse(hosts_a & hosts_b)
# 租户 A 的 work 里写文件,B 的挂载里看不到(宿主侧)。
open(os.path.join(worker._tenant_dirs("projA")["work"],
"secret-a.txt"), "w").write("SECRET-A")
Path(os.path.join(worker._tenant_dirs("projA")["work"],
"secret-a.txt")).write_text("SECRET-A", encoding="utf-8")
self.assertFalse(os.path.exists(os.path.join(
worker._tenant_dirs("projB")["work"], "secret-a.txt")))