浏览代码

Add test for porcelain.add() from ignored directory (#1674)

Add regression test to verify that porcelain.add() correctly handles
adding files when the current working directory is within a gitignored
directory.

Fixes #550
Jelmer Vernooij 1 月之前
父节点
当前提交
6fb905cde8
共有 2 个文件被更改,包括 30 次插入0 次删除
  1. 4 0
      NEWS
  2. 26 0
      tests/test_porcelain.py

+ 4 - 0
NEWS

@@ -38,6 +38,10 @@
    Previously used a hardcoded 7-character hash length.
    Previously used a hardcoded 7-character hash length.
    (Jelmer Vernooij, #824)
    (Jelmer Vernooij, #824)
 
 
+ * Add test for ``porcelain.add()`` to verify files can be added when
+   the current working directory is within a gitignored directory.
+   (Jelmer Vernooij, #550)
+
  * ParamikoSSHVendor now reads SSH configuration from ~/.ssh/config.
  * ParamikoSSHVendor now reads SSH configuration from ~/.ssh/config.
    Host settings including hostname, user, port, and identity file are
    Host settings including hostname, user, port, and identity file are
    now respected when establishing SSH connections.
    now respected when establishing SSH connections.

+ 26 - 0
tests/test_porcelain.py

@@ -1312,6 +1312,32 @@ class AddTests(PorcelainTestCase):
         self.assertEqual({"bar"}, set(added))
         self.assertEqual({"bar"}, set(added))
         self.assertEqual({"foo", "subdir/"}, ignored)
         self.assertEqual({"foo", "subdir/"}, ignored)
 
 
+    def test_add_from_ignored_directory(self) -> None:
+        # Test for issue #550 - adding files when cwd is in ignored directory
+        # Create .gitignore that ignores build/
+        with open(os.path.join(self.repo.path, ".gitignore"), "w") as f:
+            f.write("build/\n")
+
+        # Create ignored directory
+        build_dir = os.path.join(self.repo.path, "build")
+        os.mkdir(build_dir)
+
+        # Create a file in the repo (not in ignored directory)
+        src_file = os.path.join(self.repo.path, "source.py")
+        with open(src_file, "w") as f:
+            f.write("print('hello')\n")
+
+        # Save current directory and change to ignored directory
+        original_cwd = os.getcwd()
+        try:
+            os.chdir(build_dir)
+            # Add file using absolute path from within ignored directory
+            (added, ignored) = porcelain.add(self.repo.path, paths=[src_file])
+            self.assertIn(b"source.py", self.repo.open_index())
+            self.assertEqual({"source.py"}, set(added))
+        finally:
+            os.chdir(original_cwd)
+
     def test_add_file_absolute_path(self) -> None:
     def test_add_file_absolute_path(self) -> None:
         # Absolute paths are (not yet) supported
         # Absolute paths are (not yet) supported
         with open(os.path.join(self.repo.path, "foo"), "w") as f:
         with open(os.path.join(self.repo.path, "foo"), "w") as f: