@@ -3,6 +3,7 @@ import type { BIDSFile } from '../types/filetree.ts'
33import type { FileIgnoreRules } from './ignore.ts'
44import { testAsyncFileAccess } from './access.test.ts'
55
6+ import { pathsToTree } from '../files/filetree.ts'
67import { loadJSON } from './json.ts'
78
89function encodeUTF16 ( text : string ) {
@@ -18,9 +19,12 @@ function encodeUTF16(text: string) {
1819 return buffer
1920}
2021
21- function makeFile ( text : string , encoding : string ) : BIDSFile {
22+ function makeFile ( path : string , text : string , encoding : string ) : BIDSFile {
2223 const bytes = encoding === 'utf-8' ? new TextEncoder ( ) . encode ( text ) : encodeUTF16 ( text )
24+ const file = pathsToTree ( [ path ] ) . get ( path ) as BIDSFile
2325 return {
26+ path : file . path ,
27+ parent : file . parent ,
2428 readBytes : async ( size : number ) => {
2529 return new Uint8Array ( bytes )
2630 } ,
@@ -30,13 +34,13 @@ function makeFile(text: string, encoding: string): BIDSFile {
3034
3135Deno . test ( 'Test JSON error conditions' , async ( t ) => {
3236 await t . step ( 'Load valid JSON' , async ( ) => {
33- const JSONfile = makeFile ( '{"a": 1}' , 'utf-8' )
37+ const JSONfile = makeFile ( '/valid-contents.json' , ' {"a": 1}', 'utf-8' )
3438 const result = await loadJSON ( JSONfile )
3539 assertObjectMatch ( result , { a : 1 } )
3640 } )
3741
3842 await t . step ( 'Error on BOM' , async ( ) => {
39- const BOMfile = makeFile ( '\uFEFF{"a": 1}' , 'utf-8' )
43+ const BOMfile = makeFile ( '/BOM.json' , ' \uFEFF{"a": 1}', 'utf-8' )
4044 let error : any = undefined
4145 await loadJSON ( BOMfile ) . catch ( ( e ) => {
4246 error = e
@@ -45,7 +49,7 @@ Deno.test('Test JSON error conditions', async (t) => {
4549 } )
4650
4751 await t . step ( 'Error on UTF-16' , async ( ) => {
48- const UTF16file = makeFile ( '{"a": 1}' , 'utf-16' )
52+ const UTF16file = makeFile ( '/utf16.json' , ' {"a": 1}', 'utf-16' )
4953 let error : any = undefined
5054 await loadJSON ( UTF16file ) . catch ( ( e ) => {
5155 error = e
@@ -54,13 +58,14 @@ Deno.test('Test JSON error conditions', async (t) => {
5458 } )
5559
5660 await t . step ( 'Error on invalid JSON syntax' , async ( ) => {
57- const badJSON = makeFile ( '{"a": 1]' , 'utf-8' )
61+ const badJSON = makeFile ( '/bad-syntax.json' , ' {"a": 1]', 'utf-8' )
5862 let error : any = undefined
5963 await loadJSON ( badJSON ) . catch ( ( e ) => {
6064 error = e
6165 } )
6266 assertObjectMatch ( error , { code : 'JSON_INVALID' } )
6367 } )
68+ loadJSON . cache . clear ( )
6469} )
6570
6671testAsyncFileAccess ( 'Test file access errors for loadJSON' , loadJSON )
0 commit comments