-
Notifications
You must be signed in to change notification settings - Fork 17
/
test.ts
152 lines (135 loc) · 5.51 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
import { udd, UddOptions, UddResult } from "./mod.ts";
import { decode, encode } from "./deps.ts";
import {
FakeRegistry,
FakeDenoStd,
assert,
assertEquals,
assertThrowsAsync,
} from "./test_deps.ts";
async function testUdd(
before: string,
after: string,
registries: any[] = [FakeRegistry],
) {
const fn = await Deno.makeTempFile();
try {
await Deno.writeFile(fn, encode(before));
const results: UddResult[] = await udd(
fn,
{ _registries: registries, quiet: true } as UddOptions,
);
const altered = decode(await Deno.readFile(fn));
assertEquals(after, altered);
return results;
} finally {
await Deno.remove(fn);
}
}
Deno.test(async function uddFakeregistry() {
const contents = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts";';
const expected = 'import "https://fakeregistry.com/foo@0.0.2/mod.ts";';
await testUdd(contents, expected);
});
Deno.test(async function uddFakeregistryNotFound() {
const contents = 'import "https://fakeregistry.com/foo@0.0.3/mod.ts#=";';
const expected = 'import "https://fakeregistry.com/foo@0.0.3/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(async function uddFakeDenolandStd() {
const contents = 'import "https://deno.land/std@0.34.0/mod.ts";';
const expected = 'import "https://deno.land/std@0.35.0/mod.ts";';
await testUdd(contents, expected, [FakeDenoStd]);
});
Deno.test(async function uddFakeregistryEqToken() {
const contents = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts#=0.0.1";';
const expected = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts#=0.0.1";';
await testUdd(contents, expected);
});
Deno.test(async function uddFakeregistryTildeToken() {
const contents = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts#~0.0.1";';
const expected = 'import "https://fakeregistry.com/foo@0.0.2/mod.ts#~0.0.1";';
await testUdd(contents, expected);
});
Deno.test(async function uddFakeregistryLtToken() {
const contents = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts#<0.1.0";';
const expected = 'import "https://fakeregistry.com/foo@0.0.2/mod.ts#<0.1.0";';
await testUdd(contents, expected);
});
Deno.test(async function uddFakeregistryLtTokenSpaces() {
const contents =
'import "https://fakeregistry.com/foo@0.0.1/mod.ts# < 0.1.0";';
const expected =
'import "https://fakeregistry.com/foo@0.0.2/mod.ts# < 0.1.0";';
await testUdd(contents, expected);
});
Deno.test(async function uddFakeregistryInvalidFragmentSemver() {
const contents =
'import "https://fakeregistry.com/foo@0.0.1/mod.ts# < 0.1.b";';
const expected =
'import "https://fakeregistry.com/foo@0.0.1/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(async function uddFakeregistryInvalidFragmentFoo() {
const contents = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts#foo";';
const expected = 'import "https://fakeregistry.com/foo@0.0.1/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(async function uddFakeregistryEq() {
const contents = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts#=";';
const expected = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts#=";';
await testUdd(contents, expected);
});
Deno.test(async function uddFakeregistryTilde() {
const contents = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts#~";';
const expected = 'import "https://fakeregistry.com/foo@0.0.2/mod.ts#~";';
await testUdd(contents, expected);
});
Deno.test(async function uddFakeregistryCaret() {
const contents = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts#^";';
const expected = 'import "https://fakeregistry.com/foo@0.0.2/mod.ts#^";';
await testUdd(contents, expected);
});
Deno.test(async function uddFakeMultiple() {
const contents = `
import "https://deno.land/std@0.34.0/mod.ts";
import "https://deno.land/std@0.34.0/foo.ts";
import { foo } from "https://fakeregistry.com/foo@0.0.1/mod.ts";
import { bar } from "https://fakeregistry.com/foo@0.0.1/bar.ts#=";
`;
const expected = `
import "https://deno.land/std@0.35.0/mod.ts";
import "https://deno.land/std@0.35.0/foo.ts";
import { foo } from "https://fakeregistry.com/foo@0.0.2/mod.ts";
import { bar } from "https://fakeregistry.com/foo@0.0.1/bar.ts#=";
`;
const results = await testUdd(
contents,
expected,
[FakeRegistry, FakeDenoStd],
);
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(async function uddFakeregistryFragmentMove() {
const contents = 'import "https://fakeregistry.com/foo@~0.0.1/mod.ts";';
const expected = 'import "https://fakeregistry.com/foo@0.0.2/mod.ts#~";';
await testUdd(contents, expected);
});
Deno.test(async function uddFakeregistryFragmentMoveEq() {
const contents = 'import "https://fakeregistry.com/foo@=0.0.1/mod.ts";';
const expected = 'import "https://fakeregistry.com/foo@0.0.1/mod.ts#=";';
await testUdd(contents, expected);
});