forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement API AppendAllBytes 84532 (dotnet#93519)
* Implement AppendAllBytes API * Add AppendAllBytes declaration * Add unit tests for AppendAllBytes * Add AppendAllBytesAsync tests * Add AppendAllBytesAsync implemntation * Add AppendAllBytesAsync definition * Removed unnecessary seek capability * Changed FileMode to Append to make it more readable and match the behavior * Move AppendAllBytes to separate class * Add extra tests to cover more scenarios * Add tests to cover AppendAllBytesAsync * Remove unnecessary delete test file, becuase its removed by cleanup * Add API documentation * Remove extra space Co-authored-by: David Cantú <dacantu@microsoft.com> * Remove extra space Co-authored-by: David Cantú <dacantu@microsoft.com> * update description of API Co-authored-by: David Cantú <dacantu@microsoft.com> * Update param description Co-authored-by: David Cantú <dacantu@microsoft.com> * Remove extra space Co-authored-by: David Cantú <dacantu@microsoft.com> * Remove extra space Co-authored-by: David Cantú <dacantu@microsoft.com> * Remove unnecessary test scenario Co-authored-by: David Cantú <dacantu@microsoft.com> * Remove unnecessary test scenario Co-authored-by: David Cantú <dacantu@microsoft.com> * Remove extra space Co-authored-by: David Cantú <dacantu@microsoft.com> * Fix docusmentation of API * Apply suggestions from code review - Remove unwanted spaces in files - Fix API documentation - Add assertation for file exists Co-authored-by: David Cantú <dacantu@microsoft.com> * Add test for scenario when directory not exists * Fix Async documentation and match it with none-async version * Apply suggestions from code review * Change tabs for spaces --------- Co-authored-by: Moji <mojtabatajik@hotmail.com> Co-authored-by: David Cantú <dacantu@microsoft.com>
- Loading branch information
1 parent
b07b990
commit f28a8a7
Showing
5 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
src/libraries/System.IO.FileSystem/tests/File/AppendAllBytes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.IO.Tests; | ||
using System.Linq; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public class File_AppendAllBytes : FileSystemTest | ||
{ | ||
|
||
[Fact] | ||
public void NullParameters() | ||
{ | ||
string path = GetTestFilePath(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => File.AppendAllBytes(null, new byte[0])); | ||
Assert.Throws<ArgumentNullException>(() => File.AppendAllBytes(path, null)); | ||
} | ||
|
||
[Fact] | ||
public void NonExistentPath() | ||
{ | ||
Assert.Throws<DirectoryNotFoundException>(() => File.AppendAllBytes(Path.Combine(TestDirectory, GetTestFileName(), GetTestFileName()), new byte[0])); | ||
} | ||
|
||
[Fact] | ||
public void InvalidParameters() | ||
{ | ||
Assert.Throws<ArgumentException>(() => File.AppendAllBytes(string.Empty, new byte[0])); | ||
} | ||
|
||
|
||
[Fact] | ||
public void AppendAllBytes_WithValidInput_AppendsBytes() | ||
{ | ||
string path = GetTestFilePath(); | ||
|
||
byte[] initialBytes = Encoding.UTF8.GetBytes("bytes"); | ||
byte[] additionalBytes = Encoding.UTF8.GetBytes("additional bytes"); | ||
|
||
File.WriteAllBytes(path, initialBytes); | ||
File.AppendAllBytes(path, additionalBytes); | ||
|
||
byte[] result = File.ReadAllBytes(path); | ||
|
||
byte[] expectedBytes = initialBytes.Concat(additionalBytes).ToArray(); | ||
|
||
Assert.True(result.SequenceEqual(expectedBytes)); | ||
} | ||
|
||
|
||
[Fact] | ||
public void EmptyContentCreatesFile() | ||
{ | ||
string path = GetTestFilePath(); | ||
Assert.False(File.Exists(path)); | ||
File.AppendAllBytes(path, new byte[0]); | ||
Assert.True(File.Exists(path)); | ||
Assert.Empty(File.ReadAllBytes(path)); | ||
} | ||
|
||
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsFileLockingEnabled))] | ||
public void OpenFile_ThrowsIOException() | ||
{ | ||
string path = GetTestFilePath(); | ||
byte[] bytes = Encoding.UTF8.GetBytes("bytes"); | ||
|
||
using (File.Create(path)) | ||
{ | ||
Assert.Throws<IOException>(() => File.AppendAllBytes(path, bytes)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// On Unix, modifying a file that is ReadOnly will fail under normal permissions. | ||
/// If the test is being run under the superuser, however, modification of a ReadOnly | ||
/// file is allowed. On Windows, modifying a file that is ReadOnly will always fail. | ||
/// </summary> | ||
[Fact] | ||
public void AppendToReadOnlyFileAsync() | ||
{ | ||
string path = GetTestFilePath(); | ||
File.Create(path).Dispose(); | ||
File.SetAttributes(path, FileAttributes.ReadOnly); | ||
byte[] dataToAppend = Encoding.UTF8.GetBytes("bytes"); | ||
|
||
try | ||
{ | ||
if (PlatformDetection.IsNotWindows && PlatformDetection.IsPrivilegedProcess) | ||
{ | ||
File.AppendAllBytes(path, dataToAppend); | ||
Assert.Equal(dataToAppend, File.ReadAllBytes(path)); | ||
} | ||
else | ||
{ | ||
Assert.Throws<UnauthorizedAccessException>(() => File.AppendAllBytes(path, dataToAppend)); | ||
} | ||
} | ||
finally | ||
{ | ||
File.SetAttributes(path, FileAttributes.Normal); | ||
} | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/libraries/System.IO.FileSystem/tests/File/AppendAllBytesAsync.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public class File_AppendAllBytesAsync : FileSystemTest | ||
{ | ||
|
||
[Fact] | ||
public async Task NullParametersAsync() | ||
{ | ||
string path = GetTestFilePath(); | ||
|
||
await Assert.ThrowsAsync<ArgumentNullException>("path", async () => await File.AppendAllBytesAsync(null, new byte[0])); | ||
await Assert.ThrowsAsync<ArgumentNullException>("bytes", async () => await File.AppendAllBytesAsync(path, null)); | ||
} | ||
|
||
[Fact] | ||
public void NonExistentPathAsync() | ||
{ | ||
Assert.ThrowsAsync<DirectoryNotFoundException>(() => File.AppendAllBytesAsync(Path.Combine(TestDirectory, GetTestFileName(), GetTestFileName()), new byte[0])); | ||
} | ||
|
||
[Fact] | ||
public async Task InvalidParametersAsync() | ||
{ | ||
await Assert.ThrowsAsync<ArgumentException>("path", async () => await File.AppendAllBytesAsync(string.Empty, new byte[0])); | ||
} | ||
|
||
[Fact] | ||
public async Task AppendAllBytesAsync_WithValidInput_AppendsBytes() | ||
{ | ||
string path = GetTestFilePath(); | ||
|
||
byte[] initialBytes = Encoding.UTF8.GetBytes("bytes"); | ||
byte[] additionalBytes = Encoding.UTF8.GetBytes("additional bytes"); | ||
|
||
await File.WriteAllBytesAsync(path, initialBytes); | ||
await File.AppendAllBytesAsync(path, additionalBytes); | ||
|
||
byte[] result = await File.ReadAllBytesAsync(path); | ||
|
||
byte[] expectedBytes = initialBytes.Concat(additionalBytes).ToArray(); | ||
|
||
Assert.True(result.SequenceEqual(expectedBytes)); | ||
} | ||
|
||
[Fact] | ||
public async Task EmptyContentCreatesFileAsync() | ||
{ | ||
string path = GetTestFilePath(); | ||
await File.AppendAllBytesAsync(path, new byte[0]); | ||
Assert.True(File.Exists(path)); | ||
Assert.Empty(await File.ReadAllBytesAsync(path)); | ||
} | ||
|
||
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsFileLockingEnabled))] | ||
public async Task OpenFile_ThrowsIOExceptionAsync() | ||
{ | ||
string path = GetTestFilePath(); | ||
byte[] bytes = Encoding.UTF8.GetBytes("bytes"); | ||
|
||
using (File.Create(path)) | ||
{ | ||
await Assert.ThrowsAsync<IOException>(async () => await File.AppendAllBytesAsync(path, bytes)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// On Unix, modifying a file that is ReadOnly will fail under normal permissions. | ||
/// If the test is being run under the superuser, however, modification of a ReadOnly | ||
/// file is allowed. On Windows, modifying a file that is ReadOnly will always fail. | ||
/// </summary> | ||
[Fact] | ||
public async Task AppendToReadOnlyFileAsync() | ||
{ | ||
string path = GetTestFilePath(); | ||
File.Create(path).Dispose(); | ||
File.SetAttributes(path, FileAttributes.ReadOnly); | ||
byte[] dataToAppend = Encoding.UTF8.GetBytes("bytes"); | ||
|
||
try | ||
{ | ||
if (PlatformDetection.IsNotWindows && PlatformDetection.IsPrivilegedProcess) | ||
{ | ||
await File.AppendAllBytesAsync(path, dataToAppend); | ||
Assert.Equal(dataToAppend, await File.ReadAllBytesAsync(path)); | ||
} | ||
else | ||
{ | ||
await Assert.ThrowsAsync<UnauthorizedAccessException>(async () => await File.AppendAllBytesAsync(path, dataToAppend)); | ||
} | ||
} | ||
finally | ||
{ | ||
File.SetAttributes(path, FileAttributes.Normal); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters