Drives
MockFileSystem supports multiple drives with independent quotas. This is most useful when simulating Windows, where drive letters carry meaning, but the main drive can also be configured on non-Windows hosts.
Adding a drive
MockFileSystem fileSystem = new();
fileSystem
.WithDrive("D:", d => d.SetTotalSize(1024 * 1024)) // 1 MiB total
.InitializeIn("D:")
.WithFile("inventory.txt")
.WithSubdirectory("vault").Initialized(s => s
.WithAFile(".json").Which(
f => f.HasStringContent("{\"artworks\":1}")));
This adds a D: drive limited to 1 MiB and seeds it with an empty inventory.txt and a vault sub-directory containing one randomly named JSON file.
Limiting the main drive
WithDrive without an argument configures the default drive (works on any OS):
MockFileSystem fileSystem = new();
fileSystem.WithDrive(d => d.SetTotalSize(20));
// Throws IOException: not enough space on the disk
fileSystem.File.WriteAllText("catalog.txt", "a catalogue longer than 20 bytes");
When a drive runs out of space, every file operation that writes content throws IOException, exactly as on the real system.
AvailableFreeSpace tracks every byte written, appended or freed:
MockFileSystem fileSystem = new MockFileSystem()
.WithDrive(d => d.SetTotalSize(100));
IDriveInfo drive = fileSystem.GetDefaultDrive();
await That(drive.AvailableFreeSpace).IsEqualTo(100);
fileSystem.File.WriteAllText("catalog.txt", "A catalogue entry of 29 bytes");
await That(drive.AvailableFreeSpace).IsEqualTo(71);
fileSystem.File.AppendAllText("catalog.txt", "And 17 more bytes");
await That(drive.AvailableFreeSpace).IsEqualTo(54);
void WriteTooMuch() => fileSystem.File.WriteAllBytes("red-square.png", new byte[90]);
await That(WriteTooMuch).Throws<IOException>();
await That(drive.AvailableFreeSpace).IsEqualTo(54)
.Because("the failed write did not consume space");
fileSystem.File.Delete("catalog.txt");
await That(drive.AvailableFreeSpace).IsEqualTo(100);
UNC drives
WithUncDrive("server") registers a Universal Naming Convention (UNC) server, so paths like \\server\share\... resolve in the mock. Like a local drive, it starts empty, so create the share directory before writing into it:
MockFileSystem fileSystem = new();
fileSystem.WithUncDrive("build-server");
fileSystem.Directory.CreateDirectory("//build-server/share");
fileSystem.File.WriteAllText("//build-server/share/catalog.json", "[]");
IDriveInfo drive = fileSystem.DriveInfo.New("//build-server");
await That(drive.IsReady).IsTrue();
UNC drives don't appear in DriveInfo.GetDrives(): they're addressable but not enumerated, matching the real behavior.
Inspecting drives
fileSystem.DriveInfo.GetDrives() returns the registered drives. The objects are full IDriveInfo instances and report TotalSize, AvailableFreeSpace, DriveFormat, VolumeLabel and so on.
fileSystem.GetDefaultDrive() returns the drive backing the current working directory, which is convenient when you need to assert against the same drive MockFileSystem is using by default.
Mutating a drive at runtime
The callback passed to WithDrive(...) receives an IStorageDrive whose setters keep working after the test has started, so you can flip a drive offline mid-test or change its format/type without recreating the file system:
| Setter | What it changes |
|---|---|
SetTotalSize(long) | Total disk capacity (defaults to 1 GB) |
SetDriveFormat(string) | The reported DriveFormat (e.g. "NTFS", "ext4") |
SetDriveType(DriveType) | The reported DriveType (Fixed, Removable, Network, …) |
SetIsReady(bool) | Whether the drive reports itself as ready; simulates a removable drive ejecting |
ChangeUsedBytes(long delta) | Mark space as used (or free) without writing files. Throws IOException if free space goes negative |
MockFileSystem fileSystem = new();
fileSystem.WithDrive("E:", d => d.SetDriveType(DriveType.Removable));
// later in the test: simulate the user yanking the USB stick
IDriveInfo e = fileSystem.DriveInfo.New("E:");
((IStorageDrive)e).SetIsReady(false);