Skip to content

Commit

Permalink
Updates resource injector tests to be ESM-compatible.
Browse files Browse the repository at this point in the history
Refs #33.

This mostly required dropping the Jasmine spy on the `fs` module.
  • Loading branch information
dgp1130 committed Mar 4, 2023
1 parent b3d2431 commit 65d8c23
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions tools/binaries/resource_injector/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ts_project(
data = ["//:node_modules/node-html-parser"],
deps = [
":config",
"//common:fs",
"//common:file_system",
"//common:prerender_annotation_walker",
],
)
Expand All @@ -65,7 +65,7 @@ ts_project(
deps = [
":config",
":injector",
"//common:fs",
"//common:file_system_fake",
"//common/models:prerender_annotation",
"//:node_modules/@types/jasmine",
],
Expand Down
14 changes: 9 additions & 5 deletions tools/binaries/resource_injector/injector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from '../../../common/fs';
import { HTMLElement, parse } from 'node-html-parser';
import { InjectorConfig, InjectScript } from './config';
import { AnnotationNode, walkAllAnnotations } from '../../../common/prerender_annotation_walker';
import { FileSystem, diskFs } from '../../../common/file_system';

/**
* Parses the given HTML document and injects all the resources specified by the
Expand All @@ -14,8 +14,11 @@ import { AnnotationNode, walkAllAnnotations } from '../../../common/prerender_an
* injected into the HTML document.
* @returns A new HTML document with all the resources injected into it.
*/
export async function inject(html: string, config: InjectorConfig):
Promise<string> {
export async function inject(
html: string,
config: InjectorConfig,
fs: FileSystem = diskFs,
): Promise<string> {
// Parse input HTML.
const root = parse(html, {
comment: true,
Expand All @@ -40,7 +43,7 @@ export async function inject(html: string, config: InjectorConfig):
}

// Inject `<style />` tags for each inline style annotation in the document.
await replaceInlineStyleAnnotations(walkAllAnnotations(root));
await replaceInlineStyleAnnotations(walkAllAnnotations(root), fs);

// Return the new document.
return root.toString();
Expand Down Expand Up @@ -109,6 +112,7 @@ function injectScript(root: HTMLElement, action: InjectScript): void {
*/
async function replaceInlineStyleAnnotations(
nodes: Generator<AnnotationNode, void, void>,
fs: FileSystem,
): Promise<void> {
for (const node of nodes) {
const { annotation } = node;
Expand All @@ -128,7 +132,7 @@ async function replaceInlineStyleAnnotations(
null /* parentNode */,
[0, 0] /* range */,
);
inlineStyle.set_content(await fs.readFile(annotation.path, 'utf-8'));
inlineStyle.set_content(await fs.readFile(annotation.path, 'utf8'));
node.replace(inlineStyle);
}
}
Expand Down
11 changes: 7 additions & 4 deletions tools/binaries/resource_injector/injector_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fs from '../../../common/fs';
import { FileSystemFake } from '../../../common/file_system_fake';
import { createAnnotation } from '../../../common/models/prerender_annotation';
import { InjectorConfig } from './config';
import { inject } from './injector';
Expand Down Expand Up @@ -111,10 +111,13 @@ describe('injector', () => {
</html>
`.trim();

spyOn(fs, 'readFile').and.resolveTo('.foo { color: red; }');
const fs = FileSystemFake.of({
'foo.css': '.foo { color: red; }',
});
spyOn(fs, 'readFile').and.callThrough();

const injected = await inject(input, []);
expect(fs.readFile).toHaveBeenCalledWith('foo.css', 'utf-8');
const injected = await inject(input, [], fs);
expect(fs.readFile).toHaveBeenCalledWith('foo.css', 'utf8');

expect(injected).toBe(`
<!DOCTYPE html>
Expand Down
2 changes: 1 addition & 1 deletion tools/binaries/resource_injector/resource_injector_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,6 @@ describe('injector', () => {
`Binary unexpectedly succeeded. STDERR:\n${stderr}`,
);
expect(stdout).toBe('');
expect(stderr).toContain('tools/binaries/resource_injector/injector.js');
expect(stderr).toContain('tools/binaries/resource_injector/injector');
});
});

0 comments on commit 65d8c23

Please sign in to comment.