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:
| Expectation | Negated | Summary |
|---|---|---|
HasFile | DoesNotHaveFile | contains the file, optionally with its content and timestamps |
HasDirectory | DoesNotHaveDirectory | contains the directory, optionally with its files and subdirectories |
HasDrive | DoesNotHaveDrive | contains the drive |
Expectations for IFileInfo:
| Expectation | Negated | Summary |
|---|---|---|
Exists | DoesNotExist | the file exists |
HasName | has the expected name | |
HasExtension | has the expected extension | |
HasLength | has the expected length | |
HasContent | HasContent().NotEqualTo | has the expected string or binary content |
IsReadOnly | IsNotReadOnly | the file is read-only |
HasAttribute | DoesNotHaveAttribute | has the attribute flag |
HasCreationTime | has the expected creation time, optionally within a tolerance | |
HasLastAccessTime | has the expected last access time, optionally within a tolerance | |
HasLastWriteTime | has the expected last write time, optionally within a tolerance | |
WhoseParent | the containing directory meets expectations (.NET 10 or later) |
Expectations for IDirectoryInfo:
| Expectation | Negated | Summary |
|---|---|---|
Exists | DoesNotExist | the directory exists |
HasName | has the expected name | |
IsEmpty | IsNotEmpty | contains no files or subdirectories |
HasFile | DoesNotHaveFile | contains the file |
HasDirectory | DoesNotHaveDirectory | contains the subdirectory |
HasAttribute | DoesNotHaveAttribute | has the attribute flag |
HasCreationTime | has the expected creation time, optionally within a tolerance | |
HasLastAccessTime | has the expected last access time, optionally within a tolerance | |
HasLastWriteTime | has the expected last write time, optionally within a tolerance | |
WhoseParent | the parent directory meets expectations (.NET 10 or later) |
Expectations for IDriveInfo:
| Expectation | Negated | Summary |
|---|---|---|
IsReady | IsNotReady | the drive is ready |
HasName | has the expected name | |
HasVolumeLabel | has the expected volume label | |
HasDriveFormat | has the expected drive format | |
HasDriveType | has the expected drive type | |
HasTotalSize | has the expected total size | |
HasTotalFreeSpace | has the expected total free space | |
HasAvailableFreeSpace | has the expected available free space |
Expectations for IFileVersionInfo:
| Expectation | Negated | Summary |
|---|---|---|
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:
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:
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:
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.
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.
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:
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
| Property | Expectation |
|---|---|
Comments | HasComments(string) |
CompanyName | HasCompanyName(string) |
FileDescription | HasFileDescription(string) |
FileName | HasFileName(string) |
FileVersion | HasFileVersion(string) |
InternalName | HasInternalName(string) |
Language | HasLanguage(string) |
LegalCopyright | HasLegalCopyright(string) |
LegalTrademarks | HasLegalTrademarks(string) |
OriginalFilename | HasOriginalFilename(string) |
PrivateBuild | HasPrivateBuild(string) |
ProductName | HasProductName(string) |
ProductVersion | HasProductVersion(string) |
SpecialBuild | HasSpecialBuild(string) |
FileBuildPart | HasFileBuildPart(int) |
FileMajorPart | HasFileMajorPart(int) |
FileMinorPart | HasFileMinorPart(int) |
FilePrivatePart | HasFilePrivatePart(int) |
ProductBuildPart | HasProductBuildPart(int) |
ProductMajorPart | HasProductMajorPart(int) |
ProductMinorPart | HasProductMinorPart(int) |
ProductPrivatePart | HasProductPrivatePart(int) |
IsDebug | IsDebug() / IsNotDebug() |
IsPatched | IsPatched() / IsNotPatched() |
IsPreRelease | IsPreRelease() / IsNotPreRelease() |
IsPrivateBuild | IsPrivateBuild() / IsNotPrivateBuild() |
IsSpecialBuild | IsSpecialBuild() / IsNotSpecialBuild() |