-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-all-packages.js
More file actions
executable file
·320 lines (269 loc) · 9.53 KB
/
test-all-packages.js
File metadata and controls
executable file
·320 lines (269 loc) · 9.53 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env node
// TODO turn this into a pnpm command
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// ANSI color codes
const RED = '\x1b[0;31m';
const GREEN = '\x1b[0;32m';
const YELLOW = '\x1b[1;33m';
const NC = '\x1b[0m'; // No Color
// Helper functions
function dbsafename(packagePath) {
const packageName = path.basename(packagePath);
return `test_${packageName}`.replace(/[^a-zA-Z0-9]/g, '_').toLowerCase();
}
function getProjectName(packagePath, projectRoot) {
const planFile = path.isAbsolute(packagePath)
? path.join(packagePath, 'pgpm.plan')
: path.join(projectRoot, packagePath, 'pgpm.plan');
if (!fs.existsSync(planFile)) {
return null;
}
const content = fs.readFileSync(planFile, 'utf8');
const match = content.match(/^%project=(.+)$/m);
return match ? match[1].trim() : null;
}
function execCommand(command, options = {}) {
try {
const result = execSync(command, {
encoding: 'utf8',
stdio: options.silent ? 'pipe' : 'inherit',
...options
});
return { success: true, output: result };
} catch (error) {
return { success: false, error: error.message, code: error.status || error.code };
}
}
function commandExists(command) {
try {
execSync(`command -v ${command}`, { stdio: 'ignore' });
return true;
} catch {
return false;
}
}
function checkDockerPostgres() {
if (!commandExists('docker')) {
return false;
}
const result = execCommand('docker exec postgres psql -U postgres -c "SELECT 1;"', { silent: true });
return result.success;
}
function checkDirectPostgres() {
if (!commandExists('psql')) {
return false;
}
const result = execCommand('psql -c "SELECT 1;"', { silent: true });
return result.success;
}
function cleanupDb(dbname, useDocker) {
console.log(` Cleaning up database: ${dbname}`);
if (useDocker) {
execCommand(`docker exec postgres dropdb -U postgres "${dbname}"`, { silent: true });
} else {
execCommand(`dropdb "${dbname}"`, { silent: true });
}
}
function createDb(dbname, useDocker) {
if (useDocker) {
const result = execCommand(`docker exec postgres createdb -U postgres "${dbname}"`);
return result.success;
} else {
const result = execCommand(`createdb "${dbname}"`);
return result.success;
}
}
function findPackages(projectRoot) {
const packages = [];
const packagesDir = path.join(projectRoot, 'packages');
if (!fs.existsSync(packagesDir)) {
return packages;
}
function walkDir(dir) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
// Skip dist directories
if (entry.name === 'dist' || fullPath.includes('/dist')) {
continue;
}
walkDir(fullPath);
} else if (entry.isFile() && entry.name === 'pgpm.plan') {
const packagePath = path.relative(projectRoot, dir);
packages.push(packagePath);
}
}
}
walkDir(packagesDir);
return packages.sort();
}
function runPgpmCommand(command, dbname, packageName, packagePath, projectRoot) {
const fullCommand = `pgpm ${command} --database "${dbname}" --yes --package "${packageName}"`;
const result = execCommand(fullCommand, {
cwd: path.join(projectRoot, packagePath)
});
return result.success;
}
function testPackage(packagePath, projectRoot, useDocker) {
const packageName = path.basename(packagePath);
const dbname = dbsafename(packagePath);
const lqlPackageName = getProjectName(packagePath, projectRoot);
if (!lqlPackageName) {
console.error(`${RED}ERROR: Could not find %project= in ${packagePath}/pgpm.plan${NC}`);
return false;
}
console.log(`${YELLOW}Testing package: ${packageName}${NC}`);
console.log(` Package path: ${packagePath}`);
console.log(` Database name: ${dbname}`);
cleanupDb(dbname, useDocker);
console.log(` Creating database: ${dbname}`);
if (!createDb(dbname, useDocker)) {
console.error(`${RED}FAILED: Could not create database ${dbname} for package ${packageName}${NC}`);
return false;
}
const packageFullPath = path.join(projectRoot, packagePath);
if (!fs.existsSync(packageFullPath)) {
console.error(`${RED}FAILED: Could not find directory ${packageFullPath}${NC}`);
cleanupDb(dbname, useDocker);
return false;
}
console.log(' Running pgpm deploy...');
if (!runPgpmCommand('deploy', dbname, lqlPackageName, packagePath, projectRoot)) {
console.error(`${RED}FAILED: pgpm deploy failed for package ${lqlPackageName}${NC}`);
cleanupDb(dbname, useDocker);
return false;
}
console.log(' Running pgpm verify...');
if (!runPgpmCommand('verify', dbname, lqlPackageName, packagePath, projectRoot)) {
console.error(`${RED}FAILED: pgpm verify failed for package ${lqlPackageName}${NC}`);
cleanupDb(dbname, useDocker);
return false;
}
console.log(' Running pgpm revert...');
if (!runPgpmCommand('revert', dbname, lqlPackageName, packagePath, projectRoot)) {
console.error(`${RED}FAILED: pgpm revert failed for package ${lqlPackageName}${NC}`);
cleanupDb(dbname, useDocker);
return false;
}
console.log(' Running pgpm deploy (second time)...');
if (!runPgpmCommand('deploy', dbname, lqlPackageName, packagePath, projectRoot)) {
console.error(`${RED}FAILED: pgpm deploy (second time) failed for package ${lqlPackageName}${NC}`);
cleanupDb(dbname, useDocker);
return false;
}
cleanupDb(dbname, useDocker);
console.log(`${GREEN}SUCCESS: Package ${packageName} passed all tests${NC}`);
return true;
}
function usage() {
console.log('Usage: node test-all-packages.js [OPTIONS]');
console.log('');
console.log('OPTIONS:');
console.log(' --stop-on-fail Stop testing immediately when a package fails (default: continue testing all packages)');
console.log(' --help Show this help message');
console.log('');
}
function main() {
const args = process.argv.slice(2);
let stopOnFail = false;
// Parse command line arguments
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--stop-on-fail') {
stopOnFail = true;
} else if (arg === '--help' || arg === '-h') {
usage();
process.exit(0);
} else {
console.error(`Unknown option: ${arg}`);
usage();
process.exit(1);
}
}
console.log('=== PGPM Package Integration Test ===');
console.log('Testing all packages with deploy/verify/revert/deploy cycle');
if (stopOnFail) {
console.log('Mode: Stop on first failure');
} else {
console.log('Mode: Test all packages (collect all failures)');
}
console.log('');
if (!commandExists('pgpm')) {
console.error(`${RED}ERROR: pgpm CLI not found. Please install pgpm globally.${NC}`);
console.log('Run: npm install -g pgpm');
process.exit(1);
}
const useDocker = checkDockerPostgres();
const useDirect = !useDocker && checkDirectPostgres();
if (useDocker) {
console.log('Using Docker PostgreSQL container');
} else if (useDirect) {
console.log('Using direct PostgreSQL connection');
} else {
console.error(`${RED}ERROR: PostgreSQL not accessible.${NC}`);
console.log('For local development: docker-compose up -d');
console.log('For CI: ensure PostgreSQL service is running');
process.exit(1);
}
const scriptDir = __dirname;
const projectRoot = path.resolve(scriptDir, '..');
process.chdir(projectRoot);
console.log('Finding all PGPM packages...');
const packages = findPackages(projectRoot);
console.log(`Found ${packages.length} packages to test:`);
for (const pkg of packages) {
console.log(` - ${path.basename(pkg)}`);
}
console.log('');
const failedPackages = [];
const successfulPackages = [];
for (const packagePath of packages) {
const success = testPackage(packagePath, projectRoot, useDocker);
if (success) {
successfulPackages.push(path.basename(packagePath));
} else {
failedPackages.push(path.basename(packagePath));
if (stopOnFail) {
console.log('');
console.error(`${RED}STOPPING: Test failed for package ${path.basename(packagePath)} and --stop-on-fail was specified${NC}`);
console.log('');
console.log('=== TEST SUMMARY (PARTIAL) ===');
if (successfulPackages.length > 0) {
console.log(`${GREEN}Successful packages before failure (${successfulPackages.length}):${NC}`);
for (const pkg of successfulPackages) {
console.log(` ${GREEN}✓${NC} ${pkg}`);
}
console.log('');
}
console.error(`${RED}Failed package: ${path.basename(packagePath)}${NC}`);
console.log('');
console.error(`${RED}OVERALL RESULT: FAILED (stopped on first failure)${NC}`);
process.exit(1);
}
}
console.log('');
}
console.log('=== TEST SUMMARY ===');
console.log(`${GREEN}Successful packages (${successfulPackages.length}):${NC}`);
for (const pkg of successfulPackages) {
console.log(` ${GREEN}✓${NC} ${pkg}`);
}
if (failedPackages.length > 0) {
console.log('');
console.error(`${RED}Failed packages (${failedPackages.length}):${NC}`);
for (const pkg of failedPackages) {
console.error(` ${RED}✗${NC} ${pkg}`);
}
console.log('');
console.error(`${RED}OVERALL RESULT: FAILED${NC}`);
process.exit(1);
} else {
console.log('');
console.log(`${GREEN}OVERALL RESULT: ALL PACKAGES PASSED${NC}`);
process.exit(0);
}
}
main();