1- import mime from " mime"
2- import mock from " mock-fs"
1+ import mime from ' mime'
2+ import mock from ' mock-fs'
33import { afterEach , describe , expect , it , vi } from 'vitest'
4+
45import { get_file_name_from_path , get_mime_type , list_files_in_directory } from './fileUtils'
56
67vi . mock ( 'mime' , ( ) => {
7- return {
8+ return {
89 default : {
9- getType : vi . fn ( )
10- }
10+ getType : vi . fn ( ) ,
11+ } ,
1112 }
1213} )
1314
1415afterEach ( async ( ) => {
1516 mock . restore ( )
16- vi . restoreAllMocks ( ) ;
17+ vi . restoreAllMocks ( )
1718} )
1819
1920describe ( 'get_file_name_from_path' , ( ) => {
@@ -30,49 +31,49 @@ describe('get_file_name_from_path', () => {
3031 expect ( path ) . toBe ( '/birds' )
3132 } )
3233} ) ,
33- describe ( 'list_files_in_directory' , ( ) => {
34- it ( 'lists the files in a directory' , async ( ) => {
35- mock ( {
36- testDir : {
37- "cats" : "aurora, luna" ,
38- "dogs" : "penny" ,
39- }
40- } )
41- const listFiles = await list_files_in_directory ( "testDir" )
42- expect ( listFiles ) . toEqual ( [ "file:///testDir/cats" , "file:///testDir/dogs" ] )
34+ describe ( 'list_files_in_directory' , ( ) => {
35+ it ( 'lists the files in a directory' , async ( ) => {
36+ mock ( {
37+ testDir : {
38+ cats : 'aurora, luna' ,
39+ dogs : 'penny' ,
40+ } ,
41+ } )
42+ const listFiles = await list_files_in_directory ( 'testDir' )
43+ expect ( listFiles ) . toEqual ( [ 'file:///testDir/cats' , 'file:///testDir/dogs' ] )
44+ } ) ,
45+ it ( 'throws an error if path is not a directory' , async ( ) => {
46+ mock ( {
47+ testDir : {
48+ cats : 'aurora, luna' ,
49+ dogs : 'penny' ,
50+ } ,
51+ } )
52+ await expect ( async ( ) => await list_files_in_directory ( 'testDir/cats' ) ) . rejects . toThrow (
53+ 'Failed to read directory'
54+ )
55+ } ) ,
56+ it ( 'treats empty strings as cwd' , async ( ) => {
57+ mock ( {
58+ testDir : {
59+ cats : 'aurora, luna' ,
60+ dogs : 'penny' ,
61+ } ,
62+ } )
63+
64+ const listFiles = await list_files_in_directory ( '' )
65+ expect ( listFiles ) . toEqual ( [ 'file:///../../../../../../testDir' ] )
66+ } )
4367 } ) ,
44- it ( 'throws an error if path is not a directory' , async ( ) => {
45- mock ( {
46- testDir : {
47- "cats" : "aurora, luna" ,
48- "dogs" : "penny" ,
49- }
68+ describe ( 'get_mime_type' , async ( ) => {
69+ it ( "provides the natural mime type when not 'inode/directory'" , async ( ) => {
70+ vi . mocked ( mime . getType ) . mockReturnValueOnce ( 'theType' )
71+ const mimeType = await get_mime_type ( 'someFile' )
72+ expect ( mimeType ) . toEqual ( 'theType' )
5073 } )
51- await expect ( async ( ) => await list_files_in_directory ( "testDir/cats" ) ) . rejects . toThrow ( "Failed to read directory" )
52- } ) ,
53- it ( 'treats empty strings as cwd' , async ( ) => {
54- mock ( {
55- testDir : {
56- "cats" : "aurora, luna" ,
57- "dogs" : "penny" ,
58- }
74+ it ( "overrides mime type for 'inode/directory'" , async ( ) => {
75+ vi . mocked ( mime . getType ) . mockReturnValueOnce ( 'inode/directory' )
76+ const mimeType = await get_mime_type ( 'someDirectory' )
77+ expect ( mimeType ) . toEqual ( 'text/directory' )
5978 } )
60-
61- const listFiles = await list_files_in_directory ( "" )
62- expect ( listFiles ) . toEqual ( [ "file:///../../../../../../testDir" ] )
63- } )
64- } ) ,
65- describe ( "get_mime_type" , async ( ) => {
66- it ( "provides the natural mime type when not 'inode/directory'" , async ( ) => {
67- vi . mocked ( mime . getType ) . mockReturnValueOnce ( "theType" )
68- const mimeType = await get_mime_type ( "someFile" )
69- expect ( mimeType ) . toEqual ( "theType" )
7079 } )
71- it ( "overrides mime type for 'inode/directory'" , async ( ) => {
72- vi . mocked ( mime . getType ) . mockReturnValueOnce ( "inode/directory" )
73- const mimeType = await get_mime_type ( "someDirectory" )
74- expect ( mimeType ) . toEqual ( "text/directory" )
75- } )
76- }
77- )
78-
0 commit comments