Skip to content

Commit

Permalink
Merge pull request #2151 from effigies/fix/web-ignore
Browse files Browse the repository at this point in the history
fix: Load .bidsignore in web app
  • Loading branch information
rwblair authored Oct 7, 2024
2 parents de650a2 + 7591b82 commit ae54efa
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
60 changes: 57 additions & 3 deletions bids-validator/src/files/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 () => {
Expand All @@ -89,7 +143,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)
Expand Down
13 changes: 9 additions & 4 deletions bids-validator/src/files/browser.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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<FileTree> {
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
}
2 changes: 1 addition & 1 deletion web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, {}))
}

Expand Down

0 comments on commit ae54efa

Please sign in to comment.