fix: name the Unix root drive `/` instead of `/:\`

Merged
MO
16d ago
+24-1

Fixes #927

On Linux and macOS, MockFileSystem.DriveInfo.GetDrives() returned the root twice, both named /:\, and DriveInfo.New("/").Name was /:\. The real DriveInfo gives /.

What was wrong

PathVerifier.TryNormalizeDriveName has a shortcut for one-character names: it treats the character as a drive letter and appends :\, so / became /:\. The root then got registered twice: as / for paths like /foo/bar.txt (through GetPathRoot), and as /:\ for the / directory entry itself (through the shortcut). MockDriveInfo named both of them /:\, and RootDirectory of that drive pointed at /:.

What changed

The one-character shortcut now only applies to letters, using char.IsLetter like TrimSlashes and IsValidUseOfVolumeSeparatorChar already do for drive letters. / now goes through GetPathRoot like longer paths and stays /. Both registrations end up under the same key, so GetDrives() returns a single drive named /.

Drive letters (c, c:, c:\) still take the shortcut, and UNC and \\?\ paths never reached it, so Windows drives are handled as before. The only other inputs that change are single non-letter characters passed in directly. They now go through GetPathRoot as well, so 1 throws ArgumentException (as the real DriveInfo does on Windows) instead of becoming 1:\, and on Windows \ stays \ instead of becoming \:\.

How it's tested

I added two Unix-only tests to MockFileSystemTests, next to the Windows-only MockFileSystem_Constructor_ShouldNotDuplicateDrives:

  • after adding /foo/bar.txt, GetDrives() returns exactly one drive, named /
  • DriveInfo.New("/").Name is /

Both fail on main and pass with the fix. They aren't in MockDriveInfoFactoryTests because that whole class is Windows-only.

I ran this on Linux in the mcr.microsoft.com/dotnet/sdk:11.0.100 container, with the .NET 10, 9 and 8 runtimes 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.

I couldn't run the Windows tests or net472. The existing Windows-only drive tests (MockDriveInfoTests, MockDriveInfoFactoryTests and the drive tests in MockFileSystemTests) cover that path, so CI on Windows should confirm it is unchanged.