-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.mjs
More file actions
executable file
·187 lines (157 loc) · 5.39 KB
/
validate.mjs
File metadata and controls
executable file
·187 lines (157 loc) · 5.39 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
/**
* Validation script for ESM-only build
* Tests: ESM library, CLI, and browser build
*/
import { execSync } from 'node:child_process';
import { readFileSync, existsSync } from 'node:fs';
const RESET = '\x1b[0m';
const GREEN = '\x1b[32m';
const RED = '\x1b[31m';
const YELLOW = '\x1b[33m';
const BLUE = '\x1b[34m';
let passed = 0;
let failed = 0;
const failures = [];
function log(message, color = RESET) {
console.log(`${color}${message}${RESET}`);
}
function test(name, fn) {
try {
log(`\n→ Testing: ${name}`, BLUE);
fn();
log(` ✓ ${name}`, GREEN);
passed++;
} catch (error) {
log(` ✗ ${name}`, RED);
log(` ${error.message}`, RED);
failed++;
failures.push({ name, error: error.message });
}
}
function exec(command, description) {
try {
const output = execSync(command, {
encoding: 'utf-8',
stdio: 'pipe'
});
return output;
} catch (error) {
throw new Error(`${description}: ${error.message}`);
}
}
function assertContains(output, expected, message) {
if (!output.includes(expected)) {
throw new Error(
`${message}\nExpected to contain: "${expected}"\nGot: ${output.substring(0, 200)}...`
);
}
}
function assertFileExists(path) {
if (!existsSync(path)) {
throw new Error(`File does not exist: ${path}`);
}
}
log('='.repeat(60), BLUE);
log('ESM-Only Validation Suite', BLUE);
log('='.repeat(60), BLUE);
// Test 1: Build files exist
test('Build files exist', () => {
assertFileExists('lib/index.mjs');
assertFileExists('lib/index.browser.js');
assertFileExists('lib/cli.mjs');
});
// Test 2: ESM library works with YAML string
test('ESM library with YAML string input', () => {
const output = exec(
"node -e \"import('./lib/index.mjs').then(m => { const yaml = 'apiVersion: backstage.io/v1alpha1\\\\nkind: Component\\\\nmetadata:\\\\n name: test\\\\nspec:\\\\n type: website\\\\n lifecycle: production'; const c = new m.PhasetFromBackstage({organizationId: 'test'}); const r = c.convert(yaml); console.log(r.spec.name); })\"",
'ESM library failed'
);
assertContains(output, 'test', 'Component name not found in output');
});
// Test 3: CLI ESM works
test('CLI ESM (cli.mjs)', () => {
const output = exec(
'node lib/cli.mjs convert --org test123 --input catalog-info.yaml --output test-output.json',
'CLI ESM failed'
);
assertContains(output, 'Converting', 'CLI output missing conversion message');
assertContains(output, 'Converted to', 'CLI output missing success message');
assertFileExists('test-output.json');
// Cleanup
execSync('rm -f test-output.json');
});
// Test 4: Browser build excludes fs module
test('Browser build excludes direct fs imports from our code', () => {
const browserContent = readFileSync('lib/index.browser.js', 'utf-8');
// Should have yaml bundled (larger file)
if (browserContent.length < 50000) {
throw new Error(
`Browser build seems too small (${browserContent.length} bytes), yaml might not be bundled`
);
}
// The yaml library may import fs, but OUR code should not have direct fs imports
// Check that we're not importing fs with bindings from our code
const ourCodePattern = /const.*readFileSync.*=.*require.*fs/;
if (ourCodePattern.test(browserContent)) {
throw new Error('Browser build contains direct fs require from our code');
}
});
// Test 5: CLI help command works
test('CLI help command works', () => {
const output = exec('node lib/cli.mjs help', 'CLI help failed');
assertContains(output, 'Usage:', 'Help output missing usage');
assertContains(output, 'Commands:', 'Help output missing commands');
});
// Test 6: Package.json exports configuration
test('Package.json exports configuration', () => {
const pkg = JSON.parse(readFileSync('package.json', 'utf-8'));
if (!pkg.exports) {
throw new Error('package.json missing exports field');
}
if (pkg.exports['.'].import !== './lib/index.mjs') {
throw new Error('package.json exports.import points to wrong file');
}
if (pkg.exports['.'].browser !== './lib/index.browser.js') {
throw new Error('package.json exports.browser points to wrong file');
}
if (pkg.type !== 'module') {
throw new Error('package.json missing type: module');
}
});
// Test 7: Direct YAML string input works
test('Direct YAML string input works', () => {
const yamlContent = readFileSync('catalog-info.yaml', 'utf-8');
const escapedYaml = yamlContent.replace(/'/g, "\\'").replace(/\n/g, '\\n');
const output = exec(
`node -e "import('./lib/index.mjs').then(m => { const c = new m.PhasetFromBackstage({organizationId: 'test'}); const yaml = '${escapedYaml}'; const r = c.convert(yaml); console.log(r.spec.name); })"`,
'YAML string input failed'
);
assertContains(
output,
'artist-web',
'Expected component name not found from YAML string'
);
});
// Print summary
log(`\n${'='.repeat(60)}`, BLUE);
log('Test Summary', BLUE);
log('='.repeat(60), BLUE);
log(`Total tests: ${passed + failed}`);
log(`Passed: ${passed}`, GREEN);
log(`Failed: ${failed}`, failed > 0 ? RED : GREEN);
if (failures.length > 0) {
log('\nFailed tests:', RED);
failures.forEach(({ name, error }) => {
log(` • ${name}`, RED);
log(` ${error}`, YELLOW);
});
}
log(`\n${'='.repeat(60)}`, BLUE);
if (failed > 0) {
log('❌ Validation FAILED', RED);
process.exit(1);
} else {
log('✅ All validations PASSED', GREEN);
process.exit(0);
}