-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor scenario helpers to a shared location #1454
Merged
NullVoxPopuli
merged 1 commit into
embroider-build:main
from
NullVoxPopuli:refactor-helpers
Jun 19, 2023
+209
−66
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,66 +1 @@ | ||
import { PreparedApp } from 'scenario-tester'; | ||
import { join } from 'path'; | ||
import { readFileSync } from 'fs'; | ||
import globby from 'globby'; | ||
import { set } from 'lodash'; | ||
import type { JSDOM } from 'jsdom'; | ||
|
||
export interface FastbootTestHelpers { | ||
visit(url: string): Promise<JSDOM>; | ||
fetchAsset(url: string): Promise<string>; | ||
} | ||
|
||
export async function setupFastboot( | ||
app: PreparedApp, | ||
environment = 'development', | ||
envVars?: Record<string, string> | ||
): Promise<FastbootTestHelpers> { | ||
let result = await app.execute(`node node_modules/ember-cli/bin/ember build --environment=${environment}`, { | ||
env: envVars, | ||
}); | ||
|
||
if (result.exitCode !== 0) { | ||
throw new Error(`failed to build app for fastboot: ${result.output}`); | ||
} | ||
|
||
const FastBoot = require('fastboot'); | ||
|
||
let fastboot = new FastBoot({ | ||
distPath: join(app.dir, 'dist'), | ||
resilient: false, | ||
}); | ||
|
||
async function visit(url: string) { | ||
const jsdom = require('jsdom'); | ||
const { JSDOM } = jsdom; | ||
let visitOpts = { | ||
request: { headers: { host: 'localhost:4200' } }, | ||
}; | ||
let page = await fastboot.visit(url, visitOpts); | ||
let html = await page.html(); | ||
return new JSDOM(html); | ||
} | ||
|
||
async function fetchAsset(url: string): Promise<string> { | ||
const origin = 'http://example.com'; | ||
let u = new URL(url, origin); | ||
if (u.origin !== origin) { | ||
throw new Error(`fetchAsset only supports local assets, you asked for ${url}`); | ||
} | ||
return readFileSync(join(app.dir, 'dist', u.pathname), 'utf-8'); | ||
} | ||
|
||
return { visit, fetchAsset }; | ||
} | ||
|
||
export function loadFromFixtureData(fixtureNamespace: string) { | ||
const root = join(__dirname, '..', 'fixtures', fixtureNamespace); | ||
const paths = globby.sync('**', { cwd: root, dot: true }); | ||
const fixtureStructure: any = {}; | ||
|
||
paths.forEach(path => { | ||
set(fixtureStructure, path.split('/'), readFileSync(join(root, path), 'utf8')); | ||
}); | ||
|
||
return fixtureStructure; | ||
} | ||
export * from './helpers/index'; | ||
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,52 @@ | ||
import { PreparedApp } from 'scenario-tester'; | ||
import { join } from 'path'; | ||
import { readFileSync } from 'fs'; | ||
import type { JSDOM } from 'jsdom'; | ||
|
||
export interface FastbootTestHelpers { | ||
visit(url: string): Promise<JSDOM>; | ||
fetchAsset(url: string): Promise<string>; | ||
} | ||
|
||
export async function setupFastboot( | ||
app: PreparedApp, | ||
environment = 'development', | ||
envVars?: Record<string, string> | ||
): Promise<FastbootTestHelpers> { | ||
let result = await app.execute(`node node_modules/ember-cli/bin/ember build --environment=${environment}`, { | ||
env: envVars, | ||
}); | ||
|
||
if (result.exitCode !== 0) { | ||
throw new Error(`failed to build app for fastboot: ${result.output}`); | ||
} | ||
|
||
const FastBoot = require('fastboot'); | ||
|
||
let fastboot = new FastBoot({ | ||
distPath: join(app.dir, 'dist'), | ||
resilient: false, | ||
}); | ||
|
||
async function visit(url: string) { | ||
const jsdom = require('jsdom'); | ||
const { JSDOM } = jsdom; | ||
let visitOpts = { | ||
request: { headers: { host: 'localhost:4200' } }, | ||
}; | ||
let page = await fastboot.visit(url, visitOpts); | ||
let html = await page.html(); | ||
return new JSDOM(html); | ||
} | ||
|
||
async function fetchAsset(url: string): Promise<string> { | ||
const origin = 'http://example.com'; | ||
let u = new URL(url, origin); | ||
if (u.origin !== origin) { | ||
throw new Error(`fetchAsset only supports local assets, you asked for ${url}`); | ||
} | ||
return readFileSync(join(app.dir, 'dist', u.pathname), 'utf-8'); | ||
} | ||
|
||
return { visit, fetchAsset }; | ||
} |
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,45 @@ | ||
import fs from 'fs/promises'; | ||
|
||
export async function becomesModified({ | ||
filePath, | ||
assert, | ||
fn, | ||
}: { | ||
filePath: string; | ||
assert: Assert; | ||
fn: () => Promise<void>; | ||
}) { | ||
let oldStat = (await fs.stat(filePath)).mtimeMs; | ||
|
||
await fn(); | ||
|
||
let newStat = (await fs.stat(filePath)).mtimeMs; | ||
|
||
assert.notStrictEqual( | ||
oldStat, | ||
newStat, | ||
`Expected ${filePath} to be modified. Latest: ${newStat}, previously: ${oldStat}` | ||
); | ||
} | ||
|
||
export async function isNotModified({ | ||
filePath, | ||
assert, | ||
fn, | ||
}: { | ||
filePath: string; | ||
assert: Assert; | ||
fn: () => Promise<void>; | ||
}) { | ||
let oldStat = (await fs.stat(filePath)).mtimeMs; | ||
|
||
await fn(); | ||
|
||
let newStat = (await fs.stat(filePath)).mtimeMs; | ||
|
||
assert.strictEqual( | ||
oldStat, | ||
newStat, | ||
`Expected ${filePath} to be unchanged. Latest: ${newStat}, and pre-fn: ${oldStat}` | ||
); | ||
} |
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,16 @@ | ||
import { join } from 'path'; | ||
import { readFileSync } from 'fs'; | ||
import globby from 'globby'; | ||
import { set } from 'lodash'; | ||
|
||
export function loadFromFixtureData(fixtureNamespace: string) { | ||
const root = join(__dirname, '..', '..', 'fixtures', fixtureNamespace); | ||
const paths = globby.sync('**', { cwd: root, dot: true }); | ||
const fixtureStructure: any = {}; | ||
|
||
paths.forEach(path => { | ||
set(fixtureStructure, path.split('/'), readFileSync(join(root, path), 'utf8')); | ||
}); | ||
|
||
return fixtureStructure; | ||
} |
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,4 @@ | ||
export * from './fastboot'; | ||
export * from './fixtures'; | ||
export * from './v2-addon'; | ||
export * from './filesystem'; |
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,91 @@ | ||
import { PreparedApp } from 'scenario-tester'; | ||
import { spawn } from 'child_process'; | ||
|
||
export class DevWatcher { | ||
#addon: PreparedApp; | ||
#singletonAbort?: AbortController; | ||
#waitForBuildPromise?: Promise<unknown>; | ||
#lastBuild?: string; | ||
|
||
constructor(addon: PreparedApp) { | ||
this.#addon = addon; | ||
} | ||
|
||
start = () => { | ||
if (this.#singletonAbort) this.#singletonAbort.abort(); | ||
|
||
this.#singletonAbort = new AbortController(); | ||
|
||
/** | ||
* NOTE: when running rollup in a non-TTY environemnt, the "watching for changes" message does not print. | ||
*/ | ||
let rollupProcess = spawn('pnpm', ['start'], { | ||
cwd: this.#addon.dir, | ||
signal: this.#singletonAbort.signal, | ||
stdio: ['pipe', 'pipe', 'pipe'], | ||
// Have to disable color so our regex / string matching works easier | ||
// Have to include process.env, so the spawned environment has access to `pnpm` | ||
env: { ...process.env, NO_COLOR: '1' }, | ||
}); | ||
|
||
let settle: (...args: unknown[]) => void; | ||
let error: (...args: unknown[]) => void; | ||
this.#waitForBuildPromise = new Promise((resolve, reject) => { | ||
settle = resolve; | ||
error = reject; | ||
}); | ||
|
||
if (!rollupProcess.stdout) { | ||
throw new Error(`Failed to start process, pnpm start`); | ||
} | ||
if (!rollupProcess.stderr) { | ||
throw new Error(`Failed to start process, pnpm start`); | ||
} | ||
|
||
let handleData = (data: Buffer) => { | ||
let string = data.toString(); | ||
let lines = string.split('\n'); | ||
|
||
let build = lines.find(line => line.trim().match(/^created dist in (.+)$/)); | ||
let problem = lines.find(line => line.includes('Error:')); | ||
let isAbort = lines.find(line => line.includes('AbortError:')); | ||
|
||
if (isAbort) { | ||
// Test may have ended, we want to kill the watcher, | ||
// but not error, because throwing an error causes the test to fail. | ||
return settle(); | ||
} | ||
|
||
if (problem) { | ||
console.error('\n!!!\n', problem, '\n!!!\n'); | ||
error(problem); | ||
return; | ||
} | ||
|
||
if (build) { | ||
this.#lastBuild = build[1]; | ||
|
||
settle?.(); | ||
|
||
this.#waitForBuildPromise = new Promise((resolve, reject) => { | ||
settle = resolve; | ||
error = reject; | ||
}); | ||
} | ||
}; | ||
|
||
// NOTE: rollup outputs to stderr only, not stdout | ||
rollupProcess.stderr.on('data', (...args) => handleData(...args)); | ||
rollupProcess.on('error', handleData); | ||
rollupProcess.on('close', () => settle?.()); | ||
rollupProcess.on('exit', () => settle?.()); | ||
|
||
return this.#waitForBuildPromise; | ||
}; | ||
|
||
stop = () => this.#singletonAbort?.abort(); | ||
settled = () => this.#waitForBuildPromise; | ||
get lastBuild() { | ||
return this.#lastBuild; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at some point in node's history, implicit index is no longer available to use? I'd seen this in my ESM-node projects 🤷 anyone know why this has happened? did node folks decide that implicit index isn't a good idea? is it a configuration issue?
This file now emulates that behavior (and mostly to avoid having to change all the scenario tests to use
./helpers/index
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a node thing in this context 😂 I'm pretty sure this whole
import *
andexport *
is a typescript specific thing that is pretty annoying if you ask me since it messes with true ESM semanticsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also I'm pretty sure that ESM wants you to always do file-based imports anyway because realistically you can't do the "is this a file or is this a folder with an index in it" logic all that efficiently on the web