From ae94a58d89f281f64475d89f000ffe7efcac024e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Tue, 14 Jul 2020 17:47:58 +0200 Subject: [PATCH 1/3] WASM: Fix System.IO.FileSystem.DriveInfo --- .../src/System.IO.FileSystem.DriveInfo.csproj | 7 ++- .../src/System/IO/DriveInfo.Browser.cs | 20 ++++++++ .../src/System/IO/DriveInfo.Unix.cs | 40 +-------------- .../src/System/IO/DriveInfo.UnixOrBrowser.cs | 50 +++++++++++++++++++ .../tests/DriveInfo.Unix.Tests.cs | 20 ++++++-- src/libraries/tests.proj | 1 - src/mono/wasm/wasm.targets | 1 - 7 files changed, 93 insertions(+), 46 deletions(-) create mode 100644 src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Browser.cs create mode 100644 src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.UnixOrBrowser.cs diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj b/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj index 1329619b978eab..fdc1f7b5f018a9 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj @@ -49,7 +49,8 @@ - + + @@ -66,6 +67,10 @@ + + + + diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Browser.cs b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Browser.cs new file mode 100644 index 00000000000000..894c0a29a64105 --- /dev/null +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Browser.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Security; + +namespace System.IO +{ + public sealed partial class DriveInfo + { + public DriveType DriveType => DriveType.Unknown; + public string DriveFormat => "memfs"; + public long AvailableFreeSpace => 0; + public long TotalFreeSpace => 0; + public long TotalSize => 0; + + private static string[] GetMountPoints() => Environment.GetLogicalDrives(); + } +} diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Unix.cs b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Unix.cs index 5fa41b1b9639e8..bc696ef7b5724d 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Unix.cs +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Unix.cs @@ -9,31 +9,6 @@ namespace System.IO { public sealed partial class DriveInfo { - public static DriveInfo[] GetDrives() - { - string[] mountPoints = Interop.Sys.GetAllMountPoints(); - DriveInfo[] info = new DriveInfo[mountPoints.Length]; - for (int i = 0; i < info.Length; i++) - { - info[i] = new DriveInfo(mountPoints[i]); - } - - return info; - } - - private static string NormalizeDriveName(string driveName) - { - if (driveName.Contains("\0")) // string.Contains(char) is .NetCore2.1+ specific - { - throw new ArgumentException(SR.Format(SR.Arg_InvalidDriveChars, driveName), nameof(driveName)); - } - if (driveName.Length == 0) - { - throw new ArgumentException(SR.Arg_MustBeNonEmptyDriveName, nameof(driveName)); - } - return driveName; - } - public DriveType DriveType { get @@ -104,19 +79,6 @@ public long TotalSize } } - [AllowNull] - public string VolumeLabel - { - get - { - return Name; - } - set - { - throw new PlatformNotSupportedException(); - } - } - private void CheckStatfsResultAndThrowIfNecessary(int result) { if (result != 0) @@ -132,5 +94,7 @@ private void CheckStatfsResultAndThrowIfNecessary(int result) } } } + + private static string[] GetMountPoints() => Interop.Sys.GetAllMountPoints(); } } diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.UnixOrBrowser.cs b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.UnixOrBrowser.cs new file mode 100644 index 00000000000000..ee2e5690e86b02 --- /dev/null +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.UnixOrBrowser.cs @@ -0,0 +1,50 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Security; + +namespace System.IO +{ + public sealed partial class DriveInfo + { + public static DriveInfo[] GetDrives() + { + string[] mountPoints = GetMountPoints(); + DriveInfo[] info = new DriveInfo[mountPoints.Length]; + for (int i = 0; i < info.Length; i++) + { + info[i] = new DriveInfo(mountPoints[i]); + } + + return info; + } + + private static string NormalizeDriveName(string driveName) + { + if (driveName.Contains("\0")) // string.Contains(char) is .NetCore2.1+ specific + { + throw new ArgumentException(SR.Format(SR.Arg_InvalidDriveChars, driveName), nameof(driveName)); + } + if (driveName.Length == 0) + { + throw new ArgumentException(SR.Arg_MustBeNonEmptyDriveName, nameof(driveName)); + } + return driveName; + } + + [AllowNull] + public string VolumeLabel + { + get + { + return Name; + } + set + { + throw new PlatformNotSupportedException(); + } + } + } +} diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/tests/DriveInfo.Unix.Tests.cs b/src/libraries/System.IO.FileSystem.DriveInfo/tests/DriveInfo.Unix.Tests.cs index 00910c68e7bf0b..c52c10c8da6622 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/tests/DriveInfo.Unix.Tests.cs +++ b/src/libraries/System.IO.FileSystem.DriveInfo/tests/DriveInfo.Unix.Tests.cs @@ -41,7 +41,7 @@ public void TestGetDrives() } [Fact] - [PlatformSpecific(TestPlatforms.AnyUnix)] + [PlatformSpecific(TestPlatforms.AnyUnix & ~TestPlatforms.Browser)] public void PropertiesOfInvalidDrive() { string invalidDriveName = "NonExistentDriveName"; @@ -64,16 +64,26 @@ public void PropertiesOfInvalidDrive() public void PropertiesOfValidDrive() { var root = new DriveInfo("/"); - Assert.True(root.AvailableFreeSpace > 0); var format = root.DriveFormat; - Assert.Equal(DriveType.Fixed, root.DriveType); + Assert.Equal(PlatformDetection.IsBrowser ? DriveType.Unknown : DriveType.Fixed, root.DriveType); Assert.True(root.IsReady); Assert.Equal("/", root.Name); Assert.Equal("/", root.ToString()); Assert.Equal("/", root.RootDirectory.FullName); - Assert.True(root.TotalFreeSpace > 0); - Assert.True(root.TotalSize > 0); Assert.Equal("/", root.VolumeLabel); + + if (PlatformDetection.IsBrowser) + { + Assert.True(root.AvailableFreeSpace == 0); + Assert.True(root.TotalFreeSpace == 0); + Assert.True(root.TotalSize == 0); + } + else + { + Assert.True(root.AvailableFreeSpace > 0); + Assert.True(root.TotalFreeSpace > 0); + Assert.True(root.TotalSize > 0); + } } [Fact] diff --git a/src/libraries/tests.proj b/src/libraries/tests.proj index c6d78433577f8d..7a52d9795494ed 100644 --- a/src/libraries/tests.proj +++ b/src/libraries/tests.proj @@ -34,7 +34,6 @@ - diff --git a/src/mono/wasm/wasm.targets b/src/mono/wasm/wasm.targets index 58ec3bcdad21e5..520dd9c7d7b21e 100644 --- a/src/mono/wasm/wasm.targets +++ b/src/mono/wasm/wasm.targets @@ -17,7 +17,6 @@ - From 080e22cee7be4cead9297b40b9084cc9ef720fc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Tue, 14 Jul 2020 17:09:13 +0200 Subject: [PATCH 2/3] WASM: Fix Microsoft.VisualBasic.Core tests Some tests need culture data. --- .../tests/FinancialTests.cs | 2 +- .../tests/ObjectTypeTests.cs | 4 +-- .../tests/StringTypeTests.cs | 31 +++++++++++-------- .../tests/StringsTests.cs | 2 +- src/libraries/tests.proj | 1 - 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/FinancialTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/FinancialTests.cs index b5fded0d1a57ec..3b4ac13ca757bf 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/FinancialTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/FinancialTests.cs @@ -13,7 +13,7 @@ public class FinancialTests // The accuracy to which we can validate some numeric test cases depends on the platform. private static readonly int s_precision = IsArmOrArm64OrAlpine ? 12 : - PlatformDetection.IsNetFramework ? 14 : 15; + (PlatformDetection.IsBrowser || PlatformDetection.IsNetFramework) ? 14 : 15; [Theory] [InlineData(0, 1.0, 1.0, 1.0, 1.0, 0, 0)] diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/ObjectTypeTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/ObjectTypeTests.cs index 872d9647d4db69..f1e4b8ac6935f1 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/ObjectTypeTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/ObjectTypeTests.cs @@ -342,8 +342,8 @@ public static IEnumerable ObjTst_TestData() yield return new object[] { "a", "a", 0, 0 }; yield return new object[] { "a", "b", -1, -1 }; yield return new object[] { "b", "a", 1, 1 }; - yield return new object[] { "a", "ABC", 32, -1 }; - yield return new object[] { "ABC", "a", -32, 1 }; + yield return new object[] { "a", "ABC", 32, PlatformDetection.IsInvariantGlobalization ? -2 : -1 }; + yield return new object[] { "ABC", "a", -32, PlatformDetection.IsInvariantGlobalization ? 2 : 1 }; yield return new object[] { "abc", "ABC", 32, 0 }; } } diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/StringTypeTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/StringTypeTests.cs index cb0fe6c95323de..752f66aaeaf34f 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/StringTypeTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/StringTypeTests.cs @@ -364,25 +364,30 @@ public void MidStmtStr_ArgumentException(string str, int start, int length, stri } [Theory] - [InlineData(null, null, 0, 0)] - [InlineData(null, "", 0, 0)] - [InlineData("", null, 0, 0)] - [InlineData(null, "a", -1, -1)] - [InlineData("a", null, 1, 1)] - [InlineData("", "a", -97, -1)] - [InlineData("a", "", 97, 1)] - [InlineData("a", "a", 0, 0)] - [InlineData("a", "b", -1, -1)] - [InlineData("b", "a", 1, 1)] - [InlineData("a", "ABC", 32, -1)] - [InlineData("ABC", "a", -32, 1)] - [InlineData("abc", "ABC", 32, 0)] + [MemberData(nameof(StrCmp_TestData))] public void StrCmp(string left, string right, int expectedBinaryCompare, int expectedTextCompare) { Assert.Equal(expectedBinaryCompare, StringType.StrCmp(left, right, TextCompare: false)); Assert.Equal(expectedTextCompare, StringType.StrCmp(left, right, TextCompare: true)); } + public static IEnumerable StrCmp_TestData() + { + yield return new object[] { null, null, 0, 0 }; + yield return new object[] { null, "", 0, 0 }; + yield return new object[] { "", null, 0, 0 }; + yield return new object[] { null, "a", -1, -1 }; + yield return new object[] { "a", null, 1, 1 }; + yield return new object[] { "", "a", -97, -1 }; + yield return new object[] { "a", "", 97, 1 }; + yield return new object[] { "a", "a", 0, 0 }; + yield return new object[] { "a", "b", -1, -1 }; + yield return new object[] { "b", "a", 1, 1 }; + yield return new object[] { "a", "ABC", 32, PlatformDetection.IsInvariantGlobalization ? -2 : 1 }; + yield return new object[] { "ABC", "a", -32, PlatformDetection.IsInvariantGlobalization ? 2 : -1 }; + yield return new object[] { "abc", "ABC", 32, 0 }; + } + [Theory] [InlineData(null, null, true, true)] [InlineData("", null, true, true)] diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/StringsTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/StringsTests.cs index 2baca766f569b2..52d7ec1af1b877 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/StringsTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/StringsTests.cs @@ -129,7 +129,7 @@ public void Asc_Chr_Invariant(int charCode, int expected) } [ActiveIssue("https://github.com/dotnet/runtime/issues/30419", TargetFrameworkMonikers.NetFramework)] - [Theory] + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInvariantGlobalization))] [InlineData(0, 0)] [InlineData(33, 33)] [InlineData(172, 0)] diff --git a/src/libraries/tests.proj b/src/libraries/tests.proj index 7a52d9795494ed..35d7675227d437 100644 --- a/src/libraries/tests.proj +++ b/src/libraries/tests.proj @@ -22,7 +22,6 @@ - From 5a97727da5ba7a56b506492288bb7392bc749943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Tue, 14 Jul 2020 19:01:10 +0200 Subject: [PATCH 3/3] Fix test that was accidentally reversed for non-Browser --- .../Microsoft.VisualBasic.Core/tests/StringTypeTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/StringTypeTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/StringTypeTests.cs index 752f66aaeaf34f..41fc7717c8c0ec 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/StringTypeTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/StringTypeTests.cs @@ -383,8 +383,8 @@ public static IEnumerable StrCmp_TestData() yield return new object[] { "a", "a", 0, 0 }; yield return new object[] { "a", "b", -1, -1 }; yield return new object[] { "b", "a", 1, 1 }; - yield return new object[] { "a", "ABC", 32, PlatformDetection.IsInvariantGlobalization ? -2 : 1 }; - yield return new object[] { "ABC", "a", -32, PlatformDetection.IsInvariantGlobalization ? 2 : -1 }; + yield return new object[] { "a", "ABC", 32, PlatformDetection.IsInvariantGlobalization ? -2 : -1 }; + yield return new object[] { "ABC", "a", -32, PlatformDetection.IsInvariantGlobalization ? 2 : 1 }; yield return new object[] { "abc", "ABC", 32, 0 }; }