test: add global teardown — sweep ui-test entries from 02.post and dailies
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { execSync } = require('child_process');
|
||||
|
||||
function resolveUserDir() {
|
||||
if (process.env.GRAV_USER_DIR) return process.env.GRAV_USER_DIR;
|
||||
try {
|
||||
const raw = execSync(
|
||||
"docker inspect intotheeast_grav --format '{{range .Mounts}}{{if eq .Destination \"/var/www/html/user\"}}{{.Source}}{{end}}{{end}}'",
|
||||
{ encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'] }
|
||||
).trim();
|
||||
if (raw) return raw;
|
||||
} catch (_) {}
|
||||
return path.join(__dirname, '../user');
|
||||
}
|
||||
|
||||
function sweepUiTestEntries(dir) {
|
||||
if (!fs.existsSync(dir)) return 0;
|
||||
const entries = fs.readdirSync(dir).filter(e => e.includes('ui-test'));
|
||||
entries.forEach(e => fs.rmSync(path.join(dir, e), { recursive: true, force: true }));
|
||||
return entries.length;
|
||||
}
|
||||
|
||||
module.exports = async function globalTeardown() {
|
||||
const userDir = resolveUserDir();
|
||||
|
||||
// Read active trip slug from post-form.md
|
||||
const postFormPath = path.join(userDir, 'pages/02.post/post-form.md');
|
||||
let dailiesDir = null;
|
||||
if (fs.existsSync(postFormPath)) {
|
||||
const content = fs.readFileSync(postFormPath, 'utf-8');
|
||||
const m = content.match(/parent:\s*['"]?\/trips\/([^/'"]+)\/dailies/);
|
||||
if (m) {
|
||||
const tripSlug = m[1];
|
||||
const tripsBase = path.join(userDir, 'pages/01.trips');
|
||||
const tripFolder = fs.readdirSync(tripsBase).find(
|
||||
f => f === tripSlug || f.endsWith('.' + tripSlug) || f.includes(tripSlug)
|
||||
);
|
||||
if (tripFolder) {
|
||||
const dailiesBase = path.join(tripsBase, tripFolder);
|
||||
const dailiesFolder = fs.readdirSync(dailiesBase).find(
|
||||
f => f === 'dailies' || f === '01.dailies' || f.endsWith('.dailies')
|
||||
);
|
||||
if (dailiesFolder) dailiesDir = path.join(dailiesBase, dailiesFolder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sweep both the post inbox and the active trip's dailies
|
||||
const postInbox = path.join(userDir, 'pages/02.post');
|
||||
const n1 = sweepUiTestEntries(postInbox);
|
||||
const n2 = dailiesDir ? sweepUiTestEntries(dailiesDir) : 0;
|
||||
|
||||
if (n1 + n2 > 0) {
|
||||
console.log(`[teardown] removed ${n1} ui-test entries from 02.post, ${n2} from dailies`);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user