From 570ce5ace1634890622e72c4027430d484d5c9a8 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 2 Aug 2023 14:40:32 -0700 Subject: [PATCH 1/3] update tests only on save with more files excluded --- .../testing/testController/controller.ts | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/client/testing/testController/controller.ts b/src/client/testing/testController/controller.ts index eff333a4cdd9..ac38faabe7ba 100644 --- a/src/client/testing/testController/controller.ts +++ b/src/client/testing/testController/controller.ts @@ -15,6 +15,7 @@ import { CancellationTokenSource, Uri, EventEmitter, + TextDocument, } from 'vscode'; import { IExtensionSingleActivationService } from '../../activation/types'; import { ICommandManager, IWorkspaceService } from '../../common/application/types'; @@ -48,6 +49,7 @@ import { WorkspaceTestAdapter } from './workspaceTestAdapter'; import { ITestDebugLauncher } from '../common/types'; import { IServiceContainer } from '../../ioc/types'; import { PythonResultResolver } from './common/resultResolver'; +import { onDidSaveTextDocument } from '../../common/vscodeApis/workspaceApis'; // Types gymnastics to make sure that sendTriggerTelemetry only accepts the correct types. type EventPropertyType = IEventNamePropertyMapping[EventName.UNITTEST_DISCOVERY_TRIGGER]; @@ -493,12 +495,22 @@ export class PythonTestController implements ITestController, IExtensionSingleAc this.disposables.push(watcher); this.disposables.push( - watcher.onDidChange((uri) => { - traceVerbose(`Testing: Trigger refresh after change in ${uri.fsPath}`); - this.sendTriggerTelemetry('watching'); - this.refreshData.trigger(uri, false); + onDidSaveTextDocument(async (doc: TextDocument) => { + const file = doc.fileName; + // refresh on any settings file save + if ( + file.includes('settings.json') || + file.includes('pytest.ini') || + file.includes('setup.cfg') || + file.includes('pyproject.toml') + ) { + traceVerbose(`Testing: Trigger refresh after saving ${doc.uri.fsPath}`); + this.sendTriggerTelemetry('watching'); + this.refreshData.trigger(doc.uri, false); + } }), ); + this.disposables.push( watcher.onDidCreate((uri) => { traceVerbose(`Testing: Trigger refresh after creating ${uri.fsPath}`); @@ -519,13 +531,20 @@ export class PythonTestController implements ITestController, IExtensionSingleAc const pattern = new RelativePattern(workspace, '**/*.py'); const watcher = this.workspaceService.createFileSystemWatcher(pattern); this.disposables.push(watcher); - this.disposables.push( - watcher.onDidChange((uri) => { - traceVerbose(`Testing: Trigger refresh after change in ${uri.fsPath}`); - this.sendTriggerTelemetry('watching'); - // We want to invalidate tests for code change - this.refreshData.trigger(uri, true); + onDidSaveTextDocument(async (doc: TextDocument) => { + const file = doc.fileName; + // exclude the following documents from calling a test refresh + const excludeBool = + file.endsWith('.git') || + file.endsWith('.pyc') || + file.includes('__pycache__') || + file.includes('.venv'); + if (doc.fileName.endsWith('.py') && !excludeBool) { + traceVerbose(`Testing: Trigger refresh after saving ${doc.uri.fsPath}`); + this.sendTriggerTelemetry('watching'); + this.refreshData.trigger(doc.uri, false); + } }), ); this.disposables.push( From a50279c2670ac8ab2ef5e935865cdb1b4db8e47e Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 2 Aug 2023 15:02:03 -0700 Subject: [PATCH 2/3] remove unneeded file excludes and watchers --- .../testing/testController/controller.ts | 30 ++----------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/src/client/testing/testController/controller.ts b/src/client/testing/testController/controller.ts index ac38faabe7ba..17bef3a5221b 100644 --- a/src/client/testing/testController/controller.ts +++ b/src/client/testing/testController/controller.ts @@ -211,7 +211,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc if (settings.testing.autoTestDiscoverOnSaveEnabled) { traceVerbose(`Testing: Setting up watcher for ${workspace.uri.fsPath}`); this.watchForSettingsChanges(workspace); - this.watchForTestContentChanges(workspace); + this.watchForTestContentChangeOnSave(); } }); } @@ -527,40 +527,16 @@ export class PythonTestController implements ITestController, IExtensionSingleAc ); } - private watchForTestContentChanges(workspace: WorkspaceFolder): void { - const pattern = new RelativePattern(workspace, '**/*.py'); - const watcher = this.workspaceService.createFileSystemWatcher(pattern); - this.disposables.push(watcher); + private watchForTestContentChangeOnSave(): void { this.disposables.push( onDidSaveTextDocument(async (doc: TextDocument) => { - const file = doc.fileName; - // exclude the following documents from calling a test refresh - const excludeBool = - file.endsWith('.git') || - file.endsWith('.pyc') || - file.includes('__pycache__') || - file.includes('.venv'); - if (doc.fileName.endsWith('.py') && !excludeBool) { + if (doc.fileName.endsWith('.py')) { traceVerbose(`Testing: Trigger refresh after saving ${doc.uri.fsPath}`); this.sendTriggerTelemetry('watching'); this.refreshData.trigger(doc.uri, false); } }), ); - this.disposables.push( - watcher.onDidCreate((uri) => { - traceVerbose(`Testing: Trigger refresh after creating ${uri.fsPath}`); - this.sendTriggerTelemetry('watching'); - this.refreshData.trigger(uri, false); - }), - ); - this.disposables.push( - watcher.onDidDelete((uri) => { - traceVerbose(`Testing: Trigger refresh after deleting in ${uri.fsPath}`); - this.sendTriggerTelemetry('watching'); - this.refreshData.trigger(uri, false); - }), - ); } /** From 5b79058bcca529739d23ce34f40cdbb5f07a9535 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 2 Aug 2023 15:04:09 -0700 Subject: [PATCH 3/3] add detail --- src/client/testing/testController/controller.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/testing/testController/controller.ts b/src/client/testing/testController/controller.ts index 17bef3a5221b..1550323ff8f8 100644 --- a/src/client/testing/testController/controller.ts +++ b/src/client/testing/testController/controller.ts @@ -510,7 +510,8 @@ export class PythonTestController implements ITestController, IExtensionSingleAc } }), ); - + /* Keep both watchers for create and delete since config files can change test behavior without content + due to their impact on pythonPath. */ this.disposables.push( watcher.onDidCreate((uri) => { traceVerbose(`Testing: Trigger refresh after creating ${uri.fsPath}`);