Skip to content

Commit

Permalink
test(command): add provider tests (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar authored May 26, 2024
1 parent f770de2 commit 10d9254
Show file tree
Hide file tree
Showing 12 changed files with 702 additions and 22 deletions.
5 changes: 2 additions & 3 deletions command/upgrade/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ export abstract class Provider {
const registry: string =
registryUrl.startsWith("jsr:") || registryUrl.startsWith("npm:")
? registryUrl
: new URL(main || `${name}.ts`, registryUrl).href;
: new URL(main || `${name}.ts`, registryUrl + "/").href;

const cmdArgs = ["install"];

if (importMap) {
const importJson: string =
new URL(importMap, this.getRegistryUrl(name, to)).href;
const importJson: string = new URL(importMap, registryUrl + "/").href;

cmdArgs.push("--import-map", importJson);
}
Expand Down
4 changes: 2 additions & 2 deletions command/upgrade/provider/deno_land.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export class DenoLandProvider extends Provider {
}

getRepositoryUrl(name: string): string {
return new URL(`${this.moduleName ?? name}/`, this.repositoryUrl).href;
return new URL(`${this.moduleName ?? name}`, this.repositoryUrl).href;
}

getRegistryUrl(name: string, version: string): string {
return new URL(`${this.moduleName ?? name}@${version}/`, this.registryUrl)
return new URL(`${this.moduleName ?? name}@${version}`, this.registryUrl)
.href;
}
}
126 changes: 126 additions & 0 deletions command/upgrade/provider/deno_land_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { assert, assertEquals } from "@std/assert";
import {
mockFetch,
mockGlobalFetch,
resetFetch,
resetGlobalFetch,
} from "@c4spar/mock-fetch";
import {
mockCommand,
mockGlobalCommand,
resetCommand,
resetGlobalCommand,
} from "@c4spar/mock-command";
import { DenoLandProvider } from "./deno_land.ts";
import type { Versions } from "../provider.ts";

Deno.test("DenoLandProvider", async (ctx) => {
mockGlobalFetch();
mockGlobalCommand();

const provider = new DenoLandProvider();

await ctx.step({
name: "should return registry url",
fn() {
assertEquals(
provider.getRegistryUrl("foo", "1.0.0"),
"https://deno.land/x/foo@1.0.0",
);
},
});

await ctx.step({
name: "should return repository url",
fn() {
assertEquals(
provider.getRepositoryUrl("foo"),
"https://deno.land/x/foo",
);
},
});

await ctx.step({
name: "should return versions",
async fn() {
const expectedVersions: Versions = {
latest: "1.0.1",
versions: ["1.0.1", "1.0.0"],
};

mockFetch("https://cdn.deno.land/foo/meta/versions.json", {
body: JSON.stringify(expectedVersions),
});
const versions = await provider.getVersions("foo");

assertEquals(versions, expectedVersions);

resetFetch();
},
});

await ctx.step({
name: "should check if version is outdated",
async fn() {
const mock = {
body: JSON.stringify({
latest: "1.0.1",
versions: ["1.0.1", "1.0.0"],
}),
};

mockFetch("https://cdn.deno.land/foo/meta/versions.json", mock);
const isOutdated = await provider.isOutdated("foo", "1.0.0", "latest");
assert(isOutdated);

mockFetch("https://cdn.deno.land/foo/meta/versions.json", mock);
const isNotOutdated = !await provider.isOutdated(
"foo",
"1.0.1",
"latest",
);
assert(isNotOutdated);

resetFetch();
},
});

await ctx.step({
name: "should upgrade to latest version",
async fn() {
mockFetch("https://cdn.deno.land/foo/meta/versions.json", {
body: JSON.stringify({
latest: "1.0.1",
versions: ["1.0.1", "1.0.0"],
}),
});

mockCommand({
command: Deno.execPath(),
args: [
"install",
"--no-check",
"--quiet",
"--force",
"--name",
"foo",
"https://deno.land/x/foo@1.0.1/foo.ts",
],
stdout: "piped",
stderr: "piped",
});

await provider.upgrade({
name: "foo",
from: "1.0.1",
to: "latest",
});

resetFetch();
resetCommand();
},
});

resetGlobalFetch();
resetGlobalCommand();
});
4 changes: 2 additions & 2 deletions command/upgrade/provider/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ export class GithubProvider extends Provider {
}

getRepositoryUrl(_name: string): string {
return new URL(`${this.repositoryName}/`, this.repositoryUrl).href;
return new URL(this.repositoryName, this.repositoryUrl).href;
}

getRegistryUrl(_name: string, version: string): string {
return new URL(`${this.repositoryName}/${version}/`, this.registryUrl).href;
return new URL(`${this.repositoryName}/${version}`, this.registryUrl).href;
}

async listVersions(name: string, currentVersion?: string): Promise<void> {
Expand Down
172 changes: 172 additions & 0 deletions command/upgrade/provider/github_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { assert, assertEquals } from "@std/assert";
import {
mockFetch,
mockGlobalFetch,
resetFetch,
resetGlobalFetch,
} from "@c4spar/mock-fetch";
import { GithubProvider } from "./github.ts";
import {
mockCommand,
mockGlobalCommand,
resetCommand,
resetGlobalCommand,
} from "@c4spar/mock-command";

Deno.test("GithubProvider", async (ctx) => {
mockGlobalFetch();
mockGlobalCommand();

const provider = new GithubProvider({ repository: "repo/user" });

await ctx.step({
name: "should return registry url",
fn() {
assertEquals(
provider.getRegistryUrl("foo", "1.0.0"),
"https://raw.githubusercontent.com/repo/user/1.0.0",
);
},
});

await ctx.step({
name: "should return repository url",
fn() {
assertEquals(
provider.getRepositoryUrl("foo"),
"https://github.com/repo/user",
);
},
});

await ctx.step({
name: "should return versions",
async fn() {
mockFetch("https://api.github.com/repos/repo/user/git/refs/tags", {
body: JSON.stringify([
{ ref: "1.0.0" },
{ ref: "1.0.1" },
]),
});
mockFetch("https://api.github.com/repos/repo/user/branches", {
body: JSON.stringify([
{ name: "branch-1", protected: true },
{ name: "branch-2", protected: false },
]),
});
const versions = await provider.getVersions("foo");

assertEquals(versions, {
latest: "1.0.1",
versions: [
"1.0.1",
"1.0.0",
"branch-1 (\x1b[1mProtected\x1b[22m)",
"branch-2 ",
],
tags: [
"1.0.1",
"1.0.0",
],
branches: [
"branch-1 (\x1b[1mProtected\x1b[22m)",
"branch-2 ",
],
});

resetFetch();
},
});

await ctx.step({
name: "should check if version is outdated",
async fn() {
const tagsMock = {
body: JSON.stringify([
{ ref: "1.0.0" },
{ ref: "1.0.1" },
]),
};
const branchesMock = {
body: JSON.stringify([
{ name: "branch-1", protected: true },
{ name: "branch-2", protected: false },
]),
};

mockFetch(
"https://api.github.com/repos/repo/user/git/refs/tags",
tagsMock,
);
mockFetch(
"https://api.github.com/repos/repo/user/branches",
branchesMock,
);
const isOutdated = await provider.isOutdated("foo", "1.0.0", "latest");
assert(isOutdated);

mockFetch(
"https://api.github.com/repos/repo/user/git/refs/tags",
tagsMock,
);
mockFetch(
"https://api.github.com/repos/repo/user/branches",
branchesMock,
);
const isNotOutdated = !await provider.isOutdated(
"foo",
"1.0.1",
"latest",
);
assert(isNotOutdated);

resetFetch();
},
});

await ctx.step({
name: "should upgrade to latest version",
async fn() {
mockFetch("https://api.github.com/repos/repo/user/git/refs/tags", {
body: JSON.stringify([
{ ref: "1.0.0" },
{ ref: "1.0.1" },
]),
});

mockFetch("https://api.github.com/repos/repo/user/branches", {
body: JSON.stringify([
{ name: "branch-1", protected: true },
{ name: "branch-2", protected: false },
]),
});

mockCommand({
command: Deno.execPath(),
args: [
"install",
"--no-check",
"--quiet",
"--force",
"--name",
"foo",
"https://raw.githubusercontent.com/repo/user/1.0.1/foo.ts",
],
stdout: "piped",
stderr: "piped",
});

await provider.upgrade({
name: "foo",
from: "1.0.0",
to: "latest",
});

resetFetch();
resetCommand();
},
});

resetGlobalFetch();
resetGlobalCommand();
});
6 changes: 1 addition & 5 deletions command/upgrade/provider/jsr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ export class JsrProvider extends Provider {
async getVersions(
name: string,
): Promise<Versions> {
const response = await fetch(
`${this.repositoryUrl}/@${this.packageScope}/${
this.packageName ?? name
}/meta.json`,
);
const response = await fetch(`${this.getRepositoryUrl(name)}/meta.json`);
if (!response.ok) {
throw new Error(
"couldn't fetch the latest version - try again after sometime",
Expand Down
Loading

0 comments on commit 10d9254

Please sign in to comment.