Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve readDir tests and fix harmonizeRelativePath for Windows #816

Merged
merged 11 commits into from
Aug 8, 2019
105 changes: 52 additions & 53 deletions bids-validator/tests/bids.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ function assertErrorCode(errors, expected_error_code) {
assert(matchingErrors.length > 0)
}

// Default validate.BIDS options
const options = { ignoreNiftiHeaders: true, json: true }
const enableNiftiHeaders = { json: true }

describe('BIDS example datasets ', function() {
describe('basic example dataset tests', () => {
getDirectories(
dataDirectory + 'bids-examples-' + global.test_version + '/',
).forEach(function testDataset(path) {
it(path, isdone => {
const options = { ignoreNiftiHeaders: true }
validate.BIDS(createExampleFileList(path), options, function(issues) {
var warnings = issues.warnings
var session_flag = false
Expand All @@ -80,7 +83,6 @@ describe('BIDS example datasets ', function() {

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

// we need to have at least one non-dynamic test
it('validates dataset with valid nifti headers', function(isdone) {
var options = { ignoreNiftiHeaders: false }
validate.BIDS(createDatasetFileList('valid_headers'), options, function(
issues,
summary,
) {
var errors = issues.errors
var warnings = issues.warnings
assert(summary.sessions.length === 0)
assert(summary.subjects.length === 1)
assert.deepEqual(summary.tasks, ['rhyme judgment'])
assert(summary.modalities.includes('T1w'))
assert(summary.modalities.includes('bold'))
assert(summary.totalFiles === 8)
assert(
errors.findIndex(error => error.code === 60) > -1,
'errors do not contain a code 60',
)
assert.deepEqual(warnings.length, 4)
assert(
warnings.findIndex(warning => warning.code === 13) > -1,
'warnings do not contain a code 13',
)
isdone()
})
validate.BIDS(
createDatasetFileList('valid_headers'),
enableNiftiHeaders,
function(issues, summary) {
var errors = issues.errors
var warnings = issues.warnings
assert(summary.sessions.length === 0)
assert(summary.subjects.length === 1)
assert.deepEqual(summary.tasks, ['rhyme judgment'])
assert(summary.modalities.includes('T1w'))
assert(summary.modalities.includes('bold'))
assert(summary.totalFiles === 8)
assert(
errors.findIndex(error => error.code === 60) > -1,
'errors do not contain a code 60',
)
assert.deepEqual(warnings.length, 4)
assert(
warnings.findIndex(warning => warning.code === 13) > -1,
'warnings do not contain a code 13',
)
isdone()
},
)
})

// test for duplicate files present with both .nii and .nii.gz extension
it('validates dataset for duplicate files present with both .nii and .nii.gz extension', function(isdone) {
var options = { ignoreNiftiHeaders: false }
validate.BIDS(createDatasetFileList('valid_filenames'), options, function(
issues,
) {
assertErrorCode(issues.errors, 74)
isdone()
})
validate.BIDS(
createDatasetFileList('valid_filenames'),
enableNiftiHeaders,
function(issues) {
assertErrorCode(issues.errors, 74)
isdone()
},
)
})

// test for illegal characters used in acq and task name
it('validates dataset with illegal characters in task name', function(isdone) {
var options = { ignoreNiftiHeaders: false }
validate.BIDS(createDatasetFileList('valid_filenames'), options, function(
issues,
) {
assertErrorCode(issues.errors, 58)
isdone()
})
validate.BIDS(
createDatasetFileList('valid_filenames'),
enableNiftiHeaders,
function(issues) {
assertErrorCode(issues.errors, 58)
isdone()
},
)
})

// test for illegal characters used in sub name
it('validates dataset with illegal characters in sub name', function(isdone) {
var options = { ignoreNiftiHeaders: false }
validate.BIDS(createDatasetFileList('valid_filenames'), options, function(
issues,
) {
assertErrorCode(issues.errors, 64)
isdone()
})
validate.BIDS(
createDatasetFileList('valid_filenames'),
enableNiftiHeaders,
function(issues) {
assertErrorCode(issues.errors, 64)
isdone()
},
)
})

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

it('validates MRI modalities', function(isdone) {
var options = { ignoreNiftiHeaders: true }
validate.BIDS(createExampleFileList('ds001'), options, function(
issues,
summary,
Expand All @@ -201,7 +204,6 @@ describe('BIDS example datasets ', function() {
})

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

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

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

it('should throw a warning if there are _phasediff.nii without an associated _magnitude1.nii', function(isdone) {
var options = { ignoreNiftiHeaders: true }
validate.BIDS(
createDatasetFileList('phasediff_without_magnitude1'),
options,
Expand Down
3 changes: 3 additions & 0 deletions bids-validator/tests/data/symlinked_subject/CHANGES
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1.0.0 2011-10-06

- initial release
16 changes: 16 additions & 0 deletions bids-validator/tests/data/symlinked_subject/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
This dataset was obtained from the OpenfMRI project (http://www.openfmri.org).
Accession #: ds003
Description: Rhyme judgment

Release history:
10/06/2011: initial release
3/21/2013: Updated release with QA information

This dataset is made available under the Public Domain Dedication and License
v1.0, whose full text can be found at
http://www.opendatacommons.org/licenses/pddl/1.0/.
We hope that all users will follow the ODC Attribution/Share-Alike
Community Norms (http://www.opendatacommons.org/norms/odc-by-sa/);
in particular, while not legally required, we hope that all users
of the data will acknowledge the OpenfMRI project and NSF Grant
OCI-1131441 (R. Poldrack, PI) in any publications.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"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.",
"Name": "Rhyme judgment",
"BIDSVersion": "1.0.1"
}
2 changes: 2 additions & 0 deletions bids-validator/tests/data/symlinked_subject/participants.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
participant_id sex age
sub-01 M 25
1 change: 1 addition & 0 deletions bids-validator/tests/data/symlinked_subject/sub-01
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"RepetitionTime": 1.0,
"TaskName": "rhyme judgment"
}
3 changes: 0 additions & 3 deletions bids-validator/tests/env/load-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ const loadExamples = async () => {
test_version +
'.zip',
)
if (!fs.existsSync('bids-validator/tests/data')) {
fs.mkdirSync('bids-validator/tests/data')
}
fs.writeFileSync('bids-validator/tests/data/examples.zip', response.body)
const zip = new AdmZip('bids-validator/tests/data/examples.zip')
console.log('unzipping test data')
Expand Down
60 changes: 60 additions & 0 deletions bids-validator/utils/files/__tests__/readDir-examples.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @jest-environment ./bids-validator/tests/env/ExamplesEnvironment.js
*/
const readDir = require('../readDir.js').default

describe('readDir.js - examples integration', () => {
describe('readDir()', () => {
it('returns expected files', async done => {
readDir('bids-validator/tests/data/bids-examples-1.2.0/ds002/').then(
files => {
expect(Object.keys(files)).toHaveLength(245)
expect(files[0].name).toBe('CHANGES')
expect(files[25].name).toBe(
'sub-02_task-mixedeventrelatedprobe_run-01_events.tsv',
)
expect(files[200].name).toBe('sub-15_T1w.nii.gz')
done()
},
)
})
it('correctly follows symlinks for subjects with followSymbolicLink: true', async done => {
readDir('bids-validator/tests/data/symlinked_subject', {
followSymbolicDirectories: true,
}).then(files => {
expect(Object.keys(files)).toHaveLength(12)
expect(Object.values(files).map(f => f.name)).toEqual([
'CHANGES',
'README',
'dataset_description.json',
'participants.tsv',
'sub-01_T1w.nii',
'sub-01_T1w.nii.gz',
'sub-0-1_task-rhymejudgment_bold.nii.gz',
'sub-01_task-rhyme-judgment_bold.nii.gz',
'sub-01_task-rhyme-judgment_events.tsv',
'sub-01_task-rhyme_judgment_bold.nii.gz',
'sub-01_task-rhyme_judgment_events.tsv',
'task-rhymejudgment_bold.json',
])
done()
})
})
it('correctly does not follow symlinks for subjects with followSymbolicLink: false', async done => {
readDir('bids-validator/tests/data/symlinked_subject', {
followSymbolicDirectories: false,
}).then(files => {
expect(Object.keys(files)).toHaveLength(6)
expect(Object.values(files).map(f => f.name)).toEqual([
'CHANGES',
'README',
'dataset_description.json',
'participants.tsv',
'sub-01',
'task-rhymejudgment_bold.json',
])
done()
})
})
})
})
51 changes: 37 additions & 14 deletions bids-validator/utils/files/__tests__/readDir.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
/**
* @jest-environment ./bids-validator/tests/env/ExamplesEnvironment.js
*/
const readDir = require('../readDir.js')

describe('readDir.js', () => {
describe('getFiles()', () => {
it('returns expected files', async done => {
readDir('bids-validator/tests/data/bids-examples-1.2.0/ds002/').then(
files => {
expect(Object.keys(files)).toHaveLength(245)
expect(files[0].name).toBe('CHANGES')
expect(files[25].name).toBe(
'sub-02_task-mixedeventrelatedprobe_run-01_events.tsv',
)
expect(files[200].name).toBe('sub-15_T1w.nii.gz')
done()
describe('fileArrayToObject', () => {
it('transforms an array to an object', () => {
expect(
readDir.fileArrayToObject([
{ name: 'one' },
{ name: 'two' },
{ name: 'three' },
]),
).toEqual({
'0': {
name: 'one',
},
'1': {
name: 'two',
},
'2': {
name: 'three',
},
})
})
})
describe('harmonizeRelativePath', () => {
it('harmonizes a basic POSIX path', () => {
expect(readDir.harmonizeRelativePath('test/a/path')).toEqual('/a/path')
})
it('does not mangle absolute Windows paths', () => {
expect(readDir.harmonizeRelativePath('C:\\dataset\\directory')).toEqual(
'/dataset/directory',
)
})
it('does not mangle relative Windows paths', () => {
expect(readDir.harmonizeRelativePath('dataset\\directory')).toEqual(
'/directory',
)
})
it('does not mangle relative Windows paths with parent directories', () => {
expect(
readDir.harmonizeRelativePath('..\\..\\dataset\\directory'),
).toEqual('/../dataset/directory')
})
})
})
2 changes: 1 addition & 1 deletion bids-validator/utils/files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const FileAPI = require('./FileAPI')
const newFile = require('./newFile')
const readFile = require('./readFile')
const readNiftiHeader = require('./readNiftiHeader')
const readDir = require('./readDir')
const readDir = require('./readDir').default
const potentialLocations = require('./potentialLocations')
const generateMergedSidecarDict = require('./generateMergedSidecarDict')
const getBFileContent = require('./getBFileContent')
Expand Down
Loading