-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathbuild.spec.ts
139 lines (128 loc) · 4.9 KB
/
build.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const importUtils = import("@apphosting/adapter-angular/dist/utils.js");
import assert from "assert";
import { resolve } from "path";
import fs from "fs";
import path from "path";
import os from "os";
import { OutputBundleOptions } from "../interface.js";
describe("build commands", () => {
let tmpDir: string;
let outputBundleOptions: OutputBundleOptions;
let defaultAngularVersion: string;
beforeEach(() => {
tmpDir = generateTmpDir();
outputBundleOptions = {
browserDirectory: resolve(tmpDir, "dist", "test", "browser"),
bundleYamlPath: resolve(tmpDir, ".apphosting", "bundle.yaml"),
serverFilePath: resolve(tmpDir, "dist", "test", "server", "server.mjs"),
needsServerGenerated: false,
};
defaultAngularVersion = "17.3.8";
});
it("expects all output bundle files to be generated", async () => {
const { generateBuildOutput, validateOutputDirectory, createMetadata } = await importUtils;
const files = {
"dist/test/browser/browserfile": "",
"dist/test/server/server.mjs": "",
};
const packageVersion = createMetadata(defaultAngularVersion).adapterVersion;
generateTestFiles(tmpDir, files);
await generateBuildOutput(tmpDir, outputBundleOptions, defaultAngularVersion);
await validateOutputDirectory(outputBundleOptions);
const expectedFiles = {
"dist/test/browser/browserfile": "",
"dist/test/server/server.mjs": "",
".apphosting/bundle.yaml": `version: v1
runConfig:
runCommand: node dist/test/server/server.mjs
environmentVariables: []
metadata:
adapterPackageName: "@apphosting/adapter-angular"
adapterVersion: ${packageVersion}
framework: angular
frameworkVersion: 17.3.8
`,
};
validateTestFiles(tmpDir, expectedFiles);
});
it("expects SSR_PORT variable is added to bundle.yaml for Angular v17.3.2", async () => {
const { generateBuildOutput } = await importUtils;
const files = {
"dist/test/browser/browserfile": "",
"dist/test/server/server.mjs": "",
};
generateTestFiles(tmpDir, files);
await generateBuildOutput(tmpDir, outputBundleOptions, "17.3.2");
const expectedContents = ` environmentVariables:
- variable: SSR_PORT
value: "8080"
availability:
- RUNTIME`;
validateFileExistsAndContains(tmpDir, ".apphosting/bundle.yaml", expectedContents);
});
it("test failed validateOutputDirectory", async () => {
const { generateBuildOutput, validateOutputDirectory } = await importUtils;
const files = {
"dist/test/browser/browserfile": "",
"dist/test/server/notserver.mjs": "",
};
generateTestFiles(tmpDir, files);
await generateBuildOutput(tmpDir, outputBundleOptions, defaultAngularVersion);
assert.rejects(async () => await validateOutputDirectory(outputBundleOptions));
});
it("test populate output bundle options", async () => {
const { populateOutputBundleOptions } = await importUtils;
const expectedOutputBundleOptions = {
browserDirectory: "/browser",
bundleYamlPath: resolve(".apphosting", "bundle.yaml"),
needsServerGenerated: false,
serverFilePath: path.join("/server", "server.mjs"),
};
const outputPaths = {
root: new URL("file:///test"),
server: new URL("file:///server"),
browser: new URL("file:///browser"),
};
assert.deepEqual(populateOutputBundleOptions(outputPaths), expectedOutputBundleOptions);
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
});
function generateTmpDir(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), "test-files"));
}
function generateTestFiles(baseDir: string, filesToGenerate: Object): void {
Object.entries(filesToGenerate).forEach((file) => {
const fileName = file[0];
const contents = file[1];
const fileToGenerate = path.join(baseDir, fileName);
fs.mkdirSync(path.dirname(fileToGenerate), { recursive: true });
fs.writeFileSync(fileToGenerate, contents);
});
}
function ignoreBlankLines(text: string) {
return text.replace(/^\s*[\r\n]/gm, "");
}
function validateTestFiles(baseDir: string, expectedFiles: Object): void {
Object.entries(expectedFiles).forEach((file) => {
const fileName = file[0];
const expectedContents = file[1];
const fileToRead = path.join(baseDir, fileName);
const contents = fs.readFileSync(fileToRead).toString();
assert.deepEqual(ignoreBlankLines(contents), ignoreBlankLines(expectedContents));
});
}
function validateFileExistsAndContains(
baseDir: string,
expectedFileName: string,
expectedContents: string,
): void {
const fileToRead = path.join(baseDir, expectedFileName);
assert.ok(fs.existsSync(fileToRead), `File '${fileToRead}' does not exist.`);
const contents = fs.readFileSync(fileToRead).toString();
assert.ok(
contents.includes(expectedContents),
`Actual contents do not contain expected contents.\nExpected contained contents:\n${expectedContents}\nActual:\n${contents}`,
);
}