Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update tests only on save with more files excluded #21741

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

settings.json only takes effect if it is at the root of the workspace, under .vscode. So we can probably improve the pattern for it.

What about other config files, do we need to watch for pyproject.toml, setup.cfg in any levels of subdirectories?


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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karthiknadig please check this and confirm it is correct / makes sense. Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense

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);
}
Comment on lines +531 to +538
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice, should makes things much better.

}),
);
}
Expand Down
Loading