All files / benches/support compileFixture.js

94.87% Statements 37/39
100% Branches 2/2
50% Functions 1/2
94.87% Lines 37/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 1x      
/**
 * @file compileFixture.js
 * @description Compiles benchmark component source with the real compiler.
 *
 * The benchmarks measure Avenx, not an approximation of it, so a scenario is
 * written in Avenx's authoring format, written to a scratch file, and put
 * through `loadComponent` -- the same `ComponentParser` a build runs. If the
 * compiler stops emitting something the runtime needs, the benchmark fails to
 * build rather than quietly measuring a hand-written stand-in.
 * @module benches/support/compileFixture
 */
import fs from 'fs';
import os from 'os';
import path from 'path';
import { loadComponent } from '../../lib/core/tooling/loadComponent.js';
 
const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'avenx-bench-'));
let seq = 0;
 
/**
 * Compiles component source and returns the class the compiler emitted.
 * @param {string} source - Component source in Avenx authoring format.
 * @param {string} [name] - Base name, which becomes the class name.
 * @returns {Function} The compiled component class.
 */
export function compileComponent(source, name = `Bench${seq++}`) {
  const dir = path.join(scratch, name);
  fs.mkdirSync(dir, { recursive: true });
  const file = path.join(dir, `${name}.component.js`);
  fs.writeFileSync(file, source);
  return loadComponent(file, { config: { logging: { silent: true } } });
}
 
/**
 * Removes the scratch directory. Safe to call more than once.
 */
export function cleanup() {
  fs.rmSync(scratch, { recursive: true, force: true });
}