Repository navigation
Expand file tree
/
Copy pathplugins.test.ts
More file actions
224 lines (211 loc) · 8.46 KB
/
Copy pathplugins.test.ts
File metadata and controls
224 lines (211 loc) · 8.46 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { expect, it } from "vitest";
import infoJson from "./info.json" with { type: "json" };
import { getPluginRepo, tryResolveAssetUrl, tryResolveLatestJson } from "./plugins.js";
import { getLatestReleaseInfo } from "./utils/github.js";
function resolveAsset(url: string) {
return tryResolveAssetUrl(new URL(url));
}
it("getPluginRepo", () => {
expect(getPluginRepo({ repoUrl: "https://github.com/dprint/dprint-plugin-typescript" })).toEqual({
username: "dprint",
repoName: "dprint-plugin-typescript",
});
expect(getPluginRepo({ repoUrl: "https://github.com/g-plane/malva" })).toEqual({
username: "g-plane",
repoName: "malva",
});
});
it("info.json entries should have a display name and say what repo they're published from", () => {
for (const plugin of infoJson.latest) {
expect(plugin.displayName, plugin.name).toBeTruthy();
expect(plugin.repoUrl, plugin.name).toMatch(/^https:\/\/github\.com\/[A-Za-z0-9-]+\/[A-Za-z0-9._-]+$/);
}
});
it("tryResolveAssetUrl", () => {
// allowed repo — should cache
expect(
resolveAsset(
"https://plugins.dprint.dev/dprint/dprint-plugin-prettier/0.67.0/asset/dprint-plugin-prettier-x86_64-apple-darwin.zip",
),
).toEqual({
githubUrl:
"https://github.com/dprint/dprint-plugin-prettier/releases/download/0.67.0/dprint-plugin-prettier-x86_64-apple-darwin.zip",
shouldCache: true,
});
// latest tag — still resolves but not cached
expect(
resolveAsset(
"https://plugins.dprint.dev/dprint/dprint-plugin-prettier/latest/asset/dprint-plugin-prettier-x86_64-apple-darwin.zip",
),
).toEqual({
githubUrl:
"https://github.com/dprint/dprint-plugin-prettier/releases/download/latest/dprint-plugin-prettier-x86_64-apple-darwin.zip",
shouldCache: true,
});
// different repo in dprint org — should cache
expect(
resolveAsset("https://plugins.dprint.dev/dprint/dprint-plugin-exec/0.5.0/asset/some-binary.zip"),
).toEqual({
githubUrl: "https://github.com/dprint/dprint-plugin-exec/releases/download/0.5.0/some-binary.zip",
shouldCache: true,
});
// approved community repo — should cache
expect(
resolveAsset(
"https://plugins.dprint.dev/drluckyspin/dprint-plugin-swift/v0.1.0/asset/dprint-plugin-swift-aarch64-apple-darwin.zip",
),
).toEqual({
githubUrl:
"https://github.com/drluckyspin/dprint-plugin-swift/releases/download/v0.1.0/dprint-plugin-swift-aarch64-apple-darwin.zip",
shouldCache: true,
});
// org not on allow list — resolves but should not cache (redirect)
expect(
resolveAsset("https://plugins.dprint.dev/someone/some-repo/0.1.0/asset/file.zip"),
).toEqual({
githubUrl: "https://github.com/someone/some-repo/releases/download/0.1.0/file.zip",
shouldCache: false,
});
// non-matching URL
expect(
resolveAsset("https://plugins.dprint.dev/dprint/dprint-plugin-prettier/0.67.0/file.zip"),
).toEqual(undefined);
});
it("tryResolveUserLatestJson", async () => {
// non-matching
expect(
await tryResolveLatestJson(
new URL("https://plugins.dprint.dev/dsherret/latest.json"),
),
).toEqual(undefined);
expect(
await tryResolveLatestJson(
new URL("https://plugins.dprint.dev/dsherret/non-existent/latest.json"),
),
).toEqual(404);
// dprint repo
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/dprint/typescript/latest.json");
const releaseInfo = await getLatestReleaseInfo("dprint", "dprint-plugin-typescript");
expect(releaseInfo?.checksum?.length).toEqual(64);
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/typescript-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName,
checksum: releaseInfo!.checksum,
npm: { name: "@dprint/typescript" },
});
}
// dprint repo full name
{
const result = await getValidResultForUrl(
"https://plugins.dprint.dev/dprint/dprint-plugin-typescript/latest.json",
);
const releaseInfo = await getLatestReleaseInfo("dprint", "dprint-plugin-typescript");
expect(releaseInfo?.checksum?.length).toEqual(64);
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/typescript-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName,
checksum: releaseInfo!.checksum,
npm: { name: "@dprint/typescript" },
});
}
// non-dprint repo
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/malobre/vue/latest.json");
const releaseInfo = await getLatestReleaseInfo("malobre", "dprint-plugin-vue");
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/malobre/vue-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName.replace(/^v/, ""),
checksum: releaseInfo!.checksum,
});
}
// non-dprint repo full name
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/malobre/dprint-plugin-vue/latest.json");
const releaseInfo = await getLatestReleaseInfo("malobre", "dprint-plugin-vue");
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/malobre/vue-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName.replace(/^v/, ""),
checksum: releaseInfo!.checksum,
});
}
// process plugin repo
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/dprint/prettier/latest.json");
const releaseInfo = await getLatestReleaseInfo("dprint", "dprint-plugin-prettier");
expect(releaseInfo?.checksum?.length).toEqual(64);
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/prettier-${releaseInfo!.tagName}.json`,
version: releaseInfo!.tagName,
checksum: releaseInfo!.checksum,
npm: { name: "@dprint/prettier" },
});
}
// community repo published to npm
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/g-plane/malva/latest.json");
const releaseInfo = await getLatestReleaseInfo("g-plane", "malva");
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/g-plane/malva-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName.replace(/^v/, ""),
checksum: releaseInfo!.checksum,
npm: { name: "dprint-plugin-malva" },
});
}
// plugin replaced by one in the dprint org
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/kachick/kdl/latest.json");
const releaseInfo = await getLatestReleaseInfo("dprint", "dprint-plugin-kdl");
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/kachick/kdl-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName,
checksum: releaseInfo!.checksum,
deprecated:
"Replaced by dprint/dprint-plugin-kdl. Remove the kachick/kdl plugin from your config, then run `dprint add kdl`.",
});
}
// community plugins aren't resolved by the name they have in info.json, so
// they can't claim a name in the dprint org
for (const name of ["dprint_plugin_malva", "cmakefmt"]) {
expect(
await tryResolveLatestJson(new URL(`https://plugins.dprint.dev/dprint/${name}/latest.json`)),
).toEqual(404);
}
// community repo that doesn't publish its plugin as plugin.wasm
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/cmakefmt/cmakefmt/latest.json");
const releaseInfo = await getLatestReleaseInfo("cmakefmt", "cmakefmt", "dprint-cmakefmt.wasm");
expect(releaseInfo?.checksum?.length).toEqual(64);
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/cmakefmt/cmakefmt-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName.replace(/^v/, ""),
checksum: releaseInfo!.checksum,
});
}
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/azais-corentin/cmakefmt/latest.json");
const releaseInfo = await getLatestReleaseInfo("azais-corentin", "cmakefmt", "cmakefmt-dprint.wasm");
expect(releaseInfo?.checksum?.length).toEqual(64);
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/azais-corentin/cmakefmt-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName.replace(/^v/, ""),
checksum: releaseInfo!.checksum,
});
}
async function getValidResultForUrl(url: string) {
const result = await tryResolveLatestJson(new URL(url))!;
if (result == null || result === 404) {
throw new Error("Expected result.");
}
return result;
}
});