|
| 1 | +import { goTry } from "go-go-try"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import { join } from "pathe"; |
| 4 | +import { expect, test } from "vitest"; |
| 5 | +import { getPackageJson } from "./get-package-json.ts"; |
| 6 | +import { tempDir } from "./temp-dir.ts"; |
| 7 | + |
| 8 | +test("no package.json", async () => { |
| 9 | + await using dir = await tempDir(); |
| 10 | + const [err, _] = await goTry(getPackageJson(dir.path)); |
| 11 | + expect(err).toBeDefined(); |
| 12 | +}); |
| 13 | + |
| 14 | +test("with empty package.json", async () => { |
| 15 | + await using dir = await tempDir(); |
| 16 | + await fs.writeFile(join(dir.path, "package.json"), "{}"); |
| 17 | + const [err, pkgJson] = await goTry(getPackageJson(dir.path)); |
| 18 | + expect(err).toBeUndefined(); |
| 19 | + expect(pkgJson).toMatchInlineSnapshot(` |
| 20 | + { |
| 21 | + "_id": "@", |
| 22 | + "name": "", |
| 23 | + "readme": "ERROR: No README data found!", |
| 24 | + "version": "", |
| 25 | + } |
| 26 | + `); |
| 27 | +}); |
| 28 | + |
| 29 | +test("with minimal package.json", async () => { |
| 30 | + await using dir = await tempDir(); |
| 31 | + await fs.writeFile(join(dir.path, "package.json"), '{ "name": "foo", "version": "1.0.0" }'); |
| 32 | + const [err, pkgJson] = await goTry(getPackageJson(dir.path)); |
| 33 | + expect(err).toBeUndefined(); |
| 34 | + expect(pkgJson).toMatchInlineSnapshot(` |
| 35 | + { |
| 36 | + "_id": "foo@1.0.0", |
| 37 | + "name": "foo", |
| 38 | + "readme": "ERROR: No README data found!", |
| 39 | + "version": "1.0.0", |
| 40 | + } |
| 41 | + `); |
| 42 | +}); |
| 43 | + |
| 44 | +test("with minimal scoped package.json", async () => { |
| 45 | + await using dir = await tempDir(); |
| 46 | + await fs.writeFile(join(dir.path, "package.json"), '{ "name": "@foo/bar", "version": "1.0.0" }'); |
| 47 | + const [err, pkgJson] = await goTry(getPackageJson(dir.path)); |
| 48 | + expect(err).toBeUndefined(); |
| 49 | + expect(pkgJson).toMatchInlineSnapshot(` |
| 50 | + { |
| 51 | + "_id": "@foo/bar@1.0.0", |
| 52 | + "name": "@foo/bar", |
| 53 | + "readme": "ERROR: No README data found!", |
| 54 | + "version": "1.0.0", |
| 55 | + } |
| 56 | + `); |
| 57 | +}); |
| 58 | + |
| 59 | +test("with package.json from project workdir with npm package", async () => { |
| 60 | + await using dir = await tempDir(); |
| 61 | + await fs.writeFile(join(dir.path, "package.json"), '{ "dependencies": { "foo": "1.0.0" } }'); |
| 62 | + const [err, pkgJson] = await goTry(getPackageJson(dir.path)); |
| 63 | + expect(err).toBeUndefined(); |
| 64 | + expect(pkgJson).toMatchInlineSnapshot(` |
| 65 | + { |
| 66 | + "_id": "@", |
| 67 | + "dependencies": { |
| 68 | + "foo": "1.0.0", |
| 69 | + }, |
| 70 | + "name": "", |
| 71 | + "readme": "ERROR: No README data found!", |
| 72 | + "version": "", |
| 73 | + } |
| 74 | + `); |
| 75 | +}); |
| 76 | + |
| 77 | +test("with package.json from project workdir with scoped npm package", async () => { |
| 78 | + await using dir = await tempDir(); |
| 79 | + await fs.writeFile( |
| 80 | + join(dir.path, "package.json"), |
| 81 | + '{ "dependencies": { "@foo/bar": "^1.0.0" } }', |
| 82 | + ); |
| 83 | + const [err, pkgJson] = await goTry(getPackageJson(dir.path)); |
| 84 | + expect(err).toBeUndefined(); |
| 85 | + expect(pkgJson).toMatchInlineSnapshot(` |
| 86 | + { |
| 87 | + "_id": "@", |
| 88 | + "dependencies": { |
| 89 | + "@foo/bar": "^1.0.0", |
| 90 | + }, |
| 91 | + "name": "", |
| 92 | + "readme": "ERROR: No README data found!", |
| 93 | + "version": "", |
| 94 | + } |
| 95 | + `); |
| 96 | +}); |
| 97 | + |
| 98 | +test("with package.json from project workdir with local package", async () => { |
| 99 | + await using dir = await tempDir(); |
| 100 | + await fs.writeFile( |
| 101 | + join(dir.path, "package.json"), |
| 102 | + '{ "dependencies": { "foo": "/path/to/tarball.tgz" } }', |
| 103 | + ); |
| 104 | + const [err, pkgJson] = await goTry(getPackageJson(dir.path)); |
| 105 | + expect(err).toBeUndefined(); |
| 106 | + expect(pkgJson).toMatchInlineSnapshot(` |
| 107 | + { |
| 108 | + "_id": "@", |
| 109 | + "dependencies": { |
| 110 | + "foo": "/path/to/tarball.tgz", |
| 111 | + }, |
| 112 | + "name": "", |
| 113 | + "readme": "ERROR: No README data found!", |
| 114 | + "version": "", |
| 115 | + } |
| 116 | + `); |
| 117 | +}); |
| 118 | + |
| 119 | +test("with package.json from project workdir with scoped local package", async () => { |
| 120 | + await using dir = await tempDir(); |
| 121 | + await fs.writeFile( |
| 122 | + join(dir.path, "package.json"), |
| 123 | + '{ "dependencies": { "@foo/bar": "/path/to/tarball.tgz" } }', |
| 124 | + ); |
| 125 | + const [err, pkgJson] = await goTry(getPackageJson(dir.path)); |
| 126 | + expect(err).toBeUndefined(); |
| 127 | + expect(pkgJson).toMatchInlineSnapshot(` |
| 128 | + { |
| 129 | + "_id": "@", |
| 130 | + "dependencies": { |
| 131 | + "@foo/bar": "/path/to/tarball.tgz", |
| 132 | + }, |
| 133 | + "name": "", |
| 134 | + "readme": "ERROR: No README data found!", |
| 135 | + "version": "", |
| 136 | + } |
| 137 | + `); |
| 138 | +}); |
0 commit comments