Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 WriteTo truncates output file #203

Merged
merged 2 commits into from
Nov 28, 2023
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
6 changes: 5 additions & 1 deletion docs/articles/core/binary/datastream.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ internally maps to the .NET enumerations `FileMode` and `FileAccess` as follow:
> hundreds of `DataStream` on different files ready without running out of
> resources in the operative system.

<!-- ignore warning -->

> [!TIP]
> `FileOpenMode` is handy enumeration that covers most use cases. If you require
> any other combination of `FileMode` and `FileAccess` you can create the
Expand All @@ -119,7 +121,9 @@ applies to the content that targets this `DataStream`, not the entire parent

> [!NOTE]
> The method ignores the current position, it will always start writing from the
> start to the end. It will **restore** the current position after comparing.
> start to the end. It will **restore** the current position after writing. It
> will also start **truncate** the output file. It will not append or preserve
> any existing content.

The path should point to the output file. If there is any directory that doesn't
exist, it will create them first.
Expand Down
43 changes: 35 additions & 8 deletions src/Yarhl.UnitTests/IO/DataStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,17 +1368,44 @@ public void WriteToWhenLengthZeroCreatesEmptyFile()
{
string tempFile = Path.Combine(
Path.GetTempPath(),
Path.GetRandomFileName(),
Path.GetRandomFileName());

DataStream stream = new DataStream();
stream.WriteTo(tempFile);
try {
var stream = new DataStream();
stream.WriteTo(tempFile);

Assert.That(File.Exists(tempFile), Is.True);
FileStream fs = new FileStream(tempFile, FileMode.Open, FileAccess.Read);
Assert.AreEqual(0, fs.Length);
fs.Dispose();
File.Delete(tempFile);
Assert.That(File.Exists(tempFile), Is.True);

using var fs = new FileStream(tempFile, FileMode.Open, FileAccess.Read);
Assert.That(fs.Length, Is.EqualTo(0));
} finally {
File.Delete(tempFile);
}
}

[Test]
public void WriteToWhenTruncatesExistingFile()
{
string tempFile = Path.Combine(
Path.GetTempPath(),
Path.GetRandomFileName());

byte[] expected = new byte[] { 0xCA, 0xFE };
using var stream = new DataStream();
stream.Write(expected);

try {
// Pre-fill the output file with some long content
File.WriteAllText(tempFile, "hello world!");

// Overwrite with 2 bytes stream content
stream.WriteTo(tempFile);

byte[] actual = File.ReadAllBytes(tempFile);
Assert.That(actual, Is.EquivalentTo(expected));
} finally {
File.Delete(tempFile);
}
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion src/Yarhl/IO/DataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public void WriteSegmentTo(long start, long length, string fileOut)
}

// We use FileStream so it creates a file even when the length is zero
using var segment = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write);
using var segment = new FileStream(fileOut, FileMode.Create, FileAccess.Write);
WriteSegmentTo(start, length, segment);
}

Expand Down