diff --git a/Source/Testably.Abstractions.Testing/Helpers/PathHelper.cs b/Source/Testably.Abstractions.Testing/Helpers/PathHelper.cs index 2201f99c6..d6f7a42d3 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/PathHelper.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/PathHelper.cs @@ -32,7 +32,7 @@ internal static bool HasIllegalCharacters(this string path, IFileSystem fileSyst () => false); } - internal static bool IsUncPath(this string? path) + internal static bool IsUncPath([NotNullWhen(true)]this string? path) { if (path == null) { diff --git a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs index 557702a7b..45490594d 100644 --- a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs +++ b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs @@ -433,15 +433,14 @@ public IStorageContainer GetOrCreateContainer( out IStorageContainer? initialContainer) && initialContainer.LinkTarget != null) { - IStorageLocation nextLocation = + IStorageLocation? nextLocation = _fileSystem.Storage.GetLocation(initialContainer.LinkTarget); if (_containers.TryGetValue(nextLocation, out IStorageContainer? container)) { if (returnFinalTarget) { - nextLocation = ResolveFinalLinkTarget(container, location) ?? - nextLocation; + nextLocation = ResolveFinalLinkTarget(container, location); } return nextLocation; diff --git a/Tests/Testably.Abstractions.Testing.Tests/Storage/InMemoryStorageTests.cs b/Tests/Testably.Abstractions.Testing.Tests/Storage/InMemoryStorageTests.cs index 525bedc12..6602775e7 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/Storage/InMemoryStorageTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/Storage/InMemoryStorageTests.cs @@ -45,57 +45,6 @@ public void Copy_Overwrite_ShouldAdjustAvailableFreeSpace( .Be(availableFreeSpaceBefore + file2Size - file1Size); } - [Theory] - [AutoData] - public void Replace_WithoutBackup_ShouldNotChangeAvailableFreeSpace( - int file1Size, int file2Size) - { - MockFileSystem fileSystem = new(); - IDriveInfo mainDrive = fileSystem.DriveInfo.New("".PrefixRoot()); - IRandom random = RandomFactory.Shared; - byte[] file1Content = new byte[file1Size]; - byte[] file2Content = new byte[file2Size]; - random.NextBytes(file1Content); - random.NextBytes(file2Content); - - fileSystem.File.WriteAllBytes("foo", file1Content); - fileSystem.File.WriteAllBytes("bar", file2Content); - long availableFreeSpaceBefore = mainDrive.AvailableFreeSpace; - - fileSystem.File.Replace("foo", "bar", "backup"); - - long availableFreeSpaceAfter = mainDrive.AvailableFreeSpace; - availableFreeSpaceAfter.Should() - .Be(availableFreeSpaceBefore); - } - - [Theory] - [AutoData] - public void Replace_WithBackup_ShouldChangeAvailableFreeSpace( - int file1Size, int file2Size, int file3Size) - { - MockFileSystem fileSystem = new(); - IDriveInfo mainDrive = fileSystem.DriveInfo.New("".PrefixRoot()); - IRandom random = RandomFactory.Shared; - byte[] file1Content = new byte[file1Size]; - byte[] file2Content = new byte[file2Size]; - byte[] file3Content = new byte[file3Size]; - random.NextBytes(file1Content); - random.NextBytes(file2Content); - random.NextBytes(file3Content); - - fileSystem.File.WriteAllBytes("foo", file1Content); - fileSystem.File.WriteAllBytes("bar", file2Content); - fileSystem.File.WriteAllBytes("backup", file3Content); - long availableFreeSpaceBefore = mainDrive.AvailableFreeSpace; - - fileSystem.File.Replace("foo", "bar", "backup", true); - - long availableFreeSpaceAfter = mainDrive.AvailableFreeSpace; - availableFreeSpaceAfter.Should() - .Be(availableFreeSpaceBefore + file2Size); - } - [Fact] public void CurrentDirectory_ShouldBeInitializedToDefaultRoot() { @@ -126,6 +75,18 @@ public void Delete_RaceCondition_ShouldReturnFalse() exception.Should().BeOfType(); } + [Theory] + [InlineData((string?)null)] + [InlineData("")] + [InlineData(" ")] + [InlineData("\t")] + public void GetDrive_NullOrWhitespace_ShouldReturnNull(string? driveName) + { + IStorageDrive? result = Storage.GetDrive(driveName); + + result.Should().BeNull(); + } + [Theory] [AutoData] public void Move_RequestDeniedForChild_ShouldRollback( @@ -134,9 +95,9 @@ public void Move_RequestDeniedForChild_ShouldRollback( IStorageLocation location = Storage.GetLocation(locationPath); IStorageLocation destination = Storage.GetLocation(destinationPath); IStorageLocation child1Location = - Storage.GetLocation(Path.Combine(locationPath, "foo")); + Storage.GetLocation(Path.Combine(locationPath, "foo1")); IStorageLocation child2Location = - Storage.GetLocation(Path.Combine(locationPath, "bar")); + Storage.GetLocation(Path.Combine(locationPath, "foo2")); LockableContainer lockedContainer = new(FileSystem); Storage.TryAddContainer( location, @@ -164,6 +125,57 @@ public void Move_RequestDeniedForChild_ShouldRollback( exception.Should().BeOfType(); } + [Theory] + [AutoData] + public void Replace_WithBackup_ShouldChangeAvailableFreeSpace( + int file1Size, int file2Size, int file3Size) + { + MockFileSystem fileSystem = new(); + IDriveInfo mainDrive = fileSystem.DriveInfo.New("".PrefixRoot()); + IRandom random = RandomFactory.Shared; + byte[] file1Content = new byte[file1Size]; + byte[] file2Content = new byte[file2Size]; + byte[] file3Content = new byte[file3Size]; + random.NextBytes(file1Content); + random.NextBytes(file2Content); + random.NextBytes(file3Content); + + fileSystem.File.WriteAllBytes("foo", file1Content); + fileSystem.File.WriteAllBytes("bar", file2Content); + fileSystem.File.WriteAllBytes("backup", file3Content); + long availableFreeSpaceBefore = mainDrive.AvailableFreeSpace; + + fileSystem.File.Replace("foo", "bar", "backup", true); + + long availableFreeSpaceAfter = mainDrive.AvailableFreeSpace; + availableFreeSpaceAfter.Should() + .Be(availableFreeSpaceBefore + file2Size); + } + + [Theory] + [AutoData] + public void Replace_WithoutBackup_ShouldNotChangeAvailableFreeSpace( + int file1Size, int file2Size) + { + MockFileSystem fileSystem = new(); + IDriveInfo mainDrive = fileSystem.DriveInfo.New("".PrefixRoot()); + IRandom random = RandomFactory.Shared; + byte[] file1Content = new byte[file1Size]; + byte[] file2Content = new byte[file2Size]; + random.NextBytes(file1Content); + random.NextBytes(file2Content); + + fileSystem.File.WriteAllBytes("foo", file1Content); + fileSystem.File.WriteAllBytes("bar", file2Content); + long availableFreeSpaceBefore = mainDrive.AvailableFreeSpace; + + fileSystem.File.Replace("foo", "bar", "backup"); + + long availableFreeSpaceAfter = mainDrive.AvailableFreeSpace; + availableFreeSpaceAfter.Should() + .Be(availableFreeSpaceBefore); + } + [Theory] [AutoData] public void TryAddContainer_ShouldNotifyWhenAdded(string path)