1- import { mkdirSync , mkdtempSync , rmSync , symlinkSync , writeFileSync } from 'node:fs' ;
2- import { tmpdir } from 'node:os' ;
1+ import { symlinkSync } from 'node:fs' ;
32import path from 'node:path' ;
43import { expect , test } from 'rstack/test' ;
54import { discoverFmtPaths } from '../../src/fmt/discoverPaths.ts' ;
6-
7- const withProject = async ( callback : ( rootPath : string ) => Promise < void > ) : Promise < void > => {
8- const rootPath = mkdtempSync ( path . join ( tmpdir ( ) , 'rstack fmt ' ) ) ;
9-
10- try {
11- await callback ( rootPath ) ;
12- } finally {
13- rmSync ( rootPath , { force : true , recursive : true } ) ;
14- }
15- } ;
16-
17- const writeProjectFile = ( rootPath : string , filePath : string , content = '' ) : string => {
18- const absolutePath = path . join ( rootPath , filePath ) ;
19- mkdirSync ( path . dirname ( absolutePath ) , { recursive : true } ) ;
20- writeFileSync ( absolutePath , content ) ;
21- return absolutePath ;
22- } ;
5+ import { withTempProject , writeProjectFile } from './helpers.ts' ;
236
247const relativePaths = ( rootPath : string , files : string [ ] ) : string [ ] =>
258 files . map ( ( filePath ) => path . relative ( rootPath , filePath ) ) ;
269
2710test ( 'discovers non-binary files in stable order and skips hard-ignored paths' , async ( ) => {
28- await withProject ( async ( rootPath ) => {
11+ await withTempProject ( async ( rootPath ) => {
2912 writeProjectFile ( rootPath , 'b.ts' ) ;
3013 writeProjectFile ( rootPath , 'a.js' ) ;
3114 writeProjectFile ( rootPath , 'folder with spaces/c.ts' ) ;
@@ -50,7 +33,7 @@ test('discovers non-binary files in stable order and skips hard-ignored paths',
5033} ) ;
5134
5235test ( 'combines files, directories, and globs without duplicates' , async ( ) => {
53- await withProject ( async ( rootPath ) => {
36+ await withTempProject ( async ( rootPath ) => {
5437 const firstFilePath = writeProjectFile ( rootPath , 'src/a.ts' ) ;
5538 writeProjectFile ( rootPath , 'src/b.js' ) ;
5639 writeProjectFile ( rootPath , 'test/c.ts' ) ;
@@ -90,8 +73,7 @@ test('combines files, directories, and globs without duplicates', async () => {
9073} ) ;
9174
9275test ( 'applies nested gitignore rules with child negation' , async ( ) => {
93- await withProject ( async ( rootPath ) => {
94- mkdirSync ( path . join ( rootPath , '.git' ) ) ;
76+ await withTempProject ( async ( rootPath ) => {
9577 writeProjectFile ( rootPath , '.gitignore' , '*.js\ndist/\n' ) ;
9678 writeProjectFile ( rootPath , 'src/.gitignore' , '!keep.js\n' ) ;
9779 writeProjectFile ( rootPath , 'dist/.gitignore' , '!keep.js\n' ) ;
@@ -114,8 +96,7 @@ test('applies nested gitignore rules with child negation', async () => {
11496} ) ;
11597
11698test ( 'lets explicit files bypass gitignore' , async ( ) => {
117- await withProject ( async ( rootPath ) => {
118- mkdirSync ( path . join ( rootPath , '.git' ) ) ;
99+ await withTempProject ( async ( rootPath ) => {
119100 writeProjectFile ( rootPath , '.gitignore' , '/generated/\n' ) ;
120101 const keepPath = writeProjectFile ( rootPath , 'generated/keep.ts' ) ;
121102 writeProjectFile ( rootPath , 'src/index.ts' ) ;
@@ -132,7 +113,7 @@ test('lets explicit files bypass gitignore', async () => {
132113} ) ;
133114
134115test . runIf ( process . platform !== 'win32' ) ( 'does not follow file or directory symlinks' , async ( ) => {
135- await withProject ( async ( rootPath ) => {
116+ await withTempProject ( async ( rootPath ) => {
136117 const targetPath = writeProjectFile ( rootPath , 'target/index.ts' ) ;
137118 symlinkSync ( path . join ( rootPath , 'target' ) , path . join ( rootPath , 'linked-directory' ) ) ;
138119 symlinkSync ( targetPath , path . join ( rootPath , 'linked-file.ts' ) ) ;
0 commit comments