11import * as fs from 'fs/promises' ;
2+ import * as fsStream from 'fs' ;
23import * as vscode from 'vscode' ;
3- import { PathLike } from 'fs' ;
4+ import { PathLike , ReadStream } from 'fs' ;
5+ import { Readable } from 'stream' ;
46import * as path from 'path' ;
57import { FileOperations } from '../fileOperations' ;
68import { FileSizeLimitError , ScanError } from '../errors/errors' ;
79import { ScanOptions } from '../types/fileTypes' ;
810
911jest . mock ( 'fs/promises' ) ;
12+ jest . mock ( 'fs' ) ;
1013jest . mock ( '../utils/fileUtils' , ( ) => ( {
1114 isBinaryFile : ( buf : Buffer | string ) => String ( buf ) . includes ( 'BIN' )
1215} ) ) ;
@@ -23,6 +26,7 @@ jest.mock('../utils/fileUtils', () => ({
2326} ) ;
2427
2528const mockedFs = jest . mocked ( fs ) ;
29+ const mockedFsStream = jest . mocked ( fsStream ) ;
2630const ROOT = '/ws' ;
2731const BASE_OPTS : ScanOptions = {
2832 maxFileSize : 1024 * 1024 ,
@@ -47,6 +51,7 @@ beforeEach(() => {
4751 mockedFs . stat . mockReset ( ) ;
4852 mockedFs . readdir . mockReset ( ) ;
4953 mockedFs . readFile . mockReset ( ) ;
54+ mockedFsStream . createReadStream . mockReset ( ) ;
5055 jest . clearAllMocks ( ) ;
5156} ) ;
5257
@@ -68,8 +73,8 @@ describe('FileOperations: basics', () => {
6873 if ( s === path . join ( ROOT , 'src' , 'b' ) ) return [ d ( 'c.js' ) ] ;
6974 return [ ] ;
7075 } ) ;
71- // ③ readFile
72- mockedFs . readFile . mockResolvedValue ( 'DATA' ) ;
76+ // ③ createReadStream
77+ mockedFsStream . createReadStream . mockImplementation ( ( ) => Readable . from ( [ 'DATA' ] ) as unknown as ReadStream ) ;
7378
7479 const res = await fo . scanDirectory ( 'src' , BASE_OPTS ) ;
7580
@@ -93,7 +98,7 @@ describe('FileOperations: filters', () => {
9398 it ( 'excludePatterns に一致するファイルを除外する' , async ( ) => {
9499 mockedFs . stat . mockImplementation ( ( p ) => smartStat ( p ) ) ;
95100 mockedFs . readdir . mockResolvedValue ( [ d ( 'skip.txt' ) , d ( 'keep.txt' ) ] as any [ ] ) ;
96- mockedFs . readFile . mockResolvedValue ( 'C' ) ;
101+ mockedFsStream . createReadStream . mockImplementation ( ( ) => Readable . from ( [ 'C' ] ) as unknown as ReadStream ) ;
97102
98103 const res = await fo . scanDirectory ( '.' , {
99104 ...BASE_OPTS ,
@@ -110,7 +115,7 @@ describe('FileOperations: filters', () => {
110115 return smartStat ( p , tooLarge ? 2_000_000 : 100 ) ;
111116 } ) ;
112117 mockedFs . readdir . mockResolvedValue ( [ d ( 'small.txt' ) , d ( 'large.txt' ) ] as any [ ] ) ;
113- mockedFs . readFile . mockResolvedValue ( 'C' ) ;
118+ mockedFsStream . createReadStream . mockImplementation ( ( ) => Readable . from ( [ 'C' ] ) as unknown as ReadStream ) ;
114119
115120 const res = await fo . scanDirectory ( '.' , BASE_OPTS ) ;
116121 expect ( res . files . map ( f => f . relativePath ) ) . toEqual ( [ 'small.txt' ] ) ;
@@ -119,9 +124,11 @@ describe('FileOperations: filters', () => {
119124 it ( 'バイナリファイルは除外される' , async ( ) => {
120125 mockedFs . stat . mockImplementation ( ( p ) => smartStat ( p ) ) ;
121126 mockedFs . readdir . mockResolvedValue ( [ d ( 'text.txt' ) , d ( 'binary.dat' ) ] as any [ ] ) ;
122- mockedFs . readFile . mockImplementation ( ( async ( p : PathLike ) =>
123- path . basename ( String ( p ) ) . startsWith ( 'binary' ) ? Buffer . from ( 'BIN' ) : Buffer . from ( 'TXT' )
124- ) as any ) ;
127+ mockedFsStream . createReadStream . mockImplementation ( ( p : PathLike ) => {
128+ const base = path . basename ( String ( p ) ) ;
129+ const data = base . startsWith ( 'binary' ) ? 'BIN' : 'TXT' ;
130+ return Readable . from ( [ data ] ) as unknown as ReadStream ;
131+ } ) ;
125132
126133 const res = await fo . scanDirectory ( '.' , BASE_OPTS ) ;
127134 expect ( res . files . map ( f => f . relativePath ) ) . toEqual ( [ 'text.txt' ] ) ;
0 commit comments