From 0b36291251293821d101bd61ad713f00159af190 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Mon, 7 Oct 2024 10:52:35 -0400 Subject: [PATCH 1/2] fix: Load .bidsignore in web app --- bids-validator/src/files/browser.test.ts | 6 +++--- bids-validator/src/files/browser.ts | 13 +++++++++---- web/src/App.tsx | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/bids-validator/src/files/browser.test.ts b/bids-validator/src/files/browser.test.ts index e0633d063..f0badf1e5 100644 --- a/bids-validator/src/files/browser.test.ts +++ b/bids-validator/src/files/browser.test.ts @@ -27,7 +27,7 @@ Deno.test('Browser implementation of FileTree', async (t) => { ), new TestFile(['flat test dataset'], 'README.md', 'ds/README.md'), ] - const tree = fileListToTree(files) + const tree = await fileListToTree(files) const expectedTree = new FileTree('/', '/', undefined) expectedTree.files = files.map((f) => { const file = new BIDSFileBrowser(f, ignore) @@ -60,7 +60,7 @@ Deno.test('Browser implementation of FileTree', async (t) => { 'ds/sub-01/anat/sub-01_T1w.nii.gz', ), ] - const tree = fileListToTree(files) + const tree = await fileListToTree(files) const expectedTree = new FileTree('/', '/', undefined) const sub01Tree = new FileTree('/sub-01', 'sub-01', expectedTree) const anatTree = new FileTree('/sub-01/anat', 'anat', sub01Tree) @@ -89,7 +89,7 @@ Deno.test('Spread copies of BIDSFileBrowser contain name and path properties', a ), new TestFile(['flat test dataset'], 'README.md', 'ds/README.md'), ] - const tree = fileListToTree(files) + const tree = await fileListToTree(files) const expectedTree = new FileTree('/', '/', undefined) expectedTree.files = files.map((f) => { const file = new BIDSFileBrowser(f, ignore) diff --git a/bids-validator/src/files/browser.ts b/bids-validator/src/files/browser.ts index 26e0dcb50..256f5c46f 100644 --- a/bids-validator/src/files/browser.ts +++ b/bids-validator/src/files/browser.ts @@ -1,6 +1,6 @@ import { type BIDSFile, FileTree } from '../types/filetree.ts' import { filesToTree } from './filetree.ts' -import { FileIgnoreRules } from './ignore.ts' +import { FileIgnoreRules, readBidsIgnore } from './ignore.ts' import { parse, SEPARATOR_PATTERN } from '@std/path' import * as posix from '@std/path/posix' @@ -49,8 +49,13 @@ export class BIDSFileBrowser implements BIDSFile { /** * Convert from FileList (created with webkitDirectory: true) to FileTree for validator use */ -export function fileListToTree(files: File[]): FileTree { +export async function fileListToTree(files: File[]): Promise { const ignore = new FileIgnoreRules([]) - const tree = new FileTree('/', '/', undefined) - return filesToTree(files.map((f) => new BIDSFileBrowser(f, ignore, tree))) + const root = new FileTree('/', '/', undefined) + const tree = filesToTree(files.map((f) => new BIDSFileBrowser(f, ignore, root))) + const bidsignore = tree.get('.bidsignore') + if (bidsignore) { + ignore.add(await readBidsIgnore(bidsignore as BIDSFile)) + } + return tree } diff --git a/web/src/App.tsx b/web/src/App.tsx index db63b519c..657eaf044 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -52,7 +52,7 @@ function App() { const dirHandle = await directoryOpen({ recursive: true, }) - const fileTree = fileListToTree(dirHandle) + const fileTree = await fileListToTree(dirHandle) setValidation(await validate(fileTree, {})) } From 7591b82bd231a7db6afb7c813e9659c0a937c59a Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Mon, 7 Oct 2024 11:09:16 -0400 Subject: [PATCH 2/2] test: Check .bidsignore is applied during browser filetree load --- bids-validator/src/files/browser.test.ts | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/bids-validator/src/files/browser.test.ts b/bids-validator/src/files/browser.test.ts index f0badf1e5..7486ff4a2 100644 --- a/bids-validator/src/files/browser.test.ts +++ b/bids-validator/src/files/browser.test.ts @@ -77,6 +77,60 @@ Deno.test('Browser implementation of FileTree', async (t) => { sub01Tree.directories.push(anatTree) assertEquals(tree, expectedTree) }) + + await t.step('reads .bidsignore during load', async () => { + const ignore = new FileIgnoreRules([]) + const files = [ + new TestFile( + ['ignored_but_absent\n', 'ignored_and_present\n'], + '.bidsignore', + 'ds/.bidsignore', + ), + new TestFile( + ['{}'], + 'dataset_description.json', + 'ds/dataset_description.json', + ), + new TestFile( + ['tsv headers\n', 'column\tdata'], + 'participants.tsv', + 'ds/participants.tsv', + ), + new TestFile( + ['single subject test dataset'], + 'README.md', + 'ds/README.md', + ), + new TestFile( + ['Anything can be in an ignored file'], + 'ignored_and_present', + 'ds/ignored_and_present', + ), + new TestFile( + ['nifti file goes here'], + 'sub-01_T1w.nii.gz', + 'ds/sub-01/anat/sub-01_T1w.nii.gz', + ), + ] + const tree = await fileListToTree(files) + const expectedTree = new FileTree('/', '/', undefined) + const sub01Tree = new FileTree('/sub-01', 'sub-01', expectedTree) + const anatTree = new FileTree('/sub-01/anat', 'anat', sub01Tree) + expectedTree.files = files + .slice(0, 5) + .map((f) => { + const file = new BIDSFileBrowser(f, ignore) + file.parent = expectedTree + return file + }) + expectedTree.directories.push(sub01Tree) + anatTree.files = [new BIDSFileBrowser(files[5], ignore)] + anatTree.files[0].parent = anatTree + sub01Tree.directories.push(anatTree) + assertEquals(tree, expectedTree) + + assertEquals(tree.get('ignored_and_present')?.ignored, true) + }) }) Deno.test('Spread copies of BIDSFileBrowser contain name and path properties', async () => {