Skip to content

Commit 4e9de17

Browse files
committed
refactor: use unplugin-raw instead of string template to generate test plugin
1 parent 6fe7ecb commit 4e9de17

11 files changed

Lines changed: 158 additions & 94 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"tsx": "^4.21.0",
103103
"typescript": "^5.9.3",
104104
"unbuild": "^3.6.1",
105+
"unplugin-raw": "^0.6.4",
105106
"vitest": "^4.0.18"
106107
},
107108
"workspaces": [

pnpm-lock.yaml

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,46 @@
1-
export * from "./bootsrtap.js";
2-
export * from "./html.js";
3-
export * from "./manifest.js";
4-
export * from "./mocha-setup.js";
1+
import { TESTER_PLUGIN_ID, TESTER_PLUGIN_REF } from "../../../constant.js";
2+
import bootstrapRaw from "./raw/bootstrap.js?raw";
3+
import htmlRaw from "./raw/index.html?raw";
4+
import manifestRaw from "./raw/manifest.json?raw";
5+
import mochaSetupRaw from "./raw/mocha-setup.js?raw";
6+
7+
export function generateManifest(): Record<string, unknown> {
8+
const manifestStr = manifestRaw
9+
.replace("__TESTER_PLUGIN_ID__", TESTER_PLUGIN_ID);
10+
11+
return JSON.parse(manifestStr);
12+
}
13+
14+
export function generateBootstrap(options: {
15+
port: number;
16+
startupDelay: number;
17+
waitForPlugin: string;
18+
}): string {
19+
return bootstrapRaw
20+
.replace("__PORT__", String(options.port))
21+
.replace("__STARTUP_DELAY__", String(options.startupDelay || 1000))
22+
.replace("__WAIT_FOR_PLUGIN__", options.waitForPlugin)
23+
.replace("__TESTER_PLUGIN_REF__", TESTER_PLUGIN_REF);
24+
}
25+
26+
export function generateHtml(
27+
setupCode: string,
28+
testFiles: string[],
29+
): string {
30+
const tests = testFiles.map(f => `<script src="${f}"></script>`).join("\n ");
31+
32+
return htmlRaw.replace("__TEST_FILES__", tests).replace("___SETUP_CODE___", setupCode);
33+
}
34+
35+
export function generateMochaSetup(options: {
36+
port: number;
37+
timeout: number;
38+
abortOnFail: boolean;
39+
exitOnFinish: boolean;
40+
}): string {
41+
return mochaSetupRaw
42+
.replace("__TIMEOUT__", String(options.timeout || 10000))
43+
.replace("__PORT__", String(options.port))
44+
.replace("__ABORT_ON_FAIL__", String(options.abortOnFail))
45+
.replace("__EXIT_ON_FINISH__", String(options.exitOnFinish ? "true" : "false"));
46+
}

src/core/tester/test-bundler-template/manifest.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/core/tester/test-bundler-template/bootsrtap.ts renamed to src/core/tester/test-bundler-template/raw/bootstrap.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import { TESTER_PLUGIN_REF } from "../../../constant.js";
2-
3-
export function generateBootstrap(options: {
4-
port: number;
5-
startupDelay: number;
6-
waitForPlugin: string;
7-
}) {
8-
return `//Code generated by the zotero-plugin-scaffold tester
1+
/* eslint-disable eslint-comments/no-unlimited-disable */
2+
/* eslint-disable */
3+
// Code generated by the zotero-plugin-scaffold tester
94
var chromeHandle;
105

116
function install(data, reason) {}
@@ -17,14 +12,14 @@ async function startup({ id, version, resourceURI, rootURI }, reason) {
1712
].getService(Components.interfaces.amIAddonManagerStartup);
1813
const manifestURI = Services.io.newURI(rootURI + "manifest.json");
1914
chromeHandle = aomStartup.registerChrome(manifestURI, [
20-
["content", "${TESTER_PLUGIN_REF}", rootURI + "content/"],
15+
["content", "__TESTER_PLUGIN_REF__", rootURI + "content/"],
2116
]);
2217

2318
launchTests().catch((error) => {
2419
Zotero.debug(error);
2520
Zotero.HTTP.request(
2621
"POST",
27-
"http://localhost:${options.port}/update",
22+
"http://localhost:__PORT__/update",
2823
{
2924
body: JSON.stringify({
3025
type: "fail",
@@ -58,9 +53,9 @@ function uninstall(data, reason) {}
5853

5954
async function launchTests() {
6055
// Delay to allow plugin to fully load before opening the test page
61-
await Zotero.Promise.delay(${options.startupDelay || 1000});
56+
await Zotero.Promise.delay(__STARTUP_DELAY__);
6257

63-
const waitForPlugin = "${options.waitForPlugin}";
58+
const waitForPlugin = "${WAIT_FOR_PLUGIN}";
6459

6560
if (waitForPlugin) {
6661
// Wait for a plugin to be installed
@@ -98,5 +93,3 @@ function waitUtilAsync(condition, interval = 100, timeout = 1e4) {
9893
}, interval);
9994
});
10095
}
101-
`;
102-
}
Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
export function generateHtml(
2-
setupCode: string,
3-
testFiles: string[],
4-
) {
5-
const tests = testFiles.map(f => `<script src="${f}"></script>`).join("\n ");
6-
7-
return `<!-- Generated by zotero-plugin-scaffold -->
8-
<!DOCTYPE html>
1+
<!-- Generated by zotero-plugin-scaffold -->
2+
<!doctype html>
93
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
10-
<head>
11-
<meta charset="UTF-8"></meta>
12-
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
137
<title>Zotero Plugin Test</title>
148
<style>
15-
html {
16-
min-width: 400px;
17-
min-height: 600px;
18-
}
19-
body {
20-
font-family: Arial, sans-serif;
21-
}
9+
html {
10+
min-width: 400px;
11+
min-height: 600px;
12+
}
13+
body {
14+
font-family: Arial, sans-serif;
15+
}
2216
</style>
23-
</head>
24-
<body>
17+
</head>
18+
<body>
2519
<div id="mocha"></div>
2620

2721
<!-- Include Zotero Vars -->
@@ -33,17 +27,15 @@
3327

3428
<!-- Setup Mocha -->
3529
<script>
36-
${setupCode}
30+
__SETUP_CODE__;
3731
</script>
3832

3933
<!-- Unit tests -->
40-
${tests}
34+
__TEST_FILES__
4135

4236
<!-- Run Mocha -->
4337
<script class="mocha-exec">
4438
mocha.run();
4539
</script>
46-
</body>
40+
</body>
4741
</html>
48-
`;
49-
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"manifest_version": 2,
3+
"name": "Zotero Plugin Scaffold Test Runner",
4+
"version": "0.0.1",
5+
"description": "Test suite for the Zotero plugin. This is a runtime-generated plugin only for testing purposes.",
6+
"applications": {
7+
"zotero": {
8+
"id": "__TESTER_PLUGIN_ID__",
9+
"update_url": "https://example.com",
10+
"strict_max_version": "999.*.*"
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)