35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
import { execSync, spawn } from 'node:child_process';
|
|
import { mkdtempSync, mkdirSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
|
|
const FA2JSON = new URL('../../fa2json', import.meta.url).pathname;
|
|
|
|
export async function setup() {
|
|
// Create image file and format
|
|
const img = execSync('mktemp /tmp/fa2json-test-XXXXXX.img').toString().trim();
|
|
execSync(`truncate -s 10M ${img}`);
|
|
execSync(`mkfs.ext4 -q ${img}`);
|
|
|
|
// Create mount point and mount
|
|
const mnt = execSync('mktemp -d /tmp/fa2json-mnt-XXXXXX').toString().trim();
|
|
execSync(`sudo mount ${img} ${mnt}`);
|
|
|
|
// Hand ownership to current user, then sync before fa2json starts
|
|
execSync(`sudo chown ${process.getuid()} ${mnt}`);
|
|
execSync('sync');
|
|
|
|
async function teardown() {
|
|
try { execSync(`sudo umount ${mnt}`); } catch {}
|
|
try { execSync(`rm -f ${img}`); } catch {}
|
|
try { execSync(`rmdir ${mnt}`); } catch {}
|
|
}
|
|
|
|
return { img, mnt, teardown };
|
|
}
|
|
|
|
export function spawnFa2json(mnt) {
|
|
const proc = spawn('sudo', [FA2JSON, mnt], { stdio: ['ignore', 'pipe', 'inherit'] });
|
|
return proc;
|
|
}
|