Skip to content

Commit d2e9f42

Browse files
authored
Augment tests for FileMode.Append (#55513)
To validate that seeking and writing are valid in the region of the file since its initial length.
1 parent 1904cfc commit d2e9f42

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

src/libraries/System.IO.FileSystem/tests/FileStream/Position.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public void SetPositionAppendModify()
2626

2727
fs.Position = length + 1;
2828
Assert.Equal(length + 1, fs.Position);
29+
30+
fs.Write(TestBuffer);
31+
fs.Position = length + 1;
32+
Assert.Equal(length + 1, fs.Position);
2933
}
3034
}
3135

src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public void SeekAppendModifyThrows()
3232
Assert.Equal(length, fs.Position);
3333
Assert.Throws<IOException>(() => fs.Seek(-length, SeekOrigin.End));
3434
Assert.Equal(length, fs.Position);
35+
36+
fs.Write(TestBuffer);
37+
Assert.Equal(length, fs.Seek(length, SeekOrigin.Begin));
3538
}
3639
}
3740
}

src/libraries/System.IO.FileSystem/tests/FileStream/SetLength.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public void SetLengthAppendModifyThrows()
2323
Assert.Equal(length, fs.Length);
2424
Assert.Throws<IOException>(() => fs.SetLength(0));
2525
Assert.Equal(length, fs.Length);
26+
27+
fs.Write(TestBuffer);
28+
Assert.Equal(length + TestBuffer.Length, fs.Length);
29+
30+
fs.SetLength(length);
31+
Assert.Equal(length, fs.Length);
2632
}
2733
}
2834

src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Text;
45
using Xunit;
56

67
namespace System.IO.Tests
@@ -213,11 +214,23 @@ public void FileModeTruncateExisting(string streamSpecifier)
213214
[Theory, MemberData(nameof(StreamSpecifiers))]
214215
public virtual void FileModeAppend(string streamSpecifier)
215216
{
216-
using (FileStream fs = CreateFileStream(GetTestFilePath() + streamSpecifier, FileMode.Append))
217+
string fileName = GetTestFilePath() + streamSpecifier;
218+
using (FileStream fs = CreateFileStream(fileName, FileMode.Append))
217219
{
218220
Assert.False(fs.CanRead);
219221
Assert.True(fs.CanWrite);
222+
223+
fs.Write(Encoding.ASCII.GetBytes("abcde"));
224+
Assert.Equal(5, fs.Length);
225+
Assert.Equal(5, fs.Position);
226+
Assert.Equal(1, fs.Seek(1, SeekOrigin.Begin));
227+
228+
fs.Write(Encoding.ASCII.GetBytes("xyz"));
229+
Assert.Equal(4, fs.Position);
230+
Assert.Equal(5, fs.Length);
220231
}
232+
233+
Assert.Equal("axyze", File.ReadAllText(fileName));
221234
}
222235

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

245+
string initialContents = File.ReadAllText(fileName);
232246
using (FileStream fs = CreateFileStream(fileName, FileMode.Append))
233247
{
234248
// Ensure that the file was re-opened and position set to end
235249
Assert.Equal(Math.Max(1L, InitialLength), fs.Length);
236-
Assert.Equal(fs.Length, fs.Position);
250+
251+
long position = fs.Position;
252+
Assert.Equal(fs.Length, position);
253+
237254
Assert.False(fs.CanRead);
238255
Assert.True(fs.CanSeek);
239256
Assert.True(fs.CanWrite);
257+
240258
Assert.Throws<IOException>(() => fs.Seek(-1, SeekOrigin.Current));
259+
Assert.Throws<IOException>(() => fs.Seek(0, SeekOrigin.Begin));
241260
Assert.Throws<NotSupportedException>(() => fs.ReadByte());
261+
262+
fs.Write(Encoding.ASCII.GetBytes("abcde"));
263+
Assert.Equal(position + 5, fs.Position);
264+
265+
Assert.Equal(position, fs.Seek(position, SeekOrigin.Begin));
266+
Assert.Equal(position + 1, fs.Seek(1, SeekOrigin.Current));
267+
fs.Write(Encoding.ASCII.GetBytes("xyz"));
242268
}
269+
270+
Assert.Equal(initialContents + "axyze", File.ReadAllText(fileName));
243271
}
244272
}
245273
}

0 commit comments

Comments
 (0)