1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
from __future__ import annotations
import sys
from pathlib import Path
from typing import TYPE_CHECKING
import pytest
from scikit_build_core.build._file_processor import each_unignored_file
if TYPE_CHECKING:
from collections.abc import Generator
def _mk_files(tmp_path: Path, files: str) -> Generator[Path, None, None]:
"""
Create a set of files in the given temporary path based on the provided string.
The string should contain file names
"""
for line in files.splitlines():
file_contents = line.strip()
if file_contents:
file_name, _, contents = file_contents.partition(":")
file_path = tmp_path / file_name.strip()
# Create parent directories if needed
if not file_path.parent.is_dir():
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(contents.strip() or "content")
yield file_path.relative_to(tmp_path)
def _setup_test_filesystem(tmp_path: Path) -> set[Path]:
"""
Set up a test filesystem with various files and directories for testing.
"""
return set(
_mk_files(
tmp_path,
"""
README.md
setup.py
pyproject.toml
src/__init__.py
src/main.py
src/utils.py
tests/test_main.py
tests/test_utils.py
tests/tmp.py
docs/index.md
docs/api.rst
temp.tmp: temporary file
debug.log: log file
cache.db: cache file
local_ignored_file.txt
__pycache__/test.pyc
.git/config
.gitignore: *tmp*
.git/info/exclude: local_ignored_file.txt
nested_dir/not_ignored.txt: not ignored file
""",
)
)
@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")
local_ignored_file = Path("local_ignored_file.txt")
Path(".git/info").mkdir(parents=True)
Path(".git/info/exclude").write_text(f"{local_ignored_file}\n")
nested_dir = Path("nested_dir")
nested_dir.mkdir()
nested_dir.joinpath("not_ignored.txt").write_text("content")
nested_dir.joinpath("ignored.txt").write_text("content")
nested_dir.joinpath(".gitignore").write_text("ignored.txt")
nested_dir.joinpath("more").mkdir()
nested_dir.joinpath("more/ignored.txt").write_text("content")
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 set(each_unignored_file(Path())) == {
gitignore,
exposed_symlink / "file2",
file1,
file2,
nested_dir / "not_ignored.txt",
nested_dir / ".gitignore",
}
def test_dot_git_is_a_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test that each_unignored_file() does not crash when .git is a file (e.g.,
if the build is being run in a submodule)
"""
monkeypatch.chdir(tmp_path)
# Create a file named .git
git = Path(".git")
git.write_text("gitdir: ../../.git/modules/foo")
# If this throws an exception, the test will fail
assert list(each_unignored_file(Path())) == []
def test_include_patterns(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test that include patterns work correctly and override excludes.
"""
monkeypatch.chdir(tmp_path)
_setup_test_filesystem(tmp_path)
# Test including only Python files
result = set(each_unignored_file(Path(), include=["*.py"]))
expected = {
Path(s)
for s in [
".gitignore",
"README.md",
"cache.db",
"debug.log",
"docs/api.rst",
"docs/index.md",
"nested_dir/not_ignored.txt",
"pyproject.toml",
"setup.py",
"src/__init__.py",
"src/main.py",
"src/utils.py",
"tests/test_main.py",
"tests/test_utils.py",
"tests/tmp.py",
]
}
assert result == expected
# Test including specific files
result = set(each_unignored_file(Path(), include=["tests/tmp.py"]))
assert result == expected | {Path("tests/tmp.py")}
# Test including with wildcards
result = set(each_unignored_file(Path(), include=["tests/*"]))
expected = expected | {Path("tests/tmp.py")}
assert result == expected
def test_exclude_patterns(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test that exclude patterns work correctly.
"""
monkeypatch.chdir(tmp_path)
_setup_test_filesystem(tmp_path)
# Test excluding specific file types
result = set(each_unignored_file(Path(), exclude=["*.tmp", "*.log"]))
expected = {
Path(s)
for s in [
".gitignore",
"README.md",
"cache.db",
"docs/api.rst",
"docs/index.md",
"nested_dir/not_ignored.txt",
"pyproject.toml",
"setup.py",
"src/__init__.py",
"src/main.py",
"src/utils.py",
"tests/test_main.py",
"tests/test_utils.py",
]
}
assert result == expected
# Test excluding directories
result = set(each_unignored_file(Path(), exclude=["tests/"]))
expected = {
Path(s)
for s in [
".gitignore",
"README.md",
"cache.db",
"debug.log",
"docs/api.rst",
"docs/index.md",
"nested_dir/not_ignored.txt",
"pyproject.toml",
"setup.py",
"src/__init__.py",
"src/main.py",
"src/utils.py",
]
}
assert result == expected
# Test excluding with wildcards
result = set(each_unignored_file(Path(), exclude=["*.py"]))
expected = {
Path(s)
for s in [
".gitignore",
"README.md",
"cache.db",
"debug.log",
"docs/api.rst",
"docs/index.md",
"nested_dir/not_ignored.txt",
"pyproject.toml",
]
}
assert result == expected
def test_include_overrides_exclude(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""
Test that include patterns override exclude patterns.
"""
monkeypatch.chdir(tmp_path)
_setup_test_filesystem(tmp_path)
# Exclude all files but include specific ones
result = set(
each_unignored_file(
Path(), include=["src/main.py", "tests/test_main.py"], exclude=["*"]
)
)
expected = {Path(s) for s in ["src/main.py", "tests/test_main.py"]}
assert result == expected
# Exclude everything but include a file from inside a directory
result = set(
each_unignored_file(Path(), include=["tests/test_main.py"], exclude=["*"])
)
expected = {Path(s) for s in ["tests/test_main.py"]}
assert result == expected
def test_gitignore_interaction(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test interaction between include/exclude and gitignore files.
"""
monkeypatch.chdir(tmp_path)
_setup_test_filesystem(tmp_path)
# Create .gitignore that excludes some files
gitignore = tmp_path / ".gitignore"
gitignore.write_text("*.tmp\n*.log\n/cache.db\n")
# Test that gitignore is respected by default
result = set(each_unignored_file(Path()))
expected = {
Path(s)
for s in [
".gitignore",
"README.md",
"pyproject.toml",
"setup.py",
"src/__init__.py",
"src/main.py",
"src/utils.py",
"tests/test_main.py",
"tests/test_utils.py",
"tests/tmp.py",
"docs/index.md",
"docs/api.rst",
"nested_dir/not_ignored.txt",
]
}
assert result == expected
# Test that include can override gitignore
result = set(each_unignored_file(Path(), include=["*.tmp"]))
assert result == expected | {Path("temp.tmp")}
def test_nested_gitignore(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test handling of nested .gitignore files.
"""
monkeypatch.chdir(tmp_path)
_setup_test_filesystem(tmp_path)
# Create nested .gitignore in src directory
src_gitignore = tmp_path / "src" / ".gitignore"
src_gitignore.write_text("utils.py\n")
# Test that nested gitignore is respected
result = set(each_unignored_file(Path()))
expected = {
Path(s)
for s in [
".gitignore",
"README.md",
"cache.db",
"debug.log",
"docs/api.rst",
"docs/index.md",
"nested_dir/not_ignored.txt",
"pyproject.toml",
"setup.py",
"src/.gitignore",
"src/__init__.py",
"src/main.py",
"tests/test_main.py",
"tests/test_utils.py",
]
}
assert result == expected
# Test that include can override nested gitignore
result = set(each_unignored_file(Path(), include=["src/utils.py"]))
assert result == expected | {Path("src/utils.py")}
def test_build_dir_exclusion(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test that build_dir parameter correctly excludes build directories.
"""
monkeypatch.chdir(tmp_path)
_setup_test_filesystem(tmp_path)
# Create build directory
build_dir = tmp_path / "build"
build_dir.mkdir()
build_file = build_dir / "output.so"
build_file.write_text("compiled output")
# Test that build directory is excluded when specified
result = set(each_unignored_file(Path(), build_dir="build"))
expected = {
Path(s)
for s in [
".gitignore",
"README.md",
"cache.db",
"debug.log",
"docs/api.rst",
"docs/index.md",
"nested_dir/not_ignored.txt",
"pyproject.toml",
"setup.py",
"src/__init__.py",
"src/main.py",
"src/utils.py",
"tests/test_main.py",
"tests/test_utils.py",
]
}
assert result == expected
assert build_file.relative_to(tmp_path) not in result
# Test that include can override build_dir exclusion
result = set(each_unignored_file(Path(), include=["build/*"], build_dir="build"))
assert result == expected | {Path("build/output.so")}
def test_complex_combinations(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test complex combinations of include, exclude, gitignore, and build_dir.
"""
monkeypatch.chdir(tmp_path)
_setup_test_filesystem(tmp_path)
# Set up complex scenario
gitignore = tmp_path / ".gitignore"
gitignore.write_text("*.tmp\ndebug.log\n")
build_dir = tmp_path / "_build"
build_dir.mkdir()
build_file = build_dir / "lib.so"
build_file.write_text("build output")
# Complex pattern: exclude tests, include specific test, respect gitignore, exclude build
result = set(
each_unignored_file(
Path(),
include=[
"tests/test_main.py",
"*.tmp",
], # Include specific test and override gitignore for .tmp
exclude=["*"], # Exclude tests dir and rst files
build_dir="_build",
)
)
expected = {
Path(s) for s in ["tests/test_main.py", "temp.tmp"]
} # Only these should match
assert result == expected
def test_empty_directory(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test behavior with empty directory.
"""
monkeypatch.chdir(tmp_path)
result = list(each_unignored_file(Path()))
assert result == []
result = list(each_unignored_file(Path(), include=["*.py"]))
assert result == []
result = list(each_unignored_file(Path(), exclude=["*.py"]))
assert result == []
def test_nonexistent_patterns(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test behavior with patterns that don't match any files.
"""
monkeypatch.chdir(tmp_path)
_setup_test_filesystem(tmp_path)
# Include pattern that matches nothing
include_result = list(
each_unignored_file(Path(), exclude=["*"], include=["*.nonexistent"])
)
assert include_result == []
# Exclude pattern that matches nothing
exclude_result = set(each_unignored_file(Path(), exclude=["*.nonexistent"]))
expected = {
Path(s)
for s in [
".gitignore",
"README.md",
"cache.db",
"debug.log",
"docs/api.rst",
"docs/index.md",
"nested_dir/not_ignored.txt",
"pyproject.toml",
"setup.py",
"src/__init__.py",
"src/main.py",
"src/utils.py",
"tests/test_main.py",
"tests/test_utils.py",
]
}
assert exclude_result == expected
|