From 3fba515e0367e0af2313ddd5cc6df04721f6f0ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sun, 5 Mar 2023 06:57:41 +0100 Subject: [PATCH 1/7] Add test to initialize MockFileSystem with alternative drives on windows --- .../MockFileSystem.cs | 2 +- .../MockFileSystemTests.cs | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Source/Testably.Abstractions.Testing/MockFileSystem.cs b/Source/Testably.Abstractions.Testing/MockFileSystem.cs index 6929620a..00c6e5cf 100644 --- a/Source/Testably.Abstractions.Testing/MockFileSystem.cs +++ b/Source/Testably.Abstractions.Testing/MockFileSystem.cs @@ -154,7 +154,7 @@ public MockFileSystem WithSafeFileHandleStrategy( private void AddDriveFromCurrentDirectory() { - string? root = Path.GetPathRoot(Directory.GetCurrentDirectory()); + string? root = Path.GetPathRoot(System.IO.Directory.GetCurrentDirectory()); if (root != null && root[0] != _storage.MainDrive.Name[0]) { diff --git a/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs b/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs index 02112270..1aea37fe 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs @@ -91,6 +91,33 @@ public void FileSystemMock_ShouldBeInitializedWithASingleDefaultDrive() drive.VolumeLabel.Should().NotBeNullOrEmpty(); } + [SkippableFact] + public void FileSystemMock_ShouldInitializeDriveFromCurrentDirectory() + { + Skip.IfNot(Test.RunsOnWindows); + + string driveName = "D:\\"; + + Skip.IfNot(Directory.Exists(driveName), + $"Skip test, as no alternative drive '{driveName}' is mapped on this computer."); + + string currentDirectory = Directory.GetCurrentDirectory(); + MockFileSystem sut; + try + { + Directory.SetCurrentDirectory(driveName); + sut = new MockFileSystem(); + } + finally + { + Directory.SetCurrentDirectory(currentDirectory); + } + + IDriveInfo[] drives = sut.DriveInfo.GetDrives(); + drives.Length.Should().Be(2); + drives.Should().Contain(d => d.Name == driveName); + } + [SkippableTheory] [AutoData] public void WithAccessControl_Denied_CreateDirectoryShouldThrowIOException( From 7b9487187b5fd10a00b93745e6c721f1c9778d84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sun, 5 Mar 2023 06:58:44 +0100 Subject: [PATCH 2/7] Ignore NCrunch files --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 39a41f77..ea9f92ba 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,10 @@ # Generated files /Tests/*/Generated +# NCrunch +*.ncrunchproject +*.ncrunchsolution + ############################################################ ################## VISUAL STUDIO TEMPLATE ################## ############################################################ From 237749d343182fcf7a03579efbc6c9dd1050b972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sun, 5 Mar 2023 07:31:30 +0100 Subject: [PATCH 3/7] Fix test, as it has side-effects --- .../MockFileSystemTests.cs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs b/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs index 1aea37fe..21c8c7b3 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs @@ -94,24 +94,11 @@ public void FileSystemMock_ShouldBeInitializedWithASingleDefaultDrive() [SkippableFact] public void FileSystemMock_ShouldInitializeDriveFromCurrentDirectory() { - Skip.IfNot(Test.RunsOnWindows); + string? driveName = Path.GetPathRoot(Directory.GetCurrentDirectory()); - string driveName = "D:\\"; + Skip.If(!Test.RunsOnWindows || driveName?.StartsWith("C") != false); - Skip.IfNot(Directory.Exists(driveName), - $"Skip test, as no alternative drive '{driveName}' is mapped on this computer."); - - string currentDirectory = Directory.GetCurrentDirectory(); - MockFileSystem sut; - try - { - Directory.SetCurrentDirectory(driveName); - sut = new MockFileSystem(); - } - finally - { - Directory.SetCurrentDirectory(currentDirectory); - } + MockFileSystem sut = new(); IDriveInfo[] drives = sut.DriveInfo.GetDrives(); drives.Length.Should().Be(2); From 028472ee454eb63d598b0bc2337ca069a39ec806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sun, 5 Mar 2023 07:39:49 +0100 Subject: [PATCH 4/7] Fix test InitializeIn_MissingDrive_ShouldCreateDrive which doesn't test anything when drive D: already existed --- .../FileSystemInitializerExtensionsTests.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Tests/Testably.Abstractions.Testing.Tests/FileSystemInitializerExtensionsTests.cs b/Tests/Testably.Abstractions.Testing.Tests/FileSystemInitializerExtensionsTests.cs index 0d4d06aa..739c3d1e 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/FileSystemInitializerExtensionsTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/FileSystemInitializerExtensionsTests.cs @@ -223,11 +223,23 @@ public void InitializeIn_MissingDrive_ShouldCreateDrive(string directoryName) { Skip.IfNot(Test.RunsOnWindows); - directoryName = Path.Combine("D:\\", directoryName); MockFileSystem sut = new(); + IDriveInfo[] drives = sut.DriveInfo.GetDrives(); + for (char c = 'D'; c <= 'Z'; c++) + { + if (drives.Any(d => d.Name.StartsWith($"{c}"))) + { + continue; + } + + directoryName = Path.Combine($"{c}:\\", directoryName); + break; + } + sut.InitializeIn(directoryName); sut.Directory.Exists(directoryName).Should().BeTrue(); + sut.DriveInfo.GetDrives().Length.Should().Be(drives.Length + 1); } [Theory] From 9601b2eaf00f1df533029bab9a58245a3c65ba82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sun, 5 Mar 2023 07:58:06 +0100 Subject: [PATCH 5/7] Support multiple drives in failing tests --- .../MockFileSystemExtensions.cs | 13 +++++++++ .../FileSystem/DriveInfoMockTests.cs | 27 +++++++++---------- .../MockFileSystemTests.cs | 8 +++--- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/Source/Testably.Abstractions.Testing/MockFileSystemExtensions.cs b/Source/Testably.Abstractions.Testing/MockFileSystemExtensions.cs index c95771be..3a299608 100644 --- a/Source/Testably.Abstractions.Testing/MockFileSystemExtensions.cs +++ b/Source/Testably.Abstractions.Testing/MockFileSystemExtensions.cs @@ -1,4 +1,6 @@ using System; +using System.Linq; +using Testably.Abstractions.Testing.Helpers; using Testably.Abstractions.Testing.Storage; namespace Testably.Abstractions.Testing; @@ -8,6 +10,17 @@ namespace Testably.Abstractions.Testing; /// public static class MockFileSystemExtensions { + /// + /// Returns the default drive in the . + /// + public static IDriveInfo GetDefaultDrive(this MockFileSystem mockFileSystem) + { + string driveName = "".PrefixRoot(); + return mockFileSystem.DriveInfo + .GetDrives() + .First(d => d.Name.StartsWith(driveName)); + } + /// /// Changes the parameters of the default drive (e.g. 'C:\' on Windows or '/' on Linux) /// diff --git a/Tests/Testably.Abstractions.Testing.Tests/FileSystem/DriveInfoMockTests.cs b/Tests/Testably.Abstractions.Testing.Tests/FileSystem/DriveInfoMockTests.cs index ec4ebd29..c35bdb28 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/FileSystem/DriveInfoMockTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/FileSystem/DriveInfoMockTests.cs @@ -1,5 +1,4 @@ using System.IO; -using System.Linq; using System.Text; using Testably.Abstractions.Testing.FileSystem; @@ -23,7 +22,7 @@ public DriveInfoMockTests() public void AvailableFreeSpace_CannotGetNegative(long size) { FileSystem.WithDrive(d => d.SetTotalSize(size)); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); FileSystem.WithDrive(d => d.ChangeUsedBytes(-1)); @@ -44,7 +43,7 @@ public void AvailableFreeSpace_NotEnoughSpace_ShouldThrowIOException( FileSystem.File.WriteAllBytes(path, bytes); }); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); exception.Should().BeOfType() .Which.Message.Should().Contain($"'{drive.Name}'"); drive.AvailableFreeSpace.Should().Be(fileSize - 1); @@ -60,7 +59,7 @@ public void AvailableFreeSpace_ShouldBeChangedWhenAppendingToAFile( int fileSize2 = encoding.GetBytes(fileContent2).Length; FileSystem.WithDrive(d => d.SetTotalSize(fileSize1 + fileSize2 + expectedRemainingBytes)); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); FileSystem.File.WriteAllText(path, fileContent1, encoding); drive.AvailableFreeSpace.Should().Be(expectedRemainingBytes + fileSize2); @@ -77,7 +76,7 @@ public void AvailableFreeSpace_ShouldBeChangedWhenWorkingWithStreams( int reduceLength, string path, string previousContent) { FileSystem.File.WriteAllText(path, previousContent); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); long previousFreeSpace = drive.AvailableFreeSpace; FileSystemStream stream = FileSystem.File.OpenWrite(path); @@ -101,7 +100,7 @@ public void AvailableFreeSpace_ShouldBeReducedByWritingToFile( FileSystem.File.WriteAllBytes(path, bytes); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); drive.AvailableFreeSpace.Should().Be(0); } @@ -118,7 +117,7 @@ public void AvailableFreeSpace_ShouldBeReleasedWhenDeletingAFile( FileSystem.File.WriteAllBytes(path, bytes); FileSystem.File.Delete(path); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); drive.AvailableFreeSpace.Should().Be(fileSize); } @@ -129,7 +128,7 @@ public void AvailableFreeSpace_ShouldBeSetTotalSize(long size) { FileSystem.WithDrive(d => d.SetTotalSize(size)); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); drive.AvailableFreeSpace.Should().Be(size); } @@ -228,7 +227,7 @@ public void SetDriveFormat_Default_ShouldBeNTFS() { FileSystem.WithDrive(d => d.SetDriveFormat()); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); drive.DriveFormat.Should().Be("NTFS"); } @@ -238,7 +237,7 @@ public void SetDriveFormat_ShouldChangeDriveFormat(string driveFormat) { FileSystem.WithDrive(d => d.SetDriveFormat(driveFormat)); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); drive.DriveFormat.Should().Be(driveFormat); } @@ -247,7 +246,7 @@ public void SetDriveType_Default_ShouldBeFixed() { FileSystem.WithDrive(d => d.SetDriveType()); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); drive.DriveType.Should().Be(DriveType.Fixed); } @@ -257,7 +256,7 @@ public void SetDriveType_ShouldChangeDriveType(DriveType driveType) { FileSystem.WithDrive(d => d.SetDriveType(driveType)); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); drive.DriveType.Should().Be(driveType); } @@ -268,7 +267,7 @@ public void SetIsReady_ShouldChangeIsReady(bool isReady) { FileSystem.WithDrive(d => d.SetIsReady(isReady)); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); drive.IsReady.Should().Be(isReady); } @@ -277,7 +276,7 @@ public void SetTotalSize_Default_ShouldBe1Gigabyte() { FileSystem.WithDrive(d => d.SetTotalSize()); - IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single(); + IDriveInfo drive = FileSystem.GetDefaultDrive(); drive.AvailableFreeSpace.Should().Be(1024 * 1024 * 1024); } diff --git a/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs b/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs index 21c8c7b3..13cf0d2b 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs @@ -210,7 +210,7 @@ public void WithDrive_WithCallback_ShouldUpdateDrive(long totalSize) MockFileSystem sut = new(); sut.WithDrive(d => d.SetTotalSize(totalSize)); - IDriveInfo drive = sut.DriveInfo.GetDrives().Single(); + IDriveInfo drive = sut.GetDefaultDrive(); drive.TotalSize.Should().Be(totalSize); drive.TotalFreeSpace.Should().Be(totalSize); @@ -239,11 +239,13 @@ public void WithUncDrive_ShouldNotBeIncludedInGetDrives( MockFileSystem sut = new(); string uncPrefix = new(sut.Path.DirectorySeparatorChar, 2); string uncDrive = $"{uncPrefix}{server}"; + int expectedLogicalDrives = sut.Directory.GetLogicalDrives().Length; + int expectedDrives = sut.DriveInfo.GetDrives().Length; sut.WithUncDrive(uncDrive); - sut.Directory.GetLogicalDrives().Length.Should().Be(1); - sut.DriveInfo.GetDrives().Length.Should().Be(1); + sut.Directory.GetLogicalDrives().Length.Should().Be(expectedLogicalDrives); + sut.DriveInfo.GetDrives().Length.Should().Be(expectedDrives); } [SkippableTheory] From c8ec61b53677fb005be042a31892ff23d339439b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sun, 5 Mar 2023 08:48:33 +0100 Subject: [PATCH 6/7] Support multiple drives in failing tests --- .../MockFileSystemTests.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs b/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs index 13cf0d2b..0c18004d 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs @@ -74,14 +74,15 @@ public void FileSystemMock_FileInfo_Encrypt(string path, string contents) } [SkippableFact] - public void FileSystemMock_ShouldBeInitializedWithASingleDefaultDrive() + public void FileSystemMock_ShouldBeInitializedWithADefaultDrive() { string expectedDriveName = "".PrefixRoot(); MockFileSystem sut = new(); IDriveInfo[] drives = sut.DriveInfo.GetDrives(); - IDriveInfo drive = drives.Single(); + IDriveInfo drive = sut.GetDefaultDrive(); + drives.Should().NotBeEmpty(); drive.Name.Should().Be(expectedDriveName); drive.AvailableFreeSpace.Should().BeGreaterThan(0); drive.DriveFormat.Should() @@ -167,7 +168,7 @@ public void WithDrive_ExistingName_ShouldUpdateDrive() IDriveInfo[] drives = sut.DriveInfo.GetDrives(); - drives.Length.Should().Be(1); + drives.Length.Should().BeGreaterOrEqualTo(1); drives.Should().ContainSingle(d => d.Name == driveName); } From 963f8a9f97cc7fd85e87aa5f860c0d053760fbba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sun, 5 Mar 2023 09:13:10 +0100 Subject: [PATCH 7/7] Ignore StoredTexts from NCrunch --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ea9f92ba..8143a939 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ # NCrunch *.ncrunchproject *.ncrunchsolution +/.NCrunch_*/StoredText/ ############################################################ ################## VISUAL STUDIO TEMPLATE ##################