Skip to content

Commit b8b1b7b

Browse files
Copiloteleanorjboyd
andcommitted
Fix auto test discovery to respect setting changes without reload
Co-authored-by: eleanorjboyd <[email protected]>
1 parent 21dd9c9 commit b8b1b7b

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

src/client/testing/testController/controller.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,10 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
549549
this.disposables.push(
550550
onDidSaveTextDocument(async (doc: TextDocument) => {
551551
const settings = this.configSettings.getSettings(doc.uri);
552-
if (minimatch.default(doc.uri.fsPath, settings.testing.autoTestDiscoverOnSavePattern)) {
552+
if (
553+
settings.testing.autoTestDiscoverOnSaveEnabled &&
554+
minimatch.default(doc.uri.fsPath, settings.testing.autoTestDiscoverOnSavePattern)
555+
) {
553556
traceVerbose(`Testing: Trigger refresh after saving ${doc.uri.fsPath}`);
554557
this.sendTriggerTelemetry('watching');
555558
this.refreshData.trigger(doc.uri, false);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import * as assert from 'assert';
5+
import * as minimatch from 'minimatch';
6+
import { Uri } from 'vscode';
7+
import { ITestingSettings } from '../../../client/testing/configuration/types';
8+
9+
suite('PythonTestController - Auto Discovery Logic Tests', () => {
10+
11+
test('auto discovery logic respects autoTestDiscoverOnSaveEnabled setting', () => {
12+
const testUri = Uri.file('/test/test_example.py');
13+
14+
// Test case 1: Setting enabled - should trigger discovery
15+
const enabledSettings: Partial<ITestingSettings> = {
16+
autoTestDiscoverOnSaveEnabled: true,
17+
autoTestDiscoverOnSavePattern: '**/*.py',
18+
};
19+
20+
const shouldTriggerWhenEnabled =
21+
enabledSettings.autoTestDiscoverOnSaveEnabled &&
22+
minimatch.default(testUri.fsPath, enabledSettings.autoTestDiscoverOnSavePattern!);
23+
24+
assert.strictEqual(shouldTriggerWhenEnabled, true, 'Should trigger when setting is enabled');
25+
26+
// Test case 2: Setting disabled - should NOT trigger discovery
27+
const disabledSettings: Partial<ITestingSettings> = {
28+
autoTestDiscoverOnSaveEnabled: false,
29+
autoTestDiscoverOnSavePattern: '**/*.py',
30+
};
31+
32+
const shouldTriggerWhenDisabled =
33+
disabledSettings.autoTestDiscoverOnSaveEnabled &&
34+
minimatch.default(testUri.fsPath, disabledSettings.autoTestDiscoverOnSavePattern!);
35+
36+
assert.strictEqual(shouldTriggerWhenDisabled, false, 'Should NOT trigger when setting is disabled');
37+
});
38+
39+
test('auto discovery logic respects pattern setting', () => {
40+
const testUri = Uri.file('/test/example.txt'); // Non-Python file
41+
42+
// Settings with enabled but non-matching pattern
43+
const settings: Partial<ITestingSettings> = {
44+
autoTestDiscoverOnSaveEnabled: true,
45+
autoTestDiscoverOnSavePattern: '**/*.py', // Only Python files
46+
};
47+
48+
const shouldTrigger =
49+
settings.autoTestDiscoverOnSaveEnabled &&
50+
minimatch.default(testUri.fsPath, settings.autoTestDiscoverOnSavePattern!);
51+
52+
assert.strictEqual(shouldTrigger, false, 'Should NOT trigger when file does not match pattern');
53+
});
54+
55+
test('auto discovery logic triggers when both setting and pattern match', () => {
56+
const testUri = Uri.file('/test/test_example.py');
57+
58+
// Settings with both enabled and matching pattern
59+
const settings: Partial<ITestingSettings> = {
60+
autoTestDiscoverOnSaveEnabled: true,
61+
autoTestDiscoverOnSavePattern: '**/*.py',
62+
};
63+
64+
const shouldTrigger =
65+
settings.autoTestDiscoverOnSaveEnabled &&
66+
minimatch.default(testUri.fsPath, settings.autoTestDiscoverOnSavePattern!);
67+
68+
assert.strictEqual(shouldTrigger, true, 'Should trigger when both setting is enabled and pattern matches');
69+
});
70+
71+
test('auto discovery logic handles undefined settings gracefully', () => {
72+
const testUri = Uri.file('/test/test_example.py');
73+
74+
// Settings with undefined autoTestDiscoverOnSaveEnabled (should be falsy)
75+
const undefinedSettings: Partial<ITestingSettings> = {
76+
autoTestDiscoverOnSavePattern: '**/*.py',
77+
};
78+
79+
const shouldTrigger =
80+
!!undefinedSettings.autoTestDiscoverOnSaveEnabled &&
81+
minimatch.default(testUri.fsPath, undefinedSettings.autoTestDiscoverOnSavePattern!);
82+
83+
assert.strictEqual(shouldTrigger, false, 'Should NOT trigger when setting is undefined');
84+
});
85+
});

0 commit comments

Comments
 (0)