-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcommands.test.ts
689 lines (603 loc) · 19.7 KB
/
commands.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
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
import * as path from "path";
import * as fs from "fs";
import * as kl from "kolorist";
import {
DenoJson,
enableYarnBerry,
isDirectory,
isFile,
runInTempDir,
runJsr,
withTempEnv,
} from "./test_utils";
import * as assert from "node:assert/strict";
import {
exec,
ExecError,
PkgJson,
readJson,
readTextFile,
writeJson,
writeTextFile,
} from "../src/utils";
describe("install", () => {
it("jsr i @std/encoding - resolve latest version", async () => {
await withTempEnv(["i", "@std/encoding"], async (dir) => {
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.ok(
pkgJson.dependencies && "@std/encoding" in pkgJson.dependencies,
"Missing dependency entry",
);
assert.match(
pkgJson.dependencies["@std/encoding"],
/^npm:@jsr\/std__encoding@\^\d+\.\d+\.\d+.*$/,
);
assert.equal(pkgJson.type, "module");
const depPath = path.join(dir, "node_modules", "@std", "encoding");
assert.ok(await isDirectory(depPath), "Not installed in node_modules");
const npmrcPath = path.join(dir, ".npmrc");
const npmRc = await readTextFile(npmrcPath);
assert.ok(
npmRc.includes("@jsr:registry=https://npm.jsr.io"),
"Missing npmrc registry",
);
});
});
it("jsr i @std/encoding - resolve latest version in yarn berry", async () => {
await runInTempDir(async (dir) => {
await enableYarnBerry(dir);
await writeTextFile(path.join(dir, "yarn.lock"), "");
await runJsr(["i", "@std/encoding"], dir);
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.ok(
pkgJson.dependencies && "@std/encoding" in pkgJson.dependencies,
"Missing dependency entry",
);
assert.match(
pkgJson.dependencies["@std/encoding"],
/^npm:@jsr\/std__encoding@\^\d+\.\d+\.\d+.*$/,
);
const yarnRc = await readTextFile(path.join(dir, ".yarnrc.yml"));
assert.ok(
/jsr:\s*npmRegistryServer: "https:\/\/npm\.jsr\.io"/.test(yarnRc),
"Missing yarnrc.yml registry",
);
});
});
it("jsr i @std/encoding - adds to nearest package.json", async () => {
await runInTempDir(async (dir) => {
const parentPkgJson = { name: "", private: true };
const parentPkgJsonPath = path.join(dir, "package.json");
await writeJson<PkgJson>(parentPkgJsonPath, parentPkgJson);
// Create sub folder with package.json
const subPkgJsonPath = path.join(dir, "sub", "package.json");
await writeJson(subPkgJsonPath, { name: "foo" });
await runJsr(["i", "@std/encoding"], path.join(dir, "sub"));
assert.deepEqual(
await readJson<PkgJson>(path.join(dir, "package.json")),
parentPkgJson,
);
const pkgJson = await readJson<PkgJson>(subPkgJsonPath);
assert.ok(
pkgJson.dependencies && "@std/encoding" in pkgJson.dependencies,
"Missing dependency entry",
);
assert.match(
pkgJson.dependencies["@std/encoding"],
/^npm:@jsr\/std__encoding@\^\d+\.\d+\.\d+.*$/,
);
});
});
it("jsr i @std/[email protected] - with version", async () => {
await withTempEnv(["i", "@std/[email protected]"], async (dir) => {
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.deepEqual(pkgJson.dependencies, {
"@std/encoding": "npm:@jsr/std__encoding@^0.216.0",
});
});
});
it("jsr install @std/[email protected] - command", async () => {
await withTempEnv(["i", "@std/[email protected]"], async (dir) => {
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.deepEqual(pkgJson.dependencies, {
"@std/encoding": "npm:@jsr/std__encoding@^0.216.0",
});
});
});
it("jsr add @std/[email protected] - command", async () => {
await withTempEnv(["i", "@std/[email protected]"], async (dir) => {
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.deepEqual(pkgJson.dependencies, {
"@std/encoding": "npm:@jsr/std__encoding@^0.216.0",
});
});
});
it("jsr add -D @std/[email protected] - dev dependency", async () => {
await withTempEnv(
["i", "-D", "@std/[email protected]"],
async (dir) => {
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.deepEqual(pkgJson.devDependencies, {
"@std/encoding": "npm:@jsr/std__encoding@^0.216.0",
});
},
);
await withTempEnv(
["i", "--save-dev", "@std/[email protected]"],
async (dir) => {
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.deepEqual(pkgJson.devDependencies, {
"@std/encoding": "npm:@jsr/std__encoding@^0.216.0",
});
},
);
await runInTempDir(async (dir) => {
await enableYarnBerry(dir);
await writeTextFile(path.join(dir, "yarn.lock"), "");
await runJsr(["i", "--save-dev", "@std/[email protected]"], dir);
assert.ok(
await isFile(path.join(dir, ".yarnrc.yml")),
"yarnrc file not created",
);
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.deepEqual(pkgJson.devDependencies, {
"@std/encoding": "npm:@jsr/[email protected]",
});
});
if (process.platform !== "win32") {
await withTempEnv(
["i", "--bun", "--save-dev", "@std/[email protected]"],
async (dir) => {
assert.ok(
await isFile(path.join(dir, "bun.lockb")),
"bun lockfile not created",
);
const pkgJson = await readJson<PkgJson>(
path.join(dir, "package.json"),
);
assert.deepEqual(pkgJson.devDependencies, {
"@std/encoding": "npm:@jsr/[email protected]",
});
},
);
}
});
it("jsr add -O @std/[email protected] - dev dependency", async () => {
await withTempEnv(
["i", "-O", "@std/[email protected]"],
async (dir) => {
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.deepEqual(pkgJson.optionalDependencies, {
"@std/encoding": "npm:@jsr/std__encoding@^0.216.0",
});
},
);
await withTempEnv(
["i", "--save-optional", "@std/[email protected]"],
async (dir) => {
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.deepEqual(pkgJson.optionalDependencies, {
"@std/encoding": "npm:@jsr/std__encoding@^0.216.0",
});
},
);
await runInTempDir(async (dir) => {
await enableYarnBerry(dir);
await fs.promises.writeFile(path.join(dir, "yarn.lock"), "");
await runJsr(["i", "--save-optional", "@std/[email protected]"], dir);
assert.ok(
await isFile(path.join(dir, ".yarnrc.yml")),
"yarnrc file not created",
);
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.deepEqual(pkgJson.optionalDependencies, {
"@std/encoding": "npm:@jsr/[email protected]",
});
});
if (process.platform !== "win32") {
await withTempEnv(
["i", "--bun", "--save-optional", "@std/[email protected]"],
async (dir) => {
assert.ok(
await isFile(path.join(dir, "bun.lockb")),
"bun lockfile not created",
);
const pkgJson = await readJson<PkgJson>(
path.join(dir, "package.json"),
);
assert.deepEqual(pkgJson.optionalDependencies, {
"@std/encoding": "npm:@jsr/[email protected]",
});
},
);
}
});
it("jsr i - runs '<pkg-manager> install' instead", async () => {
await runInTempDir(async (dir) => {
await runJsr(["i", "@std/encoding"], dir);
assert.ok(
fs.existsSync(path.join(dir, "node_modules")),
"No node_modules created.",
);
await fs.promises.rm(path.join(dir, "node_modules"), { recursive: true });
await runJsr(["i"], dir);
assert.ok(
fs.existsSync(path.join(dir, "node_modules")),
"No node_modules created.",
);
});
});
it("jsr add --npm @std/[email protected] - forces npm", async () => {
await withTempEnv(
["i", "--npm", "@std/[email protected]"],
async (dir) => {
assert.ok(
await isFile(path.join(dir, "package-lock.json")),
"npm lockfile not created",
);
},
);
});
it("jsr add --yarn @std/[email protected] - forces yarn", async () => {
await withTempEnv(
["i", "--yarn", "@std/[email protected]"],
async (dir) => {
assert.ok(
await isFile(path.join(dir, "yarn.lock")),
"yarn lockfile not created",
);
},
);
await runInTempDir(async (dir) => {
await enableYarnBerry(dir);
await runJsr(["i", "--yarn", "@std/[email protected]"], dir);
assert.ok(
await isFile(path.join(dir, "yarn.lock")),
"yarn lockfile not created",
);
assert.ok(
await isFile(path.join(dir, ".yarnrc.yml")),
"yarnrc file not created",
);
});
});
it("jsr add --pnpm @std/[email protected] - forces pnpm", async () => {
await withTempEnv(
["i", "--pnpm", "@std/[email protected]"],
async (dir) => {
assert.ok(
await isFile(path.join(dir, "pnpm-lock.yaml")),
"pnpm lockfile not created",
);
},
);
});
it("pnpm install into existing project", async () => {
await runInTempDir(async (dir) => {
const sub = path.join(dir, "sub", "sub1");
await fs.promises.mkdir(sub, {
recursive: true,
});
await exec("pnpm", ["i", "preact"], dir);
await runJsr(["i", "--pnpm", "@std/[email protected]"], sub);
assert.ok(
await isFile(path.join(dir, "pnpm-lock.yaml")),
"pnpm lockfile not created",
);
});
});
if (process.platform !== "win32") {
it("jsr add --bun @std/[email protected] - forces bun", async () => {
await withTempEnv(
["i", "--bun", "@std/[email protected]"],
async (dir) => {
assert.ok(
await isFile(path.join(dir, "bun.lockb")),
"bun lockfile not created",
);
const config = await readTextFile(path.join(dir, "bunfig.toml"));
assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created");
},
);
});
it("jsr add --bun @std/[email protected] - forces bun for twice", async () => {
await withTempEnv(
["i", "--bun", "@std/[email protected]"],
async (dir) => {
await runJsr(["i", "--bun", "@std/[email protected]"], dir);
const config = await readTextFile(path.join(dir, "bunfig.toml"));
assert.match(config, /"@jsr"\s+=/, "bunfig.toml not created");
},
);
});
}
describe("env detection", () => {
it("detect pnpm from npm_config_user_agent", async () => {
await withTempEnv(
["i", "@std/[email protected]"],
async (dir) => {
assert.ok(
await isFile(path.join(dir, "pnpm-lock.yaml")),
"pnpm lockfile not created",
);
},
{
env: {
...process.env,
npm_config_user_agent:
`pnpm/8.14.3 ${process.env.npm_config_user_agent}`,
},
},
);
});
it("overwrite detection with arg from npm_config_user_agent", async () => {
await withTempEnv(
["i", "--npm", "@std/[email protected]"],
async (dir) => {
assert.ok(
await isFile(path.join(dir, "package-lock.json")),
"npm lockfile not created",
);
},
{
env: {
...process.env,
npm_config_user_agent:
`pnpm/8.14.3 ${process.env.npm_config_user_agent}`,
},
},
);
});
it("detect yarn from npm_config_user_agent", async () => {
await withTempEnv(
["i", "@std/[email protected]"],
async (dir) => {
assert.ok(
await isFile(path.join(dir, "yarn.lock")),
"yarn lockfile not created",
);
},
{
env: {
...process.env,
npm_config_user_agent:
`yarn/1.22.19 ${process.env.npm_config_user_agent}`,
},
},
);
await runInTempDir(async (dir) => {
await enableYarnBerry(dir);
await runJsr(["i", "@std/[email protected]"], dir, {
npm_config_user_agent:
`yarn/4.1.0 ${process.env.npm_config_user_agent}`,
});
assert.ok(
await isFile(path.join(dir, "yarn.lock")),
"yarn lockfile not created",
);
assert.ok(
await isFile(path.join(dir, ".yarnrc.yml")),
"yarnrc file not created",
);
});
});
if (process.platform !== "win32") {
it("detect bun from npm_config_user_agent", async () => {
await withTempEnv(
["i", "@std/[email protected]"],
async (dir) => {
assert.ok(
await isFile(path.join(dir, "bun.lockb")),
"bun lockfile not created",
);
},
{
env: {
...process.env,
npm_config_user_agent:
`bun/1.0.29 ${process.env.npm_config_user_agent}`,
},
},
);
});
}
});
});
describe("remove", () => {
it("jsr r @std/[email protected] - removes node_modules", async () => {
await withTempEnv(
["i", "@std/[email protected]"],
async (dir) => {
await runJsr(["r", "@std/encoding"], dir);
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.equal(pkgJson.dependencies, undefined);
const depPath = path.join(dir, "node_modules", "@std", "encoding");
assert.ok(
!(await isDirectory(depPath)),
"Folder in node_modules not removed",
);
},
);
});
it("jsr r @std/[email protected] - command", async () => {
await withTempEnv(
["i", "@std/[email protected]"],
async (dir) => {
await runJsr(["r", "@std/encoding"], dir);
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.equal(pkgJson.dependencies, undefined);
},
);
});
it("jsr remove @std/[email protected] - command", async () => {
await withTempEnv(
["i", "@std/[email protected]"],
async (dir) => {
await runJsr(["remove", "@std/encoding"], dir);
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.equal(pkgJson.dependencies, undefined);
},
);
});
it("jsr uninstall @std/[email protected] - command", async () => {
await withTempEnv(
["i", "@std/[email protected]"],
async (dir) => {
await runJsr(["uninstall", "@std/encoding"], dir);
const pkgJson = await readJson<PkgJson>(path.join(dir, "package.json"));
assert.equal(pkgJson.dependencies, undefined);
},
);
});
});
describe("publish", () => {
it("should publish a package", async () => {
await runInTempDir(async (dir) => {
const pkgJsonPath = path.join(dir, "package.json");
const pkgJson = await readJson<PkgJson>(pkgJsonPath);
pkgJson.exports = {
".": "./mod.js",
};
await writeJson(pkgJsonPath, pkgJson);
await writeTextFile(
path.join(dir, "mod.ts"),
"export const value = 42;",
);
// TODO: Change this once deno supports jsr.json
await writeJson<DenoJson>(path.join(dir, "deno.json"), {
name: "@deno/jsr-cli-test",
version: pkgJson.version!,
exports: {
".": "./mod.ts",
},
});
await runJsr(["publish", "--dry-run"], dir);
});
}).timeout(600000);
it("should not add unstable publish flags for a Deno project", async () => {
await runInTempDir(async (dir) => {
const pkgJsonPath = path.join(dir, "package.json");
await fs.promises.rm(pkgJsonPath);
await writeTextFile(
path.join(dir, "mod.ts"),
["import * as fs from 'fs';", "console.log(fs)"].join("\n"),
);
await writeJson<DenoJson>(path.join(dir, "deno.json"), {
name: "@deno/jsr-cli-test",
version: "0.0.1",
exports: {
".": "./mod.ts",
},
});
try {
await runJsr(["publish", "--dry-run"], dir);
assert.fail();
} catch (err) {
assert.ok(err instanceof ExecError, `Unknown exec error thrown`);
}
});
}).timeout(600000);
it("should leave node_modules as is", async () => {
await runInTempDir(async (dir) => {
const pkgJsonPath = path.join(dir, "package.json");
const pkgJson = await readJson<PkgJson>(pkgJsonPath);
pkgJson.exports = {
".": "./mod.js",
};
await writeJson(pkgJsonPath, pkgJson);
// Add dependency
await runJsr(["i", "--npm", "@std/[email protected]"], dir);
await writeTextFile(
path.join(dir, "mod.ts"),
[
'import * as encoding from "@std/encoding/hex";',
"console.log(encoding);",
"export const value = 42;",
].join("\n"),
);
await writeJson<DenoJson>(path.join(dir, "jsr.json"), {
name: "@deno/jsr-cli-test",
version: pkgJson.version!,
exports: {
".": "./mod.ts",
},
});
await runJsr(["publish", "--dry-run"], dir);
assert.ok(
!(await isDirectory(path.join(dir, "node_modules", ".deno"))),
".deno found inside node_modules",
);
});
});
// Windows doesn't support #!/usr/bin/env
if (process.platform !== "win32") {
it("use deno binary from DENO_BIN_PATH when set", async () => {
await runInTempDir(async (dir) => {
await writeTextFile(
path.join(dir, "mod.ts"),
"export const value = 42;",
);
// TODO: Change this once deno supports jsr.json
await writeJson<DenoJson>(path.join(dir, "deno.json"), {
name: "@deno/jsr-cli-test",
version: "1.0.0",
exports: {
".": "./mod.ts",
},
});
await runJsr(["publish", "--dry-run", "--non-existant-option"], dir, {
DENO_BIN_PATH: path.join(__dirname, "fixtures", "dummy.js"),
});
});
});
}
});
describe("run", () => {
it("should run a script", async () => {
await runInTempDir(async (dir) => {
const pkgJsonPath = path.join(dir, "package.json");
const pkgJson = await readJson<PkgJson>(pkgJsonPath);
pkgJson.scripts = {
test: 'echo "test"',
};
await writeJson(pkgJsonPath, pkgJson);
await runJsr(["run", "test"], dir);
});
});
it("should run a script without the run command", async () => {
await runInTempDir(async (dir) => {
const pkgJsonPath = path.join(dir, "package.json");
const pkgJson = await readJson<PkgJson>(pkgJsonPath);
pkgJson.scripts = {
test: 'echo "test"',
};
await writeJson(pkgJsonPath, pkgJson);
await runJsr(["test"], dir);
});
});
});
describe("show", () => {
it("should show package information", async () => {
const output = await runJsr(
["show", "@std/encoding"],
process.cwd(),
undefined,
true,
);
const txt = kl.stripColors(output);
assert.ok(txt.includes("latest:"));
assert.ok(txt.includes("npm tarball:"));
});
it("can use 'view' alias", async () => {
await runJsr(
["view", "@std/encoding"],
process.cwd(),
);
});
it("can use 'info' alias", async () => {
await runJsr(
["view", "@std/encoding"],
process.cwd(),
);
});
});