Skip to content
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

improve audit tests #1919

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/compat/src/http-audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export async function httpAudit(
let response = await (options.fetch ?? globalThis.fetch)(id);
let content = await response.text();
let type: ContentType;
if (response.status !== 200) {
throw new Error(`oops status code ${response.status} - ${response.statusText} for ${id}: ${content}`);
}
switch (response.headers.get('content-type')) {
case 'text/javascript':
type = 'javascript';
Expand All @@ -29,7 +32,7 @@ export async function httpAudit(
type = 'html';
break;
default:
throw new Error(`oops content type ${response.headers.get('content-type')}`);
throw new Error(`oops content type ${response.headers.get('content-type')} for ${id}`);
}
return { content, type };
}
Expand Down
25 changes: 21 additions & 4 deletions test-packages/support/audit-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { distance } from 'fastest-levenshtein';
import { sortBy } from 'lodash';
import { getRewrittenLocation } from './rewritten-path';

export { Import };

/*
The audit tool in @embroider/compat can be used directly to tell you about
potential problems in an app that is trying to adopt embroider. But we also
Expand All @@ -19,17 +21,16 @@ export function setupAuditTest(hooks: NestedHooks, opts: () => AuditBuildOptions
let result: { modules: { [file: string]: Module }; findings: Finding[] };
let expectAudit: ExpectAuditResults;

hooks.before(async () => {
async function visit() {
let o = opts();
if ('appURL' in o) {
result = await httpAudit(o);
} else {
result = await Audit.run(o);
}
});
}

hooks.beforeEach(assert => {
installAuditAssertions(assert);
function prepareResult(assert: Assert) {
let o = opts();
let pathRewriter: (p: string) => string;
if ('appURL' in o) {
Expand All @@ -38,15 +39,31 @@ export function setupAuditTest(hooks: NestedHooks, opts: () => AuditBuildOptions
pathRewriter = p => getRewrittenLocation(o.app, p);
}
expectAudit = new ExpectAuditResults(result, assert, pathRewriter);
}

hooks.before(async () => {
await visit();
});

hooks.beforeEach(assert => {
installAuditAssertions(assert);
prepareResult(assert);
});

return {
async rerun() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for tests that change files while vite dev is running we need to be able to rerun the vistors

await visit();
prepareResult(expectAudit.assert);
},
module(name: string) {
return expectAudit.module(name);
},
get findings() {
return expectAudit.findings;
},
get modules() {
return expectAudit.result.modules;
},
hasNoFindings() {
return expectAudit.hasNoProblems();
},
Expand Down
2 changes: 1 addition & 1 deletion tests/scenarios/helpers/command-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class CommandWatcher {
return;
}

this.process.kill();
Copy link
Contributor Author

@patricklx patricklx May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vite needs to be killed with SIGINT (default is SIGTERM), otherwise it leaves the server subprocess running, which then leaves the port open

this.process.kill('SIGINT');

// on windows the subprocess won't close if you don't end all the sockets
// we don't just end stdout because when you register a listener for stdout it auto registers stdin and stderr... for some reason :(
Expand Down
Loading