Repository: scikit-build/scikit-build-core
Pull request #364 (2023-06-09 22:01:17)

tests: add testing for scripts processing
Adding and fixing tests.



Patch:
diff --git a/src/scikit_build_core/build/_file_processor.py b/src/scikit_build_core/build/_file_processor.py
index d0d462f..0e76832 100644
--- a/src/scikit_build_core/build/_file_processor.py
+++ b/src/scikit_build_core/build/_file_processor.py
@@ -41,7 +41,7 @@ def each_unignored_file(
     exclude_spec = pathspec.GitIgnoreSpec.from_lines(exclude_lines)
     include_spec = pathspec.GitIgnoreSpec.from_lines(include)
 
-    for dirpath, _, filenames in os.walk(str(starting_path)):
+    for dirpath, _, filenames in os.walk(str(starting_path), followlinks=True):
         all_paths = (Path(dirpath) / fn for fn in filenames)
         paths = (
             p


Test patch:
diff --git a/tests/packages/custom_cmake/CMakeLists.txt b/tests/packages/custom_cmake/CMakeLists.txt
index 3cfea57..b6b3699 100644
--- a/tests/packages/custom_cmake/CMakeLists.txt
+++ b/tests/packages/custom_cmake/CMakeLists.txt
@@ -14,4 +14,4 @@ if(NOT EXAMPLE_INCLUDE_FOUND)
 endif()
 
 # Testing scripts
-install(FILES scripts/script1 DESTINATION "${SKBUILD_SCRIPTS_DIR}")
+install(PROGRAMS scripts/script1 DESTINATION "${SKBUILD_SCRIPTS_DIR}")
diff --git a/tests/packages/custom_cmake/scripts/script1 b/tests/packages/custom_cmake/scripts/script1
index 0d83742..b7a484c 100755
--- a/tests/packages/custom_cmake/scripts/script1
+++ b/tests/packages/custom_cmake/scripts/script1
@@ -1,5 +1,5 @@
-#/usr/bin/env python3
+#!/usr/bin/env python3
 
-import sys
+from pathlib import Path
 
-print(f"Ran script 1 from {sys.executable}")
+print(Path(__file__).resolve())
diff --git a/tests/test_custom_modules.py b/tests/test_custom_modules.py
index d263577..5deae17 100644
--- a/tests/test_custom_modules.py
+++ b/tests/test_custom_modules.py
@@ -1,7 +1,10 @@
 from __future__ import annotations
 
+import sys
 from pathlib import Path
 
+import pytest
+
 DIR = Path(__file__).parent
 PROJECT_DIR = DIR / "packages" / "custom_cmake"
 
@@ -10,4 +13,12 @@ def test_ep(isolated):
     isolated.install("hatchling", "scikit-build-core[pyproject]")
     isolated.install(PROJECT_DIR / "extern", isolated=False)
     isolated.install(PROJECT_DIR, "-v", isolated=False)
-    # Needs script fix: assert isolated.run("script1") == ""
+
+    if sys.platform.startswith("win"):
+        # TODO: maybe figure out how to make this work on windows?
+        pytest.skip("Can't run script on Windows")
+
+    script = isolated.run("script1", capture=True).strip()
+    pysys = isolated.execute("import sys; print(sys.executable)").strip()
+    contents = Path(script).read_text()
+    assert contents.startswith(f"#!{pysys}")
diff --git a/tests/test_file_processor.py b/tests/test_file_processor.py
new file mode 100644
index 0000000..7ec1d34
--- /dev/null
+++ b/tests/test_file_processor.py
@@ -0,0 +1,53 @@
+from __future__ import annotations
+
+import sys
+from pathlib import Path
+
+import pytest
+
+from scikit_build_core.build._file_processor import each_unignored_file
+
+
+@pytest.mark.skipif(
+    sys.version_info < (3, 8) and sys.platform.startswith("win"),
+    reason="Python 3.8+ required for symlinks on Windows",
+)
+@pytest.mark.skipif(
+    sys.implementation.name == "pypy" and sys.platform.startswith("win"),
+    reason="PyPy on Windows does not support symlinks",
+)
+def test_on_each_with_symlink(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+    """
+    Test that each_unignored_file() does not follow symlinks.
+    """
+    monkeypatch.chdir(tmp_path)
+    # Set up a gitignore
+    gitignore = Path(".gitignore")
+    gitignore.write_text("/hidden_dir")
+    # Create a directory with a symlink to a file in the same directory
+    dir = Path("dir")
+    dir.mkdir()
+    file1 = dir / "file"
+    file1.write_text("content")
+    file2 = dir / "link"
+    file2.symlink_to("file")
+    hidden_dir = Path("hidden_dir")
+    hidden_dir.mkdir()
+    hidden_file = hidden_dir / "file2"
+    hidden_file.write_text("content2")
+    exposed_symlink = dir / "exposed_symlink"
+    exposed_symlink.symlink_to("../hidden_dir")
+
+    if (
+        sys.platform.startswith("win")
+        and not exposed_symlink.joinpath("file2").is_file()
+    ):
+        pytest.skip("Windows symlink support not available")
+
+    # Test that each_unignored_file() follows the symlink
+    assert sorted(each_unignored_file(Path("."))) == [
+        gitignore,
+        exposed_symlink / "file2",
+        file1,
+        file2,
+    ]