Skip to content

Commit

Permalink
Merge pull request #113 from effigies/fix/uniformize-ignore
Browse files Browse the repository at this point in the history
rf(filetree): Uniformize bidsignore reading between web, CLI and test functions
  • Loading branch information
rwblair authored Nov 14, 2024
2 parents 116e7cb + d4f133c commit 5b5c2e2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/files/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class BIDSFileBrowser implements BIDSFile {
export async function fileListToTree(files: File[]): Promise<FileTree> {
const ignore = new FileIgnoreRules([])
const root = new FileTree('/', '/', undefined)
const tree = filesToTree(files.map((f) => new BIDSFileBrowser(f, ignore, root)))
const tree = filesToTree(files.map((f) => new BIDSFileBrowser(f, ignore, root)), ignore)
const bidsignore = tree.get('.bidsignore')
if (bidsignore) {
try {
Expand Down
19 changes: 8 additions & 11 deletions src/files/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,14 @@ async function _readFileTree(
*/
export async function readFileTree(rootPath: string): Promise<FileTree> {
const ignore = new FileIgnoreRules([])
try {
const ignoreFile = new BIDSFileDeno(
rootPath,
'.bidsignore',
ignore,
)
ignore.add(await readBidsIgnore(ignoreFile))
} catch (err) {
if (err && typeof err === 'object' && !('code' in err && err.code === 'ENOENT')) {
logger.error(`Failed to read '.bidsignore' file with the following error:\n${err}`)
const tree = await _readFileTree(rootPath, '/', ignore)
const bidsignore = tree.get('.bidsignore')
if (bidsignore) {
try {
ignore.add(await readBidsIgnore(bidsignore as BIDSFile))
} catch (err) {
console.log(`Failed to read '.bidsignore' file with the following error:\n${err}`)
}
}
return _readFileTree(rootPath, '/', ignore)
return tree
}
17 changes: 16 additions & 1 deletion src/files/filetree.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert, assertEquals } from '@std/assert'
import type { FileIgnoreRules } from './ignore.ts'
import type { FileTree } from '../types/filetree.ts'
import type { BIDSFile, FileTree } from '../types/filetree.ts'
import { filesToTree, pathsToTree } from './filetree.ts'

Deno.test('FileTree generation', async (t) => {
Expand Down Expand Up @@ -50,4 +50,19 @@ Deno.test('FileTree generation', async (t) => {
assert(tree.contains(['sub-01', 'anat']))
assert(tree.contains(['sub-01', 'anat', 'sub-01_T1w.nii.gz']))
})
await t.step('converts a list with ignores', async () => {
const tree = pathsToTree(
['/dataset_description.json', '/README.md', '/.bidsignore', '/bad_file'],
['bad_file'],
)
assertEquals(tree.directories, [])
assertEquals(tree.files.map((f) => f.name), [
'dataset_description.json',
'README.md',
'.bidsignore',
'bad_file',
])
const bad_file = tree.get('bad_file') as BIDSFile
assert(bad_file.ignored)
})
})
16 changes: 9 additions & 7 deletions src/files/filetree.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { parse, SEPARATOR_PATTERN } from '@std/path'
import * as posix from '@std/path/posix'
import { type BIDSFile, FileTree } from '../types/filetree.ts'
import { FileIgnoreRules } from './ignore.ts'

const nullFile = {
size: 0,
ignored: false,
stream: new ReadableStream(),
text: () => Promise.resolve(''),
readBytes: async (size: number, offset?: number) => new Uint8Array(),
parent: new FileTree('', '/'),
viewed: false,
}

export function pathToFile(path: string): BIDSFile {
export function pathToFile(path: string, ignored: boolean = false): BIDSFile {
const name = path.split('/').pop() as string
return { name, path, ...nullFile }
return { name, path, ignored, ...nullFile }
}

export function pathsToTree(paths: string[]): FileTree {
return filesToTree(paths.map(pathToFile))
export function pathsToTree(paths: string[], ignore?: string[]): FileTree {
const ignoreRules = new FileIgnoreRules(ignore ?? [])
return filesToTree(paths.map((path) => pathToFile(path, ignoreRules.test(path))))
}

export function filesToTree(fileList: BIDSFile[]): FileTree {
export function filesToTree(fileList: BIDSFile[], ignore?: FileIgnoreRules): FileTree {
ignore = ignore ?? new FileIgnoreRules([])
const tree: FileTree = new FileTree('/', '/')
for (const file of fileList) {
const parts = parse(file.path)
Expand All @@ -37,7 +39,7 @@ export function filesToTree(fileList: BIDSFile[]): FileTree {
current = exists
continue
}
const newTree = new FileTree(posix.join(current.path, level), level, current)
const newTree = new FileTree(posix.join(current.path, level), level, current, ignore)
current.directories.push(newTree)
current = newTree
}
Expand Down

0 comments on commit 5b5c2e2

Please sign in to comment.