|
@@ -0,0 +1,94 @@
|
|
|
|
|
+import io
|
|
|
|
|
+import os
|
|
|
|
|
+import subprocess
|
|
|
|
|
+import sys
|
|
|
|
|
+import unittest
|
|
|
|
|
+from pathlib import Path
|
|
|
|
|
+from unittest.mock import patch
|
|
|
|
|
+
|
|
|
|
|
+ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
+SCRIPTS = ROOT / "scripts"
|
|
|
|
|
+sys.path.insert(0, str(SCRIPTS))
|
|
|
|
|
+import console_io
|
|
|
|
|
+
|
|
|
|
|
+# mcp_audit.py 输出的是 JSON,编码失败时自己报错退出,不改它的输出方式
|
|
|
|
|
+ENTRYPOINTS = ("audit.py", "rule_check.py", "sync_package.py", "fetch_table.py")
|
|
|
|
|
+PRINT_MARKS = ("import console_io; console_io.setup(); "
|
|
|
|
|
+ "print('\\u2705 \\u89c4\\u5219 \\u26a0\\ufe0f \\u274c')")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def gbk_stream():
|
|
|
|
|
+ return io.TextIOWrapper(io.BytesIO(), encoding="gbk", errors="strict")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class SetupTests(unittest.TestCase):
|
|
|
|
|
+ def written(self, stream):
|
|
|
|
|
+ stream.flush()
|
|
|
|
|
+ return stream.buffer.getvalue()
|
|
|
|
|
+
|
|
|
|
|
+ def test_marks_no_longer_abort_output_on_a_gbk_stream(self):
|
|
|
|
|
+ out, err = gbk_stream(), gbk_stream()
|
|
|
|
|
+ with self.assertRaises(UnicodeEncodeError):
|
|
|
|
|
+ out.write("✅")
|
|
|
|
|
+ out = gbk_stream()
|
|
|
|
|
+ with patch.object(sys, "stdout", out), patch.object(sys, "stderr", err):
|
|
|
|
|
+ console_io.setup(platform="win32", environ={})
|
|
|
|
|
+ print("✅ 规则未变动")
|
|
|
|
|
+ data = self.written(out)
|
|
|
|
|
+ self.assertEqual(out.encoding.lower(), "gbk")
|
|
|
|
|
+ self.assertIn("规则未变动".encode("gbk"), data)
|
|
|
|
|
+ self.assertIn(b"\\u2705", data)
|
|
|
|
|
+
|
|
|
|
|
+ def test_git_bash_pipe_switches_to_utf8(self):
|
|
|
|
|
+ out, err = gbk_stream(), gbk_stream()
|
|
|
|
|
+ with patch.object(sys, "stdout", out), patch.object(sys, "stderr", err):
|
|
|
|
|
+ console_io.setup(platform="win32", environ={"MSYSTEM": "MINGW64"})
|
|
|
|
|
+ print("✅ 规则未变动")
|
|
|
|
|
+ self.assertEqual(self.written(out).decode("utf-8").strip(), "✅ 规则未变动")
|
|
|
|
|
+ self.assertEqual(err.encoding.lower(), "utf-8")
|
|
|
|
|
+
|
|
|
|
|
+ def test_explicit_encoding_choice_and_other_platforms_keep_their_encoding(self):
|
|
|
|
|
+ for platform, environ in (("win32", {"MSYSTEM": "MINGW64", "PYTHONIOENCODING": "gbk"}),
|
|
|
|
|
+ ("linux", {"MSYSTEM": "MINGW64"})):
|
|
|
|
|
+ with self.subTest(platform=platform, environ=environ):
|
|
|
|
|
+ out, err = gbk_stream(), gbk_stream()
|
|
|
|
|
+ with patch.object(sys, "stdout", out), patch.object(sys, "stderr", err):
|
|
|
|
|
+ console_io.setup(platform=platform, environ=environ)
|
|
|
|
|
+ self.assertEqual(out.encoding.lower(), "gbk")
|
|
|
|
|
+ self.assertEqual(out.errors, "backslashreplace")
|
|
|
|
|
+
|
|
|
|
|
+ def test_streams_without_reconfigure_are_left_alone(self):
|
|
|
|
|
+ with patch.object(sys, "stdout", io.StringIO()), patch.object(sys, "stderr", io.StringIO()):
|
|
|
|
|
+ console_io.setup(platform="win32", environ={"MSYSTEM": "MINGW64"})
|
|
|
|
|
+ print("✅")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class CommandTests(unittest.TestCase):
|
|
|
|
|
+ def run_marks(self, **changes):
|
|
|
|
|
+ env = {k: v for k, v in os.environ.items()
|
|
|
|
|
+ if k not in ("PYTHONIOENCODING", "PYTHONUTF8", "MSYSTEM")}
|
|
|
|
|
+ env.update(changes)
|
|
|
|
|
+ return subprocess.run([sys.executable, "-c", PRINT_MARKS], cwd=SCRIPTS, env=env,
|
|
|
|
|
+ capture_output=True)
|
|
|
|
|
+
|
|
|
|
|
+ def test_piped_output_with_marks_exits_cleanly(self):
|
|
|
|
|
+ result = self.run_marks()
|
|
|
|
|
+ self.assertEqual(result.returncode, 0, result.stderr)
|
|
|
|
|
+
|
|
|
|
|
+ @unittest.skipUnless(sys.platform == "win32", "MSYS 终端只在 Windows 上需要处理")
|
|
|
|
|
+ def test_git_bash_output_is_utf8(self):
|
|
|
|
|
+ result = self.run_marks(MSYSTEM="MINGW64")
|
|
|
|
|
+ self.assertEqual(result.returncode, 0, result.stderr)
|
|
|
|
|
+ self.assertEqual(result.stdout.decode("utf-8").strip(), "✅ 规则 ⚠️ ❌")
|
|
|
|
|
+
|
|
|
|
|
+ def test_every_command_sets_up_output_before_running(self):
|
|
|
|
|
+ for name in ENTRYPOINTS:
|
|
|
|
|
+ with self.subTest(script=name):
|
|
|
|
|
+ source = (SCRIPTS / name).read_text(encoding="utf-8")
|
|
|
|
|
+ entry = source[source.rindex('if __name__ == "__main__":'):]
|
|
|
|
|
+ self.assertIn("console_io.setup()", entry)
|
|
|
|
|
+ self.assertLess(entry.index("console_io.setup()"), entry.index("main()"))
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ unittest.main()
|