Skip to content

Commit

Permalink
fix: lockfile updating for incomplete url imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Jul 26, 2024
1 parent ecc42ea commit 58eef6e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 55 deletions.
4 changes: 2 additions & 2 deletions cli/fixtures/mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import "https://deno.land/std@0.222.0/bytes/copy.ts"
import "https://deno.land/std@0.222.0/bytes/copy.ts";
import "jsr:@luca/flag@1.0.0";
import "npm:@conventional-commits/parser@0.4.0"
import "npm:@conventional-commits/parser@0.4.0";
2 changes: 1 addition & 1 deletion cli/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("CLI", () => {
);
});

it.ignore("should update `deno.json` with `--write`", async () => {
it("should update `deno.json` with `--write`", async () => {
const { stderr, stdout } = await molt("--write");
assertEquals(
stdout,
Expand Down
31 changes: 22 additions & 9 deletions core/locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,30 @@ export const empty: LockfileJson = {
export function create(
dependency: DependencySpec,
target: string,
extracted: LockfileJson,
): Promise<LockfileJson> {
return isRemote(dependency)
? createRemoteLock(dependency)
? createRemoteLock(dependency, extracted)
: createPackageLock(dependency as DependencySpec<"jsr" | "npm">, target);
}

async function createRemoteLock(
dep: DependencySpec<"http" | "https">,
extracted: LockfileJson,
): Promise<LockfileJson> {
const { kind, name, constraint, path } = dep;
const reqs = Object.keys(extracted.remote).filter((req) =>
[kind, name, path ?? ""].every((part) => req.includes(part))
);
const lockfile = parseFromJson("", empty);
const graph = await createGraph(stringify(dep));
const deps = graph.modules.map((mod) => mod.specifier);
await Promise.all(deps.map(async (dep) => {
const res = await fetch(dep);
assertOk(res);
lockfile.insertRemote(dep, await checksum(await res.arrayBuffer()));
await Promise.all(reqs.map(async (outdated) => {
const updated = stringify({ ...Spec(outdated), constraint });
const graph = await createGraph(updated);
await Promise.all(graph.modules.map(async ({ specifier }) => {
const res = await fetch(specifier);
assertOk(res);
lockfile.insertRemote(specifier, await checksum(await res.arrayBuffer()));
}));
}));
return lockfile.toJson();
}
Expand Down Expand Up @@ -250,8 +258,13 @@ async function extractRemote(
lock: LockfileJson,
dep: DependencySpec<"http" | "https">,
): Promise<LockfileJson> {
const graph = await createGraph(stringify(dep));
const deps = graph.modules.map((mod) => mod.specifier);
const reqs = Object.keys(lock.remote).filter((req) =>
req.startsWith(stringify(dep))
);
const deps = (await Promise.all(reqs.map(async (req) => {
const graph = await createGraph(req);
return graph.modules.map((mod) => mod.specifier);
}))).flat();
return {
version: VERSION,
remote: filterValues(
Expand Down
119 changes: 78 additions & 41 deletions core/locks_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,48 @@ import { afterEach, beforeEach, describe, it } from "@std/testing/bdd";
import { parse } from "./specs.ts";
import { create, extract, LockfileJson, query } from "./locks.ts";

export const LOCKFILE: LockfileJson = JSON.parse(`{
"version": "3",
"packages": {
"specifiers": {
"jsr:@std/assert@^0.222.0": "jsr:@std/assert@0.222.0",
"jsr:@std/fmt@^0.222.0": "jsr:@std/fmt@0.222.0",
"npm:debug@^4.3.0": "npm:debug@4.3.0"
},
"jsr": {
"@std/assert@0.222.0": {
"integrity": "cbf00c0d8125a56c087e3d1ea0e638760d47206b30e9d300bad826b811719fc7",
"dependencies": [
"jsr:@std/fmt@^0.222.0"
]
},
"@std/fmt@0.222.0": {
"integrity": "0eb99babf1cc697d67e76e8753916c037bbc3ce4abcefa321e1465708b0adda1"
}
},
"npm": {
"debug@4.3.0": {
"integrity": "sha512-jjO6JD2rKfiZQnBoRzhRTbXjHLGLfH+UtGkWLc/UXAh/rzZMyjbgn0NcfFpqT8nd1kTtFnDiJcrIFkq4UKeJVg==",
"dependencies": { "ms": "ms@2.1.2" }
},
"ms@2.1.2": {
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dependencies": {}
}
}
},
"remote": {
"https://deno.land/std@0.220.0/assert/assert.ts": "bec068b2fccdd434c138a555b19a2c2393b71dfaada02b7d568a01541e67cdc5",
"https://deno.land/std@0.220.0/assert/assertion_error.ts": "9f689a101ee586c4ce92f52fa7ddd362e86434ffdf1f848e45987dc7689976b8"
},
"workspace": {
"dependencies": [
"jsr:@std/assert@^0.222.0",
"npm:debug@^4.3.0"
]
}
}`);

describe("create", () => {
beforeEach(() => fs.mock());
afterEach(() => fs.dispose());
Expand All @@ -12,6 +54,7 @@ describe("create", () => {
const lock = await create(
parse("jsr:@std/assert@^0.222.0"),
"0.222.1",
{} as LockfileJson,
);
assertEquals(lock, {
version: "3",
Expand Down Expand Up @@ -43,6 +86,7 @@ describe("create", () => {
const lock = await create(
parse("jsr:@std/assert@^0.226.0"),
"0.226.0",
{} as LockfileJson,
);
assertEquals(lock, {
version: "3",
Expand Down Expand Up @@ -72,6 +116,7 @@ describe("create", () => {
const lock = await create(
parse("jsr:@core/match@^0.2.0"),
"0.2.5",
{} as LockfileJson,
);
assertEquals(lock, {
version: "3",
Expand Down Expand Up @@ -100,10 +145,11 @@ describe("create", () => {
});
});

it.only("should create a partial lock for a npm package", async () => {
it("should create a partial lock for a npm package", async () => {
const lock = await create(
parse("npm:@conventional-commits/parser@^0.4.0"),
"0.4.1",
{} as LockfileJson,
);
assertEquals(lock, {
version: "3",
Expand Down Expand Up @@ -159,6 +205,10 @@ describe("create", () => {
const lock = await create(
parse("https://deno.land/std@0.224.0/assert/assert.ts"),
"0.224.0",
{
version: "3",
remote: LOCKFILE.remote,
},
);
assertEquals(lock, {
version: "3",
Expand All @@ -170,49 +220,27 @@ describe("create", () => {
},
});
});
});

export const LOCKFILE: LockfileJson = JSON.parse(`{
"version": "3",
"packages": {
"specifiers": {
"jsr:@std/assert@^0.222.0": "jsr:@std/assert@0.222.0",
"jsr:@std/fmt@^0.222.0": "jsr:@std/fmt@0.222.0",
"npm:debug@^4.3.0": "npm:debug@4.3.0"
},
"jsr": {
"@std/assert@0.222.0": {
"integrity": "cbf00c0d8125a56c087e3d1ea0e638760d47206b30e9d300bad826b811719fc7",
"dependencies": [
"jsr:@std/fmt@^0.222.0"
]
it("should create a partial lock for a remote incomplete dependency", async () => {
const lock = await create(
parse("https://deno.land/std@0.224.0/"),
"0.224.0",
{
version: "3",
remote: LOCKFILE.remote,
},
"@std/fmt@0.222.0": {
"integrity": "0eb99babf1cc697d67e76e8753916c037bbc3ce4abcefa321e1465708b0adda1"
}
},
"npm": {
"debug@4.3.0": {
"integrity": "sha512-jjO6JD2rKfiZQnBoRzhRTbXjHLGLfH+UtGkWLc/UXAh/rzZMyjbgn0NcfFpqT8nd1kTtFnDiJcrIFkq4UKeJVg==",
"dependencies": { "ms": "ms@2.1.2" }
);
assertEquals(lock, {
version: "3",
remote: {
"https://deno.land/std@0.224.0/assert/assert.ts":
"09d30564c09de846855b7b071e62b5974b001bb72a4b797958fe0660e7849834",
"https://deno.land/std@0.224.0/assert/assertion_error.ts":
"ba8752bd27ebc51f723702fac2f54d3e94447598f54264a6653d6413738a8917",
},
"ms@2.1.2": {
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dependencies": {}
}
}
},
"remote": {
"https://deno.land/std@0.220.0/assert/assert.ts": "bec068b2fccdd434c138a555b19a2c2393b71dfaada02b7d568a01541e67cdc5",
"https://deno.land/std@0.220.0/assert/assertion_error.ts": "9f689a101ee586c4ce92f52fa7ddd362e86434ffdf1f848e45987dc7689976b8"
},
"workspace": {
"dependencies": [
"jsr:@std/assert@^0.222.0",
"npm:debug@^4.3.0"
]
}
}`);
});
});
});

describe("extract", () => {
beforeEach(() => fs.mock());
Expand Down Expand Up @@ -280,6 +308,15 @@ describe("extract", () => {
remote: LOCKFILE.remote,
});
});

it("should extract the remote dependencies from an incomplete url", async () => {
const dep = parse("https://deno.land/std@0.220.0/");
const lock = await extract(LOCKFILE, dep);
assertEquals(lock, {
version: "3",
remote: LOCKFILE.remote,
});
});
});

describe("query", () => {
Expand Down
3 changes: 2 additions & 1 deletion core/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ class Update implements UpdateI {
? { ...dep, constraint: bump.constraint }
: dep;

const lock = await Lock.create(bumped, bump.lock);
const locked = this.#ctx.locks.extracted.get(req)!;
const lock = await Lock.create(bumped, bump.lock, locked);
this.#ctx.locks.created.set(req, lock);

await Deno.writeTextFile(
Expand Down
2 changes: 1 addition & 1 deletion core/mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { all, cmd, fs } from "@chiezo/amber";
import { collect } from "./mod.ts";

Deno.test("Molt", async () => {
Deno.test("Molt", { ignore: true }, async () => {
cmd.stub("git");
all(cmd, fs).mock();

Expand Down

0 comments on commit 58eef6e

Please sign in to comment.