From 10bff0485b26c3a5f732debcb5c4f84b93e628cf Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Thu, 7 Nov 2024 12:44:58 +0100 Subject: [PATCH] Revert "Revert "Some random clean up and improvements (#11095)" --- .../DesignTimeNodeWriterTest.cs | 4 +- .../test/DefaultRazorProjectFileSystemTest.cs | 10 +- ...soft.AspNetCore.Razor.Language.Test.csproj | 1 - .../src/CSharp/FilePathComparer.cs | 4 +- .../src/CSharp/FilePathComparison.cs | 4 +- .../CodeGeneration/CodeWriterExtensions.cs | 4 +- .../DefaultLanguageServerFeatureOptions.cs | 5 +- .../RemoteRazorProjectFileSystem.cs | 3 +- .../Utilities/FilePathNormalizer.cs | 4 +- .../Utilities/RazorProjectInfoFactory.cs | 3 +- .../RazorDynamicFileInfoProvider.cs | 4 +- src/Razor/src/rzls/Program.cs | 6 +- ...or.ExternalAccess.LegacyEditor.Test.csproj | 1 - ...ExternalAccess.RoslynWorkspace.Test.csproj | 1 - ...extDocumentUriPresentationEndpointTests.cs | 8 +- .../FindNodeTests.cs | 4 +- ...spNetCore.Razor.LanguageServer.Test.csproj | 1 - .../IProjectSnapshotManagerExtensionsTest.cs | 2 +- .../RemoteRazorProjectFileSystemTest.cs | 4 +- .../Semantic/SemanticTokensTest.cs | 6 +- .../FilePathNormalizerTest.cs | 10 +- ...etCore.Razor.ProjectEngineHost.Test.csproj | 1 - .../RazorToolingIntegrationTestBase.cs | 6 +- .../PathUtilities.cs | 3 +- .../TestProjectData.cs | 4 +- ....CodeAnalysis.Razor.Workspaces.Test.csproj | 1 - .../ProjectSystem/ProjectKeyTests.cs | 2 +- .../UriExtensionsTest.cs | 14 +- ...nguageServer.ContainedLanguage.Test.csproj | 4 - .../UriExtensionsTest.cs | 6 +- .../RazorDocumentOptionsServiceTest.cs | 4 +- .../test/OSSkipConditionFactAttribute.cs | 52 ----- .../ConditionalFactAttribute.cs | 192 ++++++++++++++++++ .../RazorIntegrationTestBase.cs | 6 +- ...NetCore.Razor.Utilities.Shared.Test.csproj | 1 + .../Utilities/PlatformInformation.cs | 19 ++ 36 files changed, 273 insertions(+), 131 deletions(-) delete mode 100644 src/Razor/test/OSSkipConditionFactAttribute.cs create mode 100644 src/Shared/Microsoft.AspNetCore.Razor.Test.Common/ConditionalFactAttribute.cs create mode 100644 src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/Utilities/PlatformInformation.cs diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/DesignTimeNodeWriterTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/DesignTimeNodeWriterTest.cs index d34c08ea4b9..b974231f2d5 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/DesignTimeNodeWriterTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/DesignTimeNodeWriterTest.cs @@ -493,7 +493,7 @@ Render Children ignoreLineEndingDifferences: true); } - [OSSkipConditionTheory(new[] { "Linux", "OSX" })] + [ConditionalTheory(Is.Windows)] [InlineData(@"test.cshtml", @"test.cshtml")] [InlineData(@"pages/test.cshtml", @"pages\test.cshtml")] [InlineData(@"pages\test.cshtml", @"pages\test.cshtml")] @@ -540,7 +540,7 @@ public void LinePragma_Is_Adjusted_On_Windows(string fileName, string expectedFi ignoreLineEndingDifferences: true); } - [OSSkipConditionTheory(new[] { "Linux", "OSX" })] + [ConditionalTheory(Is.Windows)] [InlineData(@"test.cshtml", @"test.cshtml")] [InlineData(@"pages/test.cshtml", @"pages\test.cshtml")] [InlineData(@"pages\test.cshtml", @"pages\test.cshtml")] diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/DefaultRazorProjectFileSystemTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/DefaultRazorProjectFileSystemTest.cs index 9835901f4da..c4440c48784 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/DefaultRazorProjectFileSystemTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/DefaultRazorProjectFileSystemTest.cs @@ -6,7 +6,6 @@ using System; using System.IO; using System.Linq; -using System.Runtime.InteropServices; using Xunit; namespace Microsoft.AspNetCore.Razor.Language; @@ -261,15 +260,10 @@ public void GetItem_ReturnsFileFromDisk() Assert.Equal(Path.Combine("Views", "About", "About.cshtml"), item.RelativePhysicalPath); } - [Fact] + // "This test does not makes sense for case sensitive Operating Systems." + [ConditionalFact(Is.Windows)] public void GetItem_MismatchedCase_ReturnsFileFromDisk() { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - // "This test does not makes sense for case sensitive Operating Systems." - return; - } - // Arrange var filePath = "/Views/About/About.cshtml"; var lowerCaseTestFolder = TestFolder.ToLowerInvariant(); diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Microsoft.AspNetCore.Razor.Language.Test.csproj b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Microsoft.AspNetCore.Razor.Language.Test.csproj index 7927a8dd84e..811cc7994aa 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Microsoft.AspNetCore.Razor.Language.Test.csproj +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Microsoft.AspNetCore.Razor.Language.Test.csproj @@ -30,7 +30,6 @@ - diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/FilePathComparer.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/FilePathComparer.cs index 70237b458f6..2cefe674961 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/FilePathComparer.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/FilePathComparer.cs @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Runtime.InteropServices; +using Microsoft.AspNetCore.Razor.Utilities; namespace Microsoft.CodeAnalysis.Razor; @@ -14,7 +14,7 @@ public static StringComparer Instance { get { - return _instance ??= RuntimeInformation.IsOSPlatform(OSPlatform.Linux) + return _instance ??= PlatformInformation.IsLinux ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase; } diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/FilePathComparison.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/FilePathComparison.cs index a85b6fe2d65..34062b5f1c6 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/FilePathComparison.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/FilePathComparison.cs @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Runtime.InteropServices; +using Microsoft.AspNetCore.Razor.Utilities; namespace Microsoft.CodeAnalysis.Razor; @@ -14,7 +14,7 @@ public static StringComparison Instance { get { - return _instance ??= RuntimeInformation.IsOSPlatform(OSPlatform.Linux) + return _instance ??= PlatformInformation.IsLinux ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; } diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriterExtensions.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriterExtensions.cs index 9c2c36d6991..3e87ef8f930 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriterExtensions.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriterExtensions.cs @@ -8,8 +8,8 @@ using System.Diagnostics; using System.Globalization; using System.Linq; -using System.Runtime.InteropServices; using Microsoft.AspNetCore.Razor.Language.Intermediate; +using Microsoft.AspNetCore.Razor.Utilities; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration; @@ -661,7 +661,7 @@ public static IDisposable BuildEnhancedLinePragma(this CodeWriter writer, Source private static SourceSpan RemapFilePathIfNecessary(SourceSpan sourceSpan, CodeRenderingContext context) { - if (context.Options.RemapLinePragmaPathsOnWindows && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (context.Options.RemapLinePragmaPathsOnWindows && PlatformInformation.IsWindows) { // ISSUE: https://github.com/dotnet/razor/issues/9108 // The razor tooling normalizes paths to be forward slash based, regardless of OS. diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultLanguageServerFeatureOptions.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultLanguageServerFeatureOptions.cs index cc165ade2b7..8911f1c7425 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultLanguageServerFeatureOptions.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultLanguageServerFeatureOptions.cs @@ -1,7 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT license. See License.txt in the project root for license information. -using System.Runtime.InteropServices; +using Microsoft.AspNetCore.Razor.Utilities; using Microsoft.CodeAnalysis.Razor.Workspaces; namespace Microsoft.AspNetCore.Razor.LanguageServer; @@ -25,8 +25,7 @@ internal class DefaultLanguageServerFeatureOptions : LanguageServerFeatureOption // Code action and rename paths in Windows VS Code need to be prefixed with '/': // https://github.com/dotnet/razor/issues/8131 - public override bool ReturnCodeActionAndRenamePathsWithPrefixedSlash - => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + public override bool ReturnCodeActionAndRenamePathsWithPrefixedSlash => PlatformInformation.IsWindows; public override bool ShowAllCSharpCodeActions => false; diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RemoteRazorProjectFileSystem.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RemoteRazorProjectFileSystem.cs index 15211a880bf..a195a62898d 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RemoteRazorProjectFileSystem.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RemoteRazorProjectFileSystem.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Runtime.InteropServices; using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Utilities; using Microsoft.CodeAnalysis.Razor; @@ -82,7 +81,7 @@ protected override string NormalizeAndEnsureValidPath(string path) static bool IsPathRootedForPlatform(string path) { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && path == "/") + if (PlatformInformation.IsWindows && path == "/") { // We have to special case windows and "/" because for some reason Path.IsPathRooted returns true on windows for a single "/" path. return false; diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/Utilities/FilePathNormalizer.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/Utilities/FilePathNormalizer.cs index 4d3a5e3db8f..ec419431db0 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/Utilities/FilePathNormalizer.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/Utilities/FilePathNormalizer.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Razor.Utilities; internal static class FilePathNormalizer { - private static readonly Func s_charConverter = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) + private static readonly Func s_charConverter = PlatformInformation.IsLinux ? c => c : char.ToLowerInvariant; @@ -223,7 +223,7 @@ private static (int start, int length) NormalizeCore(ReadOnlySpan source, // Replace slashes in our normalized span. NormalizeAndDedupeSlashes(destination, ref charsWritten); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + if (PlatformInformation.IsWindows && charsWritten > 1 && destination is ['/', ..] and not ['/', '/', ..]) { diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/Utilities/RazorProjectInfoFactory.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/Utilities/RazorProjectInfoFactory.cs index bb518d231a5..7eeb86d7ac1 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/Utilities/RazorProjectInfoFactory.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/Utilities/RazorProjectInfoFactory.cs @@ -5,7 +5,6 @@ using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using System.IO; -using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.Language; @@ -28,7 +27,7 @@ internal static class RazorProjectInfoFactory static RazorProjectInfoFactory() { - s_stringComparison = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) + s_stringComparison = PlatformInformation.IsLinux ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; } diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/RazorDynamicFileInfoProvider.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/RazorDynamicFileInfoProvider.cs index 96d41243c64..1e004d5bc65 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/RazorDynamicFileInfoProvider.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/DynamicFiles/RazorDynamicFileInfoProvider.cs @@ -6,13 +6,13 @@ using System.Collections.Generic; using System.ComponentModel.Composition; using System.Diagnostics; -using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor; using Microsoft.AspNetCore.Razor.ProjectSystem; using Microsoft.AspNetCore.Razor.Threading; +using Microsoft.AspNetCore.Razor.Utilities; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.ExternalAccess.Razor; using Microsoft.CodeAnalysis.Razor; @@ -305,7 +305,7 @@ public static string GetProjectSystemFilePath(Uri uri) // however, it's the only way to get the correct file path for a document to map to a corresponding project // system. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (PlatformInformation.IsWindows) { // VSWin return uri.GetAbsoluteOrUNCPath().Replace('/', '\\'); diff --git a/src/Razor/src/rzls/Program.cs b/src/Razor/src/rzls/Program.cs index 340d1491302..5c83140c6c2 100644 --- a/src/Razor/src/rzls/Program.cs +++ b/src/Razor/src/rzls/Program.cs @@ -4,12 +4,12 @@ using System; using System.Diagnostics; using System.Linq; -using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.LanguageServer.Extensions; using Microsoft.AspNetCore.Razor.LanguageServer.Hosting; using Microsoft.AspNetCore.Razor.Telemetry; +using Microsoft.AspNetCore.Razor.Utilities; using Microsoft.CodeAnalysis.Razor.Logging; using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.Extensions.DependencyInjection; @@ -30,10 +30,10 @@ public static async Task Main(string[] args) if (args[i].Contains("debug", StringComparison.OrdinalIgnoreCase)) { await Console.Error.WriteLineAsync($"Server started with process ID {Environment.ProcessId}").ConfigureAwait(true); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (PlatformInformation.IsWindows) { // Debugger.Launch() only works on Windows. - _ = Debugger.Launch(); + Debugger.Launch(); } else { diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.ExternalAccess.LegacyEditor.Test/Microsoft.AspNetCore.Razor.ExternalAccess.LegacyEditor.Test.csproj b/src/Razor/test/Microsoft.AspNetCore.Razor.ExternalAccess.LegacyEditor.Test/Microsoft.AspNetCore.Razor.ExternalAccess.LegacyEditor.Test.csproj index 35907884879..d7e7c685f78 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.ExternalAccess.LegacyEditor.Test/Microsoft.AspNetCore.Razor.ExternalAccess.LegacyEditor.Test.csproj +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.ExternalAccess.LegacyEditor.Test/Microsoft.AspNetCore.Razor.ExternalAccess.LegacyEditor.Test.csproj @@ -6,7 +6,6 @@ - diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test.csproj b/src/Razor/test/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test.csproj index 38b6311f6e2..14814afcfa8 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test.csproj +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test/Microsoft.AspNetCore.Razor.ExternalAccess.RoslynWorkspace.Test.csproj @@ -6,7 +6,6 @@ - diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DocumentPresentation/TextDocumentUriPresentationEndpointTests.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DocumentPresentation/TextDocumentUriPresentationEndpointTests.cs index 0e3e8509efd..ffdff508744 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DocumentPresentation/TextDocumentUriPresentationEndpointTests.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DocumentPresentation/TextDocumentUriPresentationEndpointTests.cs @@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.DocumentPresentation; public class TextDocumentUriPresentationEndpointTests(ITestOutputHelper testOutput) : LanguageServerTestBase(testOutput) { - [OSSkipConditionFact(["OSX", "Linux"])] + [ConditionalFact(Is.Windows)] public async Task Handle_SimpleComponent_ReturnsResult() { // Arrange @@ -83,7 +83,7 @@ await projectManager.UpdateAsync(updater => Assert.Equal("", documentEdits[0].Edits[0].NewText); } - [OSSkipConditionFact(["OSX", "Linux"])] + [ConditionalFact(Is.Windows)] public async Task Handle_SimpleComponentWithChildFile_ReturnsResult() { // Arrange @@ -148,7 +148,7 @@ await projectManager.UpdateAsync(updater => Assert.Equal("", documentEdits[0].Edits[0].NewText); } - [OSSkipConditionFact(["OSX", "Linux"])] + [ConditionalFact(Is.Windows)] public async Task Handle_ComponentWithRequiredAttribute_ReturnsResult() { // Arrange @@ -308,7 +308,7 @@ public async Task Handle_NotComponent_ReturnsNull() Assert.Null(result); } - [OSSkipConditionFact(["OSX", "Linux"])] + [ConditionalFact(Is.Windows)] public async Task Handle_ComponentWithNestedFiles_ReturnsResult() { // Arrange diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/FindNodeTests.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/FindNodeTests.cs index 5adc6703356..b9038c5dcba 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/FindNodeTests.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/FindNodeTests.cs @@ -1,10 +1,10 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT license. See License.txt in the project root for license information. -using System.Runtime.InteropServices; using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language.Syntax; using Microsoft.AspNetCore.Razor.Test.Common; +using Microsoft.AspNetCore.Razor.Utilities; using Microsoft.CodeAnalysis.Text; using Xunit; using Xunit.Abstractions; @@ -63,7 +63,7 @@ protected override async Task OnInitializedAsync() } """; - private static readonly string s_fetchDataContents = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + private static readonly string s_fetchDataContents = PlatformInformation.IsWindows ? FetchDataContents : FetchDataContents.Replace("\n", "\r\n"); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Microsoft.AspNetCore.Razor.LanguageServer.Test.csproj b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Microsoft.AspNetCore.Razor.LanguageServer.Test.csproj index 685173189f4..0a46c9e0354 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Microsoft.AspNetCore.Razor.LanguageServer.Test.csproj +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Microsoft.AspNetCore.Razor.LanguageServer.Test.csproj @@ -10,7 +10,6 @@ - diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/ProjectSystem/IProjectSnapshotManagerExtensionsTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/ProjectSystem/IProjectSnapshotManagerExtensionsTest.cs index c124083560f..e8335d1056d 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/ProjectSystem/IProjectSnapshotManagerExtensionsTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/ProjectSystem/IProjectSnapshotManagerExtensionsTest.cs @@ -177,7 +177,7 @@ public async Task TryResolveAllProjects_MiscellaneousOwnerProjectWithOthers_Retu AssertSnapshotsEqual(miscProject, project); } - [OSSkipConditionFact(["OSX", "Linux"])] + [ConditionalFact(Is.Windows)] public async Task TryResolveAllProjects_OwnerProjectDifferentCasing_ReturnsTrue() { // Arrange diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RemoteRazorProjectFileSystemTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RemoteRazorProjectFileSystemTest.cs index 919c0abb20a..e4a3f8751a5 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RemoteRazorProjectFileSystemTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RemoteRazorProjectFileSystemTest.cs @@ -3,8 +3,8 @@ #nullable disable -using System.Runtime.InteropServices; using Microsoft.AspNetCore.Razor.Test.Common; +using Microsoft.AspNetCore.Razor.Utilities; using Xunit; using Xunit.Abstractions; @@ -54,7 +54,7 @@ public void GetItem_RootedFilePath_DoesNotBelongToProject() RemoteRazorProjectFileSystem fileSystem; string documentFilePath; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (PlatformInformation.IsWindows) { fileSystem = new RemoteRazorProjectFileSystem(@"C:\path\to"); documentFilePath = @"C:\otherpath\to\file.cshtml"; diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokensTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokensTest.cs index 6ef88b7406d..20d7a817396 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokensTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokensTest.cs @@ -8,7 +8,6 @@ using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading; @@ -19,6 +18,7 @@ using Microsoft.AspNetCore.Razor.PooledObjects; using Microsoft.AspNetCore.Razor.Test.Common; using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer; +using Microsoft.AspNetCore.Razor.Utilities; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Razor.Protocol; @@ -903,7 +903,7 @@ public void GetMappedCSharpRanges_MinimalRangeVsSmallDisjointRanges_DisjointRang else { // Note that the expected lengths are different on Windows vs. Unix. - var expectedCsharpRangeLength = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 945 : 911; + var expectedCsharpRangeLength = PlatformInformation.IsWindows ? 945 : 911; Assert.True(codeDocument.TryGetMinimalCSharpRange(razorRange, out var csharpRange)); var textSpan = csharpSourceText.GetTextSpan(csharpRange); Assert.Equal(expectedCsharpRangeLength, textSpan.Length); @@ -1120,7 +1120,7 @@ private string GetBaselineFileContents(string baselineFileName) var baselineContents = semanticFile.ReadAllText(); - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (!PlatformInformation.IsWindows) { baselineContents = s_matchNewLines.Replace(baselineContents, "\n"); } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/FilePathNormalizerTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/FilePathNormalizerTest.cs index 4f3215e65ca..dc7f80d3276 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/FilePathNormalizerTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/FilePathNormalizerTest.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Razor.ProjectEngineHost.Test; public class FilePathNormalizerTest(ITestOutputHelper testOutput) : ToolingTestBase(testOutput) { - [OSSkipConditionFact(["OSX", "Linux"])] + [ConditionalFact(Is.Windows)] public void Normalize_Windows_StripsPrecedingSlash() { // Arrange @@ -24,7 +24,7 @@ public void Normalize_Windows_StripsPrecedingSlash() Assert.Equal("c:/path/to/something", path); } - [OSSkipConditionFact(["OSX", "Linux"])] + [ConditionalFact(Is.Windows)] public void Normalize_Windows_StripsPrecedingSlash_ShortPath() { // Arrange @@ -141,7 +141,7 @@ public void NormalizeDirectory_EndsWithoutSlash() Assert.Equal("C:/path/to/directory/", normalized); } - [OSSkipConditionFact(["OSX", "Linux"])] + [ConditionalFact(Is.Windows)] public void NormalizeDirectory_Windows_HandlesSingleSlashDirectory() { // Arrange @@ -261,7 +261,7 @@ public void Normalize_EmptyFilePath_ReturnsEmptyString() Assert.Equal("/", normalized); } - [OSSkipConditionFact(["Windows"])] + [ConditionalFact(Is.Not.Windows)] public void Normalize_NonWindows_AddsLeadingForwardSlash() { // Arrange @@ -314,7 +314,7 @@ public void Normalize_ReplacesBackSlashesWithForwardSlashes() Assert.Equal("C:/path/to/document.cshtml", normalized); } - [OSSkipConditionTheory(["OSX", "Linux"])] + [ConditionalTheory(Is.Windows)] [InlineData(@"C:\path\to\document.cshtml")] [InlineData(@"c:\path\to\document.cshtml")] [InlineData("C:/path/to/document.cshtml")] diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test.csproj b/src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test.csproj index 19e59e95087..9ceec560da4 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test.csproj +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test.csproj @@ -6,7 +6,6 @@ - diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Language/IntegrationTests/RazorToolingIntegrationTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Language/IntegrationTests/RazorToolingIntegrationTestBase.cs index a4ff782e2bb..f59a6dece19 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Language/IntegrationTests/RazorToolingIntegrationTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Language/IntegrationTests/RazorToolingIntegrationTestBase.cs @@ -8,9 +8,9 @@ using System.IO; using System.Linq; using System.Reflection; -using System.Runtime.InteropServices; using Microsoft.AspNetCore.Razor.PooledObjects; using Microsoft.AspNetCore.Razor.Test.Common; +using Microsoft.AspNetCore.Razor.Utilities; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Razor; @@ -54,7 +54,7 @@ public RazorToolingIntegrationTestBase(ITestOutputHelper testOutput) Configuration = RazorConfiguration.Default; FileSystem = new VirtualRazorProjectFileSystem(); PathSeparator = Path.DirectorySeparatorChar.ToString(); - WorkingDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ArbitraryWindowsPath : ArbitraryMacLinuxPath; + WorkingDirectory = PlatformInformation.IsWindows ? ArbitraryWindowsPath : ArbitraryMacLinuxPath; DefaultRootNamespace = "Test"; // Matches the default working directory DefaultFileName = "TestComponent.cshtml"; @@ -316,7 +316,7 @@ protected static CSharpSyntaxTree Parse(string text, string path = null) protected static void AssertSourceEquals(string expected, CompileToCSharpResult generated) { // Normalize the paths inside the expected result to match the OS paths - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (!PlatformInformation.IsWindows) { var windowsPath = Path.Combine(ArbitraryWindowsPath, generated.CodeDocument.Source.RelativePath).Replace('/', '\\'); expected = expected.Replace(windowsPath, generated.CodeDocument.Source.FilePath); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/PathUtilities.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/PathUtilities.cs index d98a1432973..a7c074ee2a7 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/PathUtilities.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/PathUtilities.cs @@ -3,7 +3,6 @@ using System; using System.IO; -using System.Runtime.InteropServices; using Microsoft.AspNetCore.Razor.Utilities; using Xunit; @@ -17,7 +16,7 @@ public static string CreateRootedPath(params string[] parts) if (!Path.IsPathRooted(result)) { - result = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + result = PlatformInformation.IsWindows ? @"C:\" + result : "/" + result; } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/TestProjectData.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/TestProjectData.cs index 32291f15f8d..815e9588eea 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/TestProjectData.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/TestProjectData.cs @@ -2,8 +2,8 @@ // Licensed under the MIT license. See License.txt in the project root for license information. using System.IO; -using System.Runtime.InteropServices; using Microsoft.AspNetCore.Razor.Language; +using Microsoft.AspNetCore.Razor.Utilities; using Microsoft.CodeAnalysis.Razor.ProjectSystem; namespace Microsoft.AspNetCore.Razor.Test.Common; @@ -18,7 +18,7 @@ internal static class TestProjectData { static TestProjectData() { - var baseDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "c:\\users\\example\\src" : "/home/example"; + var baseDirectory = PlatformInformation.IsWindows ? @"c:\users\example\src" : "/home/example"; SomeProjectPath = Path.Combine(baseDirectory, "SomeProject"); var someProjectObjPath = Path.Combine(SomeProjectPath, "obj"); diff --git a/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj b/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj index 232456c3724..36c2f7e6ac4 100644 --- a/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj +++ b/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj @@ -7,7 +7,6 @@ - diff --git a/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/ProjectSystem/ProjectKeyTests.cs b/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/ProjectSystem/ProjectKeyTests.cs index ce5c210063c..f62e0175a1d 100644 --- a/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/ProjectSystem/ProjectKeyTests.cs +++ b/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/ProjectSystem/ProjectKeyTests.cs @@ -40,7 +40,7 @@ public void EqualityTests(string id1, string id2) Assert.False(!key1.Equals((object)key2)); } - [OSSkipConditionTheory(new[] { "OSX", "Linux" })] + [ConditionalTheory(Is.Windows)] [InlineData(@"/c:/path/to/dir/", @"c:\path\to\dir")] [InlineData(@"/c:\path/to\dir/", @"c:\path\to\dir")] [InlineData(@"\path\to\dir\", @"path\to\dir")] diff --git a/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/UriExtensionsTest.cs b/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/UriExtensionsTest.cs index 4c9cd9cc0b9..6bad640a6c5 100644 --- a/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/UriExtensionsTest.cs +++ b/src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/UriExtensionsTest.cs @@ -16,7 +16,7 @@ public UriExtensionsTest(ITestOutputHelper testOutput) { } - [OSSkipConditionFact(new[] { "OSX", "Linux" })] + [ConditionalFact(Is.Windows)] public void GetAbsoluteOrUNCPath_AbsolutePath_ReturnsAbsolutePath() { // Arrange @@ -29,7 +29,7 @@ public void GetAbsoluteOrUNCPath_AbsolutePath_ReturnsAbsolutePath() Assert.Equal(uri.AbsolutePath, path); } - [OSSkipConditionFact(new[] { "OSX", "Linux" })] + [ConditionalFact(Is.Windows)] public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesPlusPaths() { // Arrange @@ -42,7 +42,7 @@ public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesPlusPaths() Assert.Equal(uri.AbsolutePath, path); } - [OSSkipConditionFact(new[] { "OSX", "Linux" })] + [ConditionalFact(Is.Windows)] public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesSpacePaths() { // Arrange @@ -55,7 +55,8 @@ public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesSpacePaths() Assert.Equal("c:/Some/path/to/file path.cshtml", path); } - [OSSkipConditionTheory(new[] { "OSX", "Linux" }), WorkItem("https://github.com/dotnet/razor/issues/9365")] + [ConditionalTheory(Is.Windows)] + [WorkItem("https://github.com/dotnet/razor/issues/9365")] [InlineData(@"git:/c%3A/path/to/dir/Index.cshtml", @"c:/_git_/path/to/dir/Index.cshtml")] [InlineData(@"git:/c:/path%2Fto/dir/Index.cshtml?%7B%22p", @"c:/_git_/path/to/dir/Index.cshtml")] [InlineData(@"git:/c:/path/to/dir/Index.cshtml", @"c:/_git_/path/to/dir/Index.cshtml")] @@ -71,7 +72,7 @@ public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesGitScheme(string filePath, Assert.Equal(expected, path); } - [OSSkipConditionTheory(new[] { "OSX", "Linux" })] + [ConditionalTheory(Is.Windows)] [InlineData(@"file:///c:/path/to/dir/Index.cshtml", @"c:/path/to/dir/Index.cshtml")] [InlineData(@"file:///c:\path/to\dir/Index.cshtml", @"c:/path/to/dir/Index.cshtml")] [InlineData(@"file:///C:\path\to\dir\Index.cshtml", @"C:/path/to/dir/Index.cshtml")] @@ -129,7 +130,8 @@ public void GetAbsoluteOrUNCPath_UNCPath_HandlesSpacePaths() Assert.Equal(@"\\some\path\to\file path.cshtml", path); } - [OSSkipConditionTheory(new[] { "Windows" }), WorkItem("https://github.com/dotnet/razor/issues/9365")] + [ConditionalTheory(Is.Not.Windows)] + [WorkItem("https://github.com/dotnet/razor/issues/9365")] [InlineData("git:///path/to/dir/Index.cshtml", "/_git_/path/to/dir/Index.cshtml")] [InlineData("git:///path%2Fto/dir/Index.cshtml", "/_git_/path/to/dir/Index.cshtml")] [InlineData("file:///path/to/dir/Index.cshtml", @"/path/to/dir/Index.cshtml")] diff --git a/src/Razor/test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test.csproj b/src/Razor/test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test.csproj index 5f66568e762..3c89a6a9ed9 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test.csproj +++ b/src/Razor/test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test.csproj @@ -8,10 +8,6 @@ - - - - diff --git a/src/Razor/test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test/UriExtensionsTest.cs b/src/Razor/test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test/UriExtensionsTest.cs index 24987107c32..b27574a8d03 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test/UriExtensionsTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Test/UriExtensionsTest.cs @@ -17,7 +17,7 @@ public UriExtensionsTest(ITestOutputHelper testOutput) { } - [OSSkipConditionFact(new[] { "OSX", "Linux" })] + [ConditionalFact(Is.Windows)] public void GetAbsoluteOrUNCPath_AbsolutePath_ReturnsAbsolutePath() { // Arrange @@ -30,7 +30,7 @@ public void GetAbsoluteOrUNCPath_AbsolutePath_ReturnsAbsolutePath() Assert.Equal(uri.AbsolutePath, path); } - [OSSkipConditionFact(new[] { "OSX", "Linux" })] + [ConditionalFact(Is.Windows)] public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesPlusPaths() { // Arrange @@ -43,7 +43,7 @@ public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesPlusPaths() Assert.Equal(uri.AbsolutePath, path); } - [OSSkipConditionFact(new[] { "OSX", "Linux" })] + [ConditionalFact(Is.Windows)] public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesSpacePaths() { // Arrange diff --git a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/LanguageClient/RazorDocumentOptionsServiceTest.cs b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/LanguageClient/RazorDocumentOptionsServiceTest.cs index 9e6c078dd94..72570a90160 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/LanguageClient/RazorDocumentOptionsServiceTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/LanguageClient/RazorDocumentOptionsServiceTest.cs @@ -3,12 +3,12 @@ using System.IO; using System.Linq; -using System.Runtime.InteropServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.ProjectSystem; using Microsoft.AspNetCore.Razor.Test.Common; using Microsoft.AspNetCore.Razor.Test.Common.Workspaces; +using Microsoft.AspNetCore.Razor.Utilities; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Options; @@ -90,7 +90,7 @@ private static OptionKey GetIndentationSizeOptionKey(Document document) // Adds the text to a ProjectSnapshot, generates code, and updates the workspace. private Document InitializeDocument(SourceText sourceText) { - var baseDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "c:\\users\\example\\src" : "/home/example"; + var baseDirectory = PlatformInformation.IsWindows ? @"c:\users\example\src" : "/home/example"; var hostProject = new HostProject( Path.Combine(baseDirectory, "SomeProject", "SomeProject.csproj"), Path.Combine(baseDirectory, "SomeProject", "obj"), RazorConfiguration.Default, "SomeProject"); var hostDocument = new HostDocument( diff --git a/src/Razor/test/OSSkipConditionFactAttribute.cs b/src/Razor/test/OSSkipConditionFactAttribute.cs deleted file mode 100644 index 1720cad03ca..00000000000 --- a/src/Razor/test/OSSkipConditionFactAttribute.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the MIT license. See License.txt in the project root for license information. - -using System; -using System.Runtime.InteropServices; -using Xunit; - -namespace Microsoft.AspNetCore.Razor; - -public class OSSkipConditionFactAttribute : FactAttribute -{ - /// - /// A that configures on the specified platforms. - /// - /// Valid values include WINDOWS, LINUX, OSX, and FREEBSD. - /// - public OSSkipConditionFactAttribute(string[] skippedPlatforms) - { - SetSkipIfNecessary(this, skippedPlatforms); - } - - internal static void SetSkipIfNecessary(FactAttribute fact, string[] skippedPlatforms) - { - foreach (var platform in skippedPlatforms) - { - var osPlatform = platform switch - { - "Windows" => OSPlatform.Windows, - "Linux" => OSPlatform.Linux, - "OSX" => OSPlatform.OSX, -#if NET - "FreeBSD" => OSPlatform.FreeBSD, -#endif - _ => throw new NotSupportedException($"Unsupported platform: {platform}") - }; - - if (RuntimeInformation.IsOSPlatform(osPlatform)) - { - fact.Skip = $"Ignored on {platform}"; - break; - } - } - } -} - -public class OSSkipConditionTheoryAttribute : TheoryAttribute -{ - public OSSkipConditionTheoryAttribute(string[] skippedPlatforms) - { - OSSkipConditionFactAttribute.SetSkipIfNecessary(this, skippedPlatforms); - } -} diff --git a/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/ConditionalFactAttribute.cs b/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/ConditionalFactAttribute.cs new file mode 100644 index 00000000000..c025e3b34fa --- /dev/null +++ b/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/ConditionalFactAttribute.cs @@ -0,0 +1,192 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Frozen; +using System.Collections.Generic; +using Microsoft.AspNetCore.Razor.Utilities; +using Xunit; + +namespace Microsoft.AspNetCore.Razor; + +/// +/// A that only executes if each of the given conditions are met. +/// +/// +/// Conditions can be provided using the type. For example, -or- +/// , . +/// +public sealed class ConditionalFactAttribute : FactAttribute +{ + public ConditionalFactAttribute(params string[] conditions) + { + if (!Conditions.AllTrue(conditions)) + { + base.Skip = Reason ?? Conditions.GetSkipReason(conditions); + } + } + + /// + /// This property exists to prevent users of + /// from accidentally putting documentation in the property instead of Reason. + /// Setting would cause the test to be unconditionally skip. + /// + [Obsolete($"{nameof(ConditionalFactAttribute)} should always use {nameof(Reason)} or {nameof(AlwaysSkip)}", error: true)] + public new string Skip + { + get { return base.Skip; } + set { base.Skip = value; } + } + + /// + /// Use to unconditionally skip a test. + /// + /// + /// This is useful in the rare occasion when a conditional test needs to be skipped unconditionally. + /// Typically, this is for a short term reason, such as working on a bug fix. + /// + public string AlwaysSkip + { + get { return base.Skip; } + set { base.Skip = value; } + } + + public string? Reason { get; set; } +} + +/// +/// A that only executes if each of the given conditions are met. +/// +/// +/// Conditions can be provided using the type. For example, -or- +/// , . +/// +public sealed class ConditionalTheoryAttribute : TheoryAttribute +{ + public ConditionalTheoryAttribute(params string[] conditions) + { + if (!Conditions.AllTrue(conditions)) + { + base.Skip = Reason ?? Conditions.GetSkipReason(conditions); + } + } + + /// + /// This property exists to prevent users of + /// from accidentally putting documentation in the property instead of Reason. + /// Setting would cause the test to be unconditionally skip. + /// + [Obsolete($"{nameof(ConditionalFactAttribute)} should always use {nameof(Reason)} or {nameof(AlwaysSkip)}", error: true)] + public new string Skip + { + get { return base.Skip; } + set { base.Skip = value; } + } + + /// + /// Use to unconditionally skip a test. + /// + /// + /// This is useful in the rare occasion when a conditional test needs to be skipped unconditionally. + /// Typically, this is for a short term reason, such as working on a bug fix. + /// + public string AlwaysSkip + { + get { return base.Skip; } + set { base.Skip = value; } + } + + public string? Reason { get; set; } +} + +public static class Is +{ + /// + /// Only execute if the current operating system platform is Windows. + /// + public const string Windows = nameof(Windows); + + /// + /// Only execute if the current operating system platform is Linux. + /// + public const string Linux = nameof(Linux); + + /// + /// Only execute if the current operating system platform is MacOS. + /// + public const string MacOS = nameof(MacOS); + + /// + /// Only execute if the current operating system platform is FreeBSD. + /// + public const string FreeBSD = nameof(FreeBSD); + + public static class Not + { + /// + /// Only execute if the current operating system platform is not Windows. + /// + public const string Windows = $"!{nameof(Windows)}"; + + /// + /// Only execute if the current operating system platform is Linux. + /// + public const string Linux = $"!{nameof(Linux)}"; + + /// + /// Only execute if the current operating system platform is not MacOS. + /// + public const string MacOS = $"!{nameof(MacOS)}"; + + /// + /// Only execute if the current operating system platform is not FreeBSD. + /// + public const string FreeBSD = $"!{nameof(FreeBSD)}"; + } +} + +public static class Conditions +{ + private static readonly FrozenDictionary> s_conditionMap = CreateConditionMap(); + + private static FrozenDictionary> CreateConditionMap() + { + var map = new Dictionary>(StringComparer.OrdinalIgnoreCase); + + Add(Is.Windows, static () => PlatformInformation.IsWindows); + Add(Is.Linux, static () => PlatformInformation.IsLinux); + Add(Is.MacOS, static () => PlatformInformation.IsMacOS); + Add(Is.FreeBSD, static () => PlatformInformation.IsFreeBSD); + + return map.ToFrozenDictionary(); + + void Add(string name, Func predicate) + { + map.Add(name, predicate); + + // Add negated condition + map.Add($"!{name}", () => !predicate()); + } + } + + public static bool AllTrue(string[] conditions) + { + foreach (var condition in conditions) + { + if (!s_conditionMap.TryGetValue(condition, out var predicate)) + { + throw new NotSupportedException($"Encountered unexpected condition: {condition}"); + } + + if (!predicate()) + { + return false; + } + } + + return true; + } + + public static string GetSkipReason(string[] conditions) + => $"The following conditions are not all true: {string.Join(", ", conditions)}"; +} diff --git a/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorIntegrationTestBase.cs b/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorIntegrationTestBase.cs index 8781a1ffafd..87828d221a0 100644 --- a/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorIntegrationTestBase.cs +++ b/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorIntegrationTestBase.cs @@ -6,9 +6,9 @@ using System.Collections.Immutable; using System.IO; using System.Linq; -using System.Runtime.InteropServices; using System.Text; using Microsoft.AspNetCore.Razor.Test.Common; +using Microsoft.AspNetCore.Razor.Utilities; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Razor; @@ -51,7 +51,7 @@ public RazorIntegrationTestBase() Configuration = RazorConfiguration.Default; FileSystem = new VirtualRazorProjectFileSystem(); PathSeparator = Path.DirectorySeparatorChar.ToString(); - WorkingDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ArbitraryWindowsPath : ArbitraryMacLinuxPath; + WorkingDirectory = PlatformInformation.IsWindows ? ArbitraryWindowsPath : ArbitraryMacLinuxPath; DefaultRootNamespace = "Test"; // Matches the default working directory DefaultFileName = "TestComponent.cshtml"; @@ -404,7 +404,7 @@ protected static CSharpSyntaxTree Parse(string text, CSharpParseOptions? parseOp protected static void AssertSourceEquals(string expected, CompileToCSharpResult generated) { // Normalize the paths inside the expected result to match the OS paths - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (!PlatformInformation.IsWindows) { var windowsPath = Path.Combine(ArbitraryWindowsPath, generated.CodeDocument.Source.RelativePath ?? "").Replace('/', '\\'); expected = expected.Replace(windowsPath, generated.CodeDocument.Source.FilePath); diff --git a/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared.Test/Microsoft.AspNetCore.Razor.Utilities.Shared.Test.csproj b/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared.Test/Microsoft.AspNetCore.Razor.Utilities.Shared.Test.csproj index b10885404af..1f008203c9b 100644 --- a/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared.Test/Microsoft.AspNetCore.Razor.Utilities.Shared.Test.csproj +++ b/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared.Test/Microsoft.AspNetCore.Razor.Utilities.Shared.Test.csproj @@ -12,6 +12,7 @@ + diff --git a/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/Utilities/PlatformInformation.cs b/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/Utilities/PlatformInformation.cs new file mode 100644 index 00000000000..29a6310f2ab --- /dev/null +++ b/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/Utilities/PlatformInformation.cs @@ -0,0 +1,19 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT license. See License.txt in the project root for license information. + +using System.Runtime.InteropServices; + +namespace Microsoft.AspNetCore.Razor.Utilities; + +internal static class PlatformInformation +{ + public static bool IsWindows { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + public static bool IsLinux { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + public static bool IsMacOS { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); + +#if NET + public static bool IsFreeBSD { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD); +#else + public static bool IsFreeBSD { get; } = false; +#endif +}