Skip to content

Commit

Permalink
fix(verify): use SemanticReleaseError in verify
Browse files Browse the repository at this point in the history
Replace AggregateError with SemanticReleaseError in verify
  • Loading branch information
Kampfmoehre committed Jul 12, 2022
1 parent 2f5803c commit a749358
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
8 changes: 6 additions & 2 deletions src/verify.ts
Expand Up @@ -3,8 +3,10 @@ import { promises } from "fs";
import { resolve } from "path";
import { Config, Context } from "semantic-release";
import { UserConfig } from "./UserConfig";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const SRError = require("@semantic-release/error");

export const verify = async (pluginConfig: Config & UserConfig, _: Context): Promise<void> => {
export const verify = async (pluginConfig: Config & UserConfig, _context: Context): Promise<void> => {
const errors: Error[] = [];
for (const envVar of ["NUGET_TOKEN"]) {
if (!process.env[envVar]) {
Expand Down Expand Up @@ -49,6 +51,8 @@ export const verify = async (pluginConfig: Config & UserConfig, _: Context): Pro
}

if (errors.length > 0) {
throw new AggregateError(errors);
const message = errors.map((err) => err.message).join("\n");
// context.logger.error(message);
throw new SRError("Verify failed", "VERIFY_FAILED", message);
}
};
40 changes: 22 additions & 18 deletions test/verify.test.ts
Expand Up @@ -3,6 +3,8 @@ import { Context } from "semantic-release";
import { UserConfig } from "../src/UserConfig";
import { verify } from "../src/verify";

type SemanticReleaeError = { details: string };

jest.mock("execa");

describe("verify", () => {
Expand All @@ -26,46 +28,48 @@ describe("verify", () => {
});

it("should report an error when NUGET_TOKEN is no set", async () => {
let actualErr: AggregateError | undefined;
let actualErr: SemanticReleaeError | undefined;
try {
await verify({ projectPath: "test/fixture/some.csproj" } as UserConfig, context);
} catch (err) {
actualErr = err as AggregateError;
actualErr = err as SemanticReleaeError;
}

expect(actualErr).toBeDefined();
expect(actualErr?.errors[0].message).toBe("Environment variable NUGET_TOKEN is not set.");
expect(actualErr?.details).toBe("Environment variable NUGET_TOKEN is not set.");
});

it("should report an error when publishToGitLab is true and no CI_SERVER_URL is set", async () => {
delete process.env.CI_SERVER_URL;
process.env.NUGET_TOKEN = "104E2";
process.env.CI_PROJECT_ID = "132";
process.env.CI_JOB_TOKEN = "a3lhjli";

let actualErr: AggregateError | undefined;
let actualErr: SemanticReleaeError | undefined;
try {
await verify({ publishToGitLab: true, projectPath: "test/fixture/some.csproj" } as UserConfig, context);
} catch (err) {
actualErr = err as AggregateError;
actualErr = err as SemanticReleaeError;
}

expect(actualErr).toBeDefined();
expect(actualErr?.errors[0].message).toBe("GitLab environment variable CI_SERVER_URL is not set.");
expect(actualErr?.details).toBe("GitLab environment variable CI_SERVER_URL is not set.");
});

it("should report an error when publishToGitLab is true and no CI_PROJECT_ID is set", async () => {
delete process.env.CI_PROJECT_ID;
process.env.NUGET_TOKEN = "104E2";
process.env.CI_SERVER_URL = "gitlab.com";

let actualErr: AggregateError | undefined;
let actualErr: SemanticReleaeError | undefined;
try {
await verify({ publishToGitLab: true, projectPath: "test/fixture/some.csproj" } as UserConfig, context);
} catch (err) {
actualErr = err as AggregateError;
actualErr = err as SemanticReleaeError;
}

expect(actualErr).toBeDefined();
expect(actualErr?.errors[0].message).toBe("GitLab environment variable CI_PROJECT_ID is not set.");
expect(actualErr?.details).toBe("GitLab environment variable CI_PROJECT_ID is not set.");
});

it("should report an error when publishToGitLab is true and no CI_JOB_TOKEN is set", async () => {
Expand All @@ -74,32 +78,32 @@ describe("verify", () => {
process.env.CI_SERVER_URL = "gitlab.com";
process.env.CI_PROJECT_ID = "132";

let actualErr: AggregateError | undefined;
let actualErr: SemanticReleaeError | undefined;
try {
await verify({ publishToGitLab: true, projectPath: "test/fixture/some.csproj" } as UserConfig, context);
} catch (err) {
actualErr = err as AggregateError;
actualErr = err as SemanticReleaeError;
}

expect(actualErr).toBeDefined();
expect(actualErr?.errors[0].message).toBe("GitLab environment variable CI_JOB_TOKEN is not set.");
expect(actualErr?.details).toBe("GitLab environment variable CI_JOB_TOKEN is not set.");
});

it("should report an error if path to non existing project file is given", async () => {
process.env.NUGET_TOKEN = "104E2";

let actualErr: AggregateError | undefined;
let actualErr: SemanticReleaeError | undefined;
try {
await verify(
{ projectPath: ["test/fixture/some-missing.csproj", "test/fixture/some.csproj"] } as UserConfig,
context,
);
} catch (err) {
actualErr = err as AggregateError;
actualErr = err as SemanticReleaeError;
}

expect(actualErr).toBeDefined();
expect(actualErr?.errors[0].message).toMatch(
expect(actualErr?.details).toMatch(
/The given project path.*test\/fixture\/some-missing.csproj could not be found./,
);
});
Expand All @@ -110,14 +114,14 @@ describe("verify", () => {
throw new Error("Some error");
});

let actualErr: AggregateError | undefined;
let actualErr: SemanticReleaeError | undefined;
try {
await verify({ projectPath: "test/fixture/some.csproj" } as UserConfig, context);
} catch (err) {
actualErr = err as AggregateError;
actualErr = err as SemanticReleaeError;
}

expect(actualErr).toBeDefined();
expect(actualErr?.errors[0].message).toBe("Unable to find dotnet executable in dotnet");
expect(actualErr?.details).toBe("Unable to find dotnet executable in dotnet");
});
});

0 comments on commit a749358

Please sign in to comment.