Files
dotnet-runtime/src/libraries/System.IO.FileSystem/tests/File/ReadWriteAllLines.cs
T
4dbe414c31 [browser][file system] Tests System.IO.FileSystem (#39768)
* [browser][file system] Tests System.IO.FileSystem

* Remove FileSystem from test project exclusions.

* [browser][filesystem] Comment on skipped tests due to IO.Pipes not supported

* [browser][filesystem] Comment on skipped tests due to monitor not supported

* Address review comments

* Address review comments on how to handle monitor PNSE

* Update src/libraries/System.IO.FileSystem/tests/File/GetSetTimes.cs

Co-authored-by: Jan Kotas <[email protected]>

* Add ActiveIssue for browser platform

```
        [ActiveIssue("https://github.com/dotnet/runtime/issues/39955", TestPlatforms.Browser)]
        public void NotFoundErrorIsExpected()
```

* Simplify test case with `PlatformDetection.IsSuperUser` as per review comment

* Remove unused variable from tests

* Browser platform volume does not limit each component of the path to a total of 255 characters.

- Remove the test `DirectoryWithComponentLongerThanMaxComponentAsPath_ThrowsException` for Browser platform.
- Add new test `DirectoryWithComponentLongerThanMaxComponentAsPath_BrowserDoesNotThrowsException` for Browser platform in case this check is added in the future.

* Fix windows tests

* Add ActiveIssue to test System.IO.Tests.DirectoryInfo_Name.CurrentDirectory

- System.IO.Tests.DirectoryInfo_Name.CurrentDirectory does not pass because it is using Path.GetFileName on the current directory of "/".
- See issue https://github.com/dotnet/runtime/issues/39998 for more information

* Add active issue for tests that need to support file locking.

* Add active issue for tests that need to support file locking.

* Browser volume does not have a limit on segments

* Remove browser platform test

* Remove active issue for NotFoundErrorIsExpected

* Remove platform specific from SkippingHiddenFiles

* Fix enumeration test errors failing from `HiddenFilesAreReturned`.

- WebAssembly (BROWSER) has dirent d_type but is not identifying correctly but by returning UNKNOWN the managed code properly stats the file to detect if entry is directory or not.

* Fix enumeration test errors failing from `HiddenFilesAreReturned`.

- WebAssembly (BROWSER) has dirent d_type but is not identifying correctly but by returning UNKNOWN the managed code properly stats the file to detect if entry is directory or not.

* Fix build for TARGET_WASM not defined

* Address review comment by add check for IsWindows to IsSuperUser.

* Address review comment by add check for IsWindows to IsSuperUser.

* Platform Specific test for hidden files no longer needed.

* Platform Specific test fno longer needed.

* Use active issue for rename issue

* ActiveIssue no longer needed

- PR https://github.com/dotnet/runtime/pull/40310 works around the issue for now while waiting for fix https://github.com/emscripten-core/emscripten/issues/11804 / https://github.com/emscripten-core/emscripten/pull/11812

* Use ActiveIssue for SettingUpdatesProperties

* Use ActiveIssue for TimesIncludeMillisecondPart

* Use ActiveIssue for ErrorHandlingTests failures

* Use ActiveIssue for SetUptoNanoseconds

* Use ActiveIssue for GetSetTimes test failures

* Use ActiveIssue for tests failing with DirectoryNotFoundException

* Use ActiveIssue for tests failing with UnauthorizedAccessException

* Remove debugging statement from pal_io

* Address type in CreateDirectory method name

Co-authored-by: Jan Kotas <[email protected]>
Co-authored-by: Mitchell Hwang <[email protected]>
2020-08-10 16:35:25 -04:00

360 lines
11 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
namespace System.IO.Tests
{
public class File_ReadWriteAllLines_Enumerable : FileSystemTest
{
#region Utilities
protected virtual void Write(string path, string[] content)
{
File.WriteAllLines(path, (IEnumerable<string>)content);
}
protected virtual string[] Read(string path)
{
return File.ReadAllLines(path);
}
#endregion
#region UniversalTests
[Fact]
public void InvalidPath()
{
Assert.Throws<ArgumentNullException>(() => Write(null, new string[] { "Text" }));
Assert.Throws<ArgumentException>(() => Write(string.Empty, new string[] { "Text" }));
Assert.Throws<ArgumentNullException>(() => Read(null));
Assert.Throws<ArgumentException>(() => Read(string.Empty));
}
[Fact]
public void NullLines()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => Write(path, null));
Write(path, new string[] { null });
Assert.Equal(new string[] {""}, Read(path));
}
[Fact]
public void EmptyStringCreatesFile()
{
string path = GetTestFilePath();
Write(path, new string[] { });
Assert.True(File.Exists(path));
Assert.Empty(Read(path));
}
[Theory]
[InlineData(0)]
[InlineData(100)]
public void ValidWrite(int size)
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', size) };
File.Create(path).Dispose();
Write(path, lines);
Assert.Equal(lines, Read(path));
}
[Fact]
public virtual void Overwrite()
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', 200) };
string[] overwriteLines = new string[] { new string('b', 100) };
Write(path, lines);
Write(path, overwriteLines);
Assert.Equal(overwriteLines, Read(path));
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/40065", TestPlatforms.Browser)]
public void OpenFile_ThrowsIOException()
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', 200) };
using (File.Create(path))
{
Assert.Throws<IOException>(() => Write(path, lines));
Assert.Throws<IOException>(() => Read(path));
}
}
[Fact]
public void Read_FileNotFound()
{
string path = GetTestFilePath();
Assert.Throws<FileNotFoundException>(() => Read(path));
}
/// <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.
/// </summary>
[Fact]
public void WriteToReadOnlyFile()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
File.SetAttributes(path, FileAttributes.ReadOnly);
try
{
// Operation succeeds when being run by the Unix superuser
if (PlatformDetection.IsSuperUser)
{
Write(path, new string[] { "text" });
Assert.Equal(new string[] { "text" }, Read(path));
}
else
Assert.Throws<UnauthorizedAccessException>(() => Write(path, new string[] { "text" }));
}
finally
{
File.SetAttributes(path, FileAttributes.Normal);
}
}
[Fact]
public void DisposingEnumeratorClosesFile()
{
string path = GetTestFilePath();
Write(path, new[] { "line1", "line2", "line3" });
IEnumerable<string> readLines = File.ReadLines(path);
using (IEnumerator<string> e1 = readLines.GetEnumerator())
using (IEnumerator<string> e2 = readLines.GetEnumerator())
{
Assert.Same(readLines, e1);
Assert.NotSame(e1, e2);
}
// File should be closed deterministically; this shouldn't throw.
File.OpenWrite(path).Dispose();
}
#endregion
}
public class File_ReadWriteAllLines_Enumerable_Encoded : File_ReadWriteAllLines_Enumerable
{
protected override void Write(string path, string[] content)
{
File.WriteAllLines(path, (IEnumerable<string>)content, new UTF8Encoding(false));
}
protected override string[] Read(string path)
{
return File.ReadAllLines(path, new UTF8Encoding(false));
}
[Fact]
public void NullEncoding()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => File.WriteAllLines(path, (IEnumerable<string>)new string[] { "Text" }, null));
Assert.Throws<ArgumentNullException>(() => File.ReadAllLines(path, null));
}
}
public class File_ReadLines : File_ReadWriteAllLines_Enumerable
{
protected override string[] Read(string path)
{
return File.ReadLines(path).ToArray();
}
}
public class File_ReadLines_Encoded : File_ReadWriteAllLines_Enumerable
{
protected override void Write(string path, string[] content)
{
File.WriteAllLines(path, (IEnumerable<string>)content, new UTF8Encoding(false));
}
protected override string[] Read(string path)
{
return File.ReadLines(path, new UTF8Encoding(false)).ToArray();
}
[Fact]
public void NullEncoding()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => File.WriteAllLines(path, (IEnumerable<string>)new string[] { "Text" }, null));
Assert.Throws<ArgumentNullException>(() => File.ReadLines(path, null));
}
}
public class File_ReadWriteAllLines_StringArray : FileSystemTest
{
#region Utilities
protected virtual void Write(string path, string[] content)
{
File.WriteAllLines(path, content);
}
protected virtual string[] Read(string path)
{
return File.ReadAllLines(path);
}
#endregion
#region UniversalTests
[Fact]
public void InvalidPath()
{
Assert.Throws<ArgumentNullException>(() => Write(null, new string[] { "Text" }));
Assert.Throws<ArgumentException>(() => Write(string.Empty, new string[] { "Text" }));
Assert.Throws<ArgumentNullException>(() => Read(null));
Assert.Throws<ArgumentException>(() => Read(string.Empty));
}
[Fact]
public void NullLines()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => Write(path, null));
Write(path, new string[] { null });
Assert.Equal(new string[] {""}, Read(path));
}
[Fact]
public void EmptyStringCreatesFile()
{
string path = GetTestFilePath();
Write(path, new string[] { });
Assert.True(File.Exists(path));
Assert.Empty(Read(path));
}
[Theory]
[InlineData(0)]
[InlineData(100)]
public void ValidWrite(int size)
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', size) };
File.Create(path).Dispose();
Write(path, lines);
Assert.Equal(lines, Read(path));
}
[Fact]
public virtual void Overwrite()
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', 200) };
string[] overwriteLines = new string[] { new string('b', 100) };
Write(path, lines);
Write(path, overwriteLines);
Assert.Equal(overwriteLines, Read(path));
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/40065", TestPlatforms.Browser)]
public void OpenFile_ThrowsIOException()
{
string path = GetTestFilePath();
string[] lines = new string[] { new string('c', 200) };
using (File.Create(path))
{
Assert.Throws<IOException>(() => Write(path, lines));
Assert.Throws<IOException>(() => Read(path));
}
}
[Fact]
public void Read_FileNotFound()
{
string path = GetTestFilePath();
Assert.Throws<FileNotFoundException>(() => Read(path));
}
/// <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.
/// </summary>
[Fact]
public void WriteToReadOnlyFile()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
File.SetAttributes(path, FileAttributes.ReadOnly);
try
{
// Operation succeeds when being run by the Unix superuser
if (PlatformDetection.IsSuperUser)
{
Write(path, new string[] { "text" });
Assert.Equal(new string[] { "text" }, Read(path));
}
else
Assert.Throws<UnauthorizedAccessException>(() => Write(path, new string[] { "text" }));
}
finally
{
File.SetAttributes(path, FileAttributes.Normal);
}
}
[Fact]
public void DisposingEnumeratorClosesFile()
{
string path = GetTestFilePath();
Write(path, new[] { "line1", "line2", "line3" });
IEnumerable<string> readLines = File.ReadLines(path);
using (IEnumerator<string> e1 = readLines.GetEnumerator())
using (IEnumerator<string> e2 = readLines.GetEnumerator())
{
Assert.Same(readLines, e1);
Assert.NotSame(e1, e2);
}
// File should be closed deterministically; this shouldn't throw.
File.OpenWrite(path).Dispose();
}
#endregion
}
public class File_ReadWriteAllLines_StringArray_Encoded : File_ReadWriteAllLines_StringArray
{
protected override void Write(string path, string[] content)
{
File.WriteAllLines(path, content, new UTF8Encoding(false));
}
protected override string[] Read(string path)
{
return File.ReadAllLines(path, new UTF8Encoding(false));
}
[Fact]
public void NullEncoding()
{
string path = GetTestFilePath();
Assert.Throws<ArgumentNullException>(() => File.WriteAllLines(path, new string[] { "Text" }, null));
Assert.Throws<ArgumentNullException>(() => File.ReadAllLines(path, null));
}
}
}