Skip to content

Commit fb22b2f

Browse files
yarikopticclaude
andcommitted
test: Update tests to handle error-throwing schema loader
Adjust tests to work with the merged changes from main where loadSchema now throws errors instead of silently falling back when custom schema URLs fail to load. - Add network permission checks using Deno.permissions.query - Update tests to use assertRejects for network failures - Make tests conditional based on network permission status - Handle both with-network and without-network scenarios Tests now properly validate that: - Errors are thrown when custom schemas can't be loaded - schemaSource is reported when schemas are successfully loaded - Default schema loading (no custom URL) continues to work 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 2d39a99 commit fb22b2f

File tree

2 files changed

+147
-49
lines changed

2 files changed

+147
-49
lines changed

src/setup/loadSchema.test.ts

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertEquals, assert } from '@std/assert'
1+
import { assertEquals, assert, assertRejects } from '@std/assert'
22
import { loadSchemaWithSource, loadSchema } from './loadSchema.ts'
33

44
Deno.test('loadSchemaWithSource function', async (t) => {
@@ -16,27 +16,54 @@ Deno.test('loadSchemaWithSource function', async (t) => {
1616
assertEquals(result.source, undefined)
1717
})
1818

19-
await t.step('loadSchemaWithSource tracks source when custom URL provided', async () => {
20-
// This test validates the structure even though network fetch will fail
21-
const customUrl = 'https://example.com/custom-schema.json'
22-
const result = await loadSchemaWithSource(customUrl)
23-
assert(result.schema)
24-
assert(result.schema.schema_version)
25-
// Since network fetch fails, it falls back to default schema and source is undefined
26-
assertEquals(result.source, undefined)
19+
await t.step('loadSchemaWithSource throws error when custom URL fails without network', async () => {
20+
// Check if network permission is granted
21+
const netPermission = await Deno.permissions.query({ name: 'net' })
22+
23+
if (netPermission.state !== 'granted') {
24+
// Without network permission, it should throw an error
25+
const customUrl = 'https://example.com/custom-schema.json'
26+
await assertRejects(
27+
async () => await loadSchemaWithSource(customUrl),
28+
Error,
29+
'Failed to load schema from https://example.com/custom-schema.json'
30+
)
31+
} else {
32+
// With network permission, test might behave differently
33+
// Skip this specific test when network is available
34+
console.log('Skipping test - network permission granted')
35+
}
2736
})
2837

29-
await t.step('loadSchemaWithSource handles environment variable', async () => {
38+
await t.step('loadSchemaWithSource with environment variable throws without network', async () => {
39+
// Check if network permission is granted
40+
const netPermission = await Deno.permissions.query({ name: 'net' })
41+
3042
const originalEnv = Deno.env.get('BIDS_SCHEMA')
3143
try {
3244
const customUrl = 'https://env-schema.example.com/schema.json'
3345
Deno.env.set('BIDS_SCHEMA', customUrl)
3446

35-
const result = await loadSchemaWithSource()
36-
assert(result.schema)
37-
assert(result.schema.schema_version)
38-
// Since network fetch fails, source should be undefined
39-
assertEquals(result.source, undefined)
47+
if (netPermission.state !== 'granted') {
48+
// Without network permission, it should throw
49+
await assertRejects(
50+
async () => await loadSchemaWithSource(),
51+
Error,
52+
'Failed to load schema from https://env-schema.example.com/schema.json'
53+
)
54+
} else {
55+
// With network, might still fail but for different reasons (404, etc)
56+
try {
57+
const result = await loadSchemaWithSource()
58+
// If it succeeds, check the result
59+
assert(result.schema)
60+
assert(result.schema.schema_version)
61+
} catch (error) {
62+
// Expected to fail with unreachable URL
63+
assert(error instanceof Error)
64+
assert(error.message.includes('Failed to load schema'))
65+
}
66+
}
4067
} finally {
4168
if (originalEnv !== undefined) {
4269
Deno.env.set('BIDS_SCHEMA', originalEnv)

src/validators/bids.test.ts

Lines changed: 105 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert } from '@std/assert'
1+
import { assert, assertRejects } from '@std/assert'
22
import { pathsToTree } from '../files/filetree.ts'
33
import { validate } from './bids.ts'
44

@@ -67,6 +67,9 @@ Deno.test('Smoke tests of main validation function', async (t) => {
6767
assert(warnings.get({ location: '/dataset_description.json' }).length === 0)
6868
})
6969
await t.step('Schema source is reported in validation output', async () => {
70+
// Check network permission status
71+
const netPermission = await Deno.permissions.query({ name: 'net' })
72+
7073
// Test with default schema (no source should be provided)
7174
let result = await validate(dataset, {
7275
datasetPath: '/dataset',
@@ -78,46 +81,114 @@ Deno.test('Smoke tests of main validation function', async (t) => {
7881
assert(result.summary.schemaVersion)
7982
assert(result.summary.schemaSource === undefined)
8083

81-
// Test with custom schema URL
82-
result = await validate(dataset, {
83-
datasetPath: '/dataset',
84-
debug: 'INFO',
85-
ignoreNiftiHeaders: true,
86-
blacklistModalities: [],
87-
datasetTypes: [],
88-
schema: 'https://example.com/schema.json',
89-
})
90-
assert(result.summary.schemaVersion)
91-
// Since the URL won't be reachable, it should fall back to default and not set source
92-
assert(result.summary.schemaSource === undefined)
84+
// Test with custom schema URL - should throw error without network
85+
if (netPermission.state !== 'granted') {
86+
await assertRejects(
87+
async () => await validate(dataset, {
88+
datasetPath: '/dataset',
89+
debug: 'INFO',
90+
ignoreNiftiHeaders: true,
91+
blacklistModalities: [],
92+
datasetTypes: [],
93+
schema: 'https://example.com/schema.json',
94+
}),
95+
Error,
96+
'Failed to load schema'
97+
)
98+
} else {
99+
// With network, might fail with 404 or succeed with source set
100+
try {
101+
result = await validate(dataset, {
102+
datasetPath: '/dataset',
103+
debug: 'INFO',
104+
ignoreNiftiHeaders: true,
105+
blacklistModalities: [],
106+
datasetTypes: [],
107+
schema: 'https://example.com/schema.json',
108+
})
109+
// If it works, source should be set
110+
assert(result.summary.schemaVersion)
111+
assert(result.summary.schemaSource === 'https://example.com/schema.json')
112+
} catch (error) {
113+
// Expected to fail with unreachable URL
114+
assert(error instanceof Error)
115+
assert(error.message.includes('Failed to load schema'))
116+
}
117+
}
93118

94119
// Test with version tag
95-
result = await validate(dataset, {
96-
datasetPath: '/dataset',
97-
debug: 'INFO',
98-
ignoreNiftiHeaders: true,
99-
blacklistModalities: [],
100-
datasetTypes: [],
101-
schema: 'v1.9.0',
102-
})
103-
assert(result.summary.schemaVersion)
104-
// Since network fetch will likely fail, it should fall back to default
105-
assert(result.summary.schemaSource === undefined)
120+
if (netPermission.state !== 'granted') {
121+
await assertRejects(
122+
async () => await validate(dataset, {
123+
datasetPath: '/dataset',
124+
debug: 'INFO',
125+
ignoreNiftiHeaders: true,
126+
blacklistModalities: [],
127+
datasetTypes: [],
128+
schema: 'v1.9.0',
129+
}),
130+
Error,
131+
'Failed to load schema'
132+
)
133+
} else {
134+
// With network, might succeed
135+
try {
136+
result = await validate(dataset, {
137+
datasetPath: '/dataset',
138+
debug: 'INFO',
139+
ignoreNiftiHeaders: true,
140+
blacklistModalities: [],
141+
datasetTypes: [],
142+
schema: 'v1.9.0',
143+
})
144+
assert(result.summary.schemaVersion)
145+
// If successful, source should be the constructed URL
146+
if (result.summary.schemaSource) {
147+
assert(result.summary.schemaSource.includes('v1.9.0'))
148+
}
149+
} catch (error) {
150+
// Could fail if version doesn't exist
151+
assert(error instanceof Error)
152+
assert(error.message.includes('Failed to load schema'))
153+
}
154+
}
106155

107156
// Test with BIDS_SCHEMA environment variable
108157
const originalEnv = Deno.env.get('BIDS_SCHEMA')
109158
try {
110159
Deno.env.set('BIDS_SCHEMA', 'https://custom-schema.example.com/schema.json')
111-
result = await validate(dataset, {
112-
datasetPath: '/dataset',
113-
debug: 'INFO',
114-
ignoreNiftiHeaders: true,
115-
blacklistModalities: [],
116-
datasetTypes: [],
117-
})
118-
assert(result.summary.schemaVersion)
119-
// Environment variable should override, but since network will fail, source won't be set
120-
assert(result.summary.schemaSource === undefined)
160+
161+
if (netPermission.state !== 'granted') {
162+
await assertRejects(
163+
async () => await validate(dataset, {
164+
datasetPath: '/dataset',
165+
debug: 'INFO',
166+
ignoreNiftiHeaders: true,
167+
blacklistModalities: [],
168+
datasetTypes: [],
169+
}),
170+
Error,
171+
'Failed to load schema'
172+
)
173+
} else {
174+
// With network, might fail with 404
175+
try {
176+
result = await validate(dataset, {
177+
datasetPath: '/dataset',
178+
debug: 'INFO',
179+
ignoreNiftiHeaders: true,
180+
blacklistModalities: [],
181+
datasetTypes: [],
182+
})
183+
assert(result.summary.schemaVersion)
184+
if (result.summary.schemaSource) {
185+
assert(result.summary.schemaSource === 'https://custom-schema.example.com/schema.json')
186+
}
187+
} catch (error) {
188+
assert(error instanceof Error)
189+
assert(error.message.includes('Failed to load schema'))
190+
}
191+
}
121192
} finally {
122193
if (originalEnv !== undefined) {
123194
Deno.env.set('BIDS_SCHEMA', originalEnv)

0 commit comments

Comments
 (0)