TestableIO / System.IO.Abstractions

Just like System.Web.Abstractions, but for System.IO. Yay for testable IO access!
http://www.nuget.org/packages/System.IO.Abstractions
MIT License
1.51k stars 254 forks source link

PathVerifier fails on whitespace filename under Linux and .NET Core #1070

Open AnakinRaW opened 9 months ago

AnakinRaW commented 9 months ago

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

fgreinacher commented 9 months ago

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