Skip to content

Visual Studio Code, rebuilt in Rust

Rust
9.2 KB
Raw Blame

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). apply takes a TestsDiffOp list.
  • app/src/testing.rs::Profile: a run profile, with default_profile as testProfileService.ts picks one for a test.
  • app/src/testing.rs::TestResult: one run, live or complete (testResult.ts LiveTestResult): its tasks, the tests in it with a state per task, messages and output. computed_state is 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 field testing of app/src/workbench.rs::Workbench. The file's impl Workbench block is MainThreadTesting and 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.rs and testing_gutter.rs hold 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, with testing_peek.rs (the peek at a failed test) and testing_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.rs draws 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 $expandTest for 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_tests groups the tests by the profile that runs each, run_resolved_tests creates the live result and asks for workspace trust, and run_trusted_tests sends one $runControllerTests per controller. The result completes when every controller answered, or at once when trust is refused.
  • A run an extension starts (createTestRun without a request from the app): the host sends $startedExtensionTestRun with its own run id, and the app creates the result under that id.
  • Progress, for both kinds: $startedTestRunTask adds the task to the TestResult. $applyTestRunUpdates and its single-update siblings land in apply_test_run_update, which writes into the TestResult.
  • The end: finish_test_run marks the result complete and sends the whole serialized result back with $publishTestResults, as mainThreadTesting.ts does for vscode.tests.testResults.
  • Coverage: $appendCoverage carries per-file counts only. The ranges of one file are asked from the host on demand with $getCoverageDetails and the file's id, and cached in app/src/testing_coverage.rs::TestCoverage under (uri, test) with None while 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_id and root_id read it; the first segment is the controller id. Build no id by hand.
  • Only controller roots expand at publish. TestCollection::apply returns the roots and nothing deeper, as treeProjection.ts storeItem does. Expanding file items too makes every resolveHandler run 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_state keeps the higher terminal state, as testResult.ts does; a late "running" after "failed" is dropped.
  • results is newest first and holds at most 128. state_of and is_retired read 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 leaves results; hold no reference to a TestCoverage across frames.
  • Positions and paths. Coverage positions are 1-based on the wire; app/src/testing_coverage.rs::model_position clamps one to the model as a 0-based line and column. Coverage tree keys are lower-cased for file paths on macOS (tree_path), so look a file up through TestCoverage::file, not by string compare.
  • Cancellation tokens. $runControllerTests, $startContinuousRun, $getCoverageDetails and the other token-taking methods of ExtHostTesting are listed in app/src/main_thread.rs::TOKEN_METHODS. A new one needs its entry or a send through app/src/host_sync.rs::call_cancellable, or the extension gets undefined as its token.

Traps

  • sync_inline_coverage is 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 the None placeholder 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_update handles both; a new handler that reads output must too.
  • app/src/workbench/testing_view.rs::test_output_changed is an empty function that apply_test_run_update calls 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_host does not clear controllers, collection or profiles, and no other code in native/crates clears them on a restart. Their entries are added and removed only in handle_testing_request, on the host's own calls ($unregisterTestController, $publishDiff, $removeTestProfile).
  • Profile defaults and hidden tests are in workspace storage (testingPreferredProfiles2, excludedTestItems). handle_testing_request and finish_test_run write 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_coverage runs 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_results or testing_profiles runs 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 extension port/fixtures/extensions/tests-provider, which ARGS loads in both apps.