Skip to content
Open
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
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,11 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.5.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="NUnit" Version="5.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="6.3.1" />
<PackageReference Include="NUnit.Analyzers" Version="4.15.0" />
<PackageReference Include="coverlet.collector" Version="10.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace DifferentWaysToOverwriteAFile.Test;

public class OverwriteWithAnotherFileTests
{
private const string SourcePath = "new-content.txt";
private const string DestinationPath = "target.txt";
private const string BackupPath = "target.bak";

[SetUp]
public void Setup()
{
File.WriteAllText(DestinationPath, "Hello, World!");
File.WriteAllText(SourcePath, "Hello, Code Maze!");
}

[Test]
public void GivenFileCopyWithOverwrite_ThenDestinationIsReplacedAndSourceStays()
{
FileOverwriter.CopyOver(SourcePath, DestinationPath);

Assert.That(File.ReadAllText(DestinationPath), Is.EqualTo("Hello, Code Maze!"));
Assert.That(File.Exists(SourcePath), Is.True);
}

[Test]
public void GivenFileCopyWithoutOverwrite_ThenItThrowsIOException()
{
Assert.That(() => File.Copy(SourcePath, DestinationPath), Throws.TypeOf<IOException>());
}

[Test]
public void GivenFileMoveWithOverwrite_ThenDestinationIsReplacedAndSourceIsGone()
{
FileOverwriter.MoveOver(SourcePath, DestinationPath);

Assert.That(File.ReadAllText(DestinationPath), Is.EqualTo("Hello, Code Maze!"));
Assert.That(File.Exists(SourcePath), Is.False);
}

[Test]
public void GivenFileReplace_ThenDestinationIsReplacedAndOldContentIsBackedUp()
{
FileOverwriter.ReplaceWithBackup(SourcePath, DestinationPath, BackupPath);

Assert.That(File.ReadAllText(DestinationPath), Is.EqualTo("Hello, Code Maze!"));
Assert.That(File.ReadAllText(BackupPath), Is.EqualTo("Hello, World!"));
Assert.That(File.Exists(SourcePath), Is.False);
}

[Test]
public void GivenFileModeOpenOrCreate_ThenOldBytesRemainAfterAShorterWrite()
{
using (var fileStream = new FileStream(DestinationPath, FileMode.OpenOrCreate, FileAccess.Write))
{
fileStream.Write("Hi!"u8);
}

Assert.That(File.ReadAllText(DestinationPath), Is.EqualTo("Hi!lo, World!"));
}

[Test]
public void GivenFileModeTruncate_ThenTheWholeFileIsOverwritten()
{
using (var fileStream = new FileStream(DestinationPath, FileMode.Truncate, FileAccess.Write))
{
fileStream.Write("Hi!"u8);
}

Assert.That(File.ReadAllText(DestinationPath), Is.EqualTo("Hi!"));
}

[TearDown]
public void TearDown()
{
File.Delete(SourcePath);
File.Delete(DestinationPath);
File.Delete(BackupPath);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace DifferentWaysToOverwriteAFile.Examples;

public static class FileOverwriter
{
public static void CopyOver(string sourcePath, string destinationPath)
{
File.Copy(sourcePath, destinationPath, overwrite: true);
}

public static void MoveOver(string sourcePath, string destinationPath)
{
File.Move(sourcePath, destinationPath, overwrite: true);
}

public static void ReplaceWithBackup(string sourcePath, string destinationPath, string backupPath)
{
File.Replace(sourcePath, destinationPath, backupPath);
}
}
Loading