@@ -10,8 +10,9 @@ import {
1010 isGovernedSourcePath ,
1111 main ,
1212} from "../../scripts/check-max-lines-ratchet.mts" ;
13+ import { createTempDirTracker } from "../helpers/temp-dir.js" ;
1314
14- const tempDirs : string [ ] = [ ] ;
15+ const tempDirs = createTempDirTracker ( ) ;
1516const nestedGitEnvKeys = [
1617 "GIT_ALTERNATE_OBJECT_DIRECTORIES" ,
1718 "GIT_COMMON_DIR" ,
@@ -44,11 +45,21 @@ function git(cwd: string, args: string[]): void {
4445 execFileSync ( "git" , args , { cwd, env, stdio : "ignore" } ) ;
4546}
4647
48+ function commitFixture ( root : string , message = "base" ) : void {
49+ for ( const args of [
50+ [ "init" ] ,
51+ [ "config" , "user.email" , "[email protected] " ] , 52+ [ "config" , "user.name" , "Test" ] ,
53+ [ "add" , "." ] ,
54+ [ "commit" , "-m" , message ] ,
55+ ] ) {
56+ git ( root , args ) ;
57+ }
58+ }
59+
4760afterEach ( ( ) => {
4861 vi . restoreAllMocks ( ) ;
49- for ( const dir of tempDirs . splice ( 0 ) ) {
50- fs . rmSync ( dir , { force : true , recursive : true } ) ;
51- }
62+ tempDirs . cleanup ( ) ;
5263} ) ;
5364
5465describe ( "check-max-lines-ratchet" , ( ) => {
@@ -91,24 +102,15 @@ describe("check-max-lines-ratchet", () => {
91102 } ) ;
92103
93104 it ( "rejects baseline growth even when the new suppression is listed" , ( ) => {
94- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-max-lines-" ) ) ;
95- tempDirs . push ( root ) ;
105+ const root = tempDirs . make ( "openclaw-max-lines-" , os . tmpdir ( ) ) ;
96106 fs . mkdirSync ( path . join ( root , "config" ) , { recursive : true } ) ;
97107 fs . mkdirSync ( path . join ( root , "src" ) , { recursive : true } ) ;
98108 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "src/a.ts\n" ) ;
99109 fs . writeFileSync (
100110 path . join ( root , "src/a.ts" ) ,
101111 "/* oxlint-disable max-lines -- TODO: split. */\n" ,
102112 ) ;
103- for ( const args of [
104- [ "init" ] ,
105- [ "config" , "user.email" , "[email protected] " ] , 106- [ "config" , "user.name" , "Test" ] ,
107- [ "add" , "." ] ,
108- [ "commit" , "-m" , "base" ] ,
109- ] ) {
110- git ( root , args ) ;
111- }
113+ commitFixture ( root ) ;
112114
113115 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "src/a.ts\nsrc/b.ts\n" ) ;
114116 fs . writeFileSync (
@@ -122,21 +124,12 @@ describe("check-max-lines-ratchet", () => {
122124 } ) ;
123125
124126 it ( "rejects replacing an explicit max-lines suppression with an all-rule disable" , ( ) => {
125- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-max-lines-all-rule-" ) ) ;
126- tempDirs . push ( root ) ;
127+ const root = tempDirs . make ( "openclaw-max-lines-all-rule-" , os . tmpdir ( ) ) ;
127128 fs . mkdirSync ( path . join ( root , "config" ) , { recursive : true } ) ;
128129 fs . mkdirSync ( path . join ( root , "src" ) , { recursive : true } ) ;
129130 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "src/a.ts\n" ) ;
130131 fs . writeFileSync ( path . join ( root , "src/a.ts" ) , "/* oxlint-disable max-lines */\n" ) ;
131- for ( const args of [
132- [ "init" ] ,
133- [ "config" , "user.email" , "[email protected] " ] , 134- [ "config" , "user.name" , "Test" ] ,
135- [ "add" , "." ] ,
136- [ "commit" , "-m" , "base" ] ,
137- ] ) {
138- git ( root , args ) ;
139- }
132+ commitFixture ( root ) ;
140133
141134 fs . writeFileSync ( path . join ( root , "src/a.ts" ) , "/* oxlint-disable */\n" ) ;
142135 vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
@@ -145,21 +138,12 @@ describe("check-max-lines-ratchet", () => {
145138 } ) ;
146139
147140 it ( "rejects a new all-rule disable without baseline growth" , ( ) => {
148- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-max-lines-all-rule-new-" ) ) ;
149- tempDirs . push ( root ) ;
141+ const root = tempDirs . make ( "openclaw-max-lines-all-rule-new-" , os . tmpdir ( ) ) ;
150142 fs . mkdirSync ( path . join ( root , "config" ) , { recursive : true } ) ;
151143 fs . mkdirSync ( path . join ( root , "src" ) , { recursive : true } ) ;
152144 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "" ) ;
153145 fs . writeFileSync ( path . join ( root , "src/a.ts" ) , "export const a = 1;\n" ) ;
154- for ( const args of [
155- [ "init" ] ,
156- [ "config" , "user.email" , "[email protected] " ] , 157- [ "config" , "user.name" , "Test" ] ,
158- [ "add" , "." ] ,
159- [ "commit" , "-m" , "base" ] ,
160- ] ) {
161- git ( root , args ) ;
162- }
146+ commitFixture ( root ) ;
163147
164148 fs . writeFileSync ( path . join ( root , "src/a.ts" ) , "/* oxlint-disable */\n" ) ;
165149 vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
@@ -168,24 +152,15 @@ describe("check-max-lines-ratchet", () => {
168152 } ) ;
169153
170154 it ( "transfers grandfathered debt across a verified rename" , ( ) => {
171- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-max-lines-rename-" ) ) ;
172- tempDirs . push ( root ) ;
155+ const root = tempDirs . make ( "openclaw-max-lines-rename-" , os . tmpdir ( ) ) ;
173156 fs . mkdirSync ( path . join ( root , "config" ) , { recursive : true } ) ;
174157 fs . mkdirSync ( path . join ( root , "src" ) , { recursive : true } ) ;
175158 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "src/a.ts\n" ) ;
176159 fs . writeFileSync (
177160 path . join ( root , "src/a.ts" ) ,
178161 "export const a = 1;\n/* oxlint-disable max-lines -- TODO: split. */\n" ,
179162 ) ;
180- for ( const args of [
181- [ "init" ] ,
182- [ "config" , "user.email" , "[email protected] " ] , 183- [ "config" , "user.name" , "Test" ] ,
184- [ "add" , "." ] ,
185- [ "commit" , "-m" , "base" ] ,
186- ] ) {
187- git ( root , args ) ;
188- }
163+ commitFixture ( root ) ;
189164 git ( root , [ "update-ref" , "refs/remotes/origin/main" , "HEAD" ] ) ;
190165
191166 git ( root , [ "mv" , "src/a.ts" , "src/b.ts" ] ) ;
@@ -195,21 +170,12 @@ describe("check-max-lines-ratchet", () => {
195170 } ) ;
196171
197172 it ( "defaults worktree comparisons to origin/main" , ( ) => {
198- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-max-lines-default-base-" ) ) ;
199- tempDirs . push ( root ) ;
173+ const root = tempDirs . make ( "openclaw-max-lines-default-base-" , os . tmpdir ( ) ) ;
200174 fs . mkdirSync ( path . join ( root , "config" ) , { recursive : true } ) ;
201175 fs . mkdirSync ( path . join ( root , "src" ) , { recursive : true } ) ;
202176 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "src/a.ts\n" ) ;
203177 fs . writeFileSync ( path . join ( root , "src/a.ts" ) , "/* oxlint-disable max-lines */\n" ) ;
204- for ( const args of [
205- [ "init" ] ,
206- [ "config" , "user.email" , "[email protected] " ] , 207- [ "config" , "user.name" , "Test" ] ,
208- [ "add" , "." ] ,
209- [ "commit" , "-m" , "base" ] ,
210- ] ) {
211- git ( root , args ) ;
212- }
178+ commitFixture ( root ) ;
213179 git ( root , [ "update-ref" , "refs/remotes/origin/main" , "HEAD" ] ) ;
214180
215181 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "src/a.ts\nsrc/b.ts\n" ) ;
@@ -222,23 +188,14 @@ describe("check-max-lines-ratchet", () => {
222188 } ) ;
223189
224190 it ( "compares an explicit moving base at the branch fork" , ( ) => {
225- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-max-lines-diverged-" ) ) ;
226- tempDirs . push ( root ) ;
191+ const root = tempDirs . make ( "openclaw-max-lines-diverged-" , os . tmpdir ( ) ) ;
227192 fs . mkdirSync ( path . join ( root , "config" ) , { recursive : true } ) ;
228193 fs . mkdirSync ( path . join ( root , "src" ) , { recursive : true } ) ;
229194 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "src/a.ts\nsrc/b.ts\n" ) ;
230195 fs . writeFileSync ( path . join ( root , "src/a.ts" ) , "/* oxlint-disable max-lines */\n" ) ;
231196 fs . writeFileSync ( path . join ( root , "src/b.ts" ) , "/* oxlint-disable max-lines */\n" ) ;
232- for ( const args of [
233- [ "init" ] ,
234- [ "config" , "user.email" , "[email protected] " ] , 235- [ "config" , "user.name" , "Test" ] ,
236- [ "add" , "." ] ,
237- [ "commit" , "-m" , "base" ] ,
238- [ "branch" , "release" ] ,
239- ] ) {
240- git ( root , args ) ;
241- }
197+ commitFixture ( root ) ;
198+ git ( root , [ "branch" , "release" ] ) ;
242199
243200 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "src/a.ts\n" ) ;
244201 fs . writeFileSync ( path . join ( root , "src/b.ts" ) , "export const b = 1;\n" ) ;
@@ -251,22 +208,13 @@ describe("check-max-lines-ratchet", () => {
251208 } ) ;
252209
253210 it ( "falls back to main when no merge base is available" , ( ) => {
254- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-max-lines-disconnected-" ) ) ;
255- tempDirs . push ( root ) ;
211+ const root = tempDirs . make ( "openclaw-max-lines-disconnected-" , os . tmpdir ( ) ) ;
256212 fs . mkdirSync ( path . join ( root , "config" ) , { recursive : true } ) ;
257213 fs . mkdirSync ( path . join ( root , "src" ) , { recursive : true } ) ;
258214 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "src/a.ts\n" ) ;
259215 fs . writeFileSync ( path . join ( root , "src/a.ts" ) , "/* oxlint-disable max-lines */\n" ) ;
260- for ( const args of [
261- [ "init" ] ,
262- [ "config" , "user.email" , "[email protected] " ] , 263- [ "config" , "user.name" , "Test" ] ,
264- [ "add" , "." ] ,
265- [ "commit" , "-m" , "release" ] ,
266- [ "branch" , "-m" , "release" ] ,
267- ] ) {
268- git ( root , args ) ;
269- }
216+ commitFixture ( root , "release" ) ;
217+ git ( root , [ "branch" , "-m" , "release" ] ) ;
270218 git ( root , [ "checkout" , "--orphan" , "main" ] ) ;
271219 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "" ) ;
272220 fs . writeFileSync ( path . join ( root , "src/a.ts" ) , "export const a = 1;\n" ) ;
@@ -279,21 +227,12 @@ describe("check-max-lines-ratchet", () => {
279227 } ) ;
280228
281229 it ( "checks staged content instead of unstaged worktree edits" , ( ) => {
282- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-max-lines-staged-" ) ) ;
283- tempDirs . push ( root ) ;
230+ const root = tempDirs . make ( "openclaw-max-lines-staged-" , os . tmpdir ( ) ) ;
284231 fs . mkdirSync ( path . join ( root , "config" ) , { recursive : true } ) ;
285232 fs . mkdirSync ( path . join ( root , "src" ) , { recursive : true } ) ;
286233 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "" ) ;
287234 fs . writeFileSync ( path . join ( root , "src/a.ts" ) , "export const a = 1;\n" ) ;
288- for ( const args of [
289- [ "init" ] ,
290- [ "config" , "user.email" , "[email protected] " ] , 291- [ "config" , "user.name" , "Test" ] ,
292- [ "add" , "." ] ,
293- [ "commit" , "-m" , "base" ] ,
294- ] ) {
295- git ( root , args ) ;
296- }
235+ commitFixture ( root ) ;
297236
298237 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "src/a.ts\n" ) ;
299238 fs . writeFileSync ( path . join ( root , "src/a.ts" ) , "/* oxlint-disable */\n" ) ;
@@ -306,8 +245,7 @@ describe("check-max-lines-ratchet", () => {
306245 } ) ;
307246
308247 it . skipIf ( process . platform === "win32" ) ( "keeps staged filenames NUL-framed" , ( ) => {
309- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-max-lines-nul-" ) ) ;
310- tempDirs . push ( root ) ;
248+ const root = tempDirs . make ( "openclaw-max-lines-nul-" , os . tmpdir ( ) ) ;
311249 fs . mkdirSync ( path . join ( root , "src" ) , { recursive : true } ) ;
312250 git ( root , [ "init" ] ) ;
313251 const filePath = "src/newline\nname.ts" ;
@@ -318,21 +256,12 @@ describe("check-max-lines-ratchet", () => {
318256 } ) ;
319257
320258 it ( "checks untracked sources and tolerates unstaged deletions" , ( ) => {
321- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-max-lines-worktree-" ) ) ;
322- tempDirs . push ( root ) ;
259+ const root = tempDirs . make ( "openclaw-max-lines-worktree-" , os . tmpdir ( ) ) ;
323260 fs . mkdirSync ( path . join ( root , "config" ) , { recursive : true } ) ;
324261 fs . mkdirSync ( path . join ( root , "src" ) , { recursive : true } ) ;
325262 fs . writeFileSync ( path . join ( root , "config/max-lines-baseline.txt" ) , "" ) ;
326263 fs . writeFileSync ( path . join ( root , "src/deleted.ts" ) , "export const deleted = true;\n" ) ;
327- for ( const args of [
328- [ "init" ] ,
329- [ "config" , "user.email" , "[email protected] " ] , 330- [ "config" , "user.name" , "Test" ] ,
331- [ "add" , "." ] ,
332- [ "commit" , "-m" , "base" ] ,
333- ] ) {
334- git ( root , args ) ;
335- }
264+ commitFixture ( root ) ;
336265 git ( root , [ "update-ref" , "refs/remotes/origin/main" , "HEAD" ] ) ;
337266 fs . rmSync ( path . join ( root , "src/deleted.ts" ) ) ;
338267 expect ( main ( root ) ) . toBe ( 0 ) ;
0 commit comments