-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
100 lines (86 loc) · 2.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* @typedef {import('xast').Root} Root
*/
import assert from 'node:assert/strict'
import fs from 'node:fs/promises'
import process from 'node:process'
import test from 'node:test'
import {isHidden} from 'is-hidden'
import {fromXml} from 'xast-util-from-xml'
test('fromXml', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('xast-util-from-xml')).sort(), [
'fromXml'
])
})
await t.test('should throw messages', async function () {
try {
fromXml('<root unquoted=attribute>')
assert.fail()
} catch (error) {
assert.match(String(error), /^1:16: Could not parse XML/)
}
})
await t.test('should throw for SGML directives', async function () {
try {
fromXml('<!ENTITY>')
assert.fail()
} catch (error) {
assert.match(String(error), /^1:1: Could not parse XML/)
}
})
await t.test('should throw for unknown entities (1)', async function () {
try {
fromXml('<root>&foo;</root>')
assert.fail()
} catch (error) {
assert.match(String(error), /^1:7: Could not parse XML/)
}
})
await t.test('should throw for unknown entities (2)', async function () {
try {
fromXml('<root>©</root>')
assert.fail()
} catch (error) {
assert.match(String(error), /^1:7: Could not parse XML/)
}
})
await t.test('should throw on invalid nesting', async function () {
try {
fromXml('<root><a><b><c/></a></b></root>')
assert.fail()
} catch (error) {
assert.match(String(error), /^1:17: Could not parse XML/)
}
})
})
test('fixtures', async function (t) {
const base = new URL('fixtures/', import.meta.url)
const files = await fs.readdir(base)
let index = -1
while (++index < files.length) {
const folder = files[index]
if (isHidden(folder)) continue
await t.test(folder, async function () {
const inputUrl = new URL(folder + '/index.xml', base)
const treeUrl = new URL(folder + '/index.json', base)
const input = await fs.readFile(inputUrl)
/** @type {Root} */
// Remove `undefined`s.
const actual = JSON.parse(JSON.stringify(fromXml(input)))
/** @type {Root} */
let expected
try {
expected = JSON.parse(String(await fs.readFile(treeUrl)))
if ('UPDATE' in process.env) {
throw new Error('Update')
}
} catch {
// New folder.
await fs.writeFile(treeUrl, JSON.stringify(actual, undefined, 2) + '\n')
return
}
assert.deepEqual(actual, expected)
})
}
})