Skip to content

Commit 503de5d

Browse files
authored
Merge pull request #54 from rameel/fix-copyto
Fix: Allow CopyToAsync from readonly source to writable destination
2 parents 7803d1d + 398d853 commit 503de5d

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/Ramstack.FileSystem.Abstractions/VirtualFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public ValueTask CopyToAsync(string destinationPath, bool overwrite, Cancellatio
143143
/// </remarks>
144144
public ValueTask CopyToAsync(VirtualFile destination, bool overwrite, CancellationToken cancellationToken = default)
145145
{
146-
EnsureWritable();
146+
destination.EnsureWritable();
147147
destination.Refresh();
148148

149149
if (destination.FileSystem != FileSystem)

tests/Ramstack.FileSystem.Specification.Tests/Ramstack.FileSystem.Specification.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
<ItemGroup>
4747
<ProjectReference Include="..\..\src\Ramstack.FileSystem.Abstractions\Ramstack.FileSystem.Abstractions.csproj" />
48+
<ProjectReference Include="..\..\src\Ramstack.FileSystem.Physical\Ramstack.FileSystem.Physical.csproj" />
4849
</ItemGroup>
4950

5051
<ItemGroup>

tests/Ramstack.FileSystem.Specification.Tests/VirtualFileSystemSpecificationTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Security.Cryptography;
22
using System.Text;
33

4+
using Ramstack.FileSystem.Physical;
5+
46
namespace Ramstack.FileSystem.Specification.Tests;
57

68
/// <summary>
@@ -353,6 +355,38 @@ await Assert.ThatAsync(
353355
Throws.Exception);
354356
}
355357

358+
[Test]
359+
public async Task File_Readonly_ReadonlyToWritable_Succeeds()
360+
{
361+
using var fs = GetFileSystem();
362+
using var ds = new PhysicalFileSystem(
363+
Path.Combine(
364+
Path.GetTempPath(),
365+
Path.GetRandomFileName()));
366+
367+
if (!fs.IsReadOnly)
368+
return;
369+
370+
var src = fs.GetFile("/project/README.md");
371+
var dst = ds.GetFile("/README.md");
372+
373+
await src.CopyToAsync(dst);
374+
375+
Assert.That(
376+
await dst.ExistsAsync(),
377+
Is.True);
378+
379+
Assert.That(
380+
await ds.FileExistsAsync("/README.md"),
381+
Is.True);
382+
383+
Assert.That(
384+
await src.ReadAllTextAsync(),
385+
Is.EqualTo(await src.ReadAllTextAsync()));
386+
387+
Directory.Delete(ds.Root, true);
388+
}
389+
356390
[Test]
357391
public async Task File_CopyTo_Path()
358392
{

0 commit comments

Comments
 (0)