@@ -3,6 +3,7 @@ import { join, parse } from "path";
33import {
44 containsPath ,
55 findCommonParentDir ,
6+ findDirWithFile ,
67 gatherQlFiles ,
78 getDirectoryNamesInsidePath ,
89 pathsEqual ,
@@ -11,7 +12,13 @@ import {
1112} from "../../../src/common/files" ;
1213import type { DirResult } from "tmp" ;
1314import { dirSync } from "tmp" ;
14- import { ensureDirSync , symlinkSync , writeFileSync } from "fs-extra" ;
15+ import {
16+ createFileSync ,
17+ ensureDirSync ,
18+ mkdirSync ,
19+ symlinkSync ,
20+ writeFileSync ,
21+ } from "fs-extra" ;
1522import "../../matchers/toEqualPath" ;
1623
1724describe ( "files" , ( ) => {
@@ -592,3 +599,52 @@ describe("findCommonParentDir", () => {
592599 expect ( commonDir ) . toEqualPath ( dataDir ) ;
593600 } ) ;
594601} ) ;
602+
603+ describe ( "findDirWithFile" , ( ) => {
604+ let dir : DirResult ;
605+ beforeEach ( ( ) => {
606+ dir = dirSync ( { unsafeCleanup : true } ) ;
607+ createFile ( "a" ) ;
608+ createFile ( "b" ) ;
609+ createFile ( "c" ) ;
610+
611+ createDir ( "dir1" ) ;
612+ createFile ( "dir1" , "d" ) ;
613+ createFile ( "dir1" , "e" ) ;
614+ createFile ( "dir1" , "f" ) ;
615+
616+ createDir ( "dir2" ) ;
617+ createFile ( "dir2" , "g" ) ;
618+ createFile ( "dir2" , "h" ) ;
619+ createFile ( "dir2" , "i" ) ;
620+
621+ createDir ( "dir2" , "dir3" ) ;
622+ createFile ( "dir2" , "dir3" , "j" ) ;
623+ createFile ( "dir2" , "dir3" , "k" ) ;
624+ createFile ( "dir2" , "dir3" , "l" ) ;
625+ } ) ;
626+
627+ it ( "should find files" , async ( ) => {
628+ expect ( await findDirWithFile ( dir . name , "k" ) ) . toBe (
629+ join ( dir . name , "dir2" , "dir3" ) ,
630+ ) ;
631+ expect ( await findDirWithFile ( dir . name , "h" ) ) . toBe ( join ( dir . name , "dir2" ) ) ;
632+ expect ( await findDirWithFile ( dir . name , "z" , "a" ) ) . toBe ( dir . name ) ;
633+ // there's some slight indeterminism when more than one name exists
634+ // but in general, this will find files in the current directory before
635+ // finding files in sub-dirs
636+ expect ( await findDirWithFile ( dir . name , "k" , "a" ) ) . toBe ( dir . name ) ;
637+ } ) ;
638+
639+ it ( "should not find files" , async ( ) => {
640+ expect ( await findDirWithFile ( dir . name , "x" , "y" , "z" ) ) . toBeUndefined ( ) ;
641+ } ) ;
642+
643+ function createFile ( ...segments : string [ ] ) {
644+ createFileSync ( join ( dir . name , ...segments ) ) ;
645+ }
646+
647+ function createDir ( ...segments : string [ ] ) {
648+ mkdirSync ( join ( dir . name , ...segments ) ) ;
649+ }
650+ } ) ;
0 commit comments