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