From d00d969650cc04dfd8cadb47d37de0266ae4b00d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Cant=C3=BA?= Date: Thu, 19 May 2022 22:09:22 -0700 Subject: [PATCH 1/2] Use CanCreateSymbolicLink instead of IsSymLinkSupported --- .../tests/System/IO/ReparsePointUtilities.cs | 74 ----------------- .../tests/System/IO/SymbolicLinkHelper.cs | 83 +++++++++++++++++++ .../TestUtilities/System/PlatformDetection.cs | 3 +- .../tests/TestUtilities/TestUtilities.csproj | 4 +- .../PhysicalFileProviderTests.netcoreapp.cs | 8 +- .../FileVersionInfoTest.Unix.cs | 4 +- .../tests/System.Formats.Tar.Tests.csproj | 1 - ...TarFile.ExtractToDirectory.Stream.Tests.cs | 2 +- .../TarWriter.WriteEntry.File.Tests.cs | 2 +- .../FileSystemWatcher.Directory.Changed.cs | 4 +- .../FileSystemWatcher.Directory.Create.cs | 4 +- .../FileSystemWatcher.Directory.Delete.cs | 4 +- .../tests/FileSystemWatcher.File.Changed.cs | 4 +- .../tests/FileSystemWatcher.File.Create.cs | 4 +- .../tests/FileSystemWatcher.File.Delete.cs | 4 +- .../tests/FileSystemWatcher.SymbolicLink.cs | 4 +- .../System.IO.FileSystem.Watcher.Tests.csproj | 2 - .../tests/Base/AllGetSetAttributes.cs | 8 +- .../tests/Base/BaseGetSetTimes.cs | 2 +- .../Base/SymbolicLinks/BaseSymbolicLinks.cs | 4 +- .../tests/Directory/Delete.cs | 6 +- .../tests/Directory/Exists.cs | 8 +- .../tests/Directory/GetDirectories.cs | 4 +- .../tests/Directory/GetFiles.cs | 4 +- .../tests/Directory/SetCurrentDirectory.cs | 4 +- .../tests/Directory/SymbolicLinks.cs | 2 +- .../tests/DirectoryInfo/Exists.cs | 8 +- .../tests/DirectoryInfo/SymbolicLinks.cs | 2 +- .../tests/Enumeration/AttributeTests.cs | 2 +- .../tests/Enumeration/SymbolicLinksTests.cs | 2 +- .../System.IO.FileSystem/tests/File/Delete.cs | 4 +- .../System.IO.FileSystem/tests/File/Exists.cs | 4 +- .../tests/File/SymbolicLinks.cs | 2 +- .../tests/FileInfo/Exists.cs | 4 +- .../tests/FileInfo/Length.cs | 4 +- .../tests/FileInfo/SymbolicLinks.cs | 2 +- .../tests/Junctions.Windows.cs | 2 +- .../VirtualDriveSymbolicLinks.Windows.cs | 2 +- 38 files changed, 150 insertions(+), 141 deletions(-) create mode 100644 src/libraries/Common/tests/System/IO/SymbolicLinkHelper.cs diff --git a/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs b/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs index 7a1757a8249ac..2f0c38bfab343 100644 --- a/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs +++ b/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs @@ -10,14 +10,10 @@ #define DEBUG using System; -using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Linq; using System.Runtime.InteropServices; -using System.Text; using System.Threading.Tasks; -using Xunit; public static partial class MountHelper { @@ -35,77 +31,7 @@ public static partial class MountHelper // Helper for ConditionalClass attributes internal static bool IsSubstAvailable => PlatformDetection.IsSubstAvailable; - /// - /// In some cases (such as when running without elevated privileges), - /// the symbolic link may fail to create. Only run this test if it creates - /// links successfully. - /// - internal static bool CanCreateSymbolicLinks => s_canCreateSymbolicLinks.Value; - private static readonly Lazy s_canCreateSymbolicLinks = new Lazy(() => - { - bool success = true; - - // Verify file symlink creation - string path = Path.GetTempFileName(); - string linkPath = path + ".link"; - success = CreateSymbolicLink(linkPath: linkPath, targetPath: path, isDirectory: false); - try { File.Delete(path); } catch { } - try { File.Delete(linkPath); } catch { } - - // Verify directory symlink creation - path = Path.GetTempFileName(); - linkPath = path + ".link"; - success = success && CreateSymbolicLink(linkPath: linkPath, targetPath: path, isDirectory: true); - try { Directory.Delete(path); } catch { } - try { Directory.Delete(linkPath); } catch { } - - // Reduce the risk we accidentally stop running these altogether - // on Windows, due to a bug in CreateSymbolicLink - if (!success && PlatformDetection.IsWindows) - Assert.True(!PlatformDetection.IsWindowsAndElevated); - - return success; - }); - - /// Creates a symbolic link using command line tools. - public static bool CreateSymbolicLink(string linkPath, string targetPath, bool isDirectory) - { - // It's easy to get the parameters backwards. - Assert.EndsWith(".link", linkPath); - if (linkPath != targetPath) // testing loop - Assert.False(targetPath.EndsWith(".link"), $"{targetPath} should not end with .link"); - -#if NETFRAMEWORK - bool isWindows = true; -#else - if (OperatingSystem.IsIOS() || OperatingSystem.IsTvOS() || OperatingSystem.IsMacCatalyst() || OperatingSystem.IsBrowser()) // OSes that don't support Process.Start() - { - return false; - } - - bool isWindows = OperatingSystem.IsWindows(); -#endif - Process symLinkProcess = new Process(); - if (isWindows) - { - symLinkProcess.StartInfo.FileName = "cmd"; - symLinkProcess.StartInfo.Arguments = string.Format("/c mklink{0} \"{1}\" \"{2}\"", isDirectory ? " /D" : "", linkPath, targetPath); - } - else - { - symLinkProcess.StartInfo.FileName = "/bin/ln"; - symLinkProcess.StartInfo.Arguments = string.Format("-s \"{0}\" \"{1}\"", targetPath, linkPath); - } - symLinkProcess.StartInfo.UseShellExecute = false; - symLinkProcess.StartInfo.RedirectStandardOutput = true; - - symLinkProcess.Start(); - - symLinkProcess.WaitForExit(); - - return (symLinkProcess.ExitCode == 0); - } /// On Windows, creates a junction using command line tools. public static bool CreateJunction(string junctionPath, string targetPath) diff --git a/src/libraries/Common/tests/System/IO/SymbolicLinkHelper.cs b/src/libraries/Common/tests/System/IO/SymbolicLinkHelper.cs new file mode 100644 index 0000000000000..d74259cfd67d1 --- /dev/null +++ b/src/libraries/Common/tests/System/IO/SymbolicLinkHelper.cs @@ -0,0 +1,83 @@ +using System; +using System.Diagnostics; +using System.IO; +using Xunit; + +public static class SymbolicLinkHelper +{ + /// + /// In some cases (such as when running without elevated privileges), + /// the symbolic link may fail to create. Only run this test if it creates + /// links successfully. + /// + internal static bool CanCreateSymbolicLinks => s_canCreateSymbolicLinks.Value; + + private static readonly Lazy s_canCreateSymbolicLinks = new Lazy(() => + { + bool success = true; + + // Verify file symlink creation + string path = Path.GetTempFileName(); + string linkPath = path + ".link"; + success = CreateSymbolicLink(linkPath: linkPath, targetPath: path, isDirectory: false); + try { File.Delete(path); } catch { } + try { File.Delete(linkPath); } catch { } + + // Verify directory symlink creation + path = Path.GetTempFileName(); + linkPath = path + ".link"; + success = success && CreateSymbolicLink(linkPath: linkPath, targetPath: path, isDirectory: true); + try { Directory.Delete(path); } catch { } + try { Directory.Delete(linkPath); } catch { } + + // Reduce the risk we accidentally stop running these altogether + // on Windows, due to a bug in CreateSymbolicLink + if (!success && PlatformDetection.IsWindows) + Assert.True(!PlatformDetection.IsWindowsAndElevated); + + return success; + }); + + /// Creates a symbolic link using command line tools. + public static bool CreateSymbolicLink(string linkPath, string targetPath, bool isDirectory) + { + // It's easy to get the parameters backwards. + Assert.EndsWith(".link", linkPath); + if (linkPath != targetPath) // testing loop + Assert.False(targetPath.EndsWith(".link"), $"{targetPath} should not end with .link"); + + if (PlatformDetection.IsiOS || PlatformDetection.IstvOS || PlatformDetection.IsMacCatalyst || PlatformDetection.IsBrowser) + { + return false; + } + +#if !NETFRAMEWORK + try + { + FileSystemInfo linkInfo = isDirectory ? + Directory.CreateSymbolicLink(linkPath, targetPath) : File.CreateSymbolicLink(linkPath, targetPath); + + // Detect silent failures. + Assert.True(linkInfo.Exists); + return true; + } + catch + { + return false; + } +#else + Process symLinkProcess = new Process(); + + symLinkProcess.StartInfo.FileName = "cmd"; + symLinkProcess.StartInfo.Arguments = string.Format("/c mklink{0} \"{1}\" \"{2}\"", isDirectory ? " /D" : "", linkPath, targetPath); + symLinkProcess.StartInfo.UseShellExecute = false; + symLinkProcess.StartInfo.RedirectStandardOutput = true; + + symLinkProcess.Start(); + + symLinkProcess.WaitForExit(); + + return (symLinkProcess.ExitCode == 0); +#endif + } +} diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs index 67618c7284287..31bf050d8da95 100644 --- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs +++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs @@ -89,7 +89,6 @@ public static partial class PlatformDetection public static bool IsThreadingSupported => !IsBrowser; public static bool IsBinaryFormatterSupported => IsNotMobile && !IsNativeAot; - public static bool IsSymLinkSupported => !IsiOS && !IstvOS; public static bool IsSpeedOptimized => !IsSizeOptimized; public static bool IsSizeOptimized => IsBrowser || IsAndroid || IsAppleMobile; @@ -327,6 +326,8 @@ public static bool IsSubstAvailable } } + public static bool CanCreateSymbolicLinks => SymbolicLinkHelper.CanCreateSymbolicLinks; + private static Version GetICUVersion() { int version = 0; diff --git a/src/libraries/Common/tests/TestUtilities/TestUtilities.csproj b/src/libraries/Common/tests/TestUtilities/TestUtilities.csproj index ad1ab67e89c94..e860fa1e5d852 100644 --- a/src/libraries/Common/tests/TestUtilities/TestUtilities.csproj +++ b/src/libraries/Common/tests/TestUtilities/TestUtilities.csproj @@ -1,4 +1,4 @@ - + true @@ -39,6 +39,8 @@ + diff --git a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs index 5bb0d9b2c3eb4..f732184324a43 100644 --- a/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs @@ -94,7 +94,7 @@ public void Extract_LinkEntry_TargetOutsideDirectory(TarEntryType entryType) Assert.Equal(0, Directory.GetFileSystemEntries(root.Path).Count()); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void Extract_SymbolicLinkEntry_TargetInsideDirectory() => Extract_LinkEntry_TargetInsideDirectory_Internal(TarEntryType.SymbolicLink); [Fact] diff --git a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.cs index 298509f1c92a4..16b0a055b6649 100644 --- a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.cs @@ -160,7 +160,7 @@ public void Add_Directory(TarFormat format, bool withContents) } } - [ConditionalTheory(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] [InlineData(TarFormat.V7, false)] [InlineData(TarFormat.V7, true)] [InlineData(TarFormat.Ustar, false)] diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Changed.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Changed.cs index bef5def6c97bc..b8dead41395d8 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Changed.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Changed.cs @@ -57,7 +57,7 @@ public void FileSystemWatcher_Directory_Changed_Nested(bool includeSubdirectorie } } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void FileSystemWatcher_Directory_Changed_SymLink() { string dir = Path.Combine(TestDirectory, "dir"); @@ -68,7 +68,7 @@ public void FileSystemWatcher_Directory_Changed_SymLink() // Setup the watcher watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; watcher.IncludeSubdirectories = true; - Assert.True(MountHelper.CreateSymbolicLink(Path.Combine(dir, GetRandomLinkName()), tempDir, true)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(Path.Combine(dir, GetRandomLinkName()), tempDir, true)); Action action = () => File.AppendAllText(file, "longtext"); Action cleanup = () => File.AppendAllText(file, "short"); diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Create.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Create.cs index 023cc679eb32b..0f0fdee5c79ee 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Create.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Create.cs @@ -81,7 +81,7 @@ public void FileSystemWatcher_Directory_Create_DeepDirectoryStructure() } } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void FileSystemWatcher_Directory_Create_SymLink() { string dir = CreateTestDirectory(TestDirectory, "dir"); @@ -90,7 +90,7 @@ public void FileSystemWatcher_Directory_Create_SymLink() { // Make the symlink in our path (to the temp folder) and make sure an event is raised string symLinkPath = Path.Combine(dir, GetRandomLinkName()); - Action action = () => Assert.True(MountHelper.CreateSymbolicLink(symLinkPath, temp, true)); + Action action = () => Assert.True(SymbolicLinkHelper.CreateSymbolicLink(symLinkPath, temp, true)); Action cleanup = () => Directory.Delete(symLinkPath); ExpectEvent(watcher, WatcherChangeTypes.Created, action, cleanup, symLinkPath); diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Delete.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Delete.cs index a30e355acccf6..d1940072b3be7 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Delete.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Delete.cs @@ -63,7 +63,7 @@ public void FileSystemWatcher_Directory_Delete_DeepDirectoryStructure() } } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void FileSystemWatcher_Directory_Delete_SymLink() { string dir = CreateTestDirectory(TestDirectory, "dir"); @@ -73,7 +73,7 @@ public void FileSystemWatcher_Directory_Delete_SymLink() // Make the symlink in our path (to the temp folder) and make sure an event is raised string symLinkPath = Path.Combine(dir, GetRandomLinkName()); Action action = () => Directory.Delete(symLinkPath); - Action cleanup = () => Assert.True(MountHelper.CreateSymbolicLink(symLinkPath, tempDir, true)); + Action cleanup = () => Assert.True(SymbolicLinkHelper.CreateSymbolicLink(symLinkPath, tempDir, true)); cleanup(); ExpectEvent(watcher, WatcherChangeTypes.Deleted, action, cleanup, expectedPath: symLinkPath); diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Changed.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Changed.cs index 44b41c3778e20..639c6c563fd81 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Changed.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Changed.cs @@ -59,7 +59,7 @@ public void FileSystemWatcher_File_Changed_DataModification() } } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void FileSystemWatcher_File_Changed_SymLink() { string dir = CreateTestDirectory(TestDirectory, "dir"); @@ -67,7 +67,7 @@ public void FileSystemWatcher_File_Changed_SymLink() using (var watcher = new FileSystemWatcher(dir, "*")) { watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; - Assert.True(MountHelper.CreateSymbolicLink(Path.Combine(dir, GetRandomLinkName()), file, false)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(Path.Combine(dir, GetRandomLinkName()), file, false)); Action action = () => File.AppendAllText(file, "longtext"); Action cleanup = () => File.AppendAllText(file, "short"); diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Create.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Create.cs index 5c36caf0a38fd..f2e9b3317d89a 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Create.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Create.cs @@ -120,7 +120,7 @@ public void FileSystemWatcher_File_Create_DeepDirectoryStructure() } } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void FileSystemWatcher_File_Create_SymLink() { string dir = CreateTestDirectory(TestDirectory, "dir"); @@ -129,7 +129,7 @@ public void FileSystemWatcher_File_Create_SymLink() { // Make the symlink in our path (to the temp file) and make sure an event is raised string symLinkPath = Path.Combine(dir, GetRandomLinkName()); - Action action = () => Assert.True(MountHelper.CreateSymbolicLink(symLinkPath, temp, false)); + Action action = () => Assert.True(SymbolicLinkHelper.CreateSymbolicLink(symLinkPath, temp, false)); Action cleanup = () => File.Delete(symLinkPath); ExpectEvent(watcher, WatcherChangeTypes.Created, action, cleanup, symLinkPath); diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Delete.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Delete.cs index ac076e4fd2202..5dab04336f355 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Delete.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.File.Delete.cs @@ -84,7 +84,7 @@ public void FileSystemWatcher_File_Delete_DeepDirectoryStructure() } } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void FileSystemWatcher_File_Delete_SymLink() { FileSystemWatcherTest.Execute(() => @@ -96,7 +96,7 @@ public void FileSystemWatcher_File_Delete_SymLink() // Make the symlink in our path (to the temp file) and make sure an event is raised string symLinkPath = Path.Combine(dir, GetRandomLinkName()); Action action = () => File.Delete(symLinkPath); - Action cleanup = () => Assert.True(MountHelper.CreateSymbolicLink(symLinkPath, temp, false)); + Action cleanup = () => Assert.True(SymbolicLinkHelper.CreateSymbolicLink(symLinkPath, temp, false)); cleanup(); ExpectEvent(watcher, WatcherChangeTypes.Deleted, action, cleanup, symLinkPath); diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.SymbolicLink.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.SymbolicLink.cs index 0bf1d6c6f24e0..7a9e16633c119 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.SymbolicLink.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.SymbolicLink.cs @@ -7,13 +7,13 @@ namespace System.IO.Tests { - [ConditionalClass(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public class SymbolicLink_Changed_Tests : FileSystemWatcherTest { private string CreateSymbolicLinkToTarget(string targetPath, bool isDirectory, string linkPath = null) { linkPath ??= GetRandomLinkPath(); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, targetPath, isDirectory)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, targetPath, isDirectory)); return linkPath; } diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/System.IO.FileSystem.Watcher.Tests.csproj b/src/libraries/System.IO.FileSystem.Watcher/tests/System.IO.FileSystem.Watcher.Tests.csproj index 1246c914ab81d..151d728003470 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/System.IO.FileSystem.Watcher.Tests.csproj +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/System.IO.FileSystem.Watcher.Tests.csproj @@ -37,8 +37,6 @@ Link="Common\System\IO\TempFile.cs" /> - (() => Delete(Directory.GetCurrentDirectory())); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void DeletingSymLinkDoesntDeleteTarget() { var path = GetTestFilePath(); var linkPath = GetRandomLinkPath(); Directory.CreateDirectory(path); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); // Both the symlink and the target exist Assert.True(Directory.Exists(path), "path should exist"); @@ -294,7 +294,7 @@ public void RecursiveDelete_ShouldThrowIOExceptionIfContainedFileInUse() Assert.True(testDir.Exists); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void RecursiveDeletingDoesntFollowLinks() { var target = GetTestFilePath(); diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/Exists.cs b/src/libraries/System.IO.FileSystem/tests/Directory/Exists.cs index b5daf1323139b..f5e92f6861779 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/Exists.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/Exists.cs @@ -95,14 +95,14 @@ public void DirectoryLongerThanMaxLongPath_DoesntThrow() }); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void SymLinksMayExistIndependentlyOfTarget() { var path = GetTestFilePath(); var linkPath = GetRandomLinkPath(); Directory.CreateDirectory(path); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); // Both the symlink and the target exist Assert.True(Directory.Exists(path), "path should exist"); @@ -142,14 +142,14 @@ public void SymLinksMayExistIndependentlyOfTarget() Assert.False(File.Exists(linkPath), "linkPath should no longer exist as a file"); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void SymlinkToNewDirectory() { string path = GetTestFilePath(); Directory.CreateDirectory(path); string linkPath = GetRandomLinkPath(); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); Assert.True(Directory.Exists(path)); Assert.True(Directory.Exists(linkPath)); diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories.cs b/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories.cs index ef370ff5bd6f6..be629a57d403a 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories.cs @@ -16,7 +16,7 @@ public override string[] GetEntries(string path) return Directory.GetDirectories(path); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void EnumerateWithSymLinkToDirectory() { DirectoryInfo containingFolder = Directory.CreateDirectory(GetTestFilePath()); @@ -26,7 +26,7 @@ public void EnumerateWithSymLinkToDirectory() { // Create a symlink to a folder that exists string linkPath = Path.Combine(containingFolder.FullName, GetRandomLinkName()); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, targetDir.FullName, isDirectory: true)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, targetDir.FullName, isDirectory: true)); Assert.True(Directory.Exists(linkPath)); Assert.Equal(1, GetEntries(containingFolder.FullName).Count()); diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles.cs b/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles.cs index f214f08899bcd..cd9c33a9a101a 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles.cs @@ -16,7 +16,7 @@ public override string[] GetEntries(string path) return Directory.GetFiles(path); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void EnumerateWithSymLinkToFile() { DirectoryInfo containingFolder = Directory.CreateDirectory(GetTestFilePath()); @@ -26,7 +26,7 @@ public void EnumerateWithSymLinkToFile() targetFile.Create().Dispose(); string linkPath = Path.Combine(containingFolder.FullName, GetRandomLinkName()); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, targetFile.FullName, isDirectory: false)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, targetFile.FullName, isDirectory: false)); Assert.True(File.Exists(linkPath)); Assert.Equal(1, GetEntries(containingFolder.FullName).Count()); diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/SetCurrentDirectory.cs b/src/libraries/System.IO.FileSystem/tests/Directory/SetCurrentDirectory.cs index ce3339efed2a9..bcfddefc7663d 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/SetCurrentDirectory.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/SetCurrentDirectory.cs @@ -47,7 +47,7 @@ public void SetToValidOtherDirectory() public sealed class Directory_SetCurrentDirectory_SymLink : FileSystemTest { - private static bool CanCreateSymbolicLinksAndRemoteExecutorSupported => MountHelper.CanCreateSymbolicLinks && RemoteExecutor.IsSupported; + private static bool CanCreateSymbolicLinksAndRemoteExecutorSupported => PlatformDetection.CanCreateSymbolicLinks && RemoteExecutor.IsSupported; [ConditionalFact(nameof(CanCreateSymbolicLinksAndRemoteExecutorSupported))] public void SetToPathContainingSymLink() @@ -58,7 +58,7 @@ public void SetToPathContainingSymLink() var linkPath = GetRandomLinkPath(); Directory.CreateDirectory(path); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); // Both the symlink and the target exist Assert.True(Directory.Exists(path), "path should exist"); diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/SymbolicLinks.cs b/src/libraries/System.IO.FileSystem/tests/Directory/SymbolicLinks.cs index 1052ddfc22b9b..3b5c1dbfb1855 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/SymbolicLinks.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/SymbolicLinks.cs @@ -7,7 +7,7 @@ namespace System.IO.Tests { - [ConditionalClass(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public class Directory_SymbolicLinks : BaseSymbolicLinks_FileSystem { protected override bool IsDirectoryTest => true; diff --git a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Exists.cs b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Exists.cs index d0f3d095d2da0..e01004e3d2735 100644 --- a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Exists.cs +++ b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Exists.cs @@ -118,20 +118,20 @@ public void FalseForNonRegularFile() Assert.False(di.Exists); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void SymlinkToNewDirectoryInfo() { string path = GetTestFilePath(); new DirectoryInfo(path).Create(); string linkPath = GetRandomLinkPath(); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); Assert.True(new DirectoryInfo(path).Exists); Assert.True(new DirectoryInfo(linkPath).Exists); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void SymLinksMayExistIndependentlyOfTarget() { var path = GetTestFilePath(); @@ -141,7 +141,7 @@ public void SymLinksMayExistIndependentlyOfTarget() var linkPathFI = new DirectoryInfo(linkPath); pathFI.Create(); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); // Both the symlink and the target exist pathFI.Refresh(); diff --git a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/SymbolicLinks.cs b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/SymbolicLinks.cs index 254c00df40133..4cf6d60d6c20e 100644 --- a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/SymbolicLinks.cs +++ b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/SymbolicLinks.cs @@ -7,7 +7,7 @@ namespace System.IO.Tests { - [ConditionalClass(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public class DirectoryInfo_SymbolicLinks : BaseSymbolicLinks_FileSystemInfo { protected override bool IsDirectoryTest => true; diff --git a/src/libraries/System.IO.FileSystem/tests/Enumeration/AttributeTests.cs b/src/libraries/System.IO.FileSystem/tests/Enumeration/AttributeTests.cs index a27fb3c87464a..e9e85a6e8a17c 100644 --- a/src/libraries/System.IO.FileSystem/tests/Enumeration/AttributeTests.cs +++ b/src/libraries/System.IO.FileSystem/tests/Enumeration/AttributeTests.cs @@ -64,7 +64,7 @@ public static IEnumerable PropertiesWhenItemNoLongerExists_Data yield return new object[] { "dir1", "file2" }; yield return new object[] { "file1", "file2" }; - if (MountHelper.CanCreateSymbolicLinks) + if (PlatformDetection.CanCreateSymbolicLinks) { yield return new object[] { "dir1", "link2" }; yield return new object[] { "file1", "link2" }; diff --git a/src/libraries/System.IO.FileSystem/tests/Enumeration/SymbolicLinksTests.cs b/src/libraries/System.IO.FileSystem/tests/Enumeration/SymbolicLinksTests.cs index 48603c927d184..dc5ef114c687f 100644 --- a/src/libraries/System.IO.FileSystem/tests/Enumeration/SymbolicLinksTests.cs +++ b/src/libraries/System.IO.FileSystem/tests/Enumeration/SymbolicLinksTests.cs @@ -8,7 +8,7 @@ namespace System.IO.Tests.Enumeration { - [ConditionalClass(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public class Enumeration_SymbolicLinksTests : BaseSymbolicLinks { [Fact] diff --git a/src/libraries/System.IO.FileSystem/tests/File/Delete.cs b/src/libraries/System.IO.FileSystem/tests/File/Delete.cs index e3228140b82d3..40ae346323998 100644 --- a/src/libraries/System.IO.FileSystem/tests/File/Delete.cs +++ b/src/libraries/System.IO.FileSystem/tests/File/Delete.cs @@ -83,14 +83,14 @@ public void ShouldThrowIOExceptionDeletingDirectory() Assert.Throws(() => Delete(TestDirectory)); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void DeletingSymLinkDoesntDeleteTarget() { var path = GetTestFilePath(); var linkPath = GetRandomLinkPath(); File.Create(path).Dispose(); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); // Both the symlink and the target exist Assert.True(File.Exists(path), "path should exist"); diff --git a/src/libraries/System.IO.FileSystem/tests/File/Exists.cs b/src/libraries/System.IO.FileSystem/tests/File/Exists.cs index 48d87fc2b7946..e026222a69c16 100644 --- a/src/libraries/System.IO.FileSystem/tests/File/Exists.cs +++ b/src/libraries/System.IO.FileSystem/tests/File/Exists.cs @@ -115,14 +115,14 @@ public void DirectoryLongerThanMaxPathAsPath_DoesntThrow() }); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void SymLinksMayExistIndependentlyOfTarget() { var path = GetTestFilePath(); var linkPath = GetRandomLinkPath(); File.Create(path).Dispose(); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); // Both the symlink and the target exist Assert.True(File.Exists(path), "path should exist"); diff --git a/src/libraries/System.IO.FileSystem/tests/File/SymbolicLinks.cs b/src/libraries/System.IO.FileSystem/tests/File/SymbolicLinks.cs index 56b79f940f57e..d8ffadade1a42 100644 --- a/src/libraries/System.IO.FileSystem/tests/File/SymbolicLinks.cs +++ b/src/libraries/System.IO.FileSystem/tests/File/SymbolicLinks.cs @@ -7,7 +7,7 @@ namespace System.IO.Tests { - [ConditionalClass(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public class File_SymbolicLinks : BaseSymbolicLinks_FileSystem { protected override bool IsDirectoryTest => false; diff --git a/src/libraries/System.IO.FileSystem/tests/FileInfo/Exists.cs b/src/libraries/System.IO.FileSystem/tests/FileInfo/Exists.cs index 04e13706db656..02790416bd15c 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileInfo/Exists.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileInfo/Exists.cs @@ -99,7 +99,7 @@ public void TrueForNonRegularFile() Assert.True(fi.Exists); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void SymLinksMayExistIndependentlyOfTarget() { var path = GetTestFilePath(); @@ -109,7 +109,7 @@ public void SymLinksMayExistIndependentlyOfTarget() var linkPathFI = new FileInfo(linkPath); pathFI.Create().Dispose(); - Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); // Both the symlink and the target exist pathFI.Refresh(); diff --git a/src/libraries/System.IO.FileSystem/tests/FileInfo/Length.cs b/src/libraries/System.IO.FileSystem/tests/FileInfo/Length.cs index e77cc47ab97d2..0c89d9d7b12ea 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileInfo/Length.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileInfo/Length.cs @@ -63,7 +63,7 @@ public void Length_MissingDirectory_ThrowsFileNotFound(char trailingChar) Assert.Throws(() => info.Length); } - [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public void SymLinkLength() { string path = GetTestFilePath(); @@ -72,7 +72,7 @@ public void SymLinkLength() const int FileSize = 2000; using (var tempFile = new TempFile(path, FileSize)) { - Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); + Assert.True(SymbolicLinkHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); var info = new FileInfo(path); Assert.Equal(FileSize, info.Length); diff --git a/src/libraries/System.IO.FileSystem/tests/FileInfo/SymbolicLinks.cs b/src/libraries/System.IO.FileSystem/tests/FileInfo/SymbolicLinks.cs index 13c41150ffd5c..d9b7a459dc0a0 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileInfo/SymbolicLinks.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileInfo/SymbolicLinks.cs @@ -6,7 +6,7 @@ namespace System.IO.Tests { - [ConditionalClass(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public class FileInfo_SymbolicLinks : BaseSymbolicLinks_FileSystemInfo { protected override bool IsDirectoryTest => false; diff --git a/src/libraries/System.IO.FileSystem/tests/Junctions.Windows.cs b/src/libraries/System.IO.FileSystem/tests/Junctions.Windows.cs index da5299946f44b..dc8c3ceb4fd77 100644 --- a/src/libraries/System.IO.FileSystem/tests/Junctions.Windows.cs +++ b/src/libraries/System.IO.FileSystem/tests/Junctions.Windows.cs @@ -6,7 +6,7 @@ namespace System.IO.Tests { [PlatformSpecific(TestPlatforms.Windows)] - [ConditionalClass(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] + [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks))] public class Junctions : BaseSymbolicLinks { private DirectoryInfo CreateJunction(string junctionPath, string targetPath) diff --git a/src/libraries/System.IO.FileSystem/tests/VirtualDriveSymbolicLinks.Windows.cs b/src/libraries/System.IO.FileSystem/tests/VirtualDriveSymbolicLinks.Windows.cs index 00242d1089f73..297064967af17 100644 --- a/src/libraries/System.IO.FileSystem/tests/VirtualDriveSymbolicLinks.Windows.cs +++ b/src/libraries/System.IO.FileSystem/tests/VirtualDriveSymbolicLinks.Windows.cs @@ -8,7 +8,7 @@ namespace System.IO.Tests // Need to reuse the same virtual drive for all the test methods. // Creating and disposing one virtual drive per class achieves this. [PlatformSpecific(TestPlatforms.Windows)] - [ConditionalClass(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks), nameof(MountHelper.IsSubstAvailable))] + [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.CanCreateSymbolicLinks), nameof(PlatformDetection.IsSubstAvailable))] public class VirtualDrive_SymbolicLinks : BaseSymbolicLinks { private VirtualDriveHelper VirtualDrive { get; } = new VirtualDriveHelper(); From c4a1e070607cc7f172853ab981cac45b09c71579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Cant=C3=BA?= Date: Fri, 20 May 2022 08:50:28 -0700 Subject: [PATCH 2/2] Use string interpolation on mklink --- src/libraries/Common/tests/System/IO/SymbolicLinkHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Common/tests/System/IO/SymbolicLinkHelper.cs b/src/libraries/Common/tests/System/IO/SymbolicLinkHelper.cs index d74259cfd67d1..c36552889e62d 100644 --- a/src/libraries/Common/tests/System/IO/SymbolicLinkHelper.cs +++ b/src/libraries/Common/tests/System/IO/SymbolicLinkHelper.cs @@ -69,7 +69,7 @@ public static bool CreateSymbolicLink(string linkPath, string targetPath, bool i Process symLinkProcess = new Process(); symLinkProcess.StartInfo.FileName = "cmd"; - symLinkProcess.StartInfo.Arguments = string.Format("/c mklink{0} \"{1}\" \"{2}\"", isDirectory ? " /D" : "", linkPath, targetPath); + symLinkProcess.StartInfo.Arguments = $"/c mklink{(isDirectory ? " /D" : "")} \"{linkPath}\" \"{targetPath}\""; symLinkProcess.StartInfo.UseShellExecute = false; symLinkProcess.StartInfo.RedirectStandardOutput = true;