System.IO.Abstractions icon indicating copy to clipboard operation
System.IO.Abstractions copied to clipboard

PathVerifier fails on whitespace filename under Linux and .NET Core

Open AnakinRaW opened this issue 1 year ago • 3 comments

Describe the bug When using FileSystem and MockFileSystem I noticed there is different behavior when creating a new IFileInfo using only whitespaces. The "error" trails down to the PathVerifier that the MockFileSystem uses

To Reproduce


// Both throw in Linux and windows
var mfs = new MockFileSystem();
mfs.FileInfo.New("   "); // ASCII space
mfs.FileInfo.New("\u00A0"); // Unicode char that's treated as whitespace

// Both pass in Linux
mfs.FileInfo.New("./   ") // Explicit relative path 
mfs.FileInfo.New("/   ") // Explicit absolute path 

// Passes in Linux 
var fs = new FileSystem();
fs.FileInfo.New("   "); // ASCII space

// Passes in Linux and Windows (.NET Core); throws on NET4.8
fs.FileInfo.New("\u00A0"); // Unicode char that's treated as whitespace

Expected behavior Both calls throw an exception when they should not. For Linux (whitespace) is a valid file name For Windows and Linux \u00A0 is a valid file name. However under .NET Framework it also fails

Additional context In PathInternal.OS.cs .NET has a bool IsEffectivelyEmpty(string? path) method which behaves differently to linux and windows, just to cover those cases.

Currently PathVerifier uses the string.Trim() method, which causes the two explained scenarios

  • valid unicode chars get trimmed (\u00A0)
  • a whitespace-only relative path is considered to be illegal when it actually is not under linux.

AnakinRaW avatar Dec 25 '23 10:12 AnakinRaW

Thanks for reporting! Happy to accept a PR to fix this 🙇

fgreinacher avatar Jan 09 '24 07:01 fgreinacher