1- import { afterEach , describe , expect , it } from "bun:test " ;
2- import { mkdir , mkdtemp , rm } from "node:fs/promises" ;
1+ import assert from "node:assert/strict " ;
2+ import { mkdir , mkdtemp , rm , writeFile } from "node:fs/promises" ;
33import { tmpdir } from "node:os" ;
44import { join } from "node:path" ;
5+ import { afterEach , describe , it } from "node:test" ;
56
6- import { findFiles , grep } from "./grep" ;
7+ import { globFilesSync } from "./files.ts" ;
8+ import { findFiles , grep } from "./grep.ts" ;
79
810const tempDirs : string [ ] = [ ] ;
911
@@ -17,12 +19,12 @@ afterEach(async () => {
1719 await Promise . all ( tempDirs . splice ( 0 ) . map ( ( dir ) => rm ( dir , { recursive : true , force : true } ) ) ) ;
1820} ) ;
1921
20- describe ( "grep" , ( ) => {
21- it ( "treats fixed-string patterns literally without shell interpretation" , async ( ) => {
22+ void describe ( "grep" , ( ) => {
23+ void it ( "treats fixed-string patterns literally without shell interpretation" , async ( ) => {
2224 const dir = await createTempDir ( ) ;
2325 const nestedDir = join ( dir , "nested dir" ) ;
2426 await mkdir ( nestedDir , { recursive : true } ) ;
25- await Bun . write (
27+ await writeFile (
2628 join ( nestedDir , "example.ts" ) ,
2729 "const marker = \"literal $(echo nope) 'quotes'\";\n" ,
2830 ) ;
@@ -32,73 +34,84 @@ describe("grep", () => {
3234 glob : "**/*.ts" ,
3335 } ) ;
3436
35- expect ( matches ) . toHaveLength ( 1 ) ;
36- expect ( matches [ 0 ] ?. file ) . toBe ( "nested dir/example.ts" ) ;
37+ assert . equal ( matches . length , 1 ) ;
38+ assert . equal ( matches [ 0 ] ?. file , "nested dir/example.ts" ) ;
3739 } ) ;
3840
39- it ( "enforces maxResults globally across files" , async ( ) => {
41+ void it ( "enforces maxResults globally across files" , async ( ) => {
4042 const dir = await createTempDir ( ) ;
41- await Bun . write ( join ( dir , "one.txt" ) , "needle\nneedle\n" ) ;
42- await Bun . write ( join ( dir , "two.txt" ) , "needle\nneedle\n" ) ;
43- await Bun . write ( join ( dir , "three.txt" ) , "needle\nneedle\n" ) ;
43+ await writeFile ( join ( dir , "one.txt" ) , "needle\nneedle\n" ) ;
44+ await writeFile ( join ( dir , "two.txt" ) , "needle\nneedle\n" ) ;
45+ await writeFile ( join ( dir , "three.txt" ) , "needle\nneedle\n" ) ;
4446
4547 const matches = await grep ( "needle" , dir , {
4648 fixedString : true ,
4749 maxResults : 2 ,
4850 } ) ;
4951
50- expect ( matches ) . toHaveLength ( 2 ) ;
51- expect ( matches . every ( ( match ) => match . content === "needle" ) ) . toBe ( true ) ;
52+ assert . equal ( matches . length , 2 ) ;
53+ assert . equal (
54+ matches . every ( ( match ) => match . content === "needle" ) ,
55+ true ,
56+ ) ;
5257 } ) ;
5358
54- it ( "applies path-aware glob filters instead of basename-only includes" , async ( ) => {
59+ void it ( "applies path-aware glob filters instead of basename-only includes" , async ( ) => {
5560 const dir = await createTempDir ( ) ;
5661 const nestedDir = join ( dir , "src" , "nested" ) ;
5762 await mkdir ( nestedDir , { recursive : true } ) ;
58- await Bun . write ( join ( nestedDir , "match.ts" ) , "needle\n" ) ;
59- await Bun . write ( join ( nestedDir , "skip.js" ) , "needle\n" ) ;
63+ await writeFile ( join ( nestedDir , "match.ts" ) , "needle\n" ) ;
64+ await writeFile ( join ( nestedDir , "skip.js" ) , "needle\n" ) ;
6065
6166 const matches = await grep ( "needle" , dir , {
6267 fixedString : true ,
6368 glob : "src/**/*.ts" ,
6469 } ) ;
6570
66- expect ( matches ) . toHaveLength ( 1 ) ;
67- expect ( matches [ 0 ] ?. file ) . toBe ( "src/nested/match.ts" ) ;
71+ assert . equal ( matches . length , 1 ) ;
72+ assert . equal ( matches [ 0 ] ?. file , "src/nested/match.ts" ) ;
6873 } ) ;
6974
70- it ( "auto-expands basename-only globs to recursive matching" , async ( ) => {
75+ void it ( "auto-expands basename-only globs to recursive matching" , async ( ) => {
7176 const dir = await createTempDir ( ) ;
7277 await mkdir ( join ( dir , "src" , "Views" ) , { recursive : true } ) ;
73- await Bun . write ( join ( dir , "src" , "Views" , "ContentView.swift" ) , 'print("hello")\n' ) ;
74- await Bun . write ( join ( dir , "RootFile.swift" ) , 'print("root")\n' ) ;
78+ await writeFile ( join ( dir , "src" , "Views" , "ContentView.swift" ) , 'print("hello")\n' ) ;
79+ await writeFile ( join ( dir , "RootFile.swift" ) , 'print("root")\n' ) ;
7580
7681 const matches = await grep ( "print" , dir , {
7782 fixedString : true ,
7883 glob : "*.swift" ,
7984 } ) ;
8085
81- expect ( matches ) . toHaveLength ( 2 ) ;
86+ assert . equal ( matches . length , 2 ) ;
8287 const files = matches . map ( ( m ) => m . file ) . toSorted ( ) ;
83- expect ( files ) . toContain ( "RootFile.swift" ) ;
84- expect ( files ) . toContain ( "src/Views/ContentView.swift" ) ;
88+ assert . ok ( files . includes ( "RootFile.swift" ) ) ;
89+ assert . ok ( files . includes ( "src/Views/ContentView.swift" ) ) ;
8590 } ) ;
8691
87- it ( "finds extension matches without shelling out and skips ignored paths" , async ( ) => {
92+ void it ( "finds extension matches without shelling out and skips ignored paths" , async ( ) => {
8893 const dir = await createTempDir ( ) ;
8994 await mkdir ( join ( dir , "src" , "nested" ) , { recursive : true } ) ;
9095 await mkdir ( join ( dir , ".hidden" ) , { recursive : true } ) ;
9196 await mkdir ( join ( dir , "node_modules" , "pkg" ) , { recursive : true } ) ;
9297 await mkdir ( join ( dir , ".build" ) , { recursive : true } ) ;
9398
94- await Bun . write ( join ( dir , "src" , "nested" , "match.swift" ) , "struct Match {}\n" ) ;
95- await Bun . write ( join ( dir , ".hidden" , "hidden.swift" ) , "struct Hidden {}\n" ) ;
96- await Bun . write ( join ( dir , "node_modules" , "pkg" , "dep.swift" ) , "struct Dep {}\n" ) ;
97- await Bun . write ( join ( dir , ".build" , "generated.swift" ) , "struct Generated {}\n" ) ;
99+ await writeFile ( join ( dir , "src" , "nested" , "match.swift" ) , "struct Match {}\n" ) ;
100+ await writeFile ( join ( dir , ".hidden" , "hidden.swift" ) , "struct Hidden {}\n" ) ;
101+ await writeFile ( join ( dir , "node_modules" , "pkg" , "dep.swift" ) , "struct Dep {}\n" ) ;
102+ await writeFile ( join ( dir , ".build" , "generated.swift" ) , "struct Generated {}\n" ) ;
98103
99104 const files = findFiles ( dir , "swift" ) ;
100105
101- expect ( files ) . toHaveLength ( 1 ) ;
102- expect ( files [ 0 ] ) . toBe ( join ( dir , "src" , "nested" , "match.swift" ) ) ;
106+ assert . equal ( files . length , 1 ) ;
107+ assert . equal ( files [ 0 ] , join ( dir , "src" , "nested" , "match.swift" ) ) ;
108+ } ) ;
109+
110+ void it ( "never returns a directory whose name matches the file pattern" , async ( ) => {
111+ const dir = await createTempDir ( ) ;
112+ await mkdir ( join ( dir , "directory.ts" ) ) ;
113+ await writeFile ( join ( dir , "file.ts" ) , "export {};\n" ) ;
114+
115+ assert . deepEqual ( globFilesSync ( "*.ts" , dir ) , [ "file.ts" ] ) ;
103116 } ) ;
104117} ) ;
0 commit comments