diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/MaxPathTests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/MaxPathTests.cs index 88c768aaeec..6a6c4596ac2 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/MaxPathTests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/MaxPathTests.cs @@ -15,8 +15,8 @@ public class MaxPathTests : BaseTest [SetUp] public void Setup () { - if (!IsWindows) { - Assert.Ignore ("MAX_PATH only applies on Windows"); + if (LongPathsSupported) { + Assert.Ignore ("This environment supports long paths"); } } diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/RemoveDirTests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/RemoveDirTests.cs index 121bc8282b1..973ea919696 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/RemoveDirTests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/RemoveDirTests.cs @@ -76,10 +76,10 @@ public void ReadonlyFile () [Test] public void LongPath () { - if (!IsWindows) { - Assert.Ignore ("MAX_PATH only applies on Windows"); + if (LongPathsSupported) { + Assert.Ignore ("This environment supports long paths"); } - var file = NewFile (fileName: "foo".PadRight (250, 'N')); + var file = NewFile (fileName: "foo".PadRight (MaxFileName, 'N')); var task = CreateTask (); Assert.IsTrue (task.Execute (), "task.Execute() should have succeeded."); Assert.AreEqual (1, task.RemovedDirectories.Length, "Changes should have been made."); diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs index 67279358d4c..539630c287e 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs @@ -164,6 +164,32 @@ public static string AndroidNdkPath { } } + /// + /// Windows can only create a file of 255 characters: This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function (this value is commonly 255 characters). + /// See: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation + /// + public const int MaxFileName = 255; + + static Lazy longPaths = new Lazy (() => { + if (!TestEnvironment.IsWindows) { + return true; + } + var path = Path.Combine (Path.GetTempPath (), "foo".PadRight (MaxFileName, 'N')); + try { + File.WriteAllText (path, ""); + return true; + } catch { + return false; + } finally { + // If the file exists, we should be able to delete it + if (File.Exists (path)) { + File.Delete (path); + } + } + }); + + public static bool LongPathsSupported => longPaths.Value; + protected static void WaitFor(int milliseconds) { var pause = new ManualResetEvent(false); diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/FilesTests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/FilesTests.cs index 914da582f7c..2b0b94843f6 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/FilesTests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/FilesTests.cs @@ -85,7 +85,7 @@ public void CopyIfChanged_NoExist () public void CopyIfChanged_LongPath () { var src = NewFile (contents: "foo"); - var dest = NewFile (contents: "bar", fileName: "bar".PadRight (250, 'N')); + var dest = NewFile (contents: "bar", fileName: "bar".PadRight (MaxFileName, 'N')); dest = Files.ToLongPath (dest); Assert.IsTrue (Files.CopyIfChanged (src, dest), "Changes should have occurred"); FileAssert.AreEqual (src, dest); @@ -142,7 +142,7 @@ public void CopyIfStringChanged_NoExist () [Test] public void CopyIfStringChanged_LongPath () { - var dest = NewFile (fileName: "bar".PadRight (250, 'N')); + var dest = NewFile (fileName: "bar".PadRight (MaxFileName, 'N')); dest = Files.ToLongPath (dest); Assert.IsTrue (Files.CopyIfStringChanged ("foo", dest), "Changes should have occurred"); FileAssert.Exists (dest); @@ -192,7 +192,7 @@ public void CopyIfStreamChanged_NoChanges () public void CopyIfStreamChanged_LongPath () { using (var src = NewStream ("foo")) { - var dest = NewFile (fileName: "bar".PadRight (250, 'N')); + var dest = NewFile (fileName: "bar".PadRight (MaxFileName, 'N')); dest = Files.ToLongPath (dest); Assert.IsTrue (Files.CopyIfStreamChanged (src, dest), "Changes should have occurred"); FileAssert.Exists (dest);