|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Composition; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using OmniSharp.DotNetTest.Models; |
| 10 | +using OmniSharp.Eventing; |
| 11 | +using OmniSharp.Mef; |
| 12 | +using OmniSharp.Services; |
| 13 | + |
| 14 | +namespace OmniSharp.DotNetTest.Services |
| 15 | +{ |
| 16 | + [Shared] |
| 17 | + [OmniSharpHandler(OmniSharpEndpoints.V2.DebugTestsInContextGetStartInfo, LanguageNames.CSharp)] |
| 18 | + internal class DebugTestsInContextService : BaseTestService, |
| 19 | + IRequestHandler<DebugTestsInContextGetStartInfoRequest, DebugTestGetStartInfoResponse> |
| 20 | + { |
| 21 | + private readonly DebugSessionManager _debugSessionManager; |
| 22 | + |
| 23 | + [ImportingConstructor] |
| 24 | + public DebugTestsInContextService(DebugSessionManager debugSessionManager, OmniSharpWorkspace workspace, IDotNetCliService dotNetCli, IEventEmitter eventEmitter, ILoggerFactory loggerFactory) |
| 25 | + : base(workspace, dotNetCli, eventEmitter, loggerFactory) |
| 26 | + { |
| 27 | + _debugSessionManager = debugSessionManager; |
| 28 | + } |
| 29 | + |
| 30 | + public async Task<DebugTestGetStartInfoResponse> Handle(DebugTestsInContextGetStartInfoRequest request) |
| 31 | + { |
| 32 | + var document = Workspace.GetDocument(request.FileName); |
| 33 | + if (document is null) |
| 34 | + { |
| 35 | + return new DebugTestGetStartInfoResponse |
| 36 | + { |
| 37 | + Succeeded = false, |
| 38 | + FailureReason = "File is not part of a C# project in the loaded solution.", |
| 39 | + ContextHadNoTests = true, |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + var testManager = TestManager.Create(document.Project, DotNetCli, EventEmitter, LoggerFactory); |
| 44 | + |
| 45 | + var (methodNames, testFramework) = await testManager.GetContextTestMethodNames(request.Line, request.Column, document, CancellationToken.None); |
| 46 | + |
| 47 | + if (methodNames is null) |
| 48 | + { |
| 49 | + return new DebugTestGetStartInfoResponse |
| 50 | + { |
| 51 | + Succeeded = false, |
| 52 | + FailureReason = "Could not find any tests to run", |
| 53 | + ContextHadNoTests = true, |
| 54 | + |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + testManager.Connect(); |
| 59 | + |
| 60 | + if (testManager.IsConnected) |
| 61 | + { |
| 62 | + _debugSessionManager.StartSession(testManager); |
| 63 | + return await _debugSessionManager.DebugGetStartInfoAsync(methodNames, request.RunSettings, testFramework, request.TargetFrameworkVersion, CancellationToken.None); |
| 64 | + } |
| 65 | + |
| 66 | + return new DebugTestGetStartInfoResponse |
| 67 | + { |
| 68 | + FailureReason = "Failed to connect to the 'dotnet test' process", |
| 69 | + Succeeded = false |
| 70 | + }; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments