浏览代码

tests: Add more typing

Jelmer Vernooij 1 月之前
父节点
当前提交
1f43b80452
共有 4 个文件被更改,包括 38 次插入8 次删除
  1. 1 0
      tests/__init__.py
  2. 1 1
      tests/test_archive.py
  3. 31 2
      tests/test_cli.py
  4. 5 5
      tests/test_cli_merge.py

+ 1 - 0
tests/__init__.py

@@ -153,6 +153,7 @@ def self_test_suite():
         "refs",
         "repository",
         "server",
+        "sparse_patterns",
         "stash",
         "submodule",
         "utils",

+ 1 - 1
tests/test_archive.py

@@ -36,7 +36,7 @@ from . import TestCase
 try:
     from unittest.mock import patch
 except ImportError:
-    patch = None  # type: ignore
+    patch = None
 
 
 class ArchiveTests(TestCase):

+ 31 - 2
tests/test_cli.py

@@ -54,12 +54,41 @@ class DulwichCliTestCase(TestCase):
 
     def _run_cli(self, *args, stdout_stream=None):
         """Run CLI command and capture output."""
+        class MockStream:
+            def __init__(self):
+                self._buffer = io.BytesIO()
+                self.buffer = self._buffer
+                
+            def write(self, data):
+                if isinstance(data, bytes):
+                    self._buffer.write(data)
+                else:
+                    self._buffer.write(data.encode('utf-8'))
+                    
+            def getvalue(self):
+                value = self._buffer.getvalue()
+                try:
+                    return value.decode('utf-8')
+                except UnicodeDecodeError:
+                    return value
+                    
+            def __getattr__(self, name):
+                return getattr(self._buffer, name)
+        
         old_stdout = sys.stdout
         old_stderr = sys.stderr
         old_cwd = os.getcwd()
         try:
-            sys.stdout = stdout_stream or io.StringIO()
-            sys.stderr = io.StringIO()
+            # Use custom stdout_stream if provided, otherwise use MockStream
+            if stdout_stream:
+                sys.stdout = stdout_stream
+                if not hasattr(sys.stdout, 'buffer'):
+                    sys.stdout.buffer = sys.stdout
+            else:
+                sys.stdout = MockStream()
+                
+            sys.stderr = MockStream()
+            
             os.chdir(self.repo_path)
             result = cli.main(list(args))
             return result, sys.stdout.getvalue(), sys.stderr.getvalue()

+ 5 - 5
tests/test_cli_merge.py

@@ -69,7 +69,7 @@ class CLIMergeTests(TestCase):
                     ret = main(["merge", "feature"])
                     output = mock_stdout.getvalue()
 
-                self.assertEqual(ret, None)  # Success
+                self.assertEqual(ret, 0)  # Success
                 self.assertIn("Merge successful", output)
 
                 # Check that file2.txt exists
@@ -138,7 +138,7 @@ class CLIMergeTests(TestCase):
                     ret = main(["merge", "HEAD"])
                     output = mock_stdout.getvalue()
 
-                self.assertEqual(ret, None)  # Success
+                self.assertEqual(ret, 0)  # Success
                 self.assertIn("Already up to date", output)
             finally:
                 os.chdir(old_cwd)
@@ -180,7 +180,7 @@ class CLIMergeTests(TestCase):
                     ret = main(["merge", "--no-commit", "feature"])
                     output = mock_stdout.getvalue()
 
-                self.assertEqual(ret, None)  # Success
+                self.assertEqual(ret, 0)  # Success
                 self.assertIn("not committing", output)
 
                 # Check that files are merged
@@ -222,7 +222,7 @@ class CLIMergeTests(TestCase):
                     ret = main(["merge", "--no-ff", "feature"])
                     output = mock_stdout.getvalue()
 
-                self.assertEqual(ret, None)  # Success
+                self.assertEqual(ret, 0)  # Success
                 self.assertIn("Merge successful", output)
                 self.assertIn("Created merge commit", output)
             finally:
@@ -265,7 +265,7 @@ class CLIMergeTests(TestCase):
                     ret = main(["merge", "-m", "Custom merge message", "feature"])
                     output = mock_stdout.getvalue()
 
-                self.assertEqual(ret, None)  # Success
+                self.assertEqual(ret, 0)  # Success
                 self.assertIn("Merge successful", output)
             finally:
                 os.chdir(old_cwd)