Sfoglia il codice sorgente

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 mese fa
parent
commit
6fb905cde8
2 ha cambiato i file con 30 aggiunte e 0 eliminazioni
  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.
    (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.
    Host settings including hostname, user, port, and identity file are
    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({"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:
         # Absolute paths are (not yet) supported
         with open(os.path.join(self.repo.path, "foo"), "w") as f: