Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStephen Toub <stoub@microsoft.com>2021-07-13 19:05:31 +0300
committerGitHub <noreply@github.com>2021-07-13 19:05:31 +0300
commitd2e9f42c609453a1104afa1a006242ba69b524cf (patch)
tree41bdf2aadc67c6b80762796477904040fe5d8f45 /src
parent1904cfcbe436e73b3aef7f83bc36ee4c262b3196 (diff)
Augment tests for FileMode.Append (#55513)
To validate that seeking and writing are valid in the region of the file since its initial length.
Diffstat (limited to 'src')
-rw-r--r--src/libraries/System.IO.FileSystem/tests/FileStream/Position.cs4
-rw-r--r--src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs3
-rw-r--r--src/libraries/System.IO.FileSystem/tests/FileStream/SetLength.cs6
-rw-r--r--src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs34
4 files changed, 44 insertions, 3 deletions
diff --git a/src/libraries/System.IO.FileSystem/tests/FileStream/Position.cs b/src/libraries/System.IO.FileSystem/tests/FileStream/Position.cs
index 8157f6cc91e..32817dcee26 100644
--- a/src/libraries/System.IO.FileSystem/tests/FileStream/Position.cs
+++ b/src/libraries/System.IO.FileSystem/tests/FileStream/Position.cs
@@ -26,6 +26,10 @@ namespace System.IO.Tests
fs.Position = length + 1;
Assert.Equal(length + 1, fs.Position);
+
+ fs.Write(TestBuffer);
+ fs.Position = length + 1;
+ Assert.Equal(length + 1, fs.Position);
}
}
diff --git a/src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs b/src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs
index 6971eb49927..963a1fddff5 100644
--- a/src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs
+++ b/src/libraries/System.IO.FileSystem/tests/FileStream/Seek.cs
@@ -32,6 +32,9 @@ namespace System.IO.Tests
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));
}
}
}
diff --git a/src/libraries/System.IO.FileSystem/tests/FileStream/SetLength.cs b/src/libraries/System.IO.FileSystem/tests/FileStream/SetLength.cs
index 67944a45771..3a3f547d070 100644
--- a/src/libraries/System.IO.FileSystem/tests/FileStream/SetLength.cs
+++ b/src/libraries/System.IO.FileSystem/tests/FileStream/SetLength.cs
@@ -23,6 +23,12 @@ namespace System.IO.Tests
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);
}
}
diff --git a/src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs b/src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs
index 60e3cd49ea7..47e3b492373 100644
--- a/src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs
+++ b/src/libraries/System.IO.FileSystem/tests/FileStream/ctor_str_fm.cs
@@ -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
@@ -213,11 +214,23 @@ namespace System.IO.Tests
[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))]
@@ -226,20 +239,35 @@ namespace System.IO.Tests
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));
}
}
}