-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathupdate_test.ts
More file actions
743 lines (647 loc) · 18.9 KB
/
update_test.ts
File metadata and controls
743 lines (647 loc) · 18.9 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
import * as path from "@std/path";
import {
FRESH_VERSION,
PREACT_SIGNALS_VERSION,
PREACT_VERSION,
updateProject,
} from "./update.ts";
import { expect } from "@std/expect";
import { spy, type SpyCall } from "@std/testing/mock";
import { walk } from "@std/fs/walk";
import { withTmpDir, writeFiles } from "../../fresh/src/test_utils.ts";
async function readFiles(dir: string): Promise<Record<string, string>> {
const files: Record<string, string> = {};
for await (
const entry of walk(dir, { includeDirs: false, includeFiles: true })
) {
const pathname = path.relative(dir, entry.path);
const content = await Deno.readTextFile(entry.path);
files[`/${pathname.replaceAll(/[\\]+/g, "/")}`] = content.trim();
}
return files;
}
Deno.test("update - remove JSX pragma import", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `import { h, Fragment } from "preact";
/** @jsx h */
/** @jsxFrag Fragment */
export default function Foo() {
return null;
}`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`export default function Foo() {
return null;
}`);
});
Deno.test("update - 1.x project deno.json", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(JSON.parse(files["/deno.json"]))
.toEqual({
imports: {
"fresh": `jsr:@fresh/core@^${FRESH_VERSION}`,
"@preact/signals": `npm:@preact/signals@^${PREACT_SIGNALS_VERSION}`,
"preact": `npm:preact@^${PREACT_VERSION}`,
},
});
});
Deno.test("update - 1.x project deno.json with imports", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{
"imports": {
"$fresh/": "foo"
}
}`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(JSON.parse(files["/deno.json"]))
.toEqual({
imports: {
"fresh": `jsr:@fresh/core@^${FRESH_VERSION}`,
"@preact/signals": `npm:@preact/signals@^${PREACT_SIGNALS_VERSION}`,
"preact": `npm:preact@^${PREACT_VERSION}`,
},
});
});
Deno.test("update - 1.x project deno.json tasks + lock", async () => {
await using tmp = await withTmpDir();
await writeFiles(tmp.dir, {
"/deno.json": `{
"lock": false,
"tasks": {
"check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
"cli": "echo \\"import '$fresh/src/dev/cli.ts'\\" | deno run --unstable -A -",
"manifest": "deno task cli manifest $(pwd)",
"start": "deno run -A --watch=static/,routes/ dev.ts",
"build": "deno run -A dev.ts build",
"preview": "deno run -A main.ts",
"update": "deno run -A -r https://fresh.deno.dev/update ."
}
}`,
});
await updateProject(tmp.dir);
const files = await readFiles(tmp.dir);
const updated = JSON.parse(files["/deno.json"]);
expect(updated.lock).toEqual(undefined);
expect(updated.tasks)
.toEqual({
build: "deno run -A dev.ts build",
check: "deno fmt --check && deno lint && deno check",
preview: "deno serve -A _fresh/server.js",
start: "deno run -A --watch=static/,routes/ dev.ts",
update: "deno run -A -r jsr:@fresh/update .",
});
});
Deno.test("update - 1.x project middlewares", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": "{}",
"/routes/_middleware.ts": `import { FreshContext } from "$fresh/server.ts";
interface State {
data: string;
}
export async function handler(
req: Request,
ctx: FreshContext<State>,
) {
ctx.state.data = "myData";
ctx.state.url = req.url;
const resp = await ctx.next();
resp.headers.set("server", "fresh server");
return resp;
}`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/_middleware.ts"])
.toEqual(`import { FreshContext } from "fresh";
interface State {
data: string;
}
export async function handler(
ctx: FreshContext<State>,
) {
const req = ctx.req;
ctx.state.data = "myData";
ctx.state.url = req.url;
const resp = await ctx.next();
resp.headers.set("server", "fresh server");
return resp;
}`);
});
Deno.test("update - 1.x project middlewares one arg", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": "{}",
"/routes/_middleware.ts": `export async function handler(req: Request) {
return new Response("hello world from: " + req.url);
}`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/_middleware.ts"])
.toEqual(`import { FreshContext } from "fresh";
export async function handler(ctx: FreshContext) {
const req = ctx.req;
return new Response("hello world from: " + req.url);
}`);
});
Deno.test("update - 1.x update '$fresh/*' imports", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `import { PageProps } from "$fresh/server.ts";
export default function Foo(props: PageProps) {
return null;
}`,
"/routes/foo.tsx": `import { asset, Head } from "$fresh/runtime.ts";`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`import { PageProps } from "fresh";
export default function Foo(props: PageProps) {
return null;
}`);
expect(files["/routes/foo.tsx"])
.toEqual(`import { asset, Head } from "fresh/runtime";`);
});
Deno.test("update - 1.x update handler signature", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
async GET(req, ctx) {},
async POST(req, ctx) {},
async PATCH(req, ctx) {},
async PUT(req, ctx) {},
async DELETE(req, ctx) {},
};`,
"/routes/foo.tsx": `import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
async GET(_req, ctx) {},
async POST(_req, ctx) {},
async PATCH(_req, ctx) {},
async PUT(_req, ctx) {},
async DELETE(_req, ctx) {},
};`,
"/routes/name.tsx": `import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
async GET(request, ctx) {},
async POST(request, ctx) {},
async PATCH(request, ctx) {},
async PUT(request, ctx) {},
async DELETE(request, ctx) {},
};`,
"/routes/name-unused.tsx": `import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
async GET(_request, ctx) {},
async POST(_request, ctx) {},
async PATCH(_request, ctx) {},
async PUT(_request, ctx) {},
async DELETE(_request, ctx) {},
};`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`import { Handlers } from "fresh/compat";
export const handler: Handlers = {
async GET(ctx) {
const req = ctx.req;
},
async POST(ctx) {
const req = ctx.req;
},
async PATCH(ctx) {
const req = ctx.req;
},
async PUT(ctx) {
const req = ctx.req;
},
async DELETE(ctx) {
const req = ctx.req;
},
};`);
expect(files["/routes/foo.tsx"])
.toEqual(`import { Handlers } from "fresh/compat";
export const handler: Handlers = {
async GET(ctx) {},
async POST(ctx) {},
async PATCH(ctx) {},
async PUT(ctx) {},
async DELETE(ctx) {},
};`);
expect(files["/routes/name.tsx"])
.toEqual(`import { Handlers } from "fresh/compat";
export const handler: Handlers = {
async GET(ctx) {
const request = ctx.req;
},
async POST(ctx) {
const request = ctx.req;
},
async PATCH(ctx) {
const request = ctx.req;
},
async PUT(ctx) {
const request = ctx.req;
},
async DELETE(ctx) {
const request = ctx.req;
},
};`);
expect(files["/routes/name-unused.tsx"])
.toEqual(`import { Handlers } from "fresh/compat";
export const handler: Handlers = {
async GET(ctx) {},
async POST(ctx) {},
async PATCH(ctx) {},
async PUT(ctx) {},
async DELETE(ctx) {},
};`);
});
Deno.test(
"update - 1.x update handler signature method one arg",
async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `export const handler: Handlers = {
GET(req) {
return Response.redirect(req.url);
},
};`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`export const handler: Handlers = {
GET(ctx) {
const req = ctx.req;
return Response.redirect(req.url);
},
};`);
},
);
Deno.test.ignore(
"update - 1.x update handler signature variable",
async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `export const handler: Handlers = {
GET: (req) => Response.redirect(req.url)
};`,
"/routes/foo.tsx": `export const handler: Handlers = {
GET: (req, ctx) => Response.redirect(req.url),
};`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`export const handler: Handlers = {
GET: (ctx) => {
const req = ctx.req;
return Response.redirect(req.url);
},
};`);
expect(files["/routes/foo.tsx"])
.toEqual(`export const handler: Handlers = {
GET: (ctx) => {
const req = ctx.req;
return Response.redirect(req.url);
},
};`);
},
);
Deno.test(
"update - 1.x update handler signature non-inferred",
async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `export const handler = {
GET(req: Request){
return Response.redirect(req.url);
}
};`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`import { FreshContext } from "fresh";
export const handler = {
GET(ctx: FreshContext) {
const req = ctx.req;
return Response.redirect(req.url);
},
};`);
},
);
Deno.test(
"update - 1.x update handler signature with destructure",
async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
async GET(req, { params, render, remoteAddr }) {},
async POST(req, { params, render, remoteAddr }) {},
async PATCH(req, { params, render, remoteAddr }) {},
async PUT(req, { params, render, remoteAddr }) {},
async DELETE(req, { params, render, remoteAddr }) {},
};`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`import { Handlers } from "fresh/compat";
export const handler: Handlers = {
async GET({ params, render, info, req }) {
const remoteAddr = info.remoteAddr;
},
async POST({ params, render, info, req }) {
const remoteAddr = info.remoteAddr;
},
async PATCH({ params, render, info, req }) {
const remoteAddr = info.remoteAddr;
},
async PUT({ params, render, info, req }) {
const remoteAddr = info.remoteAddr;
},
async DELETE({ params, render, info, req }) {
const remoteAddr = info.remoteAddr;
},
};`);
},
);
Deno.test("update - 1.x update define* handler signatures", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/_app.tsx": `import { defineApp } from "$fresh/server.ts";
export default defineApp(async (req, ctx) => {
return null;
});`,
"/routes/_layout.tsx": `import { defineLayout } from "$fresh/server.ts";
export default defineLayout(async (req, ctx) => {
return null;
});`,
"/routes/foo.tsx": `import { defineRoute } from "$fresh/server.ts";
export default defineRoute(async (req, ctx) => {
return null;
});`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/_app.tsx"])
.toEqual(`import { defineApp } from "fresh/compat";
export default defineApp(async (ctx) => {
const req = ctx.req;
return null;
});`);
expect(files["/routes/_layout.tsx"])
.toEqual(`import { defineLayout } from "fresh/compat";
export default defineLayout(async (ctx) => {
const req = ctx.req;
return null;
});`);
expect(files["/routes/foo.tsx"])
.toEqual(`import { defineRoute } from "fresh/compat";
export default defineRoute(async (ctx) => {
const req = ctx.req;
return null;
});`);
});
Deno.test(
"update - 1.x update component signature async",
async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx":
`export default async function Index(req: Request, ctx: RouteContext) {
if (true) {
return ctx.renderNotFound();
}
if ("foo" === "foo" as any) {
ctx.renderNotFound();
return ctx.renderNotFound();
}
return new Response(req.url);
}`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`import { FreshContext } from "fresh";
import { HttpError } from "fresh";
export default async function Index(ctx: FreshContext) {
const req = ctx.req;
if (true) {
throw new HttpError(404);
}
if ("foo" === "foo" as any) {
throw new HttpError(404);
throw new HttpError(404);
}
return new Response(req.url);
}`);
},
);
Deno.test.ignore(
"update - 1.x ctx.renderNotFound() -> throw new HttpError(404)",
async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
async GET(_req, ctx) {
return ctx.renderNotFound();
},
};`,
"/routes/foo.tsx": `export const handler = (ctx) => {
return ctx.renderNotFound();
}`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`import { Handlers } from "fresh";
export const handler: Handlers = {
async GET(ctx) {
throw HttpError(404);
},
};`);
expect(files["/routes/foo.tsx"])
.toEqual(`export const handler = (ctx) => {
throw HttpError(404);
};`);
},
);
Deno.test.ignore(
"update - 1.x ctx.remoteAddr -> ctx.info.remoteAddr",
async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
async GET(_req, ctx) {
let msg = ctx.remoteAddr.transport === "tcp" ? "ok" : "not ok";
msg += typeof ctx.renderNotFound === "function";
return new Response(msg);
},
};`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`import { Handlers } from "fresh";
export const handler: Handlers = {
async GET(ctx) {
let msg = ctx.info.remoteAddr.transport === "tcp" ? "ok" : "not ok";
msg += typeof ctx.throw === "function";
return new Response(msg);
},
};`);
},
);
Deno.test.ignore("update - 1.x destructured ctx members", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
async GET(_req, { url, renderNotFound, remoteAddr }) {
if (true) {
return new Response(!!remoteAddr ? "ok" : "not ok");
} else {
console.log(url.href);
return renderNotFound();
}
},
};`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/index.tsx"])
.toEqual(`import { Handlers } from "fresh";
export const handler: Handlers = {
async GET({ url, throw, info }) {
const renderNotFound = () => throw(404);
const remoteAddr = info.remoteAddr;
if (true) {
return new Response(!!remoteAddr ? "ok" : "not ok");
} else {
console.log(url.href);
return renderNotFound();
}
},
};`);
});
Deno.test("update - 1.x remove reference comments", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/main.ts": `/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/routes/main.ts"]).toEqual("");
});
Deno.test("update - island files", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/islands/foo.tsx": `import { IS_BROWSER } from "$fresh/runtime.ts";`,
});
await updateProject(dir);
const files = await readFiles(dir);
expect(files["/islands/foo.tsx"]).toEqual(
`import { IS_BROWSER } from "fresh/runtime";`,
);
});
Deno.test("update - ignores node_modules and vendor in logs", async () => {
await using _tmp = await withTmpDir();
const dir = _tmp.dir;
await writeFiles(dir, {
"/deno.json": `{}`,
"/routes/index.tsx": `import { PageProps } from "$fresh/server.ts";
export default function Foo(props: PageProps) {
return null;
}`,
"/node_modules/foo/bar.ts":
`import { IS_BROWSER } from "$fresh/runtime.ts";`,
"/vendor/foo/bar.ts": `import { IS_BROWSER } from "$fresh/runtime.ts";`,
});
const consoleLogSpy = spy(console, "log");
try {
await updateProject(dir);
} finally {
consoleLogSpy.restore();
}
const files = await readFiles(dir);
// Should leave dependency trees untouched
expect(files["/node_modules/foo/bar.ts"]).toEqual(
`import { IS_BROWSER } from "$fresh/runtime.ts";`,
);
expect(files["/vendor/foo/bar.ts"]).toEqual(
`import { IS_BROWSER } from "$fresh/runtime.ts";`,
);
expect(files["/routes/index.tsx"]).toEqual(
`import { PageProps } from "fresh";
export default function Foo(props: PageProps) {
return null;
}`,
);
const fullLog = consoleLogSpy.calls.map((call: SpyCall) =>
call.args.join(" ")
).join(
"\n",
);
expect(fullLog).toMatch(/Total files processed: 1/);
expect(fullLog).toMatch(/Successfully modified: 1/);
expect(fullLog).toMatch(/Unmodified \(no changes needed\): 0/);
expect(fullLog).not.toMatch(/node_modules/);
expect(fullLog).not.toMatch(/vendor/);
expect(fullLog).toMatch(/✓ routes\/index.tsx/);
});