Repository navigation
Add Microsoft.Testing.Platform server-mode test support #1552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shayanhabibi
wants to merge
40
commits into
ionide:main
Choose a base branch
from
shayanhabibi:feature/mtp-server-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
5d0affe
Build the test tree in FSAC instead of in each client
shayanhabibi d0650c8
Keep parameterised test cases distinct in the server's test names
shayanhabibi 1b2fa1a
Classify the test platform a project's tests run on
shayanhabibi d84e1fb
Gate Microsoft.Testing.Platform behind a feature flag
shayanhabibi 12c8736
Map Microsoft.Testing.Platform nodes onto the server's test tree
shayanhabibi bd0a583
Discover tests through the Microsoft.Testing.Platform server protocol
shayanhabibi 614301a
Discover Microsoft.Testing.Platform projects alongside VSTest projects
shayanhabibi 261a11c
Map a Microsoft.Testing.Platform result onto a test outcome
shayanhabibi 9881629
Run tests through the Microsoft.Testing.Platform server protocol
shayanhabibi 4ef7fee
Answer the platform's request to attach a debugger
shayanhabibi 73d1572
Run Microsoft.Testing.Platform tests alongside VSTest tests
shayanhabibi 80233d8
Cover discovery over Microsoft.Testing.Platform at the LSP level
shayanhabibi c4993dd
Don't invoke VSTest when no VSTest projects are selected
shayanhabibi 7affbc9
Keep the testing platform client inside FSAC
shayanhabibi 06fef43
Keep MTP test selections explicit
shayanhabibi 78abeb6
Attach to MTP test host before debug runs
shayanhabibi ac598ea
Skip VSTest discovery when only MTP projects are present
shayanhabibi 73cdafa
Fix FSAC format and analyzer checks
shayanhabibi ce3620a
Improve caseFragment logic in TestServer.fs
shayanhabibi 28c38ff
Defer VSTest lookup for MTP-only workspaces
shayanhabibi 2008f41
Isolate LSP MTP fixture from Paket test dependencies
shayanhabibi d6c04cc
Propagate cancellation through MTP test requests
shayanhabibi 5092dc6
Ensure MTP discovery and execution always shut down clients
shayanhabibi 09ec63b
Own launched MTP clients until shutdown, and cover cleanup and mixed …
shayanhabibi 9741d5d
Add parameterised VSTest fixtures for per-case test ids
shayanhabibi f90c351
Add a versioned, escaped TestId that routes a test to its platform
shayanhabibi 83f6c1d
Issue TestIds for discovered tests and their results
shayanhabibi 5d2ba22
Run a chosen list of discovered VSTest cases
shayanhabibi 4d976ac
Accept TestIds on test run requests
shayanhabibi 415a6bd
Run tests by the ids discovery issued them
shayanhabibi 5ecb40e
Remove PlatformUid from test items
shayanhabibi 9b519b4
Link a testing platform node to its parent by the parent's own id
shayanhabibi c1dc4f8
Cover debugging and the remaining rejections of id runs
shayanhabibi f39d038
Carry a testing platform test's console output to its result
shayanhabibi fdb1954
Keep one testing platform application from failing the others
shayanhabibi 377a368
Qualify a testing platform test's full name by its type
shayanhabibi 7f79e4a
Fix code analysis-based test updates for xunit.v3
farlee2121 0a54fd5
fix: code analysis-based test updates for xunit.v3
shayanhabibi 2daac4a
Group a testing platform test by the type the platform names
shayanhabibi 47fa28e
Merge remote-tracking branch 'upstream/main' into feature/mtp-server-…
shayanhabibi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,294 @@ | ||
| namespace FsAutoComplete.TestServer | ||
|
|
||
| open System | ||
| open System.Collections.Generic | ||
| open System.Runtime.ExceptionServices | ||
| open System.Threading | ||
| open System.Threading.Tasks | ||
| open FsAutoComplete.TestingPlatform.Client | ||
|
|
||
| /// Drives Microsoft.Testing.Platform applications over the server-mode protocol. Each application | ||
| /// is its own test runner, so there is no separate runner to locate. | ||
| module MtpWrapper = | ||
| /// Path to a dll or executable that speaks the server-mode protocol. | ||
| type TestApplication = string | ||
|
|
||
| /// A node of a test tree, paired with the application that reported it. | ||
| type DiscoveredNode = TestApplication * TestNodeUpdate | ||
|
|
||
| type TestDiscoveryUpdate = | ||
| | Progress of DiscoveredNode list | ||
| | LogMessage of ClientLogLevel * string | ||
|
|
||
| let private clientOptions = | ||
| { MtpClientOptions.Default with | ||
| ClientName = "FsAutoComplete" | ||
| } | ||
|
|
||
| /// Starts a task under the async's cancellation token and reports its outcome only once it has | ||
| /// finished: its result, its exception unwrapped, or request cancellation as async cancellation. | ||
| /// Async.AwaitTask checks for cancellation after the task it is given is already running, so a | ||
| /// racing cancellation is reported while that task carries on, and anything it produces is lost. | ||
| /// Cancellation is therefore reported late on purpose: only once the task has noticed it and | ||
| /// released everything it holds, which for a test host includes tearing the host down. | ||
| /// Internal so the tests can reach it. | ||
| let internal startTaskAsync (start: CancellationToken -> Task<'T>) : Async<'T> = | ||
| async { | ||
| let! cancellationToken = Async.CancellationToken | ||
|
|
||
| return! | ||
| Async.FromContinuations(fun (ok, error, cancelled) -> | ||
| (start cancellationToken) | ||
| .ContinueWith( | ||
| (fun (completed: Task<'T>) -> | ||
| if completed.IsCompletedSuccessfully then | ||
| ok completed.Result | ||
| else | ||
| try | ||
| completed.GetAwaiter().GetResult() |> ignore | ||
| with | ||
| | :? OperationCanceledException as failure when cancellationToken.IsCancellationRequested -> | ||
| cancelled failure | ||
| | failure -> error failure), | ||
| TaskScheduler.Default | ||
| ) | ||
| |> ignore) | ||
| } | ||
|
|
||
| let private rethrow (error: exn) : 'a = | ||
| ExceptionDispatchInfo.Capture(error).Throw() | ||
| Unchecked.defaultof<'a> | ||
|
|
||
| /// Acquires a resource and owns it from acquisition to disposal, all within one task, so no | ||
| /// cancellation can separate the two. The resource is released, then disposed, before the | ||
| /// outcome of <c>work</c> is reported. Release is best-effort, so that outcome stays the one | ||
| /// observed; it runs first so that disposal finds nothing left to wait for. | ||
| /// Internal so the tests can reach it. | ||
| let internal ownedAsync<'R, 'T when 'R :> IDisposable> | ||
| (acquire: CancellationToken -> Task<'R>) | ||
| (release: 'R -> Task) | ||
| (work: CancellationToken -> 'R -> Task<'T>) | ||
| : Async<'T> = | ||
| startTaskAsync (fun cancellationToken -> | ||
| task { | ||
| use! resource = acquire cancellationToken | ||
|
|
||
| let! outcome = | ||
| task { | ||
| try | ||
| let! result = work cancellationToken resource | ||
| return Choice1Of2 result | ||
| with error -> | ||
| return Choice2Of2 error | ||
| } | ||
|
|
||
| try | ||
| do! release resource | ||
| with _ -> | ||
| () | ||
|
|
||
| match outcome with | ||
| | Choice1Of2 result -> return result | ||
| | Choice2Of2 error -> return rethrow error | ||
| }) | ||
|
|
||
| /// Launches <c>application</c> and owns its client until it is shut down. Shutdown goes through | ||
| /// <c>ShutdownAsync</c>, which does not block a thread while the host is torn down, as a | ||
| /// synchronous <c>Dispose</c> would. | ||
| let private withClientAsync (application: TestApplication) (work: CancellationToken -> MtpClient -> Task<'T>) = | ||
| ownedAsync | ||
| (fun cancellationToken -> MtpClient.LaunchAsync(application, clientOptions, cancellationToken)) | ||
| (fun client -> client.ShutdownAsync()) | ||
| work | ||
|
|
||
| /// Runs <c>work</c> for one application of several, so that one which is not built, cannot be | ||
| /// launched or fails partway is reported at error level instead of failing the others. Its | ||
| /// outcome is <c>Error</c> with the failure; cancellation is not contained. | ||
| let private isolatedAsync | ||
| (logError: string -> unit) | ||
| (application: TestApplication) | ||
| (work: Async<'T list>) | ||
| : Async<Result<'T list, exn>> = | ||
| async { | ||
| if not (IO.File.Exists application) then | ||
| let message = | ||
| $"Skipped test application '{application}': it does not exist. Build its project first." | ||
|
|
||
| logError message | ||
| return Result.Error(IO.FileNotFoundException(message, application) :> exn) | ||
| else | ||
| match! Async.Catch work with | ||
| | Choice1Of2 nodes -> return Ok nodes | ||
| | Choice2Of2 error -> | ||
| logError $"Test application '{application}' failed: {error.Message}" | ||
| return Result.Error error | ||
| } | ||
|
|
||
| /// Collects every node an application reports, notifying as the batches arrive. | ||
| let private discoverFromAsync (notify: TestDiscoveryUpdate -> unit) (application: TestApplication) = | ||
| withClientAsync application (fun cancellationToken client -> | ||
| task { | ||
| let discovered = ResizeArray<DiscoveredNode>() | ||
|
|
||
| use _ = | ||
| client.TestNodesUpdated.Subscribe(fun batch -> | ||
| let nodes = batch.Updates |> List.map (fun node -> application, node) | ||
| discovered.AddRange nodes | ||
| notify (Progress nodes)) | ||
|
|
||
| use _ = | ||
| client.LogReceived.Subscribe(fun log -> notify (LogMessage(log.Level, log.Message))) | ||
|
|
||
| let! _capabilities = client.InitializeAsync(cancellationToken) | ||
| do! client.DiscoverTestsAsync(cancellationToken) | ||
| do! client.ExitAsync(cancellationToken) | ||
|
|
||
| return List.ofSeq discovered | ||
| }) | ||
|
|
||
| /// A node reported while running, paired with the application that reported it. A test is | ||
| /// reported more than once: once as it starts, and again with its outcome. | ||
| type RunNode = TestApplication * TestNodeUpdate | ||
|
|
||
| type TestRunUpdate = | ||
| | Progress of RunNode list | ||
| | LogMessage of ClientLogLevel * string | ||
|
|
||
| [<RequireQualifiedAccess>] | ||
| type TestSelection = | ||
| | All | ||
| | Uids of string list | ||
|
|
||
| /// Each application runs either all tests or an explicit set of discovered test uids. | ||
| type RunRequest = TestApplication * TestSelection | ||
|
|
||
| type ProcessId = int | ||
| type DidDebuggerAttach = bool | ||
|
|
||
| /// Answers the server's request to have a debugger attached. Unlike VSTest, where the client | ||
| /// launches the test host itself to get ahead of it, the platform runs its own host and asks the | ||
| /// client to attach to it; the run continues once this answers. | ||
| let attachDebuggerHandler (onAttachDebugger: ProcessId -> DidDebuggerAttach) : ServerRequestHandler = | ||
| fun name parameters _cancellationToken -> | ||
| if name <> "client/attachDebugger" then | ||
| Task.FromResult None | ||
| else | ||
| let processId = | ||
| parameters | ||
| |> Option.bind (fun p -> | ||
| match p.TryGetValue "processId" with | ||
| | true, processId -> Some(Convert.ToInt32 processId) | ||
| | _ -> None) | ||
|
|
||
| let attached = processId |> Option.map onAttachDebugger |> Option.defaultValue false | ||
|
|
||
| Dictionary<string, obj>(dict [ "success", box attached ]) :> IReadOnlyDictionary<string, obj> | ||
| |> Some | ||
| |> Task.FromResult | ||
|
|
||
| /// Runs the requested tests of one application, notifying as the outcomes arrive. | ||
| let private runOnAsync | ||
| (notify: TestRunUpdate -> unit) | ||
| (onAttachDebugger: (ProcessId -> DidDebuggerAttach) option) | ||
| ((application, selection): RunRequest) | ||
| = | ||
| withClientAsync application (fun cancellationToken client -> | ||
| task { | ||
| let reported = ResizeArray<RunNode>() | ||
|
|
||
| use _ = | ||
| client.TestNodesUpdated.Subscribe(fun batch -> | ||
| let nodes = batch.Updates |> List.map (fun node -> application, node) | ||
| reported.AddRange nodes | ||
| notify (Progress nodes)) | ||
|
|
||
| use _ = | ||
| client.LogReceived.Subscribe(fun log -> notify (LogMessage(log.Level, log.Message))) | ||
|
|
||
| let attachOnce = | ||
| onAttachDebugger | ||
| |> Option.map (fun attach -> | ||
| let mutable attachedProcess = None | ||
|
|
||
| fun processId -> | ||
| if attachedProcess = Some processId then | ||
| true | ||
| else | ||
| let didAttach = attach processId | ||
|
|
||
| if didAttach then | ||
| attachedProcess <- Some processId | ||
|
|
||
| didAttach) | ||
|
|
||
| attachOnce | ||
| |> Option.iter (fun attach -> client.ServerRequestHandler <- Some(attachDebuggerHandler attach)) | ||
|
|
||
| let! _capabilities = client.InitializeAsync(cancellationToken) | ||
|
|
||
| // Protocol 1.0 reserves attachDebugger but its server never sends that request. | ||
| // The launched application is the test host, so attach to its pid before execution. | ||
| attachOnce |> Option.iter (fun attach -> attach client.ProcessId |> ignore) | ||
|
|
||
| let! _result = | ||
| match selection with | ||
| | TestSelection.All -> client.RunTestsAsync(cancellationToken) | ||
| | TestSelection.Uids uids -> client.RunTestsAsync(uids, cancellationToken) | ||
|
|
||
| do! client.ExitAsync(cancellationToken) | ||
|
|
||
| return List.ofSeq reported | ||
| }) | ||
|
|
||
| /// Runs the requested tests of every given application. A debugger is attached only where the | ||
| /// application asks for one, which it does when the run was started under a debugger. An | ||
| /// application that cannot run is reported and the others still run; only a run in which no | ||
| /// application could run fails, with the first application's failure. | ||
| let runTestsWithDebuggerAsync | ||
| (notify: TestRunUpdate -> unit) | ||
| (onAttachDebugger: (ProcessId -> DidDebuggerAttach) option) | ||
| (requests: RunRequest list) | ||
| : Async<RunNode list> = | ||
| async { | ||
| let logError message = notify (TestRunUpdate.LogMessage(ClientLogLevel.Error, message)) | ||
|
|
||
| // The platform treats an empty uid collection as "run all". An explicit empty | ||
| // selection must therefore never be sent to an application. | ||
| let! perApplication = | ||
| requests | ||
| |> List.filter (fun (_, selection) -> selection <> TestSelection.Uids []) | ||
| |> List.map (fun request -> isolatedAsync logError (fst request) (runOnAsync notify onAttachDebugger request)) | ||
| |> Async.Sequential | ||
|
|
||
| let failures = | ||
| perApplication | ||
| |> Array.choose (function | ||
| | Result.Error error -> Some error | ||
| | Ok _ -> None) | ||
|
|
||
| if failures.Length > 0 && failures.Length = perApplication.Length then | ||
| return rethrow failures[0] | ||
| else | ||
| return perApplication |> Seq.collect (Result.defaultValue []) |> List.ofSeq | ||
| } | ||
|
|
||
| /// Runs the requested tests of every given application, undebugged. | ||
| let runTestsAsync (notify: TestRunUpdate -> unit) (requests: RunRequest list) : Async<RunNode list> = | ||
| runTestsWithDebuggerAsync notify None requests | ||
|
|
||
| /// Discovers the tests of every given application. An application that cannot be discovered is | ||
| /// reported and contributes no tests. | ||
| let discoverTestsAsync | ||
| (notify: TestDiscoveryUpdate -> unit) | ||
| (applications: TestApplication list) | ||
| : Async<DiscoveredNode list> = | ||
| async { | ||
| let logError message = notify (TestDiscoveryUpdate.LogMessage(ClientLogLevel.Error, message)) | ||
|
|
||
| let! perApplication = | ||
| applications | ||
| |> List.map (fun application -> isolatedAsync logError application (discoverFromAsync notify application)) | ||
| |> Async.Sequential | ||
|
|
||
| return perApplication |> Seq.collect (Result.defaultValue []) |> List.ofSeq | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.