mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-22 19:10:41 -10:00
Many of our RemoteInvokes are there purely because of needing to change the current thread culture but on UAP that actually impacting the whole process. Until that's addressed, we can at least avoid the RemoteInvoke on all platforms other than UAP. There are also some RemoteInvokes that can be avoided by just serializing all of the (small number of) tests in a test assembly. And there's some additional cleanup to be done as well around not explicitly returning an exit code unless required. This helps measurably with test performance. Consider the System.ComponentModel.Annotations test suite. On my machine, prior to these changes, it took ~25 seconds; after these changes, it takes ~0.3. It helps even more when running against a checked runtime, where all of our test costs are magnified. With a checked runtime, prior to these changes, it takes ~196 seconds; after these changes, it takes ~2.5 seconds. This should help measurably with CI time in coreclr. Commit migrated from https://github.com/dotnet/corefx/commit/cd18bafbabf7fe47601f6f790d157409034a2477
184 lines
6.0 KiB
C#
184 lines
6.0 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Linq;
|
|
using Microsoft.DotNet.RemoteExecutor;
|
|
using Xunit;
|
|
|
|
namespace System.IO.Tests
|
|
{
|
|
public class FileInfo_Exists : FileSystemTest
|
|
{
|
|
[Fact]
|
|
public void InitializeExistsBeforeCreation()
|
|
{
|
|
string fileName = GetTestFilePath();
|
|
FileInfo di = new FileInfo(fileName);
|
|
// don't check it, data has not yet been init'ed
|
|
File.Create(fileName).Dispose();
|
|
// data will be init'ed at the time of calling exists
|
|
Assert.True(di.Exists);
|
|
}
|
|
|
|
[Fact]
|
|
public void InitializeExistsAfterCreation()
|
|
{
|
|
string fileName = GetTestFilePath();
|
|
FileInfo di = new FileInfo(fileName);
|
|
|
|
Assert.False(di.Exists);
|
|
File.Create(fileName).Dispose();
|
|
|
|
// data should be stale
|
|
Assert.False(di.Exists);
|
|
|
|
// force refresh
|
|
di.Refresh();
|
|
Assert.True(di.Exists);
|
|
}
|
|
|
|
[Fact]
|
|
public void NonExistentFile()
|
|
{
|
|
Assert.False(new FileInfo("Da drar vi til fjells").Exists);
|
|
}
|
|
|
|
[Theory, MemberData(nameof(TrailingCharacters))]
|
|
public void MissingDirectory(char trailingChar)
|
|
{
|
|
string path = GetTestFilePath();
|
|
FileInfo info = new FileInfo(Path.Combine(path, "file" + trailingChar));
|
|
Assert.False(info.Exists);
|
|
}
|
|
|
|
[Fact]
|
|
[PlatformSpecific(CaseInsensitivePlatforms)]
|
|
public void CaseInsensitivity()
|
|
{
|
|
string path = GetTestFilePath();
|
|
File.Create(path).Dispose();
|
|
Assert.True(new FileInfo(path.ToUpperInvariant()).Exists);
|
|
Assert.True(new FileInfo(path.ToLowerInvariant()).Exists);
|
|
}
|
|
|
|
[Fact]
|
|
[PlatformSpecific(CaseSensitivePlatforms)]
|
|
public void CaseSensitivity()
|
|
{
|
|
string path = GetTestFilePath();
|
|
File.Create(path).Dispose();
|
|
Assert.False(new FileInfo(path.ToUpperInvariant()).Exists);
|
|
Assert.False(new FileInfo(path.ToLowerInvariant()).Exists);
|
|
}
|
|
|
|
[Fact]
|
|
public void TrueForNewFileInfo()
|
|
{
|
|
string fileName = GetTestFilePath();
|
|
File.Create(fileName).Dispose();
|
|
|
|
FileInfo di = new FileInfo(fileName);
|
|
Assert.True(di.Exists);
|
|
}
|
|
|
|
[Fact]
|
|
public void FalseForDirectory()
|
|
{
|
|
string fileName = GetTestFilePath();
|
|
Directory.CreateDirectory(fileName);
|
|
FileInfo di = new FileInfo(fileName);
|
|
Assert.False(di.Exists);
|
|
}
|
|
|
|
[Fact]
|
|
[PlatformSpecific(TestPlatforms.AnyUnix)] // Uses P/Invokes
|
|
public void TrueForNonRegularFile()
|
|
{
|
|
string fileName = GetTestFilePath();
|
|
Assert.Equal(0, mkfifo(fileName, 0));
|
|
FileInfo fi = new FileInfo(fileName);
|
|
Assert.True(fi.Exists);
|
|
}
|
|
|
|
[ConditionalFact(nameof(CanCreateSymbolicLinks))]
|
|
public void SymLinksMayExistIndependentlyOfTarget()
|
|
{
|
|
var path = GetTestFilePath();
|
|
var linkPath = GetTestFilePath();
|
|
|
|
var pathFI = new FileInfo(path);
|
|
var linkPathFI = new FileInfo(linkPath);
|
|
|
|
pathFI.Create().Dispose();
|
|
Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false));
|
|
|
|
// Both the symlink and the target exist
|
|
pathFI.Refresh();
|
|
linkPathFI.Refresh();
|
|
Assert.True(pathFI.Exists, "path should exist");
|
|
Assert.True(linkPathFI.Exists, "linkPath should exist");
|
|
|
|
// Delete the target. The symlink should still exist
|
|
pathFI.Delete();
|
|
pathFI.Refresh();
|
|
linkPathFI.Refresh();
|
|
Assert.False(pathFI.Exists, "path should now not exist");
|
|
Assert.True(linkPathFI.Exists, "linkPath should still exist");
|
|
|
|
// Now delete the symlink.
|
|
linkPathFI.Delete();
|
|
linkPathFI.Refresh();
|
|
Assert.False(linkPathFI.Exists, "linkPath should no longer exist");
|
|
}
|
|
|
|
[Fact]
|
|
public void UnsharedFileExists()
|
|
{
|
|
string path = GetTestFilePath();
|
|
using (FileStream stream = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
|
|
{
|
|
RemoteExecutor.Invoke((p) =>
|
|
{
|
|
FileInfo info = new FileInfo(p);
|
|
Assert.True(info.Exists);
|
|
}, path).Dispose();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
[PlatformSpecific(~TestPlatforms.OSX)]
|
|
public void LockedFileExists()
|
|
{
|
|
string path = GetTestFilePath();
|
|
File.WriteAllBytes(path, new byte[10]);
|
|
|
|
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
|
|
{
|
|
stream.Lock(0, 10);
|
|
|
|
RemoteExecutor.Invoke((p) =>
|
|
{
|
|
FileInfo info = new FileInfo(p);
|
|
Assert.True(info.Exists);
|
|
}, path).Dispose();
|
|
|
|
stream.Unlock(0, 10);
|
|
}
|
|
}
|
|
|
|
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInAppContainer))] // Can't read root in appcontainer
|
|
[PlatformSpecific(TestPlatforms.Windows)]
|
|
public void PageFileExists()
|
|
{
|
|
// Typically there is a page file on the C: drive, if not, don't bother trying to track it down.
|
|
string pageFilePath = Directory.EnumerateFiles(@"C:\", "pagefile.sys").FirstOrDefault();
|
|
if (pageFilePath != null)
|
|
{
|
|
FileInfo info = new FileInfo(pageFilePath);
|
|
Assert.True(info.Exists);
|
|
}
|
|
}
|
|
}
|
|
}
|