Skip to content

Commit

Permalink
Augment tests for FileMode.Append (#55513)
Browse files Browse the repository at this point in the history
To validate that seeking and writing are valid in the region of the file since its initial length.
  • Loading branch information
stephentoub authored Jul 13, 2021
1 parent 1904cfc commit d2e9f42
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public void SetPositionAppendModify()

fs.Position = length + 1;
Assert.Equal(length + 1, fs.Position);

fs.Write(TestBuffer);
fs.Position = length + 1;
Assert.Equal(length + 1, fs.Position);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public void SeekAppendModifyThrows()
Assert.Equal(length, fs.Position);
Assert.Throws<IOException>(() => fs.Seek(-length, SeekOrigin.End));
Assert.Equal(length, fs.Position);

fs.Write(TestBuffer);
Assert.Equal(length, fs.Seek(length, SeekOrigin.Begin));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public void SetLengthAppendModifyThrows()
Assert.Equal(length, fs.Length);
Assert.Throws<IOException>(() => fs.SetLength(0));
Assert.Equal(length, fs.Length);

fs.Write(TestBuffer);
Assert.Equal(length + TestBuffer.Length, fs.Length);

fs.SetLength(length);
Assert.Equal(length, fs.Length);
}
}

Expand Down
34 changes: 31 additions & 3 deletions src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;
using Xunit;

namespace System.IO.Tests
Expand Down Expand Up @@ -213,11 +214,23 @@ public void FileModeTruncateExisting(string streamSpecifier)
[Theory, MemberData(nameof(StreamSpecifiers))]
public virtual void FileModeAppend(string streamSpecifier)
{
using (FileStream fs = CreateFileStream(GetTestFilePath() + streamSpecifier, FileMode.Append))
string fileName = GetTestFilePath() + streamSpecifier;
using (FileStream fs = CreateFileStream(fileName, FileMode.Append))
{
Assert.False(fs.CanRead);
Assert.True(fs.CanWrite);

fs.Write(Encoding.ASCII.GetBytes("abcde"));
Assert.Equal(5, fs.Length);
Assert.Equal(5, fs.Position);
Assert.Equal(1, fs.Seek(1, SeekOrigin.Begin));

fs.Write(Encoding.ASCII.GetBytes("xyz"));
Assert.Equal(4, fs.Position);
Assert.Equal(5, fs.Length);
}

Assert.Equal("axyze", File.ReadAllText(fileName));
}

[Theory, MemberData(nameof(StreamSpecifiers))]
Expand All @@ -226,20 +239,35 @@ public virtual void FileModeAppendExisting(string streamSpecifier)
string fileName = GetTestFilePath() + streamSpecifier;
using (FileStream fs = CreateFileStream(fileName, FileMode.Create))
{
fs.WriteByte(0);
fs.WriteByte((byte)'s');
}

string initialContents = File.ReadAllText(fileName);
using (FileStream fs = CreateFileStream(fileName, FileMode.Append))
{
// Ensure that the file was re-opened and position set to end
Assert.Equal(Math.Max(1L, InitialLength), fs.Length);
Assert.Equal(fs.Length, fs.Position);

long position = fs.Position;
Assert.Equal(fs.Length, position);

Assert.False(fs.CanRead);
Assert.True(fs.CanSeek);
Assert.True(fs.CanWrite);

Assert.Throws<IOException>(() => fs.Seek(-1, SeekOrigin.Current));
Assert.Throws<IOException>(() => fs.Seek(0, SeekOrigin.Begin));
Assert.Throws<NotSupportedException>(() => fs.ReadByte());

fs.Write(Encoding.ASCII.GetBytes("abcde"));
Assert.Equal(position + 5, fs.Position);

Assert.Equal(position, fs.Seek(position, SeekOrigin.Begin));
Assert.Equal(position + 1, fs.Seek(1, SeekOrigin.Current));
fs.Write(Encoding.ASCII.GetBytes("xyz"));
}

Assert.Equal(initialContents + "axyze", File.ReadAllText(fileName));
}
}
}

0 comments on commit d2e9f42

Please sign in to comment.