|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from scikit_build_core.build._file_processor import each_unignored_file |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.skipif( |
| 12 | + sys.version_info < (3, 8) and sys.platform.startswith("win"), |
| 13 | + reason="Python 3.8+ required for symlinks on Windows", |
| 14 | +) |
| 15 | +@pytest.mark.skipif( |
| 16 | + sys.implementation.name == "pypy" and sys.platform.startswith("win"), |
| 17 | + reason="PyPy on Windows does not support symlinks", |
| 18 | +) |
| 19 | +def test_on_each_with_symlink(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 20 | + """ |
| 21 | + Test that each_unignored_file() does not follow symlinks. |
| 22 | + """ |
| 23 | + monkeypatch.chdir(tmp_path) |
| 24 | + # Set up a gitignore |
| 25 | + gitignore = Path(".gitignore") |
| 26 | + gitignore.write_text("/hidden_dir") |
| 27 | + # Create a directory with a symlink to a file in the same directory |
| 28 | + dir = Path("dir") |
| 29 | + dir.mkdir() |
| 30 | + file1 = dir / "file" |
| 31 | + file1.write_text("content") |
| 32 | + file2 = dir / "link" |
| 33 | + file2.symlink_to("file") |
| 34 | + hidden_dir = Path("hidden_dir") |
| 35 | + hidden_dir.mkdir() |
| 36 | + hidden_file = hidden_dir / "file2" |
| 37 | + hidden_file.write_text("content2") |
| 38 | + exposed_symlink = dir / "exposed_symlink" |
| 39 | + exposed_symlink.symlink_to("../hidden_dir") |
| 40 | + |
| 41 | + if ( |
| 42 | + sys.platform.startswith("win") |
| 43 | + and not exposed_symlink.joinpath("file2").is_file() |
| 44 | + ): |
| 45 | + pytest.skip("Windows symlink support not available") |
| 46 | + |
| 47 | + # Test that each_unignored_file() follows the symlink |
| 48 | + assert sorted(each_unignored_file(Path("."))) == [ |
| 49 | + gitignore, |
| 50 | + exposed_symlink / "file2", |
| 51 | + file1, |
| 52 | + file2, |
| 53 | + ] |
0 commit comments