Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ IStorageAccessHandle RequestAccess(FileAccess access, FileShare share,
int? hResult = null,
IStorageLocation? onBehalfOfLocation = null);

/// <summary>
/// Updates the location of the container.
/// </summary>
IStorageContainer UpdateLocation(IStorageLocation newLocation);

/// <summary>
/// Writes the <paramref name="bytes" /> to the <see cref="IFileInfo" />.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal sealed class InMemoryContainer : IStorageContainer
private readonly FileSystemExtensibility _extensibility = new();
private readonly MockFileSystem _fileSystem;
private bool _isEncrypted;
private readonly IStorageLocation _location;
private IStorageLocation _location;

#if FEATURE_FILESYSTEM_UNIXFILEMODE
private UnixFileMode _unixFileMode = UnixFileMode.OtherRead |
Expand Down Expand Up @@ -223,6 +223,13 @@ public IStorageAccessHandle RequestAccess(FileAccess access, FileShare share,
hResult ?? -2147024864);
}

/// <inheritdoc cref="IStorageContainer.UpdateLocation(IStorageLocation)" />
public IStorageContainer UpdateLocation(IStorageLocation newLocation)
{
_location = newLocation;
return this;
}

/// <inheritdoc cref="IStorageContainer.WriteBytes(byte[])" />
public void WriteBytes(byte[] bytes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public IStorageContainer GetOrCreateContainer(
existingDestinationContainer.GetBytes().Length;
destination.Drive?.ChangeUsedBytes(-1 * destinationBytesLength);
if (backup != null &&
_containers.TryAdd(backup, existingDestinationContainer))
_containers.TryAdd(backup, existingDestinationContainer.UpdateLocation(backup)))
{
if (_fileSystem.Execute.IsWindows &&
sourceContainer.Type == FileSystemTypes.File)
Expand Down Expand Up @@ -587,7 +587,7 @@ public IStorageContainer GetOrCreateContainer(
DateTimeKind.Utc);
}

_containers.TryAdd(destination, existingSourceContainer);
_containers.TryAdd(destination, existingSourceContainer.UpdateLocation(destination));
return destination;
}
}
Expand Down Expand Up @@ -1041,7 +1041,7 @@ private bool IncludeItemInEnumeration(
existingContainer.ClearBytes();
}

if (_containers.TryAdd(destination, sourceContainer))
if (_containers.TryAdd(destination, sourceContainer.UpdateLocation(destination)))
{
int bytesLength = sourceContainer.GetBytes().Length;
source.Drive?.ChangeUsedBytes(-1 * bytesLength);
Expand Down
7 changes: 7 additions & 0 deletions Source/Testably.Abstractions.Testing/Storage/NullContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ public IStorageAccessHandle RequestAccess(FileAccess access, FileShare share,
IStorageLocation? onBehalfOfLocation = null)
=> new NullStorageAccessHandle(access, share, deleteAccess);

/// <inheritdoc cref="IStorageContainer.UpdateLocation(IStorageLocation)" />
public IStorageContainer UpdateLocation(IStorageLocation newLocation)
{
// Do nothing in NullContainer
return this;
}

/// <inheritdoc cref="IStorageContainer.WriteBytes(byte[])" />
public void WriteBytes(byte[] bytes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public IStorageAccessHandle RequestAccess(FileAccess access, FileShare share,
return new AccessHandle(access, share, deleteAccess);
}

/// <inheritdoc cref="IStorageContainer.UpdateLocation(IStorageLocation)" />
public IStorageContainer UpdateLocation(IStorageLocation newLocation)
=> this;

/// <inheritdoc cref="IStorageContainer.WriteBytes(byte[])" />
public void WriteBytes(byte[] bytes)
{
Expand Down
21 changes: 21 additions & 0 deletions Tests/Testably.Abstractions.Tests/FileSystem/File/ReplaceTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using aweXpect.Testably;
using System.IO;

namespace Testably.Abstractions.Tests.FileSystem.File;
Expand Down Expand Up @@ -188,6 +189,26 @@ public async Task Replace_ShouldReplaceFile(
await That(FileSystem.File.ReadAllText(backupName)).IsEquivalentTo(destinationContents);
}

[Theory]
[AutoData]
public async Task Replace_Twice_ShouldReplaceFile(
string sourceName,
string destinationName)
{
FileSystem.Initialize()
.WithFile(sourceName).Which(f => f.HasStringContent("abc"))
.WithFile(destinationName).Which(f => f.HasStringContent("xyz"));
var file1 = FileSystem.FileInfo.New(sourceName);
var file2 = FileSystem.FileInfo.New(destinationName);
FileSystem.File.Replace(file2.FullName, file1.FullName, null);

var file3 = FileSystem.FileInfo.New(destinationName);
FileSystem.File.WriteAllText(destinationName, "def");
FileSystem.File.Replace(file3.FullName, file1.FullName, null);

await That(file1).HasContent("def");
}

[Theory]
[AutoData]
public async Task Replace_SourceIsDirectory_ShouldThrowUnauthorizedAccessException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using aweXpect.Testably;
using System.IO;

namespace Testably.Abstractions.Tests.FileSystem.FileInfo;
Expand Down Expand Up @@ -398,6 +399,26 @@ void Act()
}
}

[Theory]
[AutoData]
public async Task Replace_Twice_ShouldReplaceFile(
string sourceName,
string destinationName)
{
FileSystem.Initialize()
.WithFile(sourceName).Which(f => f.HasStringContent("abc"))
.WithFile(destinationName).Which(f => f.HasStringContent("xyz"));
IFileInfo file1 = FileSystem.FileInfo.New(sourceName);
IFileInfo file2 = FileSystem.FileInfo.New(destinationName);
file2.Replace(file1.FullName, null);

IFileInfo file3 = FileSystem.FileInfo.New(destinationName);
FileSystem.File.WriteAllText(destinationName, "def");
file3.Replace(file1.FullName, null);

await That(file1).HasContent("def");
}

[Theory]
[AutoData]
public async Task Replace_WhenFileIsReadOnly_ShouldThrowUnauthorizedAccessException_OnWindows(
Expand Down
Loading