Skip to content

Commit

Permalink
Support on-the-fly configuration for R test explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Nov 11, 2023
1 parent ed73ca0 commit 340b855
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
10 changes: 8 additions & 2 deletions extensions/positron-r/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion extensions/positron-r/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
12 changes: 8 additions & 4 deletions extensions/positron-r/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
}
});
}
44 changes: 37 additions & 7 deletions extensions/positron-r/src/testing/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('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<boolean>('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);

Expand Down Expand Up @@ -63,3 +91,5 @@ export function discoverTests(context: vscode.ExtensionContext) {
true
);
}


0 comments on commit 340b855

Please sign in to comment.