forked from ipld/codec-fixtures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-testmark.js
143 lines (127 loc) · 3.67 KB
/
make-testmark.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import fs from 'fs'
import { parse, patch, toString } from 'testmark.js'
import { codecs } from './codecs.js'
import { fixtureDirectories, loadFixture } from './util.js'
async function makeTestmark () {
if (process.argv.length === 2) {
throw new Error('Usage `make-testmark.js <path/to/ipld/metarepo>')
}
const repoRoot = new URL(process.argv[2], import.meta.url)
const fixtures = []
for (const { name, url } of fixtureDirectories()) {
fixtures.push([name, await loadFixture(url)])
}
for (const codec of Object.keys(codecs)) {
const dir = new URL(`specs/codecs/${codec}/fixtures/cross-codec/`, repoRoot)
let stat
try {
stat = await fs.promises.stat(dir)
} catch (err) {
console.error(`Directory does not exist: ${dir.pathname}`)
process.exit(1)
}
if (!stat.isDirectory()) {
console.error(`Is not a directory: ${dir.pathname}`)
process.exit(1)
}
const fixtureFile = new URL('index.md', dir)
let tmDoc = parse(`---
templateEngineOverride: md
---
# Cross-codec fixtures for ${codec}
## Introduction
_TODO_
## Fixtures
`)
try {
stat = await fs.promises.stat(fixtureFile)
if (!stat.isFile()) {
console.error(`Is not a file: ${fixtureFile.pathname}`)
process.exit(1)
}
const contents = await fs.promises.readFile(fixtureFile, 'utf8')
tmDoc = parse(contents)
} catch (err) {
}
const patchHunks = []
for (let [name, fixture] of fixtures) {
if (fixture[codec]) {
name = name.replace(/[\s/]/g, '__')
if (tmDoc.hunksByName.has(`${name}/${codec}/bytes`)) { // patch it
patchHunks.push({
name: `${name}/${codec}/bytes`,
blockTag: '',
body: fixture[codec].bytes.toString('hex').replace(/(.{120})/g, '$1\n')
})
patchHunks.push({
name: `${name}/${codec}/cid`,
blockTag: '',
body: fixture[codec].cid
})
if (codec === 'dag-json') {
// special case for dag-json since a human should be able to read it
patchHunks.push({
name: `${name}/${codec}/string`,
blockTag: 'json',
body: new TextDecoder().decode(fixture[codec].bytes)
})
}
for (const altCodec of Object.keys(fixture)) {
if (altCodec === codec) {
continue
}
patchHunks.push({
name: `${name}/${altCodec}/cid`,
blockTag: '',
body: fixture[altCodec].cid
})
}
} else { // create it
let section = `### ${name}
**Bytes**
[testmark]:# (${name}/${codec}/bytes)
\`\`\`
${fixture[codec].bytes.toString('hex').replace(/(.{120})/g, '$1\n')}
\`\`\`
`
if (codec === 'dag-json') {
// special case for dag-json since a human should be able to read it
section += `
**String form**
[testmark]:# (${name}/${codec}/string)
\`\`\`json
${new TextDecoder().decode(fixture[codec].bytes)}
\`\`\`
`
}
section += `
**${codec} CID**
[testmark]:# (${name}/${codec}/cid)
\`\`\`
${fixture[codec].cid}
\`\`\`
`
for (const altCodec of Object.keys(fixture)) {
if (altCodec === codec) {
continue
}
section += `
**${altCodec} CID**
[testmark]:# (${name}/${altCodec}/cid)
\`\`\`
${fixture[altCodec].cid}
\`\`\`
`
}
tmDoc.lines = tmDoc.lines.concat(section.split('\n'))
}
}
}
tmDoc = patch(tmDoc, patchHunks)
await fs.promises.writeFile(fixtureFile, toString(tmDoc), 'utf8')
}
}
makeTestmark().catch((err) => {
console.error(err)
process.exit(1)
})