Skip to main content

File system

Describes the possible expectations for the files, directories and drives of a MockFileSystem, either through the IFileSystem or directly on an IFileInfo, IDirectoryInfo, IDriveInfo or IFileVersionInfo.

Expectations for IFileSystem:

ExpectationNegatedSummary
HasFileDoesNotHaveFilecontains the file, optionally with its content and timestamps
HasDirectoryDoesNotHaveDirectorycontains the directory, optionally with its files and subdirectories
HasDriveDoesNotHaveDrivecontains the drive

Expectations for IFileInfo:

ExpectationNegatedSummary
ExistsDoesNotExistthe file exists
HasNamehas the expected name
HasExtensionhas the expected extension
HasLengthhas the expected length
HasContentHasContent().NotEqualTohas the expected string or binary content
IsReadOnlyIsNotReadOnlythe file is read-only
HasAttributeDoesNotHaveAttributehas the attribute flag
HasCreationTimehas the expected creation time, optionally within a tolerance
HasLastAccessTimehas the expected last access time, optionally within a tolerance
HasLastWriteTimehas the expected last write time, optionally within a tolerance
WhoseParentthe containing directory meets expectations (.NET 10 or later)

Expectations for IDirectoryInfo:

ExpectationNegatedSummary
ExistsDoesNotExistthe directory exists
HasNamehas the expected name
IsEmptyIsNotEmptycontains no files or subdirectories
HasFileDoesNotHaveFilecontains the file
HasDirectoryDoesNotHaveDirectorycontains the subdirectory
HasAttributeDoesNotHaveAttributehas the attribute flag
HasCreationTimehas the expected creation time, optionally within a tolerance
HasLastAccessTimehas the expected last access time, optionally within a tolerance
HasLastWriteTimehas the expected last write time, optionally within a tolerance
WhoseParentthe parent directory meets expectations (.NET 10 or later)

Expectations for IDriveInfo:

ExpectationNegatedSummary
IsReadyIsNotReadythe drive is ready
HasNamehas the expected name
HasVolumeLabelhas the expected volume label
HasDriveFormathas the expected drive format
HasDriveTypehas the expected drive type
HasTotalSizehas the expected total size
HasTotalFreeSpacehas the expected total free space
HasAvailableFreeSpacehas the expected available free space

Expectations for IFileVersionInfo:

ExpectationNegatedSummary
HasCompanyName, HasFileMajorPart, IsDebug, …IsNotDebug, …one expectation per property

The samples on this page use the following file system:

using aweXpect.Chronology; // from the aweXpect.Chronology package
using static aweXpect.Expect;

MockFileSystem fileSystem = new(o => o.SimulatingOperatingSystem(SimulationMode.Windows));
fileSystem.WithDrive("D:", d => d.SetTotalSize(2048));
fileSystem.Directory.CreateDirectory("beatles/abbey-road");
fileSystem.Directory.CreateDirectory("beatles/revolver");
fileSystem.File.WriteAllText("beatles/abbey-road/something.txt", "something");
fileSystem.File.WriteAllText("let-it-be.txt", "let it be");
fileSystem.File.WriteAllText("let-it-be-remastered.txt", "LET IT BE");
fileSystem.File.WriteAllText("hey-jude.txt", "hey jude");
fileSystem.File.SetAttributes("hey-jude.txt", FileAttributes.ReadOnly);

Files, directories and drives​

You can verify that a file, directory or drive exists in an IFileSystem, or that it does not:

await That(fileSystem).HasDirectory("beatles/abbey-road");
await That(fileSystem).HasFile("let-it-be.txt");
await That(fileSystem).HasDrive("D:\\");

await That(fileSystem).DoesNotHaveDirectory("beatles/white-album");
await That(fileSystem).DoesNotHaveFile("yesterday.txt");
await That(fileSystem).DoesNotHaveDrive("E:\\");

For example, That(fileSystem).HasFile("yesterday.txt") fails with:

Failure message
Expected that fileSystem
has file "yesterday.txt",
but it did not exist

HasDrive matches the drive name case-insensitively against IFileSystem.DriveInfo.GetDrives(). UNC drives are not returned by GetDrives() and are therefore not supported by HasDrive.

File content and timestamps​

You can verify the content of the file found by HasFile(path):

await That(fileSystem).HasFile("let-it-be.txt").WithContent("LET IT BE").IgnoringCase();
await That(fileSystem).HasFile("let-it-be.txt").WithContent().NotEqualTo("let it go");
await That(fileSystem).HasFile("let-it-be.txt").WithContent(Encoding.UTF8.GetBytes("let it be"));
await That(fileSystem).HasFile("let-it-be.txt").WhoseContent(c => c.StartsWith("let it"));

The string content supports the same options as comparing strings, and WhoseContent accepts any string expectation. When the content differs, the failure message shows where and includes the file content:

Failure message
Expected that fileSystem
has file "let-it-be.txt" with content equal to "let it go",
but it was "let it be", which differs at index 7:
↓ (actual)
"let it be"
"let it go"
↑ (expected)

File content:
let it be

You can also compare the content with another file on the same file system:

await That(fileSystem).HasFile("let-it-be.txt").WithContent().SameAs("let-it-be-remastered.txt").IgnoringCase();
await That(fileSystem).HasFile("let-it-be.txt").WithContent().NotSameAs("hey-jude.txt");

You can verify the timestamps of the file, and widen the comparison to a window with .Within(tolerance):

await That(fileSystem).HasFile("let-it-be.txt").WithCreationTime(DateTime.Now).Within(1.Seconds());
await That(fileSystem).HasFile("let-it-be.txt").WithLastAccessTime(DateTime.Now).Within(1.Seconds());
await That(fileSystem).HasFile("let-it-be.txt").WithLastWriteTime(DateTime.Now).Within(1.Seconds());

Directory content​

You can verify the files and subdirectories of the directory found by HasDirectory(path):

await That(fileSystem).HasDirectory("beatles").WithDirectories(d => d.HasCount().EqualTo(2));
await That(fileSystem).HasDirectory("beatles/abbey-road").WithFiles(f => f
.All().ComplyWith(x => x.HasContent("SOMETHING").IgnoringCase()));

Continuing with the file, directory or drive​

HasFile, HasDirectory and HasDrive each have a .Which property that continues with the found IFileInfo, IDirectoryInfo or IDriveInfo, so that the expectations of the following sections can be used in the same chain:

await That(fileSystem).HasFile("let-it-be.txt").Which.HasLength(9).And.HasContent("let it be");
await That(fileSystem).HasDirectory("beatles/revolver").Which.IsEmpty();
await That(fileSystem).HasDrive("D:\\").Which.IsReady().And.HasDriveFormat("NTFS");

File​

You can verify the properties of an IFileInfo:

IFileInfo fileInfo = fileSystem.FileInfo.New("let-it-be.txt");
IFileInfo readOnlyFileInfo = fileSystem.FileInfo.New("hey-jude.txt");

await That(fileInfo).Exists();
await That(fileSystem.FileInfo.New("yesterday.txt")).DoesNotExist();

await That(fileInfo).HasName("let-it-be.txt");
await That(fileInfo).HasExtension(".txt");
await That(fileInfo).HasLength(9);

await That(readOnlyFileInfo).IsReadOnly();
await That(fileInfo).IsNotReadOnly();

await That(readOnlyFileInfo).HasAttribute(FileAttributes.ReadOnly);
await That(fileInfo).DoesNotHaveAttribute(FileAttributes.ReadOnly);

await That(fileInfo).HasCreationTime(DateTime.Now).Within(1.Seconds());
await That(fileInfo).HasLastAccessTime(DateTime.Now).Within(1.Seconds());
await That(fileInfo).HasLastWriteTime(DateTime.Now).Within(1.Seconds());

For example, That(fileInfo).HasLength(10) fails with:

Failure message
Expected that fileInfo
has length 10,
but it was 9

You can verify the content of the file, or compare it with another file on the same file system, like with HasFile:

await That(fileInfo).HasContent("let it be");
await That(fileInfo).HasContent(Encoding.UTF8.GetBytes("let it be"));
await That(fileInfo).HasContent().NotEqualTo("let it go");
await That(fileInfo).HasContent().SameAs("let-it-be-remastered.txt").IgnoringCase();
await That(fileInfo).HasContent().NotSameAs("hey-jude.txt");

HasAttribute and DoesNotHaveAttribute check whether the flag is contained, so FileAttributes.ReadOnly | FileAttributes.Hidden satisfies HasAttribute(FileAttributes.ReadOnly). The empty (default) value throws an ArgumentException, because it would be contained in every value.

.NET 10 or later

WhoseParent continues with the containing directory, so that the directory expectations can be used:

IFileInfo something = fileSystem.FileInfo.New("beatles/abbey-road/something.txt");

await That(something).WhoseParent.HasName("abbey-road").And.IsNotEmpty();

Directory​

You can verify the properties of an IDirectoryInfo:

IDirectoryInfo dirInfo = fileSystem.DirectoryInfo.New("beatles");
IDirectoryInfo emptyDirInfo = fileSystem.DirectoryInfo.New("beatles/revolver");

await That(dirInfo).Exists();
await That(fileSystem.DirectoryInfo.New("beatles/white-album")).DoesNotExist();

await That(dirInfo).HasName("beatles");

await That(emptyDirInfo).IsEmpty();
await That(dirInfo).IsNotEmpty();

await That(dirInfo).HasFile("abbey-road/something.txt");
await That(dirInfo).DoesNotHaveFile("abbey-road/yesterday.txt");
await That(dirInfo).HasDirectory("abbey-road").Which.HasFile("something.txt");
await That(dirInfo).DoesNotHaveDirectory("white-album");

await That(dirInfo).HasAttribute(FileAttributes.Directory);
await That(dirInfo).DoesNotHaveAttribute(FileAttributes.Hidden);

await That(dirInfo).HasCreationTime(DateTime.Now).Within(1.Seconds());
await That(dirInfo).HasLastAccessTime(DateTime.Now).Within(1.Seconds());
await That(dirInfo).HasLastWriteTime(DateTime.Now).Within(1.Seconds());

HasFile and HasDirectory resolve the path relative to the directory and continue like on the file system, so .WithContent(…), .WithLastWriteTime(…), .Which etc. work as well.

.NET 10 or later

WhoseParent continues with the parent directory:

await That(emptyDirInfo).WhoseParent.HasName("beatles");

Drive​

You can verify the properties of an IDriveInfo:

IDriveInfo driveInfo = fileSystem.DriveInfo.New("D:");

await That(driveInfo).HasAvailableFreeSpace(2048);
await That(driveInfo).HasTotalSize(2048).And.HasTotalFreeSpace(2048);
await That(driveInfo).HasDriveFormat("NTFS");
await That(driveInfo).HasDriveType(DriveType.Fixed);
await That(driveInfo).HasName(driveInfo.Name).And.HasVolumeLabel(driveInfo.VolumeLabel);
await That(driveInfo).IsReady();

File version info​

You can verify an IFileVersionInfo obtained via MockFileSystem.FileVersionInfo.GetVersionInfo. Its values come from MockFileSystem.WithFileVersionInfo(glob, builder):

fileSystem.WithFileVersionInfo("*.dll", v => v
.SetCompanyName("Apple Corps")
.SetProductName("Abbey Road")
.SetFileVersion("1.9.6.9")
.SetIsDebug(true));
fileSystem.File.WriteAllText("AbbeyRoad.dll", "");

IFileVersionInfo info = fileSystem.FileVersionInfo.GetVersionInfo("AbbeyRoad.dll");

await That(info).HasCompanyName("Apple Corps").And.HasProductName("Abbey Road");
await That(info).HasFileVersion("1.9.6.9").And.HasFileMajorPart(1);
await That(info).IsDebug().And.IsNotPreRelease();

For example, That(info).HasCompanyName("EMI") fails with:

Failure message
Expected that info
has company name equal to "EMI",
but it was "Apple Corps", which differs at index 0:
↓ (actual)
"Apple Corps"
"EMI"
↑ (expected)

Every IFileVersionInfo property has its own expectation: Has…(string) for the strings, Has…(int) for the version parts and an Is…() / IsNot…() pair for the booleans.

Expectations per property
PropertyExpectation
CommentsHasComments(string)
CompanyNameHasCompanyName(string)
FileDescriptionHasFileDescription(string)
FileNameHasFileName(string)
FileVersionHasFileVersion(string)
InternalNameHasInternalName(string)
LanguageHasLanguage(string)
LegalCopyrightHasLegalCopyright(string)
LegalTrademarksHasLegalTrademarks(string)
OriginalFilenameHasOriginalFilename(string)
PrivateBuildHasPrivateBuild(string)
ProductNameHasProductName(string)
ProductVersionHasProductVersion(string)
SpecialBuildHasSpecialBuild(string)
FileBuildPartHasFileBuildPart(int)
FileMajorPartHasFileMajorPart(int)
FileMinorPartHasFileMinorPart(int)
FilePrivatePartHasFilePrivatePart(int)
ProductBuildPartHasProductBuildPart(int)
ProductMajorPartHasProductMajorPart(int)
ProductMinorPartHasProductMinorPart(int)
ProductPrivatePartHasProductPrivatePart(int)
IsDebugIsDebug() / IsNotDebug()
IsPatchedIsPatched() / IsNotPatched()
IsPreReleaseIsPreRelease() / IsNotPreRelease()
IsPrivateBuildIsPrivateBuild() / IsNotPrivateBuild()
IsSpecialBuildIsSpecialBuild() / IsNotSpecialBuild()