Debian Codegraph @

workspaces/arborist/test/fixtures/create-reify-case.js

raw
1 // Give it a directory, and it'll create a reify test case
2 // In order to support symlinks with the t.fixture(), it creates a
3 // function that takes a tap test object as an argument.
4 // usage: t.testdir(require(testCase)(t))
5 const {
6 statSync,
7 readdirSync,
8 readFileSync,
9 readlinkSync,
10 writeFileSync,
11 } = require('node:fs')
12
13 const { resolve, relative, basename } = require('node:path')
14
15 if (!process.argv[2]) {
16 console.error('pass in a folder as an argument')
17 process.exit(1)
18 }
19
20 const fixture = resolve(process.argv[2])
21 const rel = relative(resolve(__dirname, '../..'), fixture)
22 const outFile = resolve(__dirname, 'reify-cases', basename(fixture)) + '.js'
23
24 // we build up an object and then JSON.stringify it, and string replace
25 // the tokens with `t.fixture('symlink', ${symlinks.get(token)})`
26 const symlinks = new Map()
27 const jsonFiles = new Map()
28 const longFiles = new Map()
29 const buffers = new Map()
30 const crypto = require('node:crypto')
31 const token = p => p + crypto.randomBytes(8).toString('base64')
32 const hiddenLocks = []
33
34 const readFixture = dir => {
35 const res = {}
36 for (const ent of readdirSync(dir, { withFileTypes: true })) {
37 if (/^\..*\.swp/.test(ent.name)) {
38 continue
39 }
40
41 const p = resolve(dir, ent.name)
42 if (ent.isDirectory()) {
43 res[ent.name] = readFixture(p)
44 } else if (ent.isFile()) {
45 const content = readFileSync(p, 'utf8')
46 const buf = readFileSync(p)
47 // make sure hidden lockfiles are newer than the contents they cover
48 if (ent.name === '.package-lock.json') {
49 hiddenLocks.push(relative(fixture, p))
50 }
51
52 // if it's JSON, then store it in a way that's easier to look at
53 try {
54 const o = JSON.parse(content)
55 const t = token(p)
56 jsonFiles.set(t, o)
57 res[ent.name] = t
58 } catch (_) {
59 if (buf.equals(Buffer.from(content))) {
60 // it's a long file if it's more than one line
61 if (content.trim().includes('\n')) {
62 const t = token(p)
63 longFiles.set(t, content)
64 res[ent.name] = t
65 } else {
66 res[ent.name] = content
67 }
68 } else {
69 // save as a buffer if it's binary data
70 const t = token(p)
71 res[ent.name] = t
72 buffers.set(t, buf)
73 }
74 }
75 } else if (ent.isSymbolicLink()) {
76 const t = token(p)
77 const v = readlinkSync(p)
78 const st = statSync(p)
79 symlinks.set(t, [v, st.isDirectory()])
80 res[ent.name] = t
81 }
82 }
83 return res
84 }
85
86 let output = JSON.stringify(readFixture(fixture), null, 2)
87 for (const [token, [value, winOk]] of symlinks.entries()) {
88 const winGuard = winOk ? '' : `process.platform === 'win32' ? '' : `
89 output = output
90 .split(JSON.stringify(token))
91 .join(`${winGuard}t.fixture('symlink', ${JSON.stringify(value)})`)
92 }
93 const resc = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
94 for (const [token, obj] of jsonFiles.entries()) {
95 const re = new RegExp('^([\\s]*)("[^"]+": )' + resc(JSON.stringify(token)), 'm')
96 const replace = (_0, _1, _2) => {
97 return _1 + _2 +
98 'JSON.stringify(' +
99 JSON.stringify(obj, null, 2).replace(/^/gm, _1).trimLeft() +
100 ')'
101 }
102 output = output.replace(re, replace)
103 }
104 for (const [token, content] of longFiles.entries()) {
105 const re = new RegExp('^([\\s]*)("[^"]+": )' + resc(JSON.stringify(token)), 'm')
106 const replace = (_0, _1, _2) => {
107 return _1 + _2 +
108 '`' +
109 content.replace(/\\/g, '\\\\').replace(/\$/g, '\\$').replace(/`/g, '\\`') +
110 '`'
111 }
112 output = output.replace(re, replace)
113 }
114 for (const [token, buf] of buffers.entries()) {
115 output = output
116 .split(JSON.stringify(token))
117 .join(`Buffer.from(${JSON.stringify(buf.toString('base64'))}, 'base64')`)
118 }
119 output = `t.testdir(${output})`
120 if (hiddenLocks.length) {
121 output += `
122 const {utimesSync} = require('fs')
123 const n = Date.now() + 10000
124 const {resolve} = require('path')
125 `
126 for (const hiddenLock of hiddenLocks) {
127 const h = JSON.stringify(hiddenLock)
128 output += `\n utimesSync(resolve(path, ${h}), new Date(n), new Date(n))`
129 }
130 }
131
132 writeFileSync(outFile, `// generated from ${rel.replaceAll('\\', '/')}
133 module.exports = t => {
134 const path = ${output}
135 return path
136 }
137 `)