Skip to content

Commit 562248c

Browse files
authored
Merge pull request #816 from nellh/windows-file-tests
Improve readDir tests and fix harmonizeRelativePath for Windows
2 parents 21d044c + 0121e69 commit 562248c

File tree

12 files changed

+226
-94
lines changed

12 files changed

+226
-94
lines changed

bids-validator/tests/bids.spec.js

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,16 @@ function assertErrorCode(errors, expected_error_code) {
5151
assert(matchingErrors.length > 0)
5252
}
5353

54+
// Default validate.BIDS options
55+
const options = { ignoreNiftiHeaders: true, json: true }
56+
const enableNiftiHeaders = { json: true }
57+
5458
describe('BIDS example datasets ', function() {
5559
describe('basic example dataset tests', () => {
5660
getDirectories(
5761
dataDirectory + 'bids-examples-' + global.test_version + '/',
5862
).forEach(function testDataset(path) {
5963
it(path, isdone => {
60-
const options = { ignoreNiftiHeaders: true }
6164
validate.BIDS(createExampleFileList(path), options, function(issues) {
6265
var warnings = issues.warnings
6366
var session_flag = false
@@ -80,7 +83,6 @@ describe('BIDS example datasets ', function() {
8083

8184
// we need to have at least one non-dynamic test
8285
it('validates path without trailing backslash', function(isdone) {
83-
var options = { ignoreNiftiHeaders: true }
8486
validate.BIDS(createExampleFileList('ds001'), options, function(
8587
issues,
8688
summary,
@@ -106,67 +108,69 @@ describe('BIDS example datasets ', function() {
106108

107109
// we need to have at least one non-dynamic test
108110
it('validates dataset with valid nifti headers', function(isdone) {
109-
var options = { ignoreNiftiHeaders: false }
110-
validate.BIDS(createDatasetFileList('valid_headers'), options, function(
111-
issues,
112-
summary,
113-
) {
114-
var errors = issues.errors
115-
var warnings = issues.warnings
116-
assert(summary.sessions.length === 0)
117-
assert(summary.subjects.length === 1)
118-
assert.deepEqual(summary.tasks, ['rhyme judgment'])
119-
assert(summary.modalities.includes('T1w'))
120-
assert(summary.modalities.includes('bold'))
121-
assert(summary.totalFiles === 8)
122-
assert(
123-
errors.findIndex(error => error.code === 60) > -1,
124-
'errors do not contain a code 60',
125-
)
126-
assert.deepEqual(warnings.length, 4)
127-
assert(
128-
warnings.findIndex(warning => warning.code === 13) > -1,
129-
'warnings do not contain a code 13',
130-
)
131-
isdone()
132-
})
111+
validate.BIDS(
112+
createDatasetFileList('valid_headers'),
113+
enableNiftiHeaders,
114+
function(issues, summary) {
115+
var errors = issues.errors
116+
var warnings = issues.warnings
117+
assert(summary.sessions.length === 0)
118+
assert(summary.subjects.length === 1)
119+
assert.deepEqual(summary.tasks, ['rhyme judgment'])
120+
assert(summary.modalities.includes('T1w'))
121+
assert(summary.modalities.includes('bold'))
122+
assert(summary.totalFiles === 8)
123+
assert(
124+
errors.findIndex(error => error.code === 60) > -1,
125+
'errors do not contain a code 60',
126+
)
127+
assert.deepEqual(warnings.length, 4)
128+
assert(
129+
warnings.findIndex(warning => warning.code === 13) > -1,
130+
'warnings do not contain a code 13',
131+
)
132+
isdone()
133+
},
134+
)
133135
})
134136

135137
// test for duplicate files present with both .nii and .nii.gz extension
136138
it('validates dataset for duplicate files present with both .nii and .nii.gz extension', function(isdone) {
137-
var options = { ignoreNiftiHeaders: false }
138-
validate.BIDS(createDatasetFileList('valid_filenames'), options, function(
139-
issues,
140-
) {
141-
assertErrorCode(issues.errors, 74)
142-
isdone()
143-
})
139+
validate.BIDS(
140+
createDatasetFileList('valid_filenames'),
141+
enableNiftiHeaders,
142+
function(issues) {
143+
assertErrorCode(issues.errors, 74)
144+
isdone()
145+
},
146+
)
144147
})
145148

146149
// test for illegal characters used in acq and task name
147150
it('validates dataset with illegal characters in task name', function(isdone) {
148-
var options = { ignoreNiftiHeaders: false }
149-
validate.BIDS(createDatasetFileList('valid_filenames'), options, function(
150-
issues,
151-
) {
152-
assertErrorCode(issues.errors, 58)
153-
isdone()
154-
})
151+
validate.BIDS(
152+
createDatasetFileList('valid_filenames'),
153+
enableNiftiHeaders,
154+
function(issues) {
155+
assertErrorCode(issues.errors, 58)
156+
isdone()
157+
},
158+
)
155159
})
156160

157161
// test for illegal characters used in sub name
158162
it('validates dataset with illegal characters in sub name', function(isdone) {
159-
var options = { ignoreNiftiHeaders: false }
160-
validate.BIDS(createDatasetFileList('valid_filenames'), options, function(
161-
issues,
162-
) {
163-
assertErrorCode(issues.errors, 64)
164-
isdone()
165-
})
163+
validate.BIDS(
164+
createDatasetFileList('valid_filenames'),
165+
enableNiftiHeaders,
166+
function(issues) {
167+
assertErrorCode(issues.errors, 64)
168+
isdone()
169+
},
170+
)
166171
})
167172

168173
it('checks for subjects with no valid data', function(isdone) {
169-
var options = { ignoreNiftiHeaders: true }
170174
validate.BIDS(createDatasetFileList('no_valid_data'), options, function(
171175
issues,
172176
) {
@@ -176,7 +180,6 @@ describe('BIDS example datasets ', function() {
176180
})
177181

178182
it('validates MRI modalities', function(isdone) {
179-
var options = { ignoreNiftiHeaders: true }
180183
validate.BIDS(createExampleFileList('ds001'), options, function(
181184
issues,
182185
summary,
@@ -201,7 +204,6 @@ describe('BIDS example datasets ', function() {
201204
})
202205

203206
it('checks for data dictionaries without corresponding data files', function(isdone) {
204-
var options = { ignoreNiftiHeaders: true }
205207
validate.BIDS(createDatasetFileList('unused_data_dict'), options, function(
206208
issues,
207209
) {
@@ -211,7 +213,6 @@ describe('BIDS example datasets ', function() {
211213
})
212214

213215
it('checks for fieldmaps with no _magnitude file', function(isdone) {
214-
var options = { ignoreNiftiHeaders: true }
215216
validate.BIDS(
216217
createDatasetFileList('fieldmap_without_magnitude'),
217218
options,
@@ -223,7 +224,6 @@ describe('BIDS example datasets ', function() {
223224
})
224225

225226
it('should not throw a warning if all _phasediff.nii are associated with _magnitude1.nii', function(isdone) {
226-
var options = { ignoreNiftiHeaders: true }
227227
validate.BIDS(createExampleFileList('hcp_example_bids'), options, function(
228228
issues,
229229
) {
@@ -233,7 +233,6 @@ describe('BIDS example datasets ', function() {
233233
})
234234

235235
it('should throw a warning if there are _phasediff.nii without an associated _magnitude1.nii', function(isdone) {
236-
var options = { ignoreNiftiHeaders: true }
237236
validate.BIDS(
238237
createDatasetFileList('phasediff_without_magnitude1'),
239238
options,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1.0.0 2011-10-06
2+
3+
- initial release
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
This dataset was obtained from the OpenfMRI project (http://www.openfmri.org).
2+
Accession #: ds003
3+
Description: Rhyme judgment
4+
5+
Release history:
6+
10/06/2011: initial release
7+
3/21/2013: Updated release with QA information
8+
9+
This dataset is made available under the Public Domain Dedication and License
10+
v1.0, whose full text can be found at
11+
http://www.opendatacommons.org/licenses/pddl/1.0/.
12+
We hope that all users will follow the ODC Attribution/Share-Alike
13+
Community Norms (http://www.opendatacommons.org/norms/odc-by-sa/);
14+
in particular, while not legally required, we hope that all users
15+
of the data will acknowledge the OpenfMRI project and NSF Grant
16+
OCI-1131441 (R. Poldrack, PI) in any publications.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"License": "This dataset is made available under the Public Domain Dedication and License \nv1.0, whose full text can be found at \nhttp://www.opendatacommons.org/licenses/pddl/1.0/. \nWe hope that all users will follow the ODC Attribution/Share-Alike \nCommunity Norms (http://www.opendatacommons.org/norms/odc-by-sa/); \nin particular, while not legally required, we hope that all users \nof the data will acknowledge the OpenfMRI project and NSF Grant \nOCI-1131441 (R. Poldrack, PI) in any publications.",
3+
"Name": "Rhyme judgment",
4+
"BIDSVersion": "1.0.1"
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
participant_id sex age
2+
sub-01 M 25
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../valid_filenames/sub-01/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"RepetitionTime": 1.0,
3+
"TaskName": "rhyme judgment"
4+
}

bids-validator/tests/env/load-examples.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ const loadExamples = async () => {
2626
test_version +
2727
'.zip',
2828
)
29-
if (!fs.existsSync('bids-validator/tests/data')) {
30-
fs.mkdirSync('bids-validator/tests/data')
31-
}
3229
fs.writeFileSync('bids-validator/tests/data/examples.zip', response.body)
3330
const zip = new AdmZip('bids-validator/tests/data/examples.zip')
3431
console.log('unzipping test data')
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @jest-environment ./bids-validator/tests/env/ExamplesEnvironment.js
3+
*/
4+
const readDir = require('../readDir.js').default
5+
6+
describe('readDir.js - examples integration', () => {
7+
describe('readDir()', () => {
8+
it('returns expected files', async done => {
9+
readDir('bids-validator/tests/data/bids-examples-1.2.0/ds002/').then(
10+
files => {
11+
expect(Object.keys(files)).toHaveLength(245)
12+
expect(files[0].name).toBe('CHANGES')
13+
expect(files[25].name).toBe(
14+
'sub-02_task-mixedeventrelatedprobe_run-01_events.tsv',
15+
)
16+
expect(files[200].name).toBe('sub-15_T1w.nii.gz')
17+
done()
18+
},
19+
)
20+
})
21+
it('correctly follows symlinks for subjects with followSymbolicLink: true', async done => {
22+
readDir('bids-validator/tests/data/symlinked_subject', {
23+
followSymbolicDirectories: true,
24+
}).then(files => {
25+
expect(Object.keys(files)).toHaveLength(12)
26+
expect(Object.values(files).map(f => f.name)).toEqual([
27+
'CHANGES',
28+
'README',
29+
'dataset_description.json',
30+
'participants.tsv',
31+
'sub-01_T1w.nii',
32+
'sub-01_T1w.nii.gz',
33+
'sub-0-1_task-rhymejudgment_bold.nii.gz',
34+
'sub-01_task-rhyme-judgment_bold.nii.gz',
35+
'sub-01_task-rhyme-judgment_events.tsv',
36+
'sub-01_task-rhyme_judgment_bold.nii.gz',
37+
'sub-01_task-rhyme_judgment_events.tsv',
38+
'task-rhymejudgment_bold.json',
39+
])
40+
done()
41+
})
42+
})
43+
it('correctly does not follow symlinks for subjects with followSymbolicLink: false', async done => {
44+
readDir('bids-validator/tests/data/symlinked_subject', {
45+
followSymbolicDirectories: false,
46+
}).then(files => {
47+
expect(Object.keys(files)).toHaveLength(6)
48+
expect(Object.values(files).map(f => f.name)).toEqual([
49+
'CHANGES',
50+
'README',
51+
'dataset_description.json',
52+
'participants.tsv',
53+
'sub-01',
54+
'task-rhymejudgment_bold.json',
55+
])
56+
done()
57+
})
58+
})
59+
})
60+
})
Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
1-
/**
2-
* @jest-environment ./bids-validator/tests/env/ExamplesEnvironment.js
3-
*/
41
const readDir = require('../readDir.js')
52

63
describe('readDir.js', () => {
7-
describe('getFiles()', () => {
8-
it('returns expected files', async done => {
9-
readDir('bids-validator/tests/data/bids-examples-1.2.0/ds002/').then(
10-
files => {
11-
expect(Object.keys(files)).toHaveLength(245)
12-
expect(files[0].name).toBe('CHANGES')
13-
expect(files[25].name).toBe(
14-
'sub-02_task-mixedeventrelatedprobe_run-01_events.tsv',
15-
)
16-
expect(files[200].name).toBe('sub-15_T1w.nii.gz')
17-
done()
4+
describe('fileArrayToObject', () => {
5+
it('transforms an array to an object', () => {
6+
expect(
7+
readDir.fileArrayToObject([
8+
{ name: 'one' },
9+
{ name: 'two' },
10+
{ name: 'three' },
11+
]),
12+
).toEqual({
13+
'0': {
14+
name: 'one',
1815
},
16+
'1': {
17+
name: 'two',
18+
},
19+
'2': {
20+
name: 'three',
21+
},
22+
})
23+
})
24+
})
25+
describe('harmonizeRelativePath', () => {
26+
it('harmonizes a basic POSIX path', () => {
27+
expect(readDir.harmonizeRelativePath('test/a/path')).toEqual('/a/path')
28+
})
29+
it('does not mangle absolute Windows paths', () => {
30+
expect(readDir.harmonizeRelativePath('C:\\dataset\\directory')).toEqual(
31+
'/dataset/directory',
32+
)
33+
})
34+
it('does not mangle relative Windows paths', () => {
35+
expect(readDir.harmonizeRelativePath('dataset\\directory')).toEqual(
36+
'/directory',
1937
)
2038
})
39+
it('does not mangle relative Windows paths with parent directories', () => {
40+
expect(
41+
readDir.harmonizeRelativePath('..\\..\\dataset\\directory'),
42+
).toEqual('/../dataset/directory')
43+
})
2144
})
2245
})

0 commit comments

Comments
 (0)