import importlib.util
import io
import os
import subprocess
import sys
import tempfile
import unittest

PATH = os.path.join(os.path.dirname(__file__), "pre-push-clean.py")
SPEC = importlib.util.spec_from_file_location("pre_push_clean", PATH)
MOD = importlib.util.module_from_spec(SPEC); SPEC.loader.exec_module(MOD)


class PrePushUnicodePathTests(unittest.TestCase):
    def test_fallback_file_list_preserves_unicode_paths(self):
        with tempfile.TemporaryDirectory() as directory:
            old_cwd, old_stdin = os.getcwd(), sys.stdin
            try:
                os.chdir(directory)
                subprocess.run(["git", "init", "-q"], check=True)
                subprocess.run(["git", "config", "user.email", "test@example.invalid"], check=True)
                subprocess.run(["git", "config", "user.name", "test"], check=True)
                os.makedirs("霜砚", exist_ok=True)
                with open("霜砚/凭证.md", "w", encoding="utf-8") as handle:
                    handle.write("safe\n")
                subprocess.run(["git", "add", "."], check=True)
                subprocess.run(["git", "commit", "-qm", "fixture"], check=True)
                sys.stdin = io.StringIO("")
                self.assertIn("霜砚/凭证.md", MOD.get_changed_files())
            finally:
                sys.stdin = old_stdin
                os.chdir(old_cwd)


if __name__ == "__main__":
    unittest.main()
