Skip to content

Commit 567ccd2

Browse files
authored
Merge pull request #1155 from nellh/filename-validation-bidsignore
Add support for passing .bidsignore to --filename only validation
2 parents 08a74cd + e5d3d7f commit 567ccd2

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

bids-validator/utils/__tests__/filenamesOnly.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe('test filenames mode', () => {
66
})
77
it('throws an error when obviously non-BIDS input', async () => {
88
async function* badData() {
9+
yield '0001'
910
yield 'nope'
1011
yield 'not-bids'
1112
yield 'data'
@@ -15,6 +16,7 @@ describe('test filenames mode', () => {
1516
})
1617
it('passes validation with a simple dataset', async () => {
1718
async function* goodData() {
19+
yield '0001'
1820
yield 'CHANGES'
1921
yield 'dataset_description.json'
2022
yield 'participants.tsv'
@@ -25,4 +27,19 @@ describe('test filenames mode', () => {
2527
const res = await validateFilenames(goodData())
2628
expect(res).toBe(true)
2729
})
30+
it('passes validation with .bidsignore', async () => {
31+
async function* goodData() {
32+
yield 'sub-02/*'
33+
yield '0001'
34+
yield 'CHANGES'
35+
yield 'dataset_description.json'
36+
yield 'participants.tsv'
37+
yield 'README'
38+
yield 'sub-01/anat/sub-01_T1w.nii.gz'
39+
yield 'T1w.json'
40+
yield 'sub-02/not-bids-file.txt'
41+
}
42+
const res = await validateFilenames(goodData())
43+
expect(res).toBe(true)
44+
})
2845
})

bids-validator/utils/filenamesOnly.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import readline from 'readline'
55
import path from 'path'
66
import consoleFormat from './consoleFormat'
7+
import { defaultIgnore } from './files/readDir'
78
import quickTest from '../validators/bids/quickTest'
89
import fullTest from '../validators/bids/fullTest'
910

@@ -21,26 +22,50 @@ const defaultOptions = {
2122
}
2223

2324
async function generateFileObjects(stream) {
25+
const ig = defaultIgnore()
2426
const inputFiles = {}
27+
let bidsIgnore = true
2528
let index = 0
2629
for await (const line of stream) {
27-
const rootPath = `/${line}`
28-
/**
29-
* Simulated file object based on input
30-
* File size is 1 to prevent 0 size errors but makes some checks inaccurate
31-
*/
32-
const file = {
33-
name: path.basename(line),
34-
path: rootPath,
35-
relativePath: rootPath,
36-
size: 1,
30+
// Part 1, parse bidsignore until 0001 (git delimiter packet)
31+
if (line === '0001') {
32+
bidsIgnore = false
33+
} else {
34+
if (bidsIgnore) {
35+
ig.add(line)
36+
} else {
37+
// Done with bidsignore, read filename data
38+
const rootPath = `/${line}`
39+
/**
40+
* Simulated file object based on input
41+
* File size is 1 to prevent 0 size errors but makes some checks inaccurate
42+
*/
43+
const file = {
44+
name: path.basename(line),
45+
path: rootPath,
46+
relativePath: rootPath,
47+
size: 1,
48+
}
49+
if (ig.ignores(rootPath)) {
50+
file.ignore = true
51+
}
52+
inputFiles[index] = file
53+
index++
54+
}
3755
}
38-
inputFiles[index] = file
39-
index++
4056
}
4157
return inputFiles
4258
}
4359

60+
/**
61+
* Validate input from stdin as bidsignore + filenames
62+
*
63+
* Protocol uses `0000` line to separate the two streams
64+
* .bidsignore lines are read first
65+
* One filename per line is read in and bidsignore rules applied
66+
*
67+
* @param {AsyncIterable} stream Readline stream
68+
*/
4469
export async function validateFilenames(stream) {
4570
const inputFiles = await generateFileObjects(stream)
4671
const couldBeBIDS = quickTest(inputFiles)

bids-validator/utils/files/readDir.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,17 @@ async function getFilesFromFs(dir, rootPath, ig, options) {
314314
return filesAccumulator
315315
}
316316

317-
async function getBIDSIgnore(dir) {
318-
const ig = ignore()
317+
export function defaultIgnore() {
318+
return ignore()
319319
.add('.*')
320320
.add('!*.icloud')
321321
.add('/derivatives')
322322
.add('/sourcedata')
323323
.add('/code')
324+
}
325+
326+
async function getBIDSIgnore(dir) {
327+
const ig = defaultIgnore()
324328

325329
const bidsIgnoreFileObj = getBIDSIgnoreFileObj(dir)
326330
if (bidsIgnoreFileObj) {

0 commit comments

Comments
 (0)