Visual Studio Code, rebuilt in Rust
Test explorer
The Testing view, test runs and test coverage: the tests extensions publish, the runs of them, and how the results and coverage reach the views and the editor. This file goes one level under ARCHITECTURE.md, section "Extension host bridge", and under extension-host.md. Upstream is src/vs/workbench/contrib/testing and src/vs/workbench/api/browser/mainThreadTesting.ts. The port's own tests are a different subject: testing.md. The decisions per feature are in port/notes.md, "Milestone 16: debug and testing".
What is where
Data without a window:
app/src/testing.rs::TestCollection: every test item the controllers published, by id, with each item's children (mainThreadTestCollection.ts).applytakes aTestsDiffOplist.app/src/testing.rs::Profile: a run profile, withdefault_profileas testProfileService.ts picks one for a test.app/src/testing.rs::TestResult: one run, live or complete (testResult.tsLiveTestResult): its tasks, the tests in it with a state per task, messages and output.computed_stateis getComputedState.ts.app/src/testing_coverage.rs::TestCoverage: one task's coverage as a prefix tree by URI path, whose folder nodes sum their files (testCoverage.ts). The same file has the details model of codeCoverageDecorations.ts (detail_ranges,inline_coverage,describe) and the display rules of codeCoverageDisplayUtils.ts.
The service and the views, in app/src/workbench:
app/src/workbench/testing_service.rs::TestingService: the state of testServiceImpl.ts, testProfileService.ts and testResultService.ts in one struct. It is the fieldtestingofapp/src/workbench.rs::Workbench. The file'simpl Workbenchblock isMainThreadTestingand the run logic.app/src/workbench/testing_view.rs: the Testing view, its commands, the run glyphs of the editor gutter.testing_filter.rs,testing_profiles.rs,testing_continuous.rs,testing_item_menu.rsandtesting_gutter.rshold the filter box, the profile dropdowns, continuous run, the row menu and the gutter menu.app/src/workbench/testing_results.rs: the Test Results panel view, withtesting_peek.rs(the peek at a failed test) andtesting_message_colorizer.rs.app/src/workbench/testing_coverage_view.rs::CoverageService: which coverage is open, the filter to one test, and the Test Coverage view's tree state.app/src/workbench/coverage_decorations.rsdraws the inline coverage and the coverage toolbar in the editor.
Who owns the tests
The extension host owns the test controllers, the test items' code (resolveHandler, runHandler) and the coverage details. The app owns a copy of the item tree, the profiles, and every result. Nothing in the app can run a test: a run is a request to the host.
All of MainThreadTesting is forwarded to the main thread (app/src/main_thread_commands.rs::route, path 3 of extension-host.md) and served by app/src/workbench/testing_service.rs::handle_testing_request. The paths:
- Discovery: the host sends
$publishDiff. The handler applies it to the collection and answers$expandTestfor each expandable controller root in the diff, which makes the extension resolve that root's children. If an extension subscribed ($subscribeToDiffs), the same diff goes back as$acceptDiff. - A run the user starts:
run_testsgroups the tests by the profile that runs each,run_resolved_testscreates the live result and asks for workspace trust, andrun_trusted_testssends one$runControllerTestsper controller. The result completes when every controller answered, or at once when trust is refused. - A run an extension starts (
createTestRunwithout a request from the app): the host sends$startedExtensionTestRunwith its own run id, and the app creates the result under that id. - Progress, for both kinds:
$startedTestRunTaskadds the task to theTestResult.$applyTestRunUpdatesand its single-update siblings land inapply_test_run_update, which writes into theTestResult. - The end:
finish_test_runmarks the result complete and sends the whole serialized result back with$publishTestResults, as mainThreadTesting.ts does forvscode.tests.testResults. - Coverage:
$appendCoveragecarries per-file counts only. The ranges of one file are asked from the host on demand with$getCoverageDetailsand the file's id, and cached inapp/src/testing_coverage.rs::TestCoverageunder(uri, test)withNonewhile the request is out.
How state reaches the views
There is no event emitter. handle_testing_request ends with cx.notify() ($runTests and $getCoverageDetails return before it), and the views are impl Workbench methods that read self.testing in render. A row's icon is app/src/workbench/testing_service.rs::state_of at draw time, and Sort by Status orders the rows by it in explorer_children on the same render, so a state update needs no separate tree update. The context keys of testingContextKeys.ts are computed from the same state in testing_service_context and testing_coverage_context.
Coverage in the editor goes the other way round: app/src/workbench/coverage_decorations.rs::sync_inline_coverage runs on every workbench render, computes each open editor's ranges from the cached details, and writes the count badges and missed-branch indicators into the editor as InjectionOwner::Coverage injected text only when they differ from what is there.
Invariants
- A test id is the path of ids from the controller down, joined by
\0(testId.ts).app/src/testing.rs::parent_idandroot_idread it; the first segment is the controller id. Build no id by hand. - Only controller roots expand at publish.
TestCollection::applyreturns the roots and nothing deeper, as treeProjection.tsstoreItemdoes. Expanding file items too makes everyresolveHandlerrun at startup and changes what Run All runs (port/notes.md, "Test resolution timing"). - A terminal state never goes back.
app/src/testing.rs::TestResult::update_statekeeps the higher terminal state, as testResult.ts does; a late "running" after "failed" is dropped. resultsis newest first and holds at most 128.state_ofandis_retiredread the newest result that has the test, so the order is the meaning.- The open coverage is a result id plus a task id (
CoverageService.selected), looked up on each use. It is gone when its result leavesresults; hold no reference to aTestCoverageacross frames. - Positions and paths. Coverage positions are 1-based on the wire;
app/src/testing_coverage.rs::model_positionclamps one to the model as a 0-based line and column. Coverage tree keys are lower-cased forfilepaths on macOS (tree_path), so look a file up throughTestCoverage::file, not by string compare. - Cancellation tokens.
$runControllerTests,$startContinuousRun,$getCoverageDetailsand the other token-taking methods ofExtHostTestingare listed inapp/src/main_thread.rs::TOKEN_METHODS. A new one needs its entry or a send throughapp/src/host_sync.rs::call_cancellable, or the extension getsundefinedas its token.
Traps
sync_inline_coverageis in the render path. Work added there runs every frame for every open editor: keep it to a lookup in the cached details, and start requests only through theNoneplaceholder so one file asks once.- Output of a run can arrive as a string or as a
VSBuffer, which the session turns into an array of bytes.apply_test_run_updatehandles both; a new handler that reads output must too. app/src/workbench/testing_view.rs::test_output_changedis an empty function thatapply_test_run_updatecalls on every output update. It is the place for work that must follow new output; today nothing does.- The Test Results view's editors live in
app/src/editor.rs::TEST_RESULTS_GROUP, a group outside the editor grid. Code that walks "all groups in the grid" does not see them. app/src/workbench/extension_host_restart.rs::stop_extension_hostdoes not clearcontrollers,collectionorprofiles, and no other code innative/cratesclears them on a restart. Their entries are added and removed only inhandle_testing_request, on the host's own calls ($unregisterTestController,$publishDiff,$removeTestProfile).- Profile defaults and hidden tests are in workspace storage (
testingPreferredProfiles2,excludedTestItems).handle_testing_requestandfinish_test_runwrite no result to storage.
Check a change
port/tools/cargo-test -p app testing::runs the collection, state and profile tests.port/tools/cargo-test -p app testing_coverageruns upstream's testCoverage.test.ts and codeCoverageDecorations.test.ts cases and the coverage view tests.port/tools/cargo-test -p app testing_service,testing_view,testing_resultsortesting_profilesruns one view's tests in a test window.port/tools/studio --lane 'FOLDER=port/fixtures/debug-node FILE=math.test.js ARGS=--extensionDevelopmentPath=port/fixtures/extensions/tests-provider port/tools/ui-pair /tmp/out port/fixtures/ui-pair/m16/inline-coverage.json'compares coverage in the editor against the reference app. The controller in these runs is the fixture extensionport/fixtures/extensions/tests-provider, whichARGSloads in both apps.