-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from embroider-build/extract-fixture-testing
Extract an output:fixture command for manual testing
- Loading branch information
Showing
4 changed files
with
117 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}), | ||
}); | ||
}, | ||
}; | ||
} |