diff --git a/src/Cake.FileHelpers.Tests/FileHelperTests.cs b/src/Cake.FileHelpers.Tests/FileHelperTests.cs index 85dd7b1..4b8e7fe 100644 --- a/src/Cake.FileHelpers.Tests/FileHelperTests.cs +++ b/src/Cake.FileHelpers.Tests/FileHelperTests.cs @@ -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() { diff --git a/src/Cake.FileHelpers/FileHelpers.cs b/src/Cake.FileHelpers/FileHelpers.cs index b557fb4..7ab708d 100644 --- a/src/Cake.FileHelpers/FileHelpers.cs +++ b/src/Cake.FileHelpers/FileHelpers.cs @@ -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); } @@ -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); } @@ -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); } @@ -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); }