Files

83 lines
4.1 KiB
Python

import contextlib
import importlib.util
import io
import json
import os
from pathlib import Path
import sys
import tempfile
import unittest
from unittest.mock import patch
import xml.etree.ElementTree as ET
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
spec = importlib.util.spec_from_file_location("meshport_test_runner", Path(__file__).resolve().parents[1] / "test.py")
runner = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = runner
spec.loader.exec_module(runner)
class RunnerTest(unittest.TestCase):
def test_default_plan_excludes_throughput_and_native_builds(self):
steps = runner.plan(["all"], platform="darwin")
names = {step.name for step in steps}
self.assertTrue({"sdk", "go-race", "go-vet", "go-sdk-bridge", "flutter-tests", "macos-profile-storage"} <= names)
self.assertNotIn("sdk-throughput", names)
self.assertNotIn("client-rust-library", names)
self.assertIn("sdk", self.list_default())
def list_default(self):
with contextlib.redirect_stdout(io.StringIO()) as output:
self.assertEqual(runner.main(["--list"]), 0)
return output.getvalue()
def test_native_tests_depend_on_both_new_libraries(self):
step = next(s for s in runner.plan(["client"], native=True, platform="darwin") if s.name == "flutter-tests")
self.assertIn("client-rust-library", step.requires)
self.assertIn("client-go-library", step.requires)
self.assertTrue(step.env["MESHPORT_LIBRARY"].endswith("libmeshport_ffi.dylib"))
with self.assertRaises(ValueError):
runner.plan(["client"], native=True, platform="linux")
def test_removes_inherited_profiles_and_diagnostic_inputs(self):
with patch.dict(os.environ, {"MESHPORT_DRAIN_PROFILE": "private.json", "MESHPORT_LIBRARY": "old.dylib", "MESHPORT_TEST_DELAY_MS": "999"}):
env = runner.test_environment({"MESHPORT_LIBRARY": "new.dylib"})
self.assertEqual(env["MESHPORT_LIBRARY"], "new.dylib")
self.assertNotIn("MESHPORT_DRAIN_PROFILE", env)
self.assertNotIn("MESHPORT_TEST_DELAY_MS", env)
def test_failure_skips_dependents_but_runs_independent_steps_and_reports(self):
steps = [
runner.Step("failure", [sys.executable, "-c", "print('失败日志'); raise SystemExit(7)"]),
runner.Step("dependent", [sys.executable, "-c", "raise SystemExit(99)"], requires=("failure",)),
runner.Step("independent", [sys.executable, "-c", "print('ok')"]),
]
with tempfile.TemporaryDirectory() as temporary, contextlib.redirect_stdout(io.StringIO()):
directory = Path(temporary) / "run"
results = runner.run(steps, directory)
self.assertEqual([r["status"] for r in results], ["failed", "skipped", "passed"])
self.assertEqual(results[0]["exit_code"], 7)
self.assertIn("失败日志", (directory / "failure.log").read_text())
self.assertEqual(len(json.loads((directory / "report.json").read_text())["steps"]), 3)
xml = ET.parse(directory / "junit.xml").getroot()
self.assertEqual(xml.attrib["failures"], "1")
self.assertEqual(xml.attrib["skipped"], "1")
def test_fail_fast_and_timeout(self):
steps = [runner.Step("timeout", [sys.executable, "-c", "import time; time.sleep(10)"]),
runner.Step("later", [sys.executable, "-c", "pass"])]
with tempfile.TemporaryDirectory() as temporary, contextlib.redirect_stdout(io.StringIO()):
results = runner.run(steps, Path(temporary) / "run", timeout=0.05, fail_fast=True)
self.assertEqual(results[0]["exit_code"], 124)
self.assertEqual(results[1]["status"], "skipped")
def test_main_returns_failure_exit_code(self):
result = [{"name": "failed", "status": "failed", "exit_code": 3}]
with tempfile.TemporaryDirectory() as temporary, patch.object(runner, "run", return_value=result), contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(runner.main(["sdk", "--log-dir", str(Path(temporary) / "run")]), 1)
if __name__ == "__main__":
unittest.main()