Repository navigation
fix: accept whitespace-only names on Linux and macOS - #1608
Open
mohsinaliwork686-rgb wants to merge 1 commit into
Open
mohsinaliwork686-rgb wants to merge 1 commit into
mohsinaliwork686-rgb wants to merge 1 commit into
Conversation
Contributor
|
Tick the box to add this pull request to the merge queue (same as
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1070
On Linux and macOS,
MockFileSystemrejected whitespace-only names like" "withArgumentException: The path is not of a legal form., inFileInfo.New,DirectoryInfo.New,File.WriteAllText,Path.GetFullPath,Path.GetRelativePathand otherFilemethods, andFile.Existsalways returnedfalsefor them. The real file system on Linux and macOS accepts these names.What was wrong
The mock checks
path.Trim() == string.Empty(orstring.IsNullOrWhiteSpace) on every platform. In .NET,PathInternal.IsEffectivelyEmptyonly 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.IsLegalAbsoluteOrRelativeMockPath.GetFullPathandMockPath.GetRelativePath(forrelativeTo)MockFile.ExistsMockDirectoryInfoconstructorOn Windows the helper does exactly what the old checks did, so nothing changes there. Each call site already handled
nullbefore 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, theGet*Time*methods,FileandDirectory.CreateSymbolicLink,Path.GetFullPathandGetRelativePath. They now have[WindowsOnly(WindowsSpecifics.StrictPathRules)], like the invalid-character tests next to them. TheSetAttributesone also had an""case, so I moved that into its own test, which still runs everywhere.New tests (with a new
UnixSpecifics.WhitespaceOnlyNamesreason):MockFileInfoTests, Unix only:FileInfo.New(" "), write the file, and find it again throughFileInfo.Exists,File.Exists,File.ReadAllTextandDirectory.GetFilesMockFileInfoTests, Windows only:FileInfo.New(" ")still throwsMockDirectoryInfoTests, Unix only:DirectoryInfo.New(" ").Create()works andDirectory.Exists(" ")istrueMockPathTests, Unix only:GetRelativePathwith a whitespace-onlyrelativeToorpathThe Unix tests fail on
mainand pass with the fix.I ran this on Linux in the
mcr.microsoft.com/dotnet/sdk:11.0.100container, 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 pastPath.GetFullPaththere (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?