-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add external access to stacktrace APIs for Unit Testing (#58082)
- Loading branch information
Showing
5 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...res/Core/Portable/ExternalAccess/UnitTesting/API/IUnitTestingStackTraceServiceAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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.Immutable; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Host; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.UnitTesting.Api | ||
{ | ||
internal interface IUnitTestingStackTraceServiceAccessor : IWorkspaceService | ||
{ | ||
Task<ImmutableArray<UnitTestingParsedFrameWrapper>> TryParseAsync(string input, Workspace workspace, CancellationToken cancellationToken); | ||
Task<UnitTestingDefinitionItemWrapper?> TryFindMethodDefinitionAsync(Workspace workspace, UnitTestingParsedFrameWrapper parsedFrame, CancellationToken cancellationToken); | ||
(Document? document, int lineNumber) GetDocumentAndLine(Workspace workspace, UnitTestingParsedFrameWrapper parsedFrame); | ||
Task<bool> TryNavigateToAsync(Workspace workspace, UnitTestingDefinitionItemWrapper definitionItem, bool showInPreviewTab, bool activateTab, CancellationToken cancellationToken); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...Features/Core/Portable/ExternalAccess/UnitTesting/API/UnitTestingDefinitionItemWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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. | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.UnitTesting.Api | ||
{ | ||
internal readonly struct UnitTestingDefinitionItemWrapper | ||
{ | ||
internal FindUsages.DefinitionItem UnderlyingObject { get; } | ||
|
||
public UnitTestingDefinitionItemWrapper(FindUsages.DefinitionItem definition) | ||
{ | ||
UnderlyingObject = definition; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Features/Core/Portable/ExternalAccess/UnitTesting/API/UnitTestingParsedFrameWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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 Microsoft.CodeAnalysis.StackTraceExplorer; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.UnitTesting.Api | ||
{ | ||
internal readonly struct UnitTestingParsedFrameWrapper | ||
{ | ||
internal ParsedFrame UnderlyingObject { get; } | ||
|
||
public UnitTestingParsedFrameWrapper(ParsedFrame parsedFrame) | ||
{ | ||
UnderlyingObject = parsedFrame; | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...Features/Core/Portable/ExternalAccess/UnitTesting/UnitTestingStackTraceServiceAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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; | ||
using System.Collections.Immutable; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.ExternalAccess.UnitTesting.Api; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.StackTraceExplorer; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.UnitTesting | ||
{ | ||
internal class UnitTestingStackTraceServiceAccessor : IUnitTestingStackTraceServiceAccessor | ||
{ | ||
private readonly IStackTraceExplorerService _stackTraceExplorerService; | ||
|
||
[Obsolete(MefConstruction.FactoryMethodMessage, error: true)] | ||
public UnitTestingStackTraceServiceAccessor( | ||
IStackTraceExplorerService stackTraceExplorerService) | ||
{ | ||
_stackTraceExplorerService = stackTraceExplorerService; | ||
} | ||
|
||
public (Document? document, int lineNumber) GetDocumentAndLine(Workspace workspace, UnitTestingParsedFrameWrapper parsedFrame) | ||
=> _stackTraceExplorerService.GetDocumentAndLine(workspace.CurrentSolution, parsedFrame.UnderlyingObject); | ||
|
||
public async Task<UnitTestingDefinitionItemWrapper?> TryFindMethodDefinitionAsync(Workspace workspace, UnitTestingParsedFrameWrapper parsedFrame, CancellationToken cancellationToken) | ||
{ | ||
var definition = await _stackTraceExplorerService.TryFindDefinitionAsync(workspace.CurrentSolution, parsedFrame.UnderlyingObject, StackFrameSymbolPart.Method, cancellationToken).ConfigureAwait(false); | ||
return definition is null | ||
? null | ||
: new UnitTestingDefinitionItemWrapper(definition); | ||
} | ||
|
||
public async Task<ImmutableArray<UnitTestingParsedFrameWrapper>> TryParseAsync(string input, Workspace workspace, CancellationToken cancellationToken) | ||
{ | ||
var result = await StackTraceAnalyzer.AnalyzeAsync(input, cancellationToken).ConfigureAwait(false); | ||
return result.ParsedFrames.SelectAsArray(p => new UnitTestingParsedFrameWrapper(p)); | ||
} | ||
|
||
public Task<bool> TryNavigateToAsync(Workspace workspace, UnitTestingDefinitionItemWrapper definitionItem, bool showInPreviewTab, bool activateTab, CancellationToken cancellationToken) | ||
=> definitionItem.UnderlyingObject.TryNavigateToAsync(workspace, showInPreviewTab, activateTab, cancellationToken); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...s/Core/Portable/ExternalAccess/UnitTesting/UnitTestingStackTraceServiceAccessorFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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; | ||
using System.Composition; | ||
using Microsoft.CodeAnalysis.ExternalAccess.UnitTesting.Api; | ||
using Microsoft.CodeAnalysis.Host; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.StackTraceExplorer; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.UnitTesting | ||
{ | ||
[ExportWorkspaceServiceFactory(typeof(IUnitTestingStackTraceServiceAccessor))] | ||
[Shared] | ||
|
||
internal class UnitTestingStackTraceServiceAccessorFactory : IWorkspaceServiceFactory | ||
{ | ||
[ImportingConstructor] | ||
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
public UnitTestingStackTraceServiceAccessorFactory() | ||
{ | ||
} | ||
|
||
[Obsolete(MefConstruction.FactoryMethodMessage, error: true)] | ||
public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices) | ||
{ | ||
var stackTraceExplorerService = workspaceServices.GetRequiredService<IStackTraceExplorerService>(); | ||
return new UnitTestingStackTraceServiceAccessor(stackTraceExplorerService); | ||
} | ||
} | ||
} |