Files
dotnet-runtime/src/libraries/System.IO.FileSystem/tests/File/ReadWriteAllLines.cs
T
Ian Hays 8d7c94172a - Resolves dotnet/corefx#2458. FileInfo.Open(FileMode.Append) didn't allow appending per so I modified FileInfo to allow it
- References dotnet/corefx#2460. File.Move and FileInfo.MoveTo no longer always overwrite the destination file; added a non-concurrent validity check to MoveFile in the Unix implementation to serve as a temporary solution.
- Resolves dotnet/corefx#2459. Reenabled Directory_CreateDirectory.DriveLetter_Unix as it's previous failures were not the fault of the method but of the system.
- Added test checks to work around the issue in dotnet/corefx#2537 until a final decision is reached for the desktop.
- Added Interop.Link to the Interop files for the (possible) implementation of File.Move
- Consolidated all of the related files that did nothing but inherit from other classes (e.g. File_GetFiles_str and File_GetFiles_str_str were consolidated into File_GetFiles)
- Removed the "partial" keyword from some test classes
- Finished overhauling all test cases that don't directly require solutions to Build System issues before being re-added (e.g. those that require networked drives or altering the current directory)
- Added some tests to increase code coverage slightly in some cases.
- All classes and files have been renamed to match the standard set by FileStream: only include parameters in the test/class name if there are overloads
- Modified the Unix behavior for distinguishing between a FileNotFoundException and a DirectoryNotFoundException to match that of Windows.
- Removed some redundant error checks that never catch
- DirectoryInfo.MoveTo(destPath) did not have checks for destinations with paths that are over MAXPATH. I added them to better match the behavior and error timing of Directory.Move(source, dest).


Commit migrated from https://github.com/dotnet/corefx/commit/481d5c645763da79145af50d123db3f4041aa05e
2015-07-29 08:04:18 -07:00

162 lines
4.8 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Linq;
using System.Text;
using Xunit;
namespace System.IO.FileSystem.Tests
{
public class File_ReadWriteAllLines : 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]
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));
}
[Fact]
public void WriteToReadOnlyFile_UnauthException()
{
string path = GetTestFilePath();
File.Create(path).Dispose();
File.SetAttributes(path, FileAttributes.ReadOnly);
Assert.Throws<UnauthorizedAccessException>(() => Write(path, new string[] { "text" }));
File.SetAttributes(path, FileAttributes.Normal);
}
#endregion
}
public class File_ReadWriteAllLines_Encoded : File_ReadWriteAllLines
{
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));
}
}
public class File_ReadLines : File_ReadWriteAllLines
{
protected override string[] Read(string path)
{
return File.ReadLines(path).ToArray();
}
}
public class File_ReadLines_Encoded : File_ReadWriteAllLines
{
protected override void Write(string path, string[] content)
{
File.WriteAllLines(path, 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, new string[] { "Text" }, null));
Assert.Throws<ArgumentNullException>(() => File.ReadLines(path, null));
}
}
}