Skip to content

Commit

Permalink
Merge pull request #73372 from CyrusNajmabadi/runOopByDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored May 8, 2024
2 parents 24fa558 + ef49834 commit 2882436
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal TestParameters(
bool retainNonFixableDiagnostics = false,
bool includeDiagnosticsOutsideSelection = false,
string title = null,
TestHost testHost = TestHost.InProcess,
TestHost testHost = TestHost.OutOfProcess,
string workspaceKind = null,
bool includeNonLocalDocumentDiagnostics = false,
bool treatPositionIndicatorsAsCode = false)
Expand Down Expand Up @@ -414,7 +414,7 @@ internal Task TestInRegularAndScriptAsync(
object fixProviderData = null,
ParseOptions parseOptions = null,
string title = null,
TestHost testHost = TestHost.InProcess)
TestHost testHost = TestHost.OutOfProcess)
{
return TestInRegularAndScript1Async(
initialMarkup, expectedMarkup,
Expand Down Expand Up @@ -454,7 +454,7 @@ internal Task TestAsync(
OptionsCollectionAlias globalOptions = null,
object fixProviderData = null,
CodeActionPriority? priority = null,
TestHost testHost = TestHost.InProcess)
TestHost testHost = TestHost.OutOfProcess)
{
return TestAsync(
initialMarkup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ internal override async Task<IEnumerable<Diagnostic>> GetDiagnosticsAsync(
EditorTestWorkspace workspace, TestParameters parameters)
{
var (analyzer, _) = GetOrCreateDiagnosticProviderAndFixer(workspace, parameters);
AddAnalyzerToWorkspace(workspace, analyzer, parameters);
AddAnalyzerToWorkspace(workspace, analyzer);

var document = GetDocumentAndSelectSpan(workspace, out var span);
var allDiagnostics = await DiagnosticProviderTestUtilities.GetAllDiagnosticsAsync(workspace, document, span, includeNonLocalDocumentDiagnostics: parameters.includeNonLocalDocumentDiagnostics);
Expand All @@ -164,7 +164,7 @@ internal override async Task<IEnumerable<Diagnostic>> GetDiagnosticsAsync(
EditorTestWorkspace workspace, TestParameters parameters)
{
var (analyzer, fixer) = GetOrCreateDiagnosticProviderAndFixer(workspace, parameters);
AddAnalyzerToWorkspace(workspace, analyzer, parameters);
AddAnalyzerToWorkspace(workspace, analyzer);

GetDocumentAndSelectSpanOrAnnotatedSpan(workspace, out var document, out var span, out var annotation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Remote.Testing;
using Microsoft.CodeAnalysis.Serialization;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
Expand Down Expand Up @@ -100,26 +101,23 @@ protected override async Task<ImmutableArray<Diagnostic>> GetDiagnosticsWorkerAs
internal override Task<CodeRefactoring> GetCodeRefactoringAsync(EditorTestWorkspace workspace, TestParameters parameters)
=> throw new NotImplementedException("No refactoring provided in diagnostic test");

protected static void AddAnalyzerToWorkspace(Workspace workspace, DiagnosticAnalyzer analyzer, TestParameters parameters)
protected static void AddAnalyzerToWorkspace(Workspace workspace, DiagnosticAnalyzer analyzer)
{
AnalyzerReference[] analyzeReferences;
AnalyzerReference[] analyzerReferences;
if (analyzer != null)
{
Contract.ThrowIfTrue(parameters.testHost == TestHost.OutOfProcess, $"Out-of-proc testing is not supported since {analyzer} can't be serialized.");

analyzeReferences = new[] { new AnalyzerImageReference(ImmutableArray.Create(analyzer)) };
var analyzerImageReference = new AnalyzerImageReference(ImmutableArray.Create(analyzer));
analyzerReferences = [analyzerImageReference];
}
else
{
// create a serializable analyzer reference:
analyzeReferences = new[]
{
analyzerReferences = [
new AnalyzerFileReference(DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(LanguageNames.CSharp).GetType().Assembly.Location, TestAnalyzerAssemblyLoader.LoadFromFile),
new AnalyzerFileReference(DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(LanguageNames.VisualBasic).GetType().Assembly.Location, TestAnalyzerAssemblyLoader.LoadFromFile)
};
new AnalyzerFileReference(DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(LanguageNames.VisualBasic).GetType().Assembly.Location, TestAnalyzerAssemblyLoader.LoadFromFile)];
}

workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(analyzeReferences));
workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(analyzerReferences));
}

protected static Document GetDocumentAndSelectSpan(EditorTestWorkspace workspace, out TextSpan span)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static async Task TestAsync(
string? equivalenceKey = null,
LanguageVersion languageVersion = LanguageVersion.CSharp9,
OptionsCollection? options = null,
TestHost testHost = TestHost.InProcess,
TestHost testHost = TestHost.OutOfProcess,
string[]? actions = null)
{
if (index != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ void Method()
var parameters = new TestParameters();
using var workspace = CreateWorkspaceFromOptions(source, parameters);

var analyzerReference = new AnalyzerImageReference(ImmutableArray.Create<DiagnosticAnalyzer>(new CSharpCompilerDiagnosticAnalyzer()));
workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference }));
var analyzerReference = new AnalyzerImageReference([new CSharpCompilerDiagnosticAnalyzer()]);
workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences([analyzerReference]));

var diagnosticService = Assert.IsType<DiagnosticAnalyzerService>(workspace.ExportProvider.GetExportedValue<IDiagnosticAnalyzerService>());
var incrementalAnalyzer = diagnosticService.CreateIncrementalAnalyzer(workspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal TestParameters(
bool retainNonFixableDiagnostics = false,
bool includeDiagnosticsOutsideSelection = false,
string title = null,
TestHost testHost = TestHost.InProcess,
TestHost testHost = TestHost.OutOfProcess,
string workspaceKind = null,
bool includeNonLocalDocumentDiagnostics = false,
bool treatPositionIndicatorsAsCode = false)
Expand Down Expand Up @@ -417,7 +417,7 @@ internal Task TestInRegularAndScriptAsync(
object fixProviderData = null,
ParseOptions parseOptions = null,
string title = null,
TestHost testHost = TestHost.InProcess)
TestHost testHost = TestHost.OutOfProcess)
{
return TestInRegularAndScript1Async(
initialMarkup, expectedMarkup,
Expand Down Expand Up @@ -457,7 +457,7 @@ internal Task TestAsync(
OptionsCollectionAlias globalOptions = null,
object fixProviderData = null,
CodeActionPriority? priority = null,
TestHost testHost = TestHost.InProcess)
TestHost testHost = TestHost.OutOfProcess)
{
return TestAsync(
initialMarkup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ internal override async Task<IEnumerable<Diagnostic>> GetDiagnosticsAsync(
TestWorkspace workspace, TestParameters parameters)
{
var (analyzer, _) = GetOrCreateDiagnosticProviderAndFixer(workspace, parameters);
AddAnalyzerToWorkspace(workspace, analyzer, parameters);
AddAnalyzerToWorkspace(workspace, analyzer);

var document = GetDocumentAndSelectSpan(workspace, out var span);
var allDiagnostics = await DiagnosticProviderTestUtilities.GetAllDiagnosticsAsync(workspace, document, span, includeNonLocalDocumentDiagnostics: parameters.includeNonLocalDocumentDiagnostics);
Expand All @@ -164,7 +164,7 @@ internal override async Task<IEnumerable<Diagnostic>> GetDiagnosticsAsync(
TestWorkspace workspace, TestParameters parameters)
{
var (analyzer, fixer) = GetOrCreateDiagnosticProviderAndFixer(workspace, parameters);
AddAnalyzerToWorkspace(workspace, analyzer, parameters);
AddAnalyzerToWorkspace(workspace, analyzer);

GetDocumentAndSelectSpanOrAnnotatedSpan(workspace, out var document, out var span, out var annotation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal override async Task<IEnumerable<Diagnostic>> GetDiagnosticsAsync(
TestWorkspace workspace, TestParameters parameters)
{
var (analyzer, _) = CreateDiagnosticProviderAndFixer(workspace);
AddAnalyzerToWorkspace(workspace, analyzer, parameters);
AddAnalyzerToWorkspace(workspace, analyzer);

var document = GetDocumentAndSelectSpan(workspace, out var span);
var diagnostics = await DiagnosticProviderTestUtilities.GetAllDiagnosticsAsync(workspace, document, span, includeNonLocalDocumentDiagnostics: parameters.includeNonLocalDocumentDiagnostics);
Expand All @@ -79,7 +79,7 @@ internal override async Task<IEnumerable<Diagnostic>> GetDiagnosticsAsync(
TestWorkspace workspace, TestParameters parameters)
{
var (analyzer, fixer) = CreateDiagnosticProviderAndFixer(workspace);
AddAnalyzerToWorkspace(workspace, analyzer, parameters);
AddAnalyzerToWorkspace(workspace, analyzer);

GetDocumentAndSelectSpanOrAnnotatedSpan(workspace, out var document, out var span, out var annotation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.RemoveUnnecessarySuppressions;
using Microsoft.CodeAnalysis.Serialization;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.UnitTests.Diagnostics;
using Xunit.Abstractions;
Expand All @@ -32,7 +33,7 @@ protected AbstractUnncessarySuppressionDiagnosticTest(ITestOutputHelper logger)
private void AddAnalyzersToWorkspace(TestWorkspace workspace)
{
var analyzerReference = new AnalyzerImageReference(OtherAnalyzers.Add(SuppressionAnalyzer));
workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference }));
workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences([analyzerReference]));
}

internal override async Task<IEnumerable<Diagnostic>> GetDiagnosticsAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Remote.Testing;
using Microsoft.CodeAnalysis.Serialization;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
Expand Down Expand Up @@ -100,26 +101,23 @@ protected override async Task<ImmutableArray<Diagnostic>> GetDiagnosticsWorkerAs
internal override Task<CodeRefactoring> GetCodeRefactoringAsync(TestWorkspace workspace, TestParameters parameters)
=> throw new NotImplementedException("No refactoring provided in diagnostic test");

protected static void AddAnalyzerToWorkspace(Workspace workspace, DiagnosticAnalyzer analyzer, TestParameters parameters)
protected static void AddAnalyzerToWorkspace(Workspace workspace, DiagnosticAnalyzer analyzer)
{
AnalyzerReference[] analyzeReferences;
AnalyzerReference[] analyzerReferences;
if (analyzer != null)
{
Contract.ThrowIfTrue(parameters.testHost == TestHost.OutOfProcess, $"Out-of-proc testing is not supported since {analyzer} can't be serialized.");

analyzeReferences = new[] { new AnalyzerImageReference(ImmutableArray.Create(analyzer)) };
var analyzerImageReference = new AnalyzerImageReference([analyzer]);
analyzerReferences = [analyzerImageReference];
}
else
{
// create a serializable analyzer reference:
analyzeReferences = new[]
{
analyzerReferences = [
new AnalyzerFileReference(DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(LanguageNames.CSharp).GetType().Assembly.Location, TestAnalyzerAssemblyLoader.LoadFromFile),
new AnalyzerFileReference(DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(LanguageNames.VisualBasic).GetType().Assembly.Location, TestAnalyzerAssemblyLoader.LoadFromFile)
};
new AnalyzerFileReference(DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(LanguageNames.VisualBasic).GetType().Assembly.Location, TestAnalyzerAssemblyLoader.LoadFromFile)];
}

workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(analyzeReferences));
workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(analyzerReferences));
}

protected static Document GetDocumentAndSelectSpan(TestWorkspace workspace, out TextSpan span)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Imports Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Extensions
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.Diagnostics
Imports Microsoft.CodeAnalysis.Remote.Testing
Imports Microsoft.CodeAnalysis.VisualBasic.CodeFixes.SimplifyTypeNames
Imports Microsoft.CodeAnalysis.VisualBasic.SimplifyTypeNames

Expand Down Expand Up @@ -1821,7 +1822,7 @@ End Module

Await TestInRegularAndScriptAsync(source.Value, expected.Value)

Using workspace = TestWorkspace.CreateVisualBasic(source.Value, composition:=GetComposition())
Using workspace = TestWorkspace.CreateVisualBasic(source.Value, composition:=GetComposition().WithTestHostParts(TestHost.OutOfProcess))
Dim diagnosticAndFixes = Await GetDiagnosticAndFixesAsync(workspace, New TestParameters())
Dim span = diagnosticAndFixes.Item1.First().Location.SourceSpan
Assert.NotEqual(span.Start, 0)
Expand Down Expand Up @@ -1869,7 +1870,7 @@ End Namespace

Await TestInRegularAndScriptAsync(source.Value, expected.Value)

Using workspace = TestWorkspace.CreateVisualBasic(source.Value, composition:=GetComposition())
Using workspace = TestWorkspace.CreateVisualBasic(source.Value, composition:=GetComposition().WithTestHostParts(TestHost.OutOfProcess))
Dim diagnosticAndFixes = Await GetDiagnosticAndFixesAsync(workspace, New TestParameters())
Dim span = diagnosticAndFixes.Item1.First().Location.SourceSpan
Assert.Equal(span.Start, expected.Value.ReplaceLineEndings(vbLf).IndexOf("new C", StringComparison.Ordinal) + 4)
Expand Down Expand Up @@ -1903,7 +1904,7 @@ End Module

Await TestInRegularAndScriptAsync(source.Value, expected.Value)

Using workspace = TestWorkspace.CreateVisualBasic(source.Value, composition:=GetComposition())
Using workspace = TestWorkspace.CreateVisualBasic(source.Value, composition:=GetComposition().WithTestHostParts(TestHost.OutOfProcess))
Dim diagnosticAndFixes = Await GetDiagnosticAndFixesAsync(workspace, New TestParameters())
Dim span = diagnosticAndFixes.Item1.First().Location.SourceSpan
Assert.Equal(span.Start, expected.Value.ReplaceLineEndings(vbLf).IndexOf("Console.WriteLine(""goo"")", StringComparison.Ordinal))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.Host;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Serialization;

using static TemporaryStorageService;

internal partial class SerializerService
{
[DebuggerDisplay("{" + nameof(Display) + ",nq}")]
private sealed class SerializedMetadataReference : PortableExecutableReference, ISupportTemporaryStorage
{
private readonly Metadata _metadata;
private readonly ImmutableArray<TemporaryStorageStreamHandle> _storageHandles;
private readonly DocumentationProvider _provider;

public IReadOnlyList<ITemporaryStorageStreamHandle> StorageHandles => _storageHandles;

public SerializedMetadataReference(
MetadataReferenceProperties properties,
string? fullPath,
Metadata metadata,
ImmutableArray<TemporaryStorageStreamHandle> storageHandles,
DocumentationProvider initialDocumentation)
: base(properties, fullPath, initialDocumentation)
{
Contract.ThrowIfTrue(storageHandles.IsDefault);
_metadata = metadata;
_storageHandles = storageHandles;

_provider = initialDocumentation;
}

protected override DocumentationProvider CreateDocumentationProvider()
{
// this uses documentation provider given at the constructor
throw ExceptionUtilities.Unreachable();
}

protected override Metadata GetMetadataImpl()
=> _metadata;

protected override PortableExecutableReference WithPropertiesImpl(MetadataReferenceProperties properties)
=> new SerializedMetadataReference(properties, FilePath, _metadata, _storageHandles, _provider);

public override string ToString()
{
var metadata = TryGetMetadata(this);
var modules = GetModules(metadata);

return $"""
{nameof(SerializedMetadataReference)}
FilePath={this.FilePath}
Kind={this.Properties.Kind}
Aliases={this.Properties.Aliases.Join(",")}
EmbedInteropTypes={this.Properties.EmbedInteropTypes}
MetadataKind={metadata switch { null => "null", AssemblyMetadata => "assembly", ModuleMetadata => "module", _ => metadata.GetType().Name }}
Guids={modules.Select(m => GetMetadataGuid(m).ToString()).Join(",")}
""";

static ImmutableArray<ModuleMetadata> GetModules(Metadata? metadata)
{
if (metadata is AssemblyMetadata assemblyMetadata)
{
if (TryGetModules(assemblyMetadata, out var modules))
return modules;
}
else if (metadata is ModuleMetadata moduleMetadata)
{
return [moduleMetadata];
}

return [];
}
}
}
}
Loading

0 comments on commit 2882436

Please sign in to comment.