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

fix: OSX and Windows tests #1989

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bids-validator/src/deps/fs.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { walk } from 'https://deno.land/[email protected]/fs/walk.ts'
export { walk, EOL } from 'https://deno.land/[email protected]/fs/mod.ts'
3 changes: 2 additions & 1 deletion bids-validator/src/deps/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ export {
basename,
dirname,
extname,
posix,
fromFileUrl,
parse,
SEPARATOR,
SEPARATOR_PATTERN,
} from 'https://deno.land/[email protected]/path/mod.ts'
export {
globToRegExp,
Expand Down
6 changes: 3 additions & 3 deletions bids-validator/src/files/browser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BIDSFile } from '../types/file.ts'
import { FileTree } from '../types/filetree.ts'
import { FileIgnoreRules } from './ignore.ts'
import { parse, join, SEPARATOR } from '../deps/path.ts'
import { parse, posix, SEPARATOR_PATTERN } from '../deps/path.ts'

/**
* Browser implement of BIDSFile wrapping native File/FileList types
Expand Down Expand Up @@ -56,7 +56,7 @@ export function fileListToTree(files: File[]): Promise<FileTree> {
// Top level file
tree.files.push(file)
} else {
const levels = fPath.dir.split(SEPARATOR).slice(1)
const levels = fPath.dir.split(SEPARATOR_PATTERN).slice(1)
let currentLevelTree = tree
for (const level of levels) {
const exists = currentLevelTree.directories.find(
Expand All @@ -68,7 +68,7 @@ export function fileListToTree(files: File[]): Promise<FileTree> {
} else {
// Otherwise make a new level and continue if needed
const newTree = new FileTree(
join(currentLevelTree.path, level),
posix.join(currentLevelTree.path, level),
level,
currentLevelTree,
)
Expand Down
26 changes: 21 additions & 5 deletions bids-validator/src/files/deno.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { assertEquals, assertRejects } from '../deps/asserts.ts'
import { assert, assertEquals, assertRejects } from '../deps/asserts.ts'
import { readAll, readerFromStreamReader } from '../deps/stream.ts'
import { dirname, basename, join } from '../deps/path.ts'
import { BIDSFileDeno, UnicodeDecodeError } from './deno.ts'
import { dirname, basename, join, fromFileUrl } from '../deps/path.ts'
import { EOL } from '../deps/fs.ts'
import { BIDSFileDeno, readFileTree, UnicodeDecodeError } from './deno.ts'
import { requestReadPermission } from '../setup/requestPermissions.ts'
import { FileIgnoreRules } from './ignore.ts'

await requestReadPermission()

// Use this file for testing file behavior
const testUrl = import.meta.url
const testPath = testUrl.slice('file://'.length)
const testPath = fromFileUrl(testUrl)
const testDir = dirname(testPath)
const testFilename = basename(testPath)
const ignore = new FileIgnoreRules([])
Expand Down Expand Up @@ -55,7 +56,22 @@ Deno.test('Deno implementation of BIDSFile', async (t) => {
const bomFilename = 'bom-utf8.json'
const file = new BIDSFileDeno(bomDir, bomFilename, ignore)
const text = await file.text()
assertEquals(text, '{\n "example": "JSON for test suite"\n}\n')
assertEquals(text, ['{', ' "example": "JSON for test suite"', '}', ''].join(EOL))
},
)
})

Deno.test('Deno implementation of FileTree', async (t) => {
const srcdir = dirname(testDir)
const parent = basename(testDir)
const tree = await readFileTree(srcdir)
await t.step('uses POSIX relative paths', async () => {
assertEquals(tree.path, '/')
const parentObj = tree.directories.find((dir) => dir.name === parent)
assert(parentObj !== undefined)
assertEquals(parentObj.path, `/${parent}`)
const testObj = parentObj.files.find((file) => file.name === testFilename)
assert(testObj !== undefined)
assertEquals(testObj.path, `/${parent}/${testFilename}`)
})
})
6 changes: 3 additions & 3 deletions bids-validator/src/files/deno.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Deno specific implementation for reading files
*/
import { join, basename } from '../deps/path.ts'
import { posix, join, basename } from '../deps/path.ts'
import { BIDSFile } from '../types/file.ts'
import { FileTree } from '../types/filetree.ts'
import { requestReadPermission } from '../setup/requestPermissions.ts'
Expand Down Expand Up @@ -122,7 +122,7 @@ export async function _readFileTree(
if (dirEntry.isFile || dirEntry.isSymlink) {
const file = new BIDSFileDeno(
rootPath,
join(relativePath, dirEntry.name),
posix.join(relativePath, dirEntry.name),
ignore,
)
// For .bidsignore, read in immediately and add the rules
Expand All @@ -134,7 +134,7 @@ export async function _readFileTree(
if (dirEntry.isDirectory) {
const dirTree = await _readFileTree(
rootPath,
join(relativePath, dirEntry.name),
posix.join(relativePath, dirEntry.name),
ignore,
tree,
)
Expand Down
10 changes: 8 additions & 2 deletions bids-validator/src/validators/filenameIdentify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ Deno.test('test hasMatch', async (t) => {
})

await t.step('No match', async () => {
const fileName = Deno.makeTempFileSync().split('/')[2]
const file = new BIDSFileDeno('/tmp', fileName, ignore)
const tmpFile = Deno.makeTempFileSync()
const parts = tmpFile.split('/')
const file = new BIDSFileDeno(
parts.slice(0, parts.length - 1).join('/'),
parts[parts.length - 1],
ignore,
)

const context = new BIDSContext(fileTree, file, issues)
await hasMatch(schema, context)
Expand All @@ -86,6 +91,7 @@ Deno.test('test hasMatch', async (t) => {
.includes('NOT_INCLUDED'),
true,
)
Deno.removeSync(tmpFile)
})
await t.step('2 matches, no pruning', async () => {
const path = `${PATH}/../bids-examples/fnirs_automaticity`
Expand Down
4 changes: 2 additions & 2 deletions bids-validator/src/validators/filenameIdentify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* object in the schema for reference.
*/
// @ts-nocheck
import { SEPARATOR, globToRegExp } from '../deps/path.ts'
import { SEPARATOR_PATTERN, globToRegExp } from '../deps/path.ts'
import { GenericSchema, Schema } from '../types/schema.ts'
import { BIDSContext } from '../schema/context.ts'
import { lookupModality } from '../schema/modalities.ts'
Expand Down Expand Up @@ -75,7 +75,7 @@ export function _findRuleMatches(node, path, context) {
export async function datatypeFromDirectory(schema, context) {
const subEntity = schema.objects.entities.subject.name
const sesEntity = schema.objects.entities.session.name
const parts = context.file.path.split(SEPARATOR)
const parts = context.file.path.split(SEPARATOR_PATTERN)
let datatypeIndex = parts.length - 2
if (datatypeIndex < 1) {
return Promise.resolve()
Expand Down
28 changes: 20 additions & 8 deletions bids-validator/src/validators/filenameValidate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,52 @@ const fileTree = new FileTree('/tmp', '/')
const issues = new DatasetIssues()
const ignore = new FileIgnoreRules([])

const splitFile = (path: string) => {
const parts = path.split('/')
return {
dirname: parts.slice(0, parts.length - 1).join('/'),
basename: parts[parts.length - 1],
}
}

Deno.test('test missingLabel', async (t) => {
await t.step('File with underscore and no hyphens errors out.', async () => {
const fileName = Deno.makeTempFileSync({
const tmpFile = Deno.makeTempFileSync({
prefix: 'no_labels_',
suffix: '_entities.wav',
}).split('/')[2]
let file = new BIDSFileDeno('/tmp', fileName, ignore)
})
const { dirname, basename } = splitFile(tmpFile)
const file = new BIDSFileDeno(dirname, basename, ignore)

let context = new BIDSContext(fileTree, file, issues)
const context = new BIDSContext(fileTree, file, issues)
await missingLabel(schema, context)
assertEquals(
context.issues
.getFileIssueKeys(context.file.path)
.includes('ENTITY_WITH_NO_LABEL'),
true,
)
Deno.removeSync(tmpFile)
})

await t.step(
"File with underscores and hyphens doesn't error out.",
async () => {
const fileName = Deno.makeTempFileSync({
const tmpFile = Deno.makeTempFileSync({
prefix: 'we-do_have-',
suffix: '_entities.wav',
}).split('/')[2]
let file = new BIDSFileDeno('/tmp', fileName, ignore)
let context = new BIDSContext(fileTree, file, issues)
})
const { dirname, basename } = splitFile(tmpFile)
const file = new BIDSFileDeno(dirname, basename, ignore)
const context = new BIDSContext(fileTree, file, issues)
await missingLabel(schema, context)
assertEquals(
context.issues
.getFileIssueKeys(context.file.path)
.includes('ENTITY_WITH_NO_LABEL'),
false,
)
Deno.removeSync(tmpFile)
},
)
})
4 changes: 2 additions & 2 deletions bids-validator/src/validators/filenameValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CheckFunction, RuleCheckFunction } from '../types/check.ts'
import { DatasetIssues } from '../issues/datasetIssues.ts'
import { BIDSContext } from '../schema/context.ts'
import { GenericSchema, Schema, Entity, Format } from '../types/schema.ts'
import { SEPARATOR } from '../deps/path.ts'
import { SEPARATOR_PATTERN } from '../deps/path.ts'
import { hasProp } from '../utils/objectPathHandler.ts'

const sidecarExtensions = ['.json', '.tsv', '.bvec', '.bval']
Expand All @@ -25,7 +25,7 @@ export async function filenameValidate(
}

export function isAtRoot(context: BIDSContext) {
if (context.file.path.split(SEPARATOR).length !== 2) {
if (context.file.path.split(SEPARATOR_PATTERN).length !== 2) {
return false
}
return true
Expand Down
1 change: 0 additions & 1 deletion bids-validator/src/validators/isBidsy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* derivatives to have the lowest common denomenator of bids like file names.
*/
// @ts-nocheck
import { SEPARATOR } from '../deps/path.ts'
import { BIDSContext } from '../schema/context.ts'
import { CheckFunction } from '../../types/check.ts'
import { BIDSFile } from '../types/file.ts'
Expand Down
Loading