This repository was archived by the owner on Apr 8, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { exec } from '../util/exec' ;
22import { readGit } from './readGit' ;
3+ import fs from 'fs' ;
34
45jest . mock ( '../util/exec' ) ;
6+ jest . mock ( 'fs' ) ;
7+ const fsMocked = jest . mocked < any > ( fs ) ;
58const execFileMock = exec as jest . Mock ;
69
710describe ( 'readGit' , ( ) => {
11+ beforeEach ( ( ) => {
12+ fsMocked . statSync . mockImplementation ( ( path : any ) => {
13+ return {
14+ isFile ( ) {
15+ if ( ! path ) { return false ; }
16+ return true ;
17+ } ,
18+ } ;
19+ } ) ;
20+ } ) ;
21+
822 it ( 'should return the expected list of files when called' , async ( ) => {
923 execFileMock . mockResolvedValue ( { stdout : 'foo\nbar\n' , stderr : '' } ) ;
1024
@@ -25,4 +39,20 @@ describe('readGit', () => {
2539 } ) ,
2640 ) ;
2741 } ) ;
42+
43+ it ( 'should not return non-files' , async ( ) => {
44+ execFileMock . mockResolvedValue ( { stdout : 'foo\nbar\nbaz\n' , stderr : '' } ) ;
45+ fsMocked . statSync . mockImplementation ( ( path : any ) => {
46+ return {
47+ isFile ( ) {
48+ if ( ! path || path === 'baz' ) { return false ; }
49+ return true ;
50+ } ,
51+ } ;
52+ } ) ;
53+
54+ const result = await readGit ( 'some/dir' ) ;
55+
56+ expect ( result ) . toStrictEqual ( [ 'foo' , 'bar' ] ) ;
57+ } ) ;
2858} ) ;
Original file line number Diff line number Diff line change 1+ import fs , { Stats } from 'fs' ;
12import { exec } from '../util/exec' ;
23
34export const readGit = async ( dir : string ) : Promise < string [ ] > => {
45 const { stdout } = await exec ( 'git ls-files' , { cwd : dir } ) ;
5- return stdout . split ( '\n' ) . filter ( x => ! ! x ) ;
6+ return stdout . split ( '\n' ) . filter ( ( filePath ) => {
7+ let stats : Stats | undefined = undefined ;
8+ try {
9+ stats = fs . statSync ( filePath ) ;
10+ } catch ( e ) {
11+ return false ; // Ignore missing files and symlinks
12+ }
13+
14+ // Ignore if path is not a file
15+ if ( ! stats . isFile ( ) ) {
16+ return false ;
17+ }
18+
19+ return true ;
20+ } ) ;
621} ;
You can’t perform that action at this time.
0 commit comments