Files
fa2json/test/lib/setup.mjs

32 lines
1.1 KiB
JavaScript

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