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

Augment tests for FileMode.Append #55513

Merged
merged 1 commit into from
Jul 13, 2021
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
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect that FileMode.Append means "I want to append data to EOF" rather than "I want to write data at requested offset". The official doc for FileMode.Append says:

Trying to seek to a position before the end of the file throws an IOException exception

I believe that current implementation of Append is buggy and we should fix it instead of adding a test that ensures that it's buggy. I am going to reopen #55465 for that

Copy link
Member Author

@stephentoub stephentoub Aug 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I interpret that to mean before the end of the file at the time it was opened, and the current implementation is consistent with that and has been for 20 years. If you believe it's worth taking a breaking change, that's a fine discussion to have, but it's absolutely a breaking change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding a test that ensures that it's buggy

The tests validate the expected behavior.


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));
}
}
}