/
server_helpers_test.ts
84 lines (78 loc) · 3.25 KB
/
server_helpers_test.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
import { SEP } from "std/path/separator.ts";
import { assert, assertEquals } from "std/testing/asserts.ts";
import { existsDir, existsFile, MagicString, restoreUrl, toLocalPath } from "../server/helpers.ts";
import { parseDeps } from "../server/deps.ts";
Deno.test("server/helper.ts", async (t) => {
await t.step(`existsDir`, async () => {
// true test cases
const dir = await Deno.makeTempDir();
assert(await existsDir(dir));
assert(await existsDir(await Deno.realPath(getStandardFolder())));
// false test cases
const file = await Deno.makeTempFile();
assertEquals(await existsDir(file), false);
assertEquals(await existsDir(`${dir}${SEP}foo${SEP}bar`), false);
});
await t.step(`existsFile`, async () => {
// true test cases
const file = await Deno.makeTempFile();
assert(await existsFile(file));
// false test cases
const dir = await Deno.makeTempDir();
assert(!await existsFile(`${dir}`));
assert(!await existsFile(`${dir}${SEP}foo${SEP}bar`));
});
await t.step("toLocalPath", () => {
assertEquals(toLocalPath("https://foo.com/lib@0.1.0?action"), "/-/foo.com/lib@0.1.0?action");
assertEquals(toLocalPath("https://deno.land/x/aleph@0.1.0/"), "/-/deno.land/x/aleph@0.1.0");
assertEquals(toLocalPath("http://foo.com/bar?lang=us-en"), "/-/http_foo.com/bar?lang=us-en");
assertEquals(toLocalPath("http://foo.com:8080/bar"), "/-/http_foo.com_8080/bar");
assertEquals(toLocalPath("file://foo/bar/"), "file://foo/bar/");
assertEquals(toLocalPath("/foo/bar/"), "/foo/bar/");
});
await t.step("restoreUrl", () => {
assertEquals(restoreUrl("/-/foo.com/lib@0.1.0?action"), "https://foo.com/lib@0.1.0?action");
assertEquals(restoreUrl("/-/deno.land/x/aleph@0.1.0"), "https://deno.land/x/aleph@0.1.0");
assertEquals(restoreUrl("/-/http_foo.com/bar?lang=us-en"), "http://foo.com/bar?lang=us-en");
assertEquals(restoreUrl("/-/http_foo.com_8080/bar"), "http://foo.com:8080/bar");
});
await t.step("MagicString", async () => {
const code = `// Deno 🦕 App (应用)
import React from "htts://esm.sh/react";
import foo from "./foo.js";
import { bar } from './bar.js';
const baz = await import('./baz.js');
const worker = new Worker('./worker.js', { type: 'module' });
`;
const overwritedCode = `// Deno 🦕 App (应用)
import React from "htts://esm.sh/react?dev";
import foo from "./foo.js?v=123";
import { bar } from "./bar.js?v=123";
const baz = await import("./baz.js?v=123");
const worker = new Worker("./worker.js?v=123", { type: 'module' });
`;
const deps = await parseDeps("./app.js", code);
const m = new MagicString(code);
for (const dep of deps) {
if (dep.loc) {
let url = dep.specifier;
if (url.startsWith("htts://esm.sh/")) {
url += "?dev";
} else {
url += "?v=123";
}
m.overwrite(dep.loc.start - 1, dep.loc.end - 1, `"${url}"`);
}
}
assertEquals(m.toString(), overwritedCode);
});
});
/**
* Returns an operating system-specific
* example folder.
* @returns 'C:\Windows' for Windows or
* '/tmp' for unix-based operating systems
*/
const getStandardFolder = () => {
return Deno.build.os === "windows" ? "C:\\Windows" : "/tmp";
};