@@ -2,6 +2,7 @@ import {Command} from 'obsidian';
22import { RulesRunner } from '../src/rules-runner' ;
33import { CustomReplace } from '../src/ui/linter-components/custom-replace-option' ;
44import dedent from 'ts-dedent' ;
5+ import { LintCommand } from 'src/ui/linter-components/custom-command-option' ;
56
67const rulesRunner = new RulesRunner ( ) ;
78const appCommandsMock = {
@@ -31,7 +32,7 @@ const appCommandsMock = {
3132
3233type CustomCommandTestCase = {
3334 testName : string ,
34- listOfCommands : Command [ ] ,
35+ listOfCommands : LintCommand [ ] ,
3536 expectedCommandCount : Map < string , number > ;
3637 expectedNumberOfCommandsRun : number ;
3738 skipFileValue : boolean
@@ -48,8 +49,8 @@ const customCommandTestCases: CustomCommandTestCase[] = [
4849 {
4950 testName : 'When an app lint command is run it should be executed' ,
5051 listOfCommands : [
51- { id : 'first id' , name : 'command name' } ,
52- { id : 'second id' , name : 'command name 2' } ,
52+ { id : 'first id' , name : 'command name' , enabled : true } ,
53+ { id : 'second id' , name : 'command name 2' , enabled : true } ,
5354 ] ,
5455 expectedCommandCount : new Map ( [
5556 [ 'first id' , 1 ] ,
@@ -61,7 +62,7 @@ const customCommandTestCases: CustomCommandTestCase[] = [
6162 {
6263 testName : 'A lint command with an empty id should not get run' ,
6364 listOfCommands : [
64- { id : '' , name : '' } ,
65+ { id : '' , name : '' , enabled : true } ,
6566 ] ,
6667 expectedCommandCount : new Map ( [
6768 [ '' , 0 ] ,
@@ -72,8 +73,8 @@ const customCommandTestCases: CustomCommandTestCase[] = [
7273 {
7374 testName : 'When custom commands are run with two of the same command, the second command instance is skipped' ,
7475 listOfCommands : [
75- { id : 'first id' , name : 'command name' } ,
76- { id : 'first id' , name : 'command name' } ,
76+ { id : 'first id' , name : 'command name' , enabled : true } ,
77+ { id : 'first id' , name : 'command name' , enabled : true } ,
7778 ] ,
7879 expectedCommandCount : new Map ( [
7980 [ 'first id' , 1 ] ,
@@ -84,13 +85,23 @@ const customCommandTestCases: CustomCommandTestCase[] = [
8485 {
8586 testName : 'When the file is listed to be skipped, no custom commands are run' ,
8687 listOfCommands : [
87- { id : 'first id' , name : 'command name' } ,
88- { id : 'second id' , name : 'command name 2' } ,
88+ { id : 'first id' , name : 'command name' , enabled : true } ,
89+ { id : 'second id' , name : 'command name 2' , enabled : true } ,
8990 ] ,
9091 expectedCommandCount : new Map < string , number > ( ) ,
9192 expectedNumberOfCommandsRun : 0 ,
9293 skipFileValue : true ,
9394 } ,
95+ {
96+ testName : 'When the custom commands are not enabled, nothing gets run' ,
97+ listOfCommands : [
98+ { id : 'first id' , name : 'command name' , enabled : false } ,
99+ { id : 'second id' , name : 'command name 2' , enabled : false } ,
100+ ] ,
101+ expectedCommandCount : new Map < string , number > ( ) ,
102+ expectedNumberOfCommandsRun : 0 ,
103+ skipFileValue : false ,
104+ } ,
94105] ;
95106
96107
@@ -106,7 +117,7 @@ const customReplaceTestCases: CustomReplaceTestCase[] = [
106117 testName : 'A custom replace with no find value does not affect the text' ,
107118 listOfRegexReplacements : [
108119 {
109- label : '' , find : '' , replace : 'hello' , flags : 'g' ,
120+ label : '' , find : '' , replace : 'hello' , flags : 'g' , enabled : true ,
110121 } ,
111122 ] ,
112123 before : dedent `
@@ -122,10 +133,10 @@ const customReplaceTestCases: CustomReplaceTestCase[] = [
122133 testName : 'A custom replace with a null or undefined find value does not affect the text' ,
123134 listOfRegexReplacements : [
124135 {
125- label : '' , find : 'How' , replace : null , flags : '' ,
136+ label : '' , find : 'How' , replace : null , flags : '' , enabled : true ,
126137 } ,
127138 {
128- label : 'Replace 2' , find : 'look' , replace : undefined , flags : '' ,
139+ label : 'Replace 2' , find : 'look' , replace : undefined , flags : '' , enabled : true ,
129140 } ,
130141 ] ,
131142 before : dedent `
@@ -141,7 +152,7 @@ const customReplaceTestCases: CustomReplaceTestCase[] = [
141152 testName : 'A custom replace searching for multiple blank lines in a row works (has proper escaping of a slash)' ,
142153 listOfRegexReplacements : [
143154 {
144- label : 'condense multiple blanks into 1' , find : '\n{3,}' , replace : '\n\n' , flags : 'g' ,
155+ label : 'condense multiple blanks into 1' , find : '\n{3,}' , replace : '\n\n' , flags : 'g' , enabled : true ,
145156 } ,
146157 ] ,
147158 before : dedent `
@@ -160,7 +171,7 @@ const customReplaceTestCases: CustomReplaceTestCase[] = [
160171 testName : 'A custom replace using capture groups works' ,
161172 listOfRegexReplacements : [
162173 {
163- label : 'Remove a question mark proceeded by a k or an e' , find : '(k|e)(\\?)' , replace : '$1' , flags : 'g' ,
174+ label : 'Remove a question mark proceeded by a k or an e' , find : '(k|e)(\\?)' , replace : '$1' , flags : 'g' , enabled : true ,
164175 } ,
165176 ] ,
166177 before : dedent `
@@ -176,7 +187,7 @@ const customReplaceTestCases: CustomReplaceTestCase[] = [
176187 testName : 'A custom replace using ^ and $ works' ,
177188 listOfRegexReplacements : [
178189 {
179- label : 'Replace Did at the start of a line or look? at the end of a line' , find : '(^Did)|(look\\?$)' , replace : 'swapped' , flags : 'gm' ,
190+ label : 'Replace Did at the start of a line or look? at the end of a line' , find : '(^Did)|(look\\?$)' , replace : 'swapped' , flags : 'gm' , enabled : true ,
180191 } ,
181192 ] ,
182193 before : dedent `
@@ -192,7 +203,7 @@ const customReplaceTestCases: CustomReplaceTestCase[] = [
192203 testName : 'A custom replace should respect linter ignore ranges' ,
193204 listOfRegexReplacements : [
194205 {
195- label : 'Replace Did at the start of a line or look? at the end of a line' , find : '(^Did)|(look\\?$)' , replace : 'swapped' , flags : 'gm' ,
206+ label : 'Replace Did at the start of a line or look? at the end of a line' , find : '(^Did)|(look\\?$)' , replace : 'swapped' , flags : 'gm' , enabled : true ,
196207 } ,
197208 ] ,
198209 before : dedent `
@@ -212,23 +223,23 @@ const customReplaceTestCases: CustomReplaceTestCase[] = [
212223 testName : 'A custom replace with an undefined label should still run.' ,
213224 listOfRegexReplacements : [
214225 {
215- label : undefined , find : 'lobo' , replace : 'hello' , flags : 'g' ,
226+ label : undefined , find : 'lobo' , replace : 'hello' , flags : 'g' , enabled : true ,
216227 } ,
217228 ] ,
218229 before : dedent `
219- How does this look ?
230+ How does this lobo ?
220231 Did it stay the same?
221232 ` ,
222233 after : dedent `
223- How does this look ?
234+ How does this hello ?
224235 Did it stay the same?
225236 ` ,
226237 } ,
227238 { // relates for https://github.com/platers/obsidian-linter/issues/1121
228239 testName : 'A custom replace should respect linter ignore ranges that use the Obsidian comment format' ,
229240 listOfRegexReplacements : [
230241 {
231- label : 'Replace Did at the start of a line or look? at the end of a line' , find : '(^Did)|(look\\?$)' , replace : 'swapped' , flags : 'gm' ,
242+ label : 'Replace Did at the start of a line or look? at the end of a line' , find : '(^Did)|(look\\?$)' , replace : 'swapped' , flags : 'gm' , enabled : true ,
232243 } ,
233244 ] ,
234245 before : dedent `
@@ -244,6 +255,22 @@ const customReplaceTestCases: CustomReplaceTestCase[] = [
244255 %% linter-enable %%
245256 ` ,
246257 } ,
258+ {
259+ testName : 'A custom replace that is not enabled should not run' ,
260+ listOfRegexReplacements : [
261+ {
262+ label : undefined , find : 'lobo' , replace : 'hello' , flags : 'g' , enabled : false ,
263+ } ,
264+ ] ,
265+ before : dedent `
266+ How does this look?
267+ Did it stay the same?
268+ ` ,
269+ after : dedent `
270+ How does this look?
271+ Did it stay the same?
272+ ` ,
273+ } ,
247274] ;
248275
249276describe ( 'Rules Runner' , ( ) => {
0 commit comments