Skip to content

Commit

Permalink
update tests only on save with more files excluded (#21741)
Browse files Browse the repository at this point in the history
fixes #21014 and
#21061
  • Loading branch information
eleanorjboyd authored Aug 2, 2023
1 parent 84bbff9 commit ca4dfd4
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions src/client/testing/testController/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
CancellationTokenSource,
Uri,
EventEmitter,
TextDocument,
} from 'vscode';
import { IExtensionSingleActivationService } from '../../activation/types';
import { ICommandManager, IWorkspaceService } from '../../common/application/types';
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -209,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();
}
});
}
Expand Down Expand Up @@ -493,12 +495,23 @@ 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);
}
}),
);
/* 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}`);
Expand All @@ -515,31 +528,14 @@ 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);

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);
}),
);
this.disposables.push(
watcher.onDidCreate((uri) => {
traceVerbose(`Testing: Trigger refresh after creating ${uri.fsPath}`);
this.sendTriggerTelemetry('watching');
this.refreshData.trigger(uri, false);
}),
);
private watchForTestContentChangeOnSave(): void {
this.disposables.push(
watcher.onDidDelete((uri) => {
traceVerbose(`Testing: Trigger refresh after deleting in ${uri.fsPath}`);
this.sendTriggerTelemetry('watching');
this.refreshData.trigger(uri, false);
onDidSaveTextDocument(async (doc: TextDocument) => {
if (doc.fileName.endsWith('.py')) {
traceVerbose(`Testing: Trigger refresh after saving ${doc.uri.fsPath}`);
this.sendTriggerTelemetry('watching');
this.refreshData.trigger(doc.uri, false);
}
}),
);
}
Expand Down

0 comments on commit ca4dfd4

Please sign in to comment.