Initial commit

This commit is contained in:
hanyixuanten
2026-09-09 09:28:29 +08:00
commit 7978a373e8
7 changed files with 531 additions and 0 deletions
View File
+84
View File
@@ -0,0 +1,84 @@
import io
import unittest
import zipfile
from pathlib import Path
from tempfile import TemporaryDirectory
from tools.build_hydrooj import archive_format, build_ssl_context, classify_member, create_hydro_bundle_zip, create_hydro_zip, find_problem_roots, normalize_problem_id
class BuildHydroOJTests(unittest.TestCase):
def test_normalize_problem_id_preserves_safe_ascii_and_lowercases(self):
self.assertEqual(normalize_problem_id("CSP-J 2025 / Day1"), "csp-j-2025-day1")
def test_classify_member_recognizes_inputs_and_outputs(self):
self.assertEqual(classify_member("day1/number1.in"), "input")
self.assertEqual(classify_member("day1/number1.out"), "output")
self.assertEqual(classify_member("day1/problem.pdf"), "statement")
def test_ssl_context_supports_the_official_expired_certificate(self):
self.assertFalse(build_ssl_context().check_hostname)
self.assertEqual(build_ssl_context().verify_mode.name, "CERT_NONE")
def test_archive_format_detects_zip_and_rar_magic(self):
with TemporaryDirectory() as temp:
zip_path = Path(temp) / "a.zip"
with zipfile.ZipFile(zip_path, "w") as archive:
archive.writestr("a.in", "1\n")
self.assertEqual(archive_format(zip_path), "zip")
rar_path = Path(temp) / "a.rar"
rar_path.write_bytes(b"Rar!\x1a\x07\x01\x00")
self.assertEqual(archive_format(rar_path), "rar")
def test_creates_hydro_problem_zip_with_metadata_and_testdata(self):
with TemporaryDirectory() as temp:
root = Path(temp)
data_dir = root / "input-data"
data_dir.mkdir()
(data_dir / "case1.in").write_text("1\n")
(data_dir / "case1.ans").write_text("2\n")
output = root / "demo.zip"
pdf = root / "demo.pdf"
pdf.write_bytes(b"%PDF-demo")
create_hydro_zip(output, "demo", "演示题", data_dir, ["CCF", "NOIP"], pdf)
with zipfile.ZipFile(output) as archive:
names = set(archive.namelist())
self.assertIn("demo/problem.yaml", names)
self.assertIn("demo/problem_zh.md", names)
self.assertIn("demo/testdata/config.yaml", names)
self.assertIn("demo/testdata/case1.in", names)
self.assertIn("demo/testdata/case1.out", names)
self.assertIn("demo/additional_file/demo.pdf", names)
self.assertNotIn("demo/testdata/case1.ans", names)
self.assertIn("file://demo.pdf", archive.read("demo/problem_zh.md").decode())
self.assertIn("title: 演示题", archive.read("demo/problem.yaml").decode())
def test_creates_one_bundle_zip_containing_multiple_problems(self):
with TemporaryDirectory() as temp:
root = Path(temp)
first = root / "first"; second = root / "second"
first.mkdir(); second.mkdir()
(first / "a.in").write_text("1\n"); (first / "a.ans").write_text("2\n")
(second / "b.in").write_text("3\n"); (second / "b.ans").write_text("4\n")
output = root / "exam.zip"
create_hydro_bundle_zip(output, "exam", "考试", [("first", first), ("second", second)], [])
with zipfile.ZipFile(output) as archive:
names = set(archive.namelist())
self.assertIn("exam/first/problem.yaml", names)
self.assertIn("exam/second/problem.yaml", names)
self.assertIn("exam/first/testdata/a.out", names)
self.assertIn("exam/second/testdata/b.out", names)
def test_finds_roots_that_contain_matched_test_pairs(self):
with TemporaryDirectory() as temp:
archive = Path(temp) / "tests.zip"
with zipfile.ZipFile(archive, "w") as z:
z.writestr("event/a/a1.in", "1\n")
z.writestr("event/a/a1.out", "2\n")
z.writestr("event/b/readme.txt", "no data")
with zipfile.ZipFile(archive) as z:
self.assertEqual(find_problem_roots(z.namelist()), {"event/a"})
if __name__ == "__main__":
unittest.main()