forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidators.test.ts
87 lines (76 loc) · 2.64 KB
/
validators.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
85
86
87
import { describe, expect, test } from "vitest";
import {
isAllowedExistingFile,
validateProjectDirectory,
validateTemplateUrl,
} from "../validators";
describe("validators", () => {
describe("validateProjectDirectory", () => {
let args = {};
test("allow valid project names", async () => {
expect(validateProjectDirectory("foo", args)).toBeUndefined();
expect(validateProjectDirectory("foo/bar/baz", args)).toBeUndefined();
expect(validateProjectDirectory("./foobar", args)).toBeUndefined();
expect(validateProjectDirectory("f".repeat(58), args)).toBeUndefined();
});
test("disallow invalid project names", async () => {
// Invalid pages project names should return an error
expect(validateProjectDirectory("foobar-", args)).not.toBeUndefined();
expect(validateProjectDirectory("-foobar-", args)).not.toBeUndefined();
expect(validateProjectDirectory("fo*o{ba)r", args)).not.toBeUndefined();
expect(
validateProjectDirectory("f".repeat(59), args),
).not.toBeUndefined();
});
test("disallow existing, non-empty directories", async () => {
// Existing, non-empty directories should return an error
expect(validateProjectDirectory(".", args)).not.toBeUndefined();
});
test("Relax validation when --existing-script is passed", async () => {
args = { existingScript: "FooBar" };
expect(validateProjectDirectory("foobar-", args)).toBeUndefined();
expect(validateProjectDirectory("FooBar", args)).toBeUndefined();
expect(validateProjectDirectory("f".repeat(59), args)).toBeUndefined();
});
});
describe("isAllowedExistingFile", () => {
const allowed = [
"LICENSE",
"LICENSE.md",
"license",
".npmignore",
".git",
".DS_Store",
];
test.each(allowed)("%s", (val) => {
expect(isAllowedExistingFile(val)).toBe(true);
});
const disallowed = ["foobar", "potato"];
test.each(disallowed)("%s", (val) => {
expect(isAllowedExistingFile(val)).toBe(false);
});
});
describe("validateTemplateUrl", () => {
const allowed = [
"https://github.com/cloudflare/workers-sdk/templates/foo",
"cloudflare/workers-sdk/templates/foo",
"user/my-template",
"user/my-template#experimental",
"user/my-template#99bf5f8653bd026555cceffa61ee9120eb2c4645",
"[email protected]:user/my-template.git",
"bitbucket:user/my-template",
"gitlab:user/my-template",
];
test.each(allowed)("%s", (val) => {
expect(validateTemplateUrl(val)).toBeUndefined();
});
const disallowed = [
"potato",
"http://foo.com/user/my-template",
"ftp://foo.com/user/my-template",
];
test.each(disallowed)("%s", (val) => {
expect(validateTemplateUrl(val)).toEqual(expect.any(String));
});
});
});