diff --git a/scripts/powershell/common.ps1 b/scripts/powershell/common.ps1 index f56fc26577..5161fce1ad 100644 --- a/scripts/powershell/common.ps1 +++ b/scripts/powershell/common.ps1 @@ -209,7 +209,13 @@ function Test-FileExists { function Test-DirHasFiles { param([string]$Path, [string]$Description) - if ((Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -ErrorAction SilentlyContinue | Where-Object { -not $_.PSIsContainer } | Select-Object -First 1)) { + # A directory counts as non-empty when Get-ChildItem returns any entry + # (files or subdirectories) — matching the JSON contracts checks in + # check-prerequisites.ps1 / setup-tasks.ps1, and treating a directory whose + # only contents are subdirectories (e.g. contracts/v1/openapi.yaml) as + # non-empty like bash check_dir. Filtering out subdirectories would + # mis-report such a directory as empty. + if ((Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -ErrorAction SilentlyContinue | Select-Object -First 1)) { Write-Output " [OK] $Description" return $true } else { diff --git a/tests/test_setup_tasks.py b/tests/test_setup_tasks.py index 0e3fb85f41..25faa1ed22 100644 --- a/tests/test_setup_tasks.py +++ b/tests/test_setup_tasks.py @@ -840,3 +840,49 @@ def test_setup_tasks_ps_errors_without_feature_context( output = result.stderr + result.stdout assert result.returncode != 0 assert "Feature directory not found" in output + + +# --------------------------------------------------------------------------- +# Directory non-emptiness parity: a dir whose only contents are subdirectories +# (e.g. contracts/v1/openapi.yaml) must count as non-empty in both shells. +# --------------------------------------------------------------------------- + +def _run_bash_check_dir(repo: Path, target: Path) -> subprocess.CompletedProcess: + script = repo / ".specify" / "scripts" / "bash" / "common.sh" + return subprocess.run( + ["bash", "-c", 'source "$1"; check_dir "$2" "contracts/"', "bash", str(script), str(target)], + cwd=repo, capture_output=True, text=True, check=False, env=_clean_env(), + ) + + +def _run_powershell_test_dir(repo: Path, target: Path) -> subprocess.CompletedProcess: + script = repo / ".specify" / "scripts" / "powershell" / "common.ps1" + exe = "pwsh" if HAS_PWSH else _WINDOWS_POWERSHELL + return subprocess.run( + [exe, "-NoProfile", "-Command", + '& { param($common, $dir) . $common; Test-DirHasFiles -Path $dir -Description "contracts/" }', + str(script), str(target)], + cwd=repo, capture_output=True, text=True, check=False, env=_clean_env(), + ) + + +@requires_bash +def test_check_dir_bash_counts_subdir_only_contracts(tasks_repo: Path) -> None: + """bash check_dir treats a dir containing only subdirectories as non-empty.""" + contracts = tasks_repo / "contracts" / "v1" + contracts.mkdir(parents=True) + (contracts / "openapi.yaml").write_text("openapi: 3.0\n", encoding="utf-8") + result = _run_bash_check_dir(tasks_repo, tasks_repo / "contracts") + assert result.returncode == 0, result.stderr + assert "✓" in result.stdout and "✗" not in result.stdout + + +@pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available") +def test_dir_has_files_ps_counts_subdir_only_contracts(tasks_repo: Path) -> None: + """Test-DirHasFiles must match bash: a subdir-only dir counts as non-empty.""" + contracts = tasks_repo / "contracts" / "v1" + contracts.mkdir(parents=True) + (contracts / "openapi.yaml").write_text("openapi: 3.0\n", encoding="utf-8") + result = _run_powershell_test_dir(tasks_repo, tasks_repo / "contracts") + assert result.returncode == 0, result.stderr + assert "[OK]" in result.stdout and "[FAIL]" not in result.stdout