Skip to content

Commit

Permalink
Fix "Append" aliases (#124)
Browse files Browse the repository at this point in the history
In the "FileAppend" aliases (FileAppendText and FileAppendLines), open the file stream in file mode "Append" instead of "OpenOrCreate" so that the content is appended to the end of the file instead of (partially) overwriting the file from the beginning
  • Loading branch information
ap0llo authored Mar 13, 2023
1 parent 8e78cde commit b431a17
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 4 deletions.
126 changes: 126 additions & 0 deletions src/Cake.FileHelpers.Tests/FileHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,132 @@ public void TestWriteAndReadLines()
Assert.Equal (contents[i], read[i]);
}

[Fact]
public void TestAppendToNewFileAndReadText()
{
const string file = "./testdata/Text.txt";
const string contents = "This is a test";

context.CakeContext.FileAppendText(file, contents);

var read = context.CakeContext.FileReadText(file);

Assert.Equal(contents, read);
}

[Fact]
public void TestAppendToExistingFileAndReadText()
{
const string file = "./testdata/Text.txt";
const string contents1 = "This is ";
const string contents2 = "a test";

context.CakeContext.FileAppendText(file, contents1);
context.CakeContext.FileAppendText(file, contents2);

var read = context.CakeContext.FileReadText(file);

Assert.Equal(contents1 + contents2, read);
}

[Fact]
public void TestAppendToNewFileAndReadTextWithUTF8Encoding()
{
const string file = "./testdata/Text.txt";
const string contents = "Monkey🐒";

context.CakeContext.FileAppendText(file, Encoding.UTF8, contents);

var read = context.CakeContext.FileReadText(file, Encoding.UTF8);

Assert.Equal(contents, read);
}

[Fact]
public void TestAppendToExistingFileAndReadTextWithUTF8Encoding()
{
const string file = "./testdata/Text.txt";
const string contents1 = "Monkey";
const string contents2 = "🐒";

context.CakeContext.FileAppendText(file, Encoding.UTF8, contents1);
context.CakeContext.FileAppendText(file, Encoding.UTF8, contents2);

var read = context.CakeContext.FileReadText(file, Encoding.UTF8);

Assert.Equal(contents1 + contents2, read);
}

[Fact]
public void TestAppendLinesToNewFileAndReadLines()
{
const string file = "./testdata/Text.txt";
var contents = new[] { "This", "is", "a", "test" };

context.CakeContext.FileAppendLines(file, contents);

var read = context.CakeContext.FileReadLines(file);

Assert.Equal(contents.Length, read.Length);

for (int i = 0; i < read.Length; i++)
Assert.Equal(contents[i], read[i]);
}

[Fact]
public void TestAppendLinesToExistingFileAndReadLines()
{
const string file = "./testdata/Text.txt";
var contents = new[] { "This", "is", "a", "test" };
var contents1 = new[] { contents[0], contents[1] };
var contents2 = new[] { contents[2], contents[3] };

context.CakeContext.FileAppendLines(file, contents1);
context.CakeContext.FileAppendLines(file, contents2);

var read = context.CakeContext.FileReadLines(file);

Assert.Equal(contents.Length, read.Length);

for (int i = 0; i < read.Length; i++)
Assert.Equal(contents[i], read[i]);
}

[Fact]
public void TestAppendLinesToNewFileAndReadLinesWithUTF8Encoding()
{
const string file = "./testdata/Text.txt";
var contents = new[] { "This is a test", "Monkey🐒" };

context.CakeContext.FileAppendLines(file, Encoding.UTF8, contents);

var read = context.CakeContext.FileReadLines(file, Encoding.UTF8);

Assert.Equal(contents.Length, read.Length);

for (int i = 0; i < read.Length; i++)
Assert.Equal(contents[i], read[i]);
}

[Fact]
public void TestAppendLinesToExistingFileAndReadLinesWithUTF8Encoding()
{
const string file = "./testdata/Text.txt";
var contents = new[] { "This is a test", "Monkey🐒" };
var contents1 = new[] { contents[0] };
var contents2 = new[] { contents[1] };

context.CakeContext.FileAppendLines(file, Encoding.UTF8, contents1);
context.CakeContext.FileAppendLines(file, Encoding.UTF8, contents2);

var read = context.CakeContext.FileReadLines(file, Encoding.UTF8);

Assert.Equal(contents.Length, read.Length);

for (int i = 0; i < read.Length; i++)
Assert.Equal(contents[i], read[i]);
}

[Fact]
public void FindTextInFilesGlob()
{
Expand Down
8 changes: 4 additions & 4 deletions src/Cake.FileHelpers/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static void FileWriteLines(this ICakeContext context, FilePath file, Enco
[CakeMethodAlias]
public static void FileAppendText (this ICakeContext context, FilePath file, string text)
{
using var streamWriter = CreateStreamWriter(context, file, FileMode.OpenOrCreate);
using var streamWriter = CreateStreamWriter(context, file, FileMode.Append);
streamWriter.Write(text);
}

Expand All @@ -149,7 +149,7 @@ public static void FileAppendText (this ICakeContext context, FilePath file, str
[CakeMethodAlias]
public static void FileAppendText(this ICakeContext context, FilePath file, Encoding encoding, string text)
{
using var streamWriter = CreateStreamWriter(context, file, FileMode.OpenOrCreate, encoding);
using var streamWriter = CreateStreamWriter(context, file, FileMode.Append, encoding);
streamWriter.Write(text);
}

Expand All @@ -162,7 +162,7 @@ public static void FileAppendText(this ICakeContext context, FilePath file, Enco
[CakeMethodAlias]
public static void FileAppendLines (this ICakeContext context, FilePath file, string[] lines)
{
using var streamWriter = CreateStreamWriter(context, file, FileMode.OpenOrCreate);
using var streamWriter = CreateStreamWriter(context, file, FileMode.Append);
WriteLines(streamWriter, lines);
}

Expand All @@ -176,7 +176,7 @@ public static void FileAppendLines (this ICakeContext context, FilePath file, st
[CakeMethodAlias]
public static void FileAppendLines(this ICakeContext context, FilePath file, Encoding encoding, string[] lines)
{
using var streamWriter = CreateStreamWriter(context, file, FileMode.OpenOrCreate, encoding);
using var streamWriter = CreateStreamWriter(context, file, FileMode.Append, encoding);
WriteLines(streamWriter, lines);
}

Expand Down

0 comments on commit b431a17

Please sign in to comment.