Skip to content

Commit fecaf42

Browse files
committed
fix(tests): Pass TSV data as stream in regression test
1 parent ab1550e commit fecaf42

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

src/files/streams.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { assert, assertEquals } from '@std/assert'
22
import { createUTF8Stream, UnicodeDecodeError } from './streams.ts'
3-
4-
function streamFromUint8Array(arr: Uint8Array): ReadableStream<Uint8Array> {
5-
return new ReadableStream({
6-
start(controller) {
7-
controller.enqueue(arr)
8-
controller.close()
9-
},
10-
})
11-
}
3+
import { streamFromUint8Array, streamFromString } from '../tests/utils.ts'
124

135
Deno.test('createUTF8Stream', async (t) => {
146
await t.step('should return a TransformStream with UTF8StreamTransformer', () => {
@@ -17,7 +9,7 @@ Deno.test('createUTF8Stream', async (t) => {
179
})
1810

1911
await t.step('should correctly transform UTF-8 input', async () => {
20-
const rawstream = streamFromUint8Array(new TextEncoder().encode('Hello, world!'))
12+
const rawstream = streamFromString('Hello, world!')
2113
const reader = rawstream.pipeThrough(createUTF8Stream()).getReader()
2214
const { value } = await reader.read()
2315
assertEquals(value, 'Hello, world!')

src/files/tsv.test.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { assert, assertEquals, assertObjectMatch } from '@std/assert'
22
import { pathToFile } from './filetree.ts'
33
import { loadTSV } from './tsv.ts'
4+
import { streamFromString } from '../tests/utils.ts'
45
import { ColumnsMap } from '../types/columns.ts'
56

6-
function streamFromString(str: string): ReadableStream<Uint8Array> {
7-
return new ReadableStream({
8-
start(controller) {
9-
controller.enqueue(new TextEncoder().encode(str))
10-
controller.close()
11-
},
12-
})
13-
}
14-
157
Deno.test('TSV loading', async (t) => {
168
await t.step('Empty file produces empty map', async () => {
179
const file = pathToFile('/empty.tsv')

src/tests/regression.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { assert } from '@std/assert'
22
import { pathsToTree } from '../files/filetree.ts'
33
import { validate } from '../validators/bids.ts'
44
import type { BIDSFile } from '../types/filetree.ts'
5+
import { streamFromString } from './utils.ts'
56

67
Deno.test('Regression tests', async (t) => {
78
await t.step('Verify ignored files in scans.tsv do not trigger error', async () => {
@@ -17,7 +18,7 @@ Deno.test('Regression tests', async (t) => {
1718
// Without ignore, NOT_INCLUDED is triggered for CT, but the scans file is happy
1819
let ds = pathsToTree(paths)
1920
let scans_tsv = ds.get('sub-01/sub-01_scans.tsv') as BIDSFile
20-
scans_tsv.text = () => Promise.resolve(scans_content)
21+
scans_tsv.stream = streamFromString(scans_content)
2122
let result = await validate(ds, {
2223
datasetPath: '/dataset',
2324
debug: 'ERROR',
@@ -30,7 +31,7 @@ Deno.test('Regression tests', async (t) => {
3031
// With ignore, NOT_INCLUDED is not triggered for CT, and the scans file is still happy
3132
ds = pathsToTree(paths, ignore)
3233
scans_tsv = ds.get('sub-01/sub-01_scans.tsv') as BIDSFile
33-
scans_tsv.text = () => Promise.resolve(scans_content)
34+
scans_tsv.stream = streamFromString(scans_content)
3435
result = await validate(ds, {
3536
datasetPath: '/dataset',
3637
debug: 'ERROR',

src/tests/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function streamFromUint8Array(arr: Uint8Array): ReadableStream<Uint8Array> {
2+
return new ReadableStream({
3+
start(controller) {
4+
controller.enqueue(arr)
5+
controller.close()
6+
},
7+
})
8+
}
9+
10+
export function streamFromString(str: string): ReadableStream<Uint8Array> {
11+
return streamFromUint8Array(new TextEncoder().encode(str))
12+
}

0 commit comments

Comments
 (0)