Skip to content

Commit

Permalink
Use SetCurrentDirectory on tests which use an unrooted path on
Browse files Browse the repository at this point in the history
non-Windows out of the app's working directory, which is readonly on
some platforms.

Closes: dotnet#67853
  • Loading branch information
directhex committed Sep 1, 2022
1 parent 5119157 commit c31e824
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -466,34 +466,37 @@ public void DriveLetter_Windows()
Assert.Equal(current.FullName, driveLetter.FullName);
}

[Fact]
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[PlatformSpecific(TestPlatforms.AnyUnix)] // drive letters casing
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
public void DriveLetter_Unix()
{
// On Unix, there's no special casing for drive letters. These may or may not be valid names, depending
// on the file system underlying the current directory. Unix file systems typically allow these, but,
// for example, these names are not allowed if running on a file system mounted from a Windows machine.
DirectoryInfo driveLetter;
try
{
driveLetter = Create("C:");
}
catch (IOException)
{
return;
}
var current = Create(".");
Assert.Equal("C:", driveLetter.Name);
Assert.Equal(Path.Combine(current.FullName, "C:"), driveLetter.FullName);
try
RemoteExecutor.Invoke(() =>
{
// If this test is inherited then it's possible this call will fail due to the "C:" directory
// being deleted in that other test before this call. What we care about testing (proper path
// handling) is unaffected by this race condition.
Directory.Delete("C:");
}
catch (DirectoryNotFoundException) { }
Directory.SetCurrentDirectory(Path.GetTempPath());
// On Unix, there's no special casing for drive letters. These may or may not be valid names, depending
// on the file system underlying the current directory. Unix file systems typically allow these, but,
// for example, these names are not allowed if running on a file system mounted from a Windows machine.
DirectoryInfo driveLetter;
try
{
driveLetter = Create("C:");
}
catch (IOException)
{
return;
}
var current = Create(".");
Assert.Equal("C:", driveLetter.Name);
Assert.Equal(Path.Combine(current.FullName, "C:"), driveLetter.FullName);
try
{
// If this test is inherited then it's possible this call will fail due to the "C:" directory
// being deleted in that other test before this call. What we care about testing (proper path
// handling) is unaffected by this race condition.
Directory.Delete("C:");
}
catch (DirectoryNotFoundException) { }
}).Dispose();
}

[Fact]
Expand Down
33 changes: 22 additions & 11 deletions src/libraries/System.IO.FileSystem/tests/Directory/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Text;
using Xunit;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.DotNet.XUnitExtensions;

namespace System.IO.Tests
Expand All @@ -13,6 +14,10 @@ public class Directory_Delete_str : FileSystemTest

static bool IsBindMountSupportedAndOnUnixAndSuperUser => IsBindMountSupported && PlatformDetection.IsUnixAndSuperUser;

static bool IsRemoteExecutorSupportedAndUsingNewNormalization => RemoteExecutor.IsSupported && UsingNewNormalization;

static bool IsRemoteExecutorSupportedAndLongPathsAreNotBlockedAndUsingNewNormalization => RemoteExecutor.IsSupported && LongPathsAreNotBlocked && UsingNewNormalization;

#region Utilities

protected virtual void Delete(string path)
Expand Down Expand Up @@ -121,23 +126,29 @@ public void DeletingSymLinkDoesntDeleteTarget()
Assert.False(Directory.Exists(linkPath), "linkPath should no longer exist");
}

[ConditionalFact(nameof(UsingNewNormalization))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
[ConditionalFact(nameof(IsRemoteExecutorSupportedAndUsingNewNormalization))]
public void ExtendedDirectoryWithSubdirectories()
{
DirectoryInfo testDir = Directory.CreateDirectory(IOInputs.ExtendedPrefix + GetTestFilePath());
testDir.CreateSubdirectory(GetTestFileName());
Assert.Throws<IOException>(() => Delete(testDir.FullName));
Assert.True(testDir.Exists);
RemoteExecutor.Invoke(() =>
{
Directory.SetCurrentDirectory(Path.GetTempPath());
DirectoryInfo testDir = Directory.CreateDirectory(IOInputs.ExtendedPrefix + GetTestFilePath());
testDir.CreateSubdirectory(GetTestFileName());
Assert.Throws<IOException>(() => Delete(testDir.FullName));
Assert.True(testDir.Exists);
}).Dispose();
}

[ConditionalFact(nameof(LongPathsAreNotBlocked), nameof(UsingNewNormalization))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
[ConditionalFact(nameof(IsRemoteExecutorSupportedAndLongPathsAreNotBlockedAndUsingNewNormalization))]
public void LongPathExtendedDirectory()
{
DirectoryInfo testDir = Directory.CreateDirectory(IOServices.GetPath(IOInputs.ExtendedPrefix + TestDirectory, characterCount: 500));
Delete(testDir.FullName);
Assert.False(testDir.Exists);
RemoteExecutor.Invoke(() =>
{
Directory.SetCurrentDirectory(Path.GetTempPath());
DirectoryInfo testDir = Directory.CreateDirectory(IOServices.GetPath(IOInputs.ExtendedPrefix + TestDirectory, characterCount: 500));
Delete(testDir.FullName);
Assert.False(testDir.Exists);
}).Dispose();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ public void ExtendedPathAlreadyExistsAsFile()

[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)] // Makes call to native code (libc)
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
public void FalseForNonRegularFile()
{
string fileName = GetTestFilePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public void FalseForFile()

[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)] // Uses P/Invokes
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
public void FalseForNonRegularFile()
{
string fileName = GetTestFilePath();
Expand Down
1 change: 0 additions & 1 deletion src/libraries/System.IO.FileSystem/tests/File/Exists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ public void PathAlreadyExistsAsDirectory()

[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)] // Uses P/Invokes
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
public void FalseForNonRegularFile()
{
string fileName = GetTestFilePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ static async Task WaitConnectionAndWritePipeStreamAsync(NamedPipeServerStream na

[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
public async Task ReadAllBytes_NonSeekableFileStream_InUnix()
{
string fifoPath = GetTestFilePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ static async Task WaitConnectionAndWritePipeStreamAsync(NamedPipeServerStream na

[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
public async Task ReadAllBytesAsync_NonSeekableFileStream_InUnix()
{
string fifoPath = GetTestFilePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public void FalseForDirectory()

[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)] // Uses P/Invokes
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
public void TrueForNonRegularFile()
{
string fileName = GetTestFilePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public void PathAlreadyExistsAsDirectory()

[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)] // Uses P/Invokes
[ActiveIssue("https://github.com/dotnet/runtime/issues/67853", TestPlatforms.iOS | TestPlatforms.tvOS)]
public void TrueForNonRegularFile()
{
string fileName = GetTestFilePath();
Expand Down

0 comments on commit c31e824

Please sign in to comment.