Skip to content

Commit

Permalink
Merge pull request #93 from embroider-build/extract-fixture-testing
Browse files Browse the repository at this point in the history
Extract an output:fixture command for manual testing
  • Loading branch information
ef4 authored Oct 1, 2024
2 parents 6518086 + 5de0c11 commit 60f7607
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 31 deletions.
50 changes: 50 additions & 0 deletions lib/bin/output-fixture.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Usage:
*
* pnpm output:fixture $fixtureName [...flags]
*
* Example:
*
* pnpm output:fixture fixture-ts --typescript
*
*
*/

import assert from 'node:assert';
import path from 'node:path';
import fs from 'node:fs';
import { newApp } from '../new-app.mjs';
import copyWithTemplate from '../copy-with-template.js';

function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path + '/' + file).isDirectory();
});
}

const [, , fixtureName, ...args] = process.argv;
const fixturesDir = path.join(import.meta.dirname, '../../tests');

let available = getDirectories(fixturesDir);

assert(
fixtureName,
`Must pass a fixture name to output-fixture. Available fixtures: ${available.join(', ')}`,
);

let name = `app-with-${fixtureName}`;
let info = await newApp({ name, flags: args });

let selectedFixture = path.join(fixturesDir, fixtureName);

// apply the fixture on top of the generated app
copyWithTemplate(selectedFixture, info.dir, {
name,
});

console.info(`
New app created in ${info.dir}
using fixture @ ${selectedFixture}
`);
50 changes: 50 additions & 0 deletions lib/new-app.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import assert from 'node:assert';
import { join } from 'node:path';
import { execa } from 'execa';
import tmp from 'tmp-promise';
import { sync as resolveBinSync } from 'resolve-bin';

function findEmber() {
return resolveBinSync('ember-cli', { executable: 'ember' });
}

export const emberCli = findEmber();

const blueprintPath = join(import.meta.dirname, '..');

export async function newApp(options) {
const name = options.name;
const flags = options.flags ?? [];

assert(name, `newApp({ name }) is required`);
assert(Array.isArray(flags), `Option, flags, must be an array`);

let tmpDir = await tmp.dir({ unsafeCleanup: true });

let emberCliArgs = ['new', name, '-b', blueprintPath, '--pnpm', ...flags];

await execa(emberCli, emberCliArgs, {
cwd: tmpDir.path,
preferLocal: true,
});

return {
/**
* Result from tmp-promise
* (used for cleanup, later)
*/
tmp: tmpDir,
/**
* The actual tmp path
*/
tmpDir: tmpDir.path,
/**
* The name of the app
*/
appName: name,
/**
* The directory of the app
*/
dir: join(tmpDir.path, name),
};
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "vitest"
"test": "vitest",
"output:fixture": "node ./lib/bin/output-fixture.mjs"
},
"dependencies": {
"ember-cli": "^5.11.0",
Expand Down
45 changes: 15 additions & 30 deletions tests/helpers.mjs
Original file line number Diff line number Diff line change
@@ -1,77 +1,62 @@
import assert from 'node:assert';
import { join } from 'node:path';

import { beforeAll, afterAll } from 'vitest';
import { sync as resolveBinSync } from 'resolve-bin';
import { execa } from 'execa';
import tmp from 'tmp-promise';

import copyWithTemplate from '../lib/copy-with-template';
import copyWithTemplate from '../lib/copy-with-template.js';
import { newApp } from '../lib/new-app.mjs';

function findEmber() {
return resolveBinSync('ember-cli', { executable: 'ember' });
}

export const emberCli = findEmber();

const blueprintPath = join(__dirname, '..');
const appName = 'fancy-app-in-test';

export function newProjectWithFixtures({
flags = [],
fixturePath,
name = appName,
} = {}) {
let tmpDir;
let info;

assert(fixturePath, `a fixturePath is required`);

beforeAll(async () => {
tmpDir = await tmp.dir({ unsafeCleanup: true });

let emberCliArgs = [
'new',
name,
'-b',
blueprintPath,
'--pnpm',
'--skip-git',
...flags,
];

await execa(emberCli, emberCliArgs, {
cwd: tmpDir.path,
preferLocal: true,
});
info = await newApp({ name, flags: ['--skip-git', ...flags] });

// apply the fixture on top of the generated app
copyWithTemplate(join(__dirname, 'fixture'), join(tmpDir.path, name), {
copyWithTemplate(fixturePath, info.dir, {
name,
});

// Sync the lints for the fixtures with the project's config
await execa(`pnpm`, ['lint:fix'], {
cwd: join(tmpDir.path, name),
cwd: info.dir,
});
});

afterAll(async () => {
try {
await tmpDir.cleanup();
await tmp.cleanup();
} catch {
// if it fails to cleaup we don't want to break CI
}
});

return {
tmpDir: () => tmpDir.path,
tmp: () => info.tmpDir,
appName: () => name,
dir: () => join(tmpDir.path, name),
$: (...args) => execa({ cwd: join(tmpDir.path, name) })(...args),
execa: (program, args, options = {}) =>
execa(program, args, {
cwd: join(tmpDir.path, name),
dir: () => info.dir,
$: (...args) => execa({ cwd: info.dir })(...args),
execa: (program, args, options = {}) => {
return execa(program, args, {
cwd: info.dir,
...options,
}),
});
},
};
}

0 comments on commit 60f7607

Please sign in to comment.