Skip to content

fix: accept whitespace-only names on Linux and macOS - #1608

Open
mohsinaliwork686-rgb wants to merge 1 commit into
TestableIO:mainfrom
mohsinaliwork686-rgb:fix/whitespace-names-on-unix
Open

mohsinaliwork686-rgb wants to merge 1 commit into
TestableIO:mainfrom
mohsinaliwork686-rgb:fix/whitespace-names-on-unix

Conversation

@mohsinaliwork686-rgb

Copy link
Copy Markdown
Contributor

Fixes #1070

On Linux and macOS, MockFileSystem rejected whitespace-only names like " " with ArgumentException: The path is not of a legal form., in FileInfo.New, DirectoryInfo.New, File.WriteAllText, Path.GetFullPath, Path.GetRelativePath and other File methods, and File.Exists always returned false for them. The real file system on Linux and macOS accepts these names.

What was wrong

The mock checks path.Trim() == string.Empty (or string.IsNullOrWhiteSpace) on every platform. In .NET, PathInternal.IsEffectivelyEmpty only treats a whitespace-only path as empty on Windows. On Unix only an empty path is.

What changed

Following the suggestion on the issue to run the check only on Windows, there's a new internal helper, PathVerifier.IsEffectivelyEmpty. An empty path is always effectively empty, and a whitespace-only path is too, but only on Windows. It replaces the whitespace check in the five places that had it:

  • PathVerifier.IsLegalAbsoluteOrRelative
  • MockPath.GetFullPath and MockPath.GetRelativePath (for relativeTo)
  • MockFile.Exists
  • the MockDirectoryInfo constructor

On Windows the helper does exactly what the old checks did, so nothing changes there. Each call site already handled null before this check, and empty paths are still rejected everywhere.

How it's tested

22 existing tests (32 cases) asserted the Windows rule on every platform: whitespace-only paths in File.Create, Delete, Copy, Move, AppendAllLines, SetAttributes, the Get*Time* methods, File and Directory.CreateSymbolicLink, Path.GetFullPath and GetRelativePath. They now have [WindowsOnly(WindowsSpecifics.StrictPathRules)], like the invalid-character tests next to them. The SetAttributes one also had an "" case, so I moved that into its own test, which still runs everywhere.

New tests (with a new UnixSpecifics.WhitespaceOnlyNames reason):

  • MockFileInfoTests, Unix only: FileInfo.New(" "), write the file, and find it again through FileInfo.Exists, File.Exists, File.ReadAllText and Directory.GetFiles
  • MockFileInfoTests, Windows only: FileInfo.New(" ") still throws
  • MockDirectoryInfoTests, Unix only: DirectoryInfo.New(" ").Create() works and Directory.Exists(" ") is true
  • MockPathTests, Unix only: GetRelativePath with a whitespace-only relativeTo or path

The Unix tests fail on main and pass with the fix.

I ran this on Linux in the mcr.microsoft.com/dotnet/sdk:11.0.100 container, with the .NET 10, 9 and 8 SDKs added. The Release build of the solution has no warnings or errors. The Wrappers, TestingHelpers and Parity tests all pass on net11.0, net10.0, net9.0 and net8.0, and the API tests pass too. On macOS, the TestingHelpers, Wrappers and Parity tests also pass on net10.0, net9.0 and net8.0 (there is no .NET 11 SDK on that machine).

I couldn't run the Windows tests or net472. On Windows the helper behaves like the old checks, so the existing tests, which now run only there, cover it.

One question I left alone: on Windows, .NET (Core) only treats spaces as empty, so "\u00A0" or "\t" get past Path.GetFullPath there (the issue mentions "\u00A0"), while .NET Framework rejects every kind of whitespace. The mock keeps rejecting all whitespace on Windows, which matches .NET Framework. Should I narrow that to spaces on .NET (Core), or leave it for a separate change?

@mergify

mergify Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

This branch has not been deployed

No deployments
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.

PathVerifier fails on whitespace filename under Linux and .NET Core

1 participant