Skip to content

Commit 6ad4f65

Browse files
c1825846easymikey
authored andcommitted
test: assert 3rd party reexports (google#987)
closes google#979
1 parent aa021be commit 6ad4f65

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

scripts/generate-vendor-tests.mjs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env node
2+
3+
// Copyright 2024 Google LLC
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// https://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
import fs from 'node:fs'
18+
import path from 'node:path'
19+
import process from 'node:process'
20+
21+
import { YAML, fs as vendorFs } from '../build/vendor.js'
22+
23+
const copyright = `// Copyright 2024 Google LLC
24+
//
25+
// Licensed under the Apache License, Version 2.0 (the "License");
26+
// you may not use this file except in compliance with the License.
27+
// You may obtain a copy of the License at
28+
//
29+
// https://www.apache.org/licenses/LICENSE-2.0
30+
//
31+
// Unless required by applicable law or agreed to in writing, software
32+
// distributed under the License is distributed on an "AS IS" BASIS,
33+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34+
// See the License for the specific language governing permissions and
35+
// limitations under the License.`
36+
37+
const testLibImports = `import assert from 'node:assert'
38+
import { test, describe } from 'node:test'`
39+
40+
const newLine = '\n'
41+
42+
const vendors = [
43+
{
44+
name: 'YAML',
45+
module: YAML,
46+
},
47+
{
48+
name: 'fs',
49+
module: vendorFs,
50+
},
51+
]
52+
53+
const cwd = process.cwd()
54+
55+
vendors.forEach(({ name, module }) => {
56+
const outputFile = path.resolve(
57+
cwd,
58+
`test/vendor-${name.toLowerCase()}.test.js`
59+
)
60+
61+
const moduleImport = `import { ${name} } from '../build/vendor.js'`
62+
63+
const fileText = [
64+
copyright,
65+
newLine,
66+
testLibImports,
67+
moduleImport,
68+
newLine,
69+
createDescribeBlock(
70+
name,
71+
createTestBlock(
72+
'has proper exports',
73+
Object.entries(module)
74+
.map(([k, v]) => `assert.equal(typeof ${name}.${k}, '${typeof v}')`)
75+
.join('\n')
76+
)
77+
),
78+
].join('\n')
79+
80+
fs.writeFileSync(outputFile, fileText)
81+
})
82+
83+
function createDescribeBlock(vendorName, content) {
84+
return [`describe('vendor ${vendorName}', () => {`, content, '})'].join('\n')
85+
}
86+
87+
function createTestBlock(label, content) {
88+
return [`test('${label}', () => {`, content, '})'].join('\n')
89+
}

0 commit comments

Comments
 (0)