Skip to content

Commit 35536d1

Browse files
authored
Cleanup current directory tests (#74311)
* Cleanup current directory tests Replace hardcoded list of platforms with symlinked temp directory with a relaxed condition that allows the test to pass even when the temp directory is symlinked. * Fix OSX test failures /tmp does not seem to be an ordinary symlink
1 parent cb52e3b commit 35536d1

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

src/libraries/Microsoft.VisualBasic.Core/tests/FileSystemTests.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ protected override void Dispose(bool disposing)
2323
base.Dispose(disposing);
2424
}
2525

26-
// On OSX/MacCatalyst, the temp directory /tmp/ is a symlink to /private/tmp, so setting the current
27-
// directory to a symlinked path will result in GetCurrentDirectory returning the absolute
28-
// path that followed the symlink.
29-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotOSX), nameof(PlatformDetection.IsNotMacCatalyst))]
26+
[Fact]
3027
[ActiveIssue("https://github.com/dotnet/runtime/issues/50572", TestPlatforms.Android)]
3128
public void ChDir()
3229
{
3330
var savedDirectory = System.IO.Directory.GetCurrentDirectory();
3431
FileSystem.ChDir(TestDirectory);
35-
Assert.Equal(TestDirectory, System.IO.Directory.GetCurrentDirectory());
32+
33+
// If the test directory has symlinks, setting the current directory to a symlinked path will result
34+
// in GetCurrentDirectory returning the absolute path that followed the symlink. We can only verify
35+
// the test directory name in that case.
36+
Assert.Equal(System.IO.Path.GetFileName(TestDirectory), System.IO.Path.GetFileName(System.IO.Directory.GetCurrentDirectory()));
37+
3638
FileSystem.ChDir(savedDirectory);
3739
Assert.Equal(savedDirectory, System.IO.Directory.GetCurrentDirectory());
3840
}
@@ -82,8 +84,7 @@ static string getString(string fileName)
8284
}
8385
}
8486

85-
// Can't get current directory on OSX before setting it.
86-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotOSX))]
87+
[Fact]
8788
[ActiveIssue("https://github.com/dotnet/runtime/issues/50572", TestPlatforms.Android)]
8889
public void CurDir()
8990
{

src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft/VisualBasic/FileIO/FileSystemTests.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,24 +285,25 @@ public void CreateDirectory_LongPath()
285285
}
286286
}
287287

288-
// Can't get current directory on OSX before setting it.
289-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotOSX))]
288+
[Fact]
290289
public void CurrentDirectoryGet()
291290
{
292291
var CurrentDirectory = System.IO.Directory.GetCurrentDirectory();
293292
Assert.Equal(FileIO.FileSystem.CurrentDirectory, CurrentDirectory);
294293
}
295294

296-
// On OSX/MacCatalyst, the temp directory /tmp/ is a symlink to /private/tmp, so setting the current
297-
// directory to a symlinked path will result in GetCurrentDirectory returning the absolute
298-
// path that followed the symlink.
299-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotOSX), nameof(PlatformDetection.IsNotMacCatalyst))]
295+
[Fact]
300296
[ActiveIssue("https://github.com/dotnet/runtime/issues/50572", TestPlatforms.Android)]
301297
public void CurrentDirectorySet()
302298
{
303299
var SavedCurrentDirectory = System.IO.Directory.GetCurrentDirectory();
304300
FileIO.FileSystem.CurrentDirectory = TestDirectory;
305-
Assert.Equal(TestDirectory, FileIO.FileSystem.CurrentDirectory);
301+
302+
// If the test directory has symlinks, setting the current directory to a symlinked path will result
303+
// in GetCurrentDirectory returning the absolute path that followed the symlink. We can only verify
304+
// the test directory name in that case.
305+
Assert.Equal(System.IO.Path.GetFileName(TestDirectory), System.IO.Path.GetFileName(FileIO.FileSystem.CurrentDirectory));
306+
306307
FileIO.FileSystem.CurrentDirectory = SavedCurrentDirectory;
307308
Assert.Equal(FileIO.FileSystem.CurrentDirectory, SavedCurrentDirectory);
308309
}

src/libraries/System.IO.FileSystem/tests/Directory/SetCurrentDirectory.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ public void SetToValidOtherDirectory()
3333
RemoteExecutor.Invoke(() =>
3434
{
3535
Directory.SetCurrentDirectory(TestDirectory);
36-
// On OSX, the temp directory /tmp/ is a symlink to /private/tmp, so setting the current
37-
// directory to a symlinked path will result in GetCurrentDirectory returning the absolute
38-
// path that followed the symlink.
39-
if (!OperatingSystem.IsMacOS())
36+
if (OperatingSystem.IsMacOS())
37+
{
38+
// On OSX, the temp directory /tmp/ is a symlink to /private/tmp, so setting the current
39+
// directory to a symlinked path will result in GetCurrentDirectory returning the absolute
40+
// path that followed the symlink.
41+
Assert.Equal("/private" + TestDirectory, Directory.GetCurrentDirectory());
42+
}
43+
else
4044
{
4145
Assert.Equal(TestDirectory, Directory.GetCurrentDirectory());
4246
}

src/libraries/System.Private.CoreLib/src/System/IO/Path.Unix.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ private static string GetFullPathInternal(string path)
6262
path = Combine(Interop.Sys.GetCwd(), path);
6363
}
6464

65-
// We would ideally use realpath to do this, but it resolves symlinks, requires that the file actually exist,
66-
// and turns it into a full path, which we only want if fullCheck is true.
65+
// We would ideally use realpath to do this, but it resolves symlinks and requires that the file actually exist.
6766
string collapsedString = PathInternal.RemoveRelativeSegments(path, PathInternal.GetRootLength(path));
6867

6968
Debug.Assert(collapsedString.Length < path.Length || collapsedString.ToString() == path,

src/libraries/System.Runtime.Extensions/tests/System/EnvironmentTests.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ public void CurrentDirectory_SetToValidOtherDirectory()
4444
Environment.CurrentDirectory = TestDirectory;
4545
Assert.Equal(Directory.GetCurrentDirectory(), Environment.CurrentDirectory);
4646

47-
if (!OperatingSystem.IsMacOS())
48-
{
49-
// On OSX, the temp directory /tmp/ is a symlink to /private/tmp, so setting the current
50-
// directory to a symlinked path will result in GetCurrentDirectory returning the absolute
51-
// path that followed the symlink.
52-
Assert.Equal(TestDirectory, Directory.GetCurrentDirectory());
53-
}
47+
// If the temp directory is symlink, setting the current directory to a symlinked path will result
48+
// in GetCurrentDirectory returning the absolute path that followed the symlink. We can only verify
49+
// the test directory name in that case.
50+
Assert.Equal(Path.GetFileName(TestDirectory), Path.GetFileName(Environment.CurrentDirectory));
51+
5452
}).Dispose();
5553
}
5654

0 commit comments

Comments
 (0)