mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-22 19:10:41 -10:00
Prior to this set of changes, 110 of the FileSystem tests were failing on Linux. After this change, all allowed tests pass. Primary product fixes: - Using rmdir/unlink for directory/file deletion rather than using remove - Disallowing FileStreams to be opened for directories - Supporting copying files when only target directory (not target name) is specified - Overhauling directory enumeration to ensure to match Win32 behaviors related to search pattern normalization, input validation, supporting re-iteration through an enumerable, outputting partial paths based on user-supplied path, using a SafeHandle to ensure resources are cleaned up - Fixing FileSystemObject.LastWriteTime to correctly return the LastWriteTime rather than LastAccessTime - Fixing FileAttributes.ReadOnly handling due to inappropriate caching - Validating inputs to FileSystemObject.Attributes, and make sure that changes to attributes are visible afterwards - Adding support for getting CreationTime if it's supported by the OS - Marking links with FileAttributes.ReparsePoint - Working around current runtime bug related to exception handling and finally blocks getting executed twice Primary test fixes: - Suppressing tests as platform-specific that dependend on Win32-isms like drive letters, alternate data streams, etc. - Changing a bunch of '\' usage to instead use Path.DirectorySeparatorChar or Path.Combine - Adapting to behavioral differences between Windows and Unix, e.g. whether a directory can be deleted if it's readonly, invalid path characters, max path lengths, file sharing differences, etc. These changes should all accrue to OSX as well, but we'll need to separately do an exercise to clean this library and tests up there for anything OSX-specific. Commit migrated from https://github.com/dotnet/corefx/commit/dd48c9dc1f0a7142a005ff1d4c162e10facda306
278 lines
9.0 KiB
C#
278 lines
9.0 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;
|
|
using System.Runtime.CompilerServices;
|
|
using System.IO;
|
|
using Xunit;
|
|
|
|
public class FileInfo_Create
|
|
{
|
|
public static String s_strDtTmVer = "2009/02/18";
|
|
public static String s_strClassMethod = "FileInfo.Create";
|
|
public static String s_strTFName = "Create.cs";
|
|
public static String s_strTFPath = Directory.GetCurrentDirectory();
|
|
|
|
[Fact]
|
|
public static void runTest()
|
|
{
|
|
int iCountErrors = 0;
|
|
int iCountTestcases = 0;
|
|
String strLoc = "Loc_000oo";
|
|
|
|
try
|
|
{
|
|
FileInfo file2 = null;
|
|
FileStream fs;
|
|
String fileName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
|
|
|
|
try
|
|
{
|
|
new FileInfo(fileName).Delete();
|
|
}
|
|
catch (Exception) { }
|
|
|
|
strLoc = "Loc_099u8";
|
|
|
|
// [] Create a File with '.'.
|
|
iCountTestcases++;
|
|
file2 = new FileInfo(".");
|
|
|
|
try
|
|
{
|
|
file2.Create();
|
|
iCountErrors++;
|
|
printerr("Error_298dy! Expected exception not thrown, file2==" + file2.FullName);
|
|
file2.Delete();
|
|
}
|
|
catch (UnauthorizedAccessException)
|
|
{
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_209xj! Incorrect exception thrown, exc==" + exc.ToString());
|
|
}
|
|
|
|
// [] Try to create an existing file.
|
|
|
|
strLoc = "Loc_209xc";
|
|
|
|
iCountTestcases++;
|
|
fileName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
|
|
file2 = new FileInfo(fileName);
|
|
fs = file2.Create();
|
|
fs.Dispose();
|
|
file2 = new FileInfo(fileName);
|
|
try
|
|
{
|
|
fs = file2.Create();
|
|
fs.Dispose();
|
|
file2.Delete();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_6566! Incorrect exception thrown, exc==" + exc.ToString());
|
|
}
|
|
|
|
// [] Pass valid File name
|
|
|
|
strLoc = "Loc_498vy";
|
|
|
|
iCountTestcases++;
|
|
fileName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName()); ;
|
|
file2 = new FileInfo(fileName);
|
|
try
|
|
{
|
|
fs = file2.Create();
|
|
if (file2.FullName != fileName)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_0010! Unexpected File name :: " + file2.FullName);
|
|
}
|
|
fs.Dispose();
|
|
file2.Delete();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_28829! Unexpected exception thrown, exc==" + exc.ToString());
|
|
}
|
|
|
|
|
|
|
|
// [] Try to create the file in the parent directory
|
|
|
|
strLoc = "Loc_09t83";
|
|
|
|
iCountTestcases++;
|
|
fileName = Path.Combine(TestInfo.CurrentDirectory, "abc", "..", Path.GetRandomFileName());
|
|
file2 = new FileInfo(fileName);
|
|
try
|
|
{
|
|
fs = file2.Create();
|
|
fs.Dispose();
|
|
file2.Delete();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_0980! Incorrect exception thrown, exc==" + exc.ToString());
|
|
}
|
|
|
|
// [] Create a file with symbols.
|
|
|
|
strLoc = "Loc_87yg7";
|
|
|
|
iCountTestcases++;
|
|
fileName = Path.Combine(TestInfo.CurrentDirectory, "!@#$%^&");
|
|
file2 = new FileInfo(fileName);
|
|
fs = file2.Create();
|
|
fs.Dispose();
|
|
if (!file2.FullName.Equals(fileName))
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_0109x! Incorrect File name, file2==" + file2.FullName);
|
|
}
|
|
file2.Delete();
|
|
|
|
// [] Create a file in nested sub directory
|
|
|
|
strLoc = "Loc_209ud";
|
|
|
|
iCountTestcases++;
|
|
fileName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
|
|
file2 = new FileInfo(Path.Combine(TestInfo.CurrentDirectory, "Test", "Test", "Test", fileName));
|
|
try
|
|
{
|
|
file2.Create();
|
|
if (file2.FullName.IndexOf(Path.Combine("Test", "Test", "Test", fileName)) == -1)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_0010! Unexpected File name :: " + file2.FullName);
|
|
}
|
|
}
|
|
catch (DirectoryNotFoundException)
|
|
{
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_2019u! Incorrect exception thrown, exc==" + exc.ToString());
|
|
}
|
|
|
|
// [] Try invalid File name
|
|
|
|
strLoc = "Loc_2089x";
|
|
|
|
iCountTestcases++;
|
|
try
|
|
{
|
|
file2 = new FileInfo("\0");
|
|
file2.Create();
|
|
iCountErrors++;
|
|
printerr("Error_19883! Expected exception not thrown, file2==" + file2.FullName);
|
|
file2.Delete();
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_0198xu! Incorrect exception thrown, exc==" + exc.ToString());
|
|
}
|
|
|
|
// [] Pass nested level File structure.
|
|
|
|
strLoc = "Loc_098gt";
|
|
|
|
iCountTestcases++;
|
|
fileName = Path.GetRandomFileName();
|
|
file2 = new FileInfo(Path.Combine(TestInfo.CurrentDirectory, "abc", "xyz", "..", "..", fileName));
|
|
try
|
|
{
|
|
fs = file2.Create();
|
|
fs.Dispose();
|
|
file2.Delete();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_9092c! Unexpected exception thrown, exc==" + exc.ToString());
|
|
}
|
|
|
|
/* Test disabled because it modifies state outside the current working directory
|
|
// [] Create a file in root
|
|
strLoc = "Loc_89ytb";
|
|
|
|
String CurrentDirectory2 = Directory.GetCurrentDirectory();
|
|
fileName = Path.GetRandomFileName();
|
|
String tempPath = CurrentDirectory2.Substring(0, CurrentDirectory2.IndexOf("\\") + 1) + fileName;
|
|
if (!File.Exists(tempPath) && !Directory.Exists(tempPath))
|
|
{
|
|
file2 = new FileInfo(tempPath);
|
|
fs = file2.Create();
|
|
fs.Dispose();
|
|
iCountTestcases++;
|
|
if (!file2.Exists)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_t78g7! File not created, file==" + file2.FullName);
|
|
}
|
|
file2.Delete();
|
|
}
|
|
*/
|
|
|
|
if (!Interop.IsLinux) // testing case insensitivity
|
|
{
|
|
// [] Create file in current file2 by giving full File check casing as well
|
|
strLoc = "loc_89tbh";
|
|
fileName = Path.GetRandomFileName();
|
|
file2 = new FileInfo(Path.Combine(TestInfo.CurrentDirectory.ToLowerInvariant(), fileName));
|
|
fs = file2.Create();
|
|
fs.Dispose();
|
|
iCountTestcases++;
|
|
if (!file2.Exists)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_t87gy! File not created, file==" + file2.FullName);
|
|
}
|
|
file2.Delete();
|
|
|
|
strLoc = "loc_89mjd";
|
|
fileName = Path.GetRandomFileName();
|
|
file2 = new FileInfo(Path.Combine(TestInfo.CurrentDirectory.ToUpperInvariant(), fileName));
|
|
fs = file2.Create();
|
|
fs.Dispose();
|
|
iCountTestcases++;
|
|
if (!file2.Exists)
|
|
{
|
|
iCountErrors++;
|
|
printerr("Error_hf3t4! File not created, file==" + file2.FullName);
|
|
}
|
|
file2.Delete();
|
|
}
|
|
}
|
|
catch (Exception exc_general)
|
|
{
|
|
++iCountErrors;
|
|
Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
|
|
}
|
|
//// Finish Diagnostics
|
|
if (iCountErrors != 0)
|
|
{
|
|
Console.WriteLine("FAiL! " + s_strTFName + " ,iCountErrors==" + iCountErrors.ToString());
|
|
}
|
|
|
|
Assert.Equal(0, iCountErrors);
|
|
}
|
|
|
|
public static void printerr(String err, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
|
|
{
|
|
Console.WriteLine("ERROR: ({0}, {1}, {2}) {3}", memberName, filePath, lineNumber, err);
|
|
}
|
|
}
|
|
|