-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.ts
161 lines (143 loc) · 5.99 KB
/
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
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
import { udd, UddOptions, UddResult } from "./mod.ts";
import { RegistryCtor } from "./registry.ts";
import { assertEquals, FakeDenoLand, FakeRegistry } from "./test_deps.ts";
async function testUdd(
before: string,
after: string,
registries: RegistryCtor[] = [FakeRegistry],
) {
const fn = await Deno.makeTempFile();
try {
const encoder = new TextEncoder();
await Deno.writeFile(fn, encoder.encode(before));
const results: UddResult[] = await udd(
fn,
{ _registries: registries, quiet: true } as UddOptions,
);
const decoder = new TextDecoder();
const altered = decoder.decode(await Deno.readFile(fn));
assertEquals(after, altered);
return results;
} finally {
await Deno.remove(fn);
}
}
Deno.test("uddFakeregistry", async () => {
const contents = 'import "https://fakeregistry.com/[email protected]/mod.ts";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts";';
await testUdd(contents, expected);
});
Deno.test("uddFakeregistryNotFound", async () => {
const contents = 'import "https://fakeregistry.com/[email protected]/mod.ts#=";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts#=";';
const results = await testUdd(contents, expected);
assertEquals(1, results.length);
assertEquals(false, results[0].success);
assertEquals("no compatible version found", results[0].message);
});
Deno.test("uddFakeDenolandStd", async () => {
const contents = 'import "https://deno.land/[email protected]/mod.ts";';
const expected = 'import "https://deno.land/[email protected]/mod.ts";';
await testUdd(contents, expected, [FakeDenoLand]);
});
Deno.test("uddFakeregistryEqToken", async () => {
const contents = 'import "https://fakeregistry.com/[email protected]/mod.ts#=0.0.1";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts#=0.0.1";';
await testUdd(contents, expected);
});
Deno.test("uddFakeregistryTildeToken", async () => {
const contents = 'import "https://fakeregistry.com/[email protected]/mod.ts#~0.0.1";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts#~0.0.1";';
await testUdd(contents, expected);
});
Deno.test("uddFakeregistryLtToken", async () => {
const contents = 'import "https://fakeregistry.com/[email protected]/mod.ts#<0.1.0";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts#<0.1.0";';
await testUdd(contents, expected);
});
Deno.test("uddFakeregistryLtTokenSpaces", async () => {
const contents =
'import "https://fakeregistry.com/[email protected]/mod.ts# < 0.1.0";';
const expected =
'import "https://fakeregistry.com/[email protected]/mod.ts# < 0.1.0";';
await testUdd(contents, expected);
});
Deno.test("uddFakeregistryInvalidFragmentSemver", async () => {
const contents =
'import "https://fakeregistry.com/[email protected]/mod.ts# < 0.1.b";';
const expected =
'import "https://fakeregistry.com/[email protected]/mod.ts# < 0.1.b";';
const results = await testUdd(contents, expected);
assertEquals(results.length, 1);
assertEquals(results[0].success, false);
assertEquals(results[0].message, "invalid semver version: 0.1.b");
});
Deno.test("uddFakeregistryInvalidFragmentFoo", async () => {
const contents = 'import "https://fakeregistry.com/[email protected]/mod.ts#foo";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts#foo";';
const results = await testUdd(contents, expected);
assertEquals(results.length, 1);
assertEquals(results[0].success, false);
assertEquals(results[0].message, "invalid semver fragment: foo");
});
Deno.test("uddFakeregistryEq", async () => {
const contents = 'import "https://fakeregistry.com/[email protected]/mod.ts#=";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts#=";';
await testUdd(contents, expected);
});
Deno.test("uddFakeregistryTilde", async () => {
const contents = 'import "https://fakeregistry.com/[email protected]/mod.ts#~";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts#~";';
await testUdd(contents, expected);
});
Deno.test("uddFakeregistryCaret", async () => {
const contents = 'import "https://fakeregistry.com/[email protected]/mod.ts#^";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts#^";';
await testUdd(contents, expected);
});
Deno.test("uddFakeMultiple", async () => {
const contents = `
import "https://deno.land/[email protected]/mod.ts";
import "https://deno.land/[email protected]/foo.ts";
import { foo } from "https://fakeregistry.com/[email protected]/mod.ts";
import { bar } from "https://fakeregistry.com/[email protected]/bar.ts#=";
`;
const expected = `
import "https://deno.land/[email protected]/mod.ts";
import "https://deno.land/[email protected]/foo.ts";
import { foo } from "https://fakeregistry.com/[email protected]/mod.ts";
import { bar } from "https://fakeregistry.com/[email protected]/bar.ts#=";
`;
const results = await testUdd(
contents,
expected,
[FakeRegistry, FakeDenoLand],
);
assertEquals(4, results.length);
// the ordering is a little weird...
// (it corresponds to the order passed registries)
// FIXME make less fragile by improving search.ts to provide urls in order
assertEquals([true, undefined, true, true], results.map((x) => x.success));
});
Deno.test("uddFakeregistryFragmentMove", async () => {
const contents = 'import "https://fakeregistry.com/foo@~0.0.1/mod.ts";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts#~";';
await testUdd(contents, expected);
});
Deno.test("uddFakeregistryFragmentMoveEq", async () => {
const contents = 'import "https://fakeregistry.com/foo@=0.0.1/mod.ts";';
const expected = 'import "https://fakeregistry.com/[email protected]/mod.ts#=";';
await testUdd(contents, expected);
});
Deno.test("uddGitHubRawBranchName", async () => {
const contents = `
import "https://raw.githubusercontent.com/foo/bar/main/mod.ts";
import "https://raw.githubusercontent.com/foo/bar/main/mod.ts#=";
`;
const expected = `
import "https://raw.githubusercontent.com/foo/bar/main/mod.ts";
import "https://raw.githubusercontent.com/foo/bar/main/mod.ts#=";
`;
const results = await testUdd(contents, expected);
assertEquals(results.length, 0);
});