11import { type assert , assertObjectMatch } from '@std/assert'
22import type { BIDSFile } from '../types/filetree.ts'
33import type { FileIgnoreRules } from './ignore.ts'
4+ import { testAsyncFileAccess } from './access.test.ts'
45
6+ import { pathsToTree } from '../files/filetree.ts'
57import { loadJSON } from './json.ts'
68
79function encodeUTF16 ( text : string ) {
@@ -17,9 +19,12 @@ function encodeUTF16(text: string) {
1719 return buffer
1820}
1921
20- function makeFile ( text : string , encoding : string ) : BIDSFile {
22+ function makeFile ( path : string , text : string , encoding : string ) : BIDSFile {
2123 const bytes = encoding === 'utf-8' ? new TextEncoder ( ) . encode ( text ) : encodeUTF16 ( text )
24+ const file = pathsToTree ( [ path ] ) . get ( path ) as BIDSFile
2225 return {
26+ path : file . path ,
27+ parent : file . parent ,
2328 readBytes : async ( size : number ) => {
2429 return new Uint8Array ( bytes )
2530 } ,
@@ -29,13 +34,13 @@ function makeFile(text: string, encoding: string): BIDSFile {
2934
3035Deno . test ( 'Test JSON error conditions' , async ( t ) => {
3136 await t . step ( 'Load valid JSON' , async ( ) => {
32- const JSONfile = makeFile ( '{"a": 1}' , 'utf-8' )
37+ const JSONfile = makeFile ( '/valid-contents.json' , ' {"a": 1}', 'utf-8' )
3338 const result = await loadJSON ( JSONfile )
3439 assertObjectMatch ( result , { a : 1 } )
3540 } )
3641
3742 await t . step ( 'Error on BOM' , async ( ) => {
38- const BOMfile = makeFile ( '\uFEFF{"a": 1}' , 'utf-8' )
43+ const BOMfile = makeFile ( '/BOM.json' , ' \uFEFF{"a": 1}', 'utf-8' )
3944 let error : any = undefined
4045 await loadJSON ( BOMfile ) . catch ( ( e ) => {
4146 error = e
@@ -44,7 +49,7 @@ Deno.test('Test JSON error conditions', async (t) => {
4449 } )
4550
4651 await t . step ( 'Error on UTF-16' , async ( ) => {
47- const UTF16file = makeFile ( '{"a": 1}' , 'utf-16' )
52+ const UTF16file = makeFile ( '/utf16.json' , ' {"a": 1}', 'utf-16' )
4853 let error : any = undefined
4954 await loadJSON ( UTF16file ) . catch ( ( e ) => {
5055 error = e
@@ -53,11 +58,14 @@ Deno.test('Test JSON error conditions', async (t) => {
5358 } )
5459
5560 await t . step ( 'Error on invalid JSON syntax' , async ( ) => {
56- const badJSON = makeFile ( '{"a": 1]' , 'utf-8' )
61+ const badJSON = makeFile ( '/bad-syntax.json' , ' {"a": 1]', 'utf-8' )
5762 let error : any = undefined
5863 await loadJSON ( badJSON ) . catch ( ( e ) => {
5964 error = e
6065 } )
6166 assertObjectMatch ( error , { code : 'JSON_INVALID' } )
6267 } )
68+ loadJSON . cache . clear ( )
6369} )
70+
71+ testAsyncFileAccess ( 'Test file access errors for loadJSON' , loadJSON )
0 commit comments