Skip to content

Exclude directories, not just files, in ExcludePath#2885

Merged
nohwnd merged 1 commit into
mainfrom
nohwnd-fix-excludepath-directories
Jul 17, 2026
Merged

Exclude directories, not just files, in ExcludePath#2885
nohwnd merged 1 commit into
mainfrom
nohwnd-fix-excludepath-directories

Conversation

@nohwnd

@nohwnd nohwnd commented Jul 17, 2026

Copy link
Copy Markdown
Member

Fix #1575

Problem

Run.ExcludePath is documented as "Directories or files to be excluded from the run.", but directory exclusions were silently ignored — only file paths and wildcard patterns actually excluded anything.

The cause is in Filter-Excluded (src/Pester.RSpec.ps1): each discovered test file's full path was only tested against every exclusion with -like. When the exclusion is a directory (e.g. C:\proj\excluded), no file inside that directory matches, because -like needs an exact/glob match against the whole file path. So Invoke-Pester -Path <root> -ExcludePath <root>\excluded still ran the tests under excluded.

Fix

Keep the existing wildcard/exact -like behavior, and additionally treat a non-wildcard exclusion as a directory prefix: a file is excluded when its normalized full path starts with the exclusion plus a directory separator.

  • Trailing separators are trimmed, so dir and dir\ behave the same.
  • The prefix comparison is case-insensitive on Windows and case-sensitive on Linux/macOS (matching the filesystem).
  • Wildcard exclusions (e.g. */excluded/*) are left entirely to -like, so existing glob behavior is unchanged.

Tests

Added a regression block in tst/Pester.RSpec.ts.ps1 (Excluding directories from a run) with a two-subdirectory layout, asserting that:

  • excluding a directory removes the tests underneath it while a sibling directory still runs, and
  • the same works when the excluded path has a trailing separator.

Both fail on main and pass with this change. The rest of Pester.RSpec.ts.ps1 is unaffected (the 3 remaining failures in that file are pre-existing and unrelated — SkipRemainingOnFailureCount). PSScriptAnalyzer with the repo settings reports no new findings on the changed lines.

Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com

Filter-Excluded only matched discovered test files against each
ExcludePath entry with -like, so a directory path never matched any
file inside it and directory exclusions were silently ignored, even
though Run.ExcludePath is documented as "Directories or files to be
excluded from the run."

Keep the existing wildcard/exact -like matching and additionally treat
a non-wildcard exclusion as a directory prefix: a file is excluded when
its full path starts with the exclusion plus a directory separator.
Trailing separators are trimmed so 'dir' and 'dir\' behave the same,
and the prefix comparison is case-insensitive on Windows and
case-sensitive on Linux/macOS.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd added this to the 6.1.0 milestone Jul 17, 2026
@nohwnd
nohwnd merged commit 70355ab into main Jul 17, 2026
13 checks passed
@nohwnd
nohwnd deleted the nohwnd-fix-excludepath-directories branch July 17, 2026 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v5] ExcludePath argument and configuration item don't work for directories, only files

1 participant