Skip to content
Source
Sign inSign up
TanStack/container
Open History to see the last commit

path-reference.mjs

101 lines3.3 KB

1import { readFile, writeFile, mkdir } from 'node:fs/promises'
2import vm from 'node:vm'
3import path from 'node:path'
4import { createHash } from 'node:crypto'
5const root = new URL('../node_modules/path-browserify/', import.meta.url)
6const metadata = JSON.parse(
7 await readFile(new URL('package.json', root), 'utf8'),
8)
9if (metadata.version !== '1.0.1')
10 throw new Error(
11 'Review upstream test changes before changing the pinned path reference',
12 )
13// A strict synchronous adapter for these selected tape methods, not the tape runner.
14const adapter = `
15const results={assertions:0,tests:0,skipped:[]};
16function check(ok,message){results.assertions++;if(!ok)throw new Error(message||'Upstream assertion failed')}
17function tape(name,options,fn){
18 if(typeof options==='function'){fn=options;options={}};
19 if(options.skip){results.skipped.push(name);return};
20 results.tests++;let ended=false;
21 fn({strictEqual:(a,b)=>check(a===b,name+': '+String(a)+' !== '+String(b)),
22 throws:(fn,expected)=>{let error;try{fn()}catch(e){error=e};check(error&&error instanceof expected,name+': expected '+expected.name)},
23 end:()=>{ended=true}});
24 if(!ended)throw new Error('Test did not finish: '+name);
26tape.results=results;module.exports=tape;
28const adapterContext = { module: { exports: {} } }
29vm.runInNewContext(adapter, adapterContext)
30const tape = adapterContext.module.exports
31// Keep Node's real Windows helpers available. Some otherwise POSIX test groups
32// conditionally add Windows assertions when win32 exists.
33const posix = path.posix
34const files = {
35 '/node_modules/tape/package.json': JSON.stringify({
36 name: 'tape',
37 main: 'index.cjs',
38 }),
39 '/node_modules/tape/index.cjs': adapter,
40 '/upstream/index.js': `module.exports=require('node:path')`,
42const sources = []
43const names = [
44 'test-path.js',
45 ...[
46 'basename',
47 'dirname',
48 'extname',
49 'isabsolute',
50 'join',
51 'relative',
52 'resolve',
53 'zero-length-strings',
54 ].map((x) => `test-path-${x}.js`),
56for (const name of names) {
57 const source = await readFile(new URL('test/' + name, root), 'utf8')
58 sources.push({
59 name,
60 sha256: createHash('sha256').update(source).digest('hex'),
61 })
62 vm.runInNewContext(source, {
63 require: (id) => {
64 if (id === 'tape') return tape
65 if (id === '../') return posix
66 throw new Error('Unexpected upstream require ' + id)
67 },
68 __filename: '/upstream/test/' + name,
69 process,
70 TypeError,
71 })
72 files['/upstream/test/' + name] =
73 `var __filename=${JSON.stringify('/upstream/test/' + name)};var process=require('node:process');\n` +
74 source
76files['/main.cjs'] =
77 names.map((name) => `require('./upstream/test/${name}');`).join('\n') +
78 `\nconsole.log(JSON.stringify(require('tape').results));`
79const destination = new URL(
80 '../public/feasibility/path-upstream.json',
81 import.meta.url,
83await mkdir(new URL('.', destination), { recursive: true })
84await writeFile(
85 destination,
86 JSON.stringify(
87 {
88 package: 'path-browserify',
89 version: metadata.version,
90 license: await readFile(new URL('LICENSE', root), 'utf8'),
91 sources,
92 expected: tape.results,
93 files,
94 },
95 null,
96 2,
97 ) + '\n',
99console.log(
100 `Pinned upstream path subset: ${tape.results.assertions} assertions, ${tape.results.skipped.length} explicit skipped groups`,