diff --git a/extensions/positron-r/package.json b/extensions/positron-r/package.json index 6c3bb994a4dc..4d590ce14307 100644 --- a/extensions/positron-r/package.json +++ b/extensions/positron-r/package.json @@ -145,10 +145,10 @@ "default": "off", "description": "%r.configuration.tracing.description%" }, - "positron.r.testing.experimental": { + "positron.r.testing": { "type": "boolean", "default": true, - "description": "%r.configuration.experimentalPackageTesting.description%" + "description": "%r.configuration.packageTesting.description%" }, "positron.r.restoreWorkspace": { "scope": "window", @@ -403,6 +403,12 @@ "path": "./snippets/r.code-snippets" } ], + "viewsWelcome": [ + { + "view": "testing", + "contents": "R tests appear here when (1) current workspace includes an R package that uses testthat and (2) the setting Positron > R: Testing is set to 'true'.\n[Adjust Settings](command:workbench.action.openSettings?\"positron.r.testing\")" + } + ], "taskDefinitions": [ { "type": "rPackageTask", diff --git a/extensions/positron-r/package.nls.json b/extensions/positron-r/package.nls.json index 054ff7e7d04e..cc04492d1e5d 100644 --- a/extensions/positron-r/package.nls.json +++ b/extensions/positron-r/package.nls.json @@ -33,7 +33,7 @@ "r.configuration.logLevel.debug.description": "Debug messages", "r.configuration.logLevel.trace.description": "Verbose tracing messages", "r.configuration.logLevel.description": "Log level for the R Kernel (requires restart to take effect)", - "r.configuration.experimentalPackageTesting.description": "Experimental UI support for testing R packages", + "r.configuration.packageTesting.description": "Explore and run testthat tests", "r.configuration.restoreWorkspace.description": "Restore the workspace from .RData on startup (requires restart to take effect)", "r.configuration.extraArguments.description": "Extra command-line arguments for R intialization", "r.configuration.quietMode.description": "Run R in quiet mode, to suppress initial copyright and welcome messages (requires restart to take effect)", diff --git a/extensions/positron-r/src/extension.ts b/extensions/positron-r/src/extension.ts index c8843dc23a10..fad221273484 100644 --- a/extensions/positron-r/src/extension.ts +++ b/extensions/positron-r/src/extension.ts @@ -9,7 +9,7 @@ import { registerCommands } from './commands'; import { registerFormatter } from './formatting'; import { providePackageTasks } from './tasks'; import { setContexts } from './contexts'; -import { discoverTests } from './testing/testing'; +import { setupTestExplorer, refreshTestExplorer } from './testing/testing'; import { rRuntimeProvider } from './provider'; import { RRuntime } from './runtime'; @@ -38,7 +38,11 @@ export function activate(context: vscode.ExtensionContext) { // Provide tasks. providePackageTasks(context); - // Discover R package tests. - discoverTests(context); - + // Setup testthat test explorer. + setupTestExplorer(context); + vscode.workspace.onDidChangeConfiguration(async event => { + if (event.affectsConfiguration('positron.r.testing')) { + refreshTestExplorer(context); + } + }); } diff --git a/extensions/positron-r/src/testing/testing.ts b/extensions/positron-r/src/testing/testing.ts index 347f77a2504d..427909290402 100644 --- a/extensions/positron-r/src/testing/testing.ts +++ b/extensions/positron-r/src/testing/testing.ts @@ -9,23 +9,51 @@ import { createTestthatWatchers } from './watcher'; import { runHandler } from './runner'; import { Logger } from '../extension'; -export function discoverTests(context: vscode.ExtensionContext) { - const extConfig = vscode.workspace.getConfiguration('positron.r'); - const testingFeatureFlag = extConfig.get('testing.experimental'); +let controller: vscode.TestController | undefined; + +export async function setupTestExplorer(context: vscode.ExtensionContext) { + if (testExplorerEnabled()) { + return discoverTests(context); + } +} - if (!testingFeatureFlag) { - return []; +export function refreshTestExplorer(context: vscode.ExtensionContext) { + const enabled = testExplorerEnabled(); + const inPlace = hasTestingController(); + + if ((enabled && inPlace) || (!enabled && !inPlace)) { + return; + } + + if (enabled) { + return discoverTests(context); } + controller?.dispose(); + controller = undefined; +} + +function testExplorerEnabled(): boolean { + const extConfig = vscode.workspace.getConfiguration('positron.r'); + const testingEnabled = extConfig.get('testing'); + + return testingEnabled === true; +} + +function hasTestingController(): boolean { + return controller !== undefined; +} + +export function discoverTests(context: vscode.ExtensionContext) { // TODO: check workspace folder(s) for package-hood // if no package, return // if exactly one package among the workspace folders, proceed // consider adding package metadata to testingTools (eg name and filepath) // if >1 package, consult our non-existent policy re: multi-root, multi-package workspace - const controller = vscode.tests.createTestController( + controller = vscode.tests.createTestController( 'rPackageTests', - 'R Package Tests' + 'R Package Test Explorer' ); context.subscriptions.push(controller); @@ -63,3 +91,5 @@ export function discoverTests(context: vscode.ExtensionContext) { true ); } + +