Skip to content

Commit

Permalink
refactor(prepare): use shared functions to retrieve pubspec file cont…
Browse files Browse the repository at this point in the history
…ent (#205)
  • Loading branch information
zeshuaro committed Jan 21, 2024
1 parent a6b46e6 commit 747035c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { readFileSync, writeFileSync } from "fs";
import { parse } from "yaml";
import { PrepareContext } from "semantic-release";
import SemanticReleaseError from "@semantic-release/error";
import { writeFileSync } from "fs";
import { PrepareContext } from "semantic-release";

import { Pubspec } from "./schemas.js";
import { PluginConfig } from "./types.js";
import { getConfig } from "./utils.js";

const PUBSPEC_PATH = "pubspec.yaml";
import {
PUBSPEC_PATH,
getConfig,
getPubspecFromString,
getPubspecString,
} from "./utils.js";

export const prepare = async (
pluginConfig: PluginConfig,
{ nextRelease: { version }, logger }: PrepareContext,
) => {
const { updateBuildNumber } = getConfig(pluginConfig);

const data = readFileSync(PUBSPEC_PATH, "utf-8");
const pubspec = Pubspec.parse(parse(data));
const data = getPubspecString();
const pubspec = getPubspecFromString(data);
const pubspecVersionEscaped = pubspec.version.replace(
/[/\-\\^$*+?.()|[\]{}]/g,
"\\$&",
Expand Down
13 changes: 12 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import SemanticReleaseError from "@semantic-release/error";
import { readFileSync } from "fs";
import { JWT } from "google-auth-library";
import { ServiceAccount } from "./schemas.js";
import { parse } from "yaml";
import { Pubspec, ServiceAccount } from "./schemas.js";
import { PluginConfig } from "./types.js";

export const PUBSPEC_PATH = "pubspec.yaml";
const DEFAULT_CONFIG: PluginConfig = {
cli: "dart",
publishPub: true,
Expand Down Expand Up @@ -34,6 +37,14 @@ export const getGoogleIdentityToken = async (serviceAccountStr: string) => {
return creds.id_token;
};

export const getPubspecString = () => {
return readFileSync(PUBSPEC_PATH, "utf-8");
};

export const getPubspecFromString = (data: string) => {
return Pubspec.parse(parse(data));
};

const getServiceAccount = (serviceAccountStr: string) => {
try {
return ServiceAccount.parse(JSON.parse(serviceAccountStr));
Expand Down
45 changes: 44 additions & 1 deletion tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import SemanticReleaseError from "@semantic-release/error";
import { codeBlock } from "common-tags";
import { readFileSync } from "fs";
import { Credentials, JWT } from "google-auth-library";
import { afterEach, describe, expect, test, vi } from "vitest";
import { mock } from "vitest-mock-extended";
import { parse } from "yaml";
import { PluginConfig } from "../src";
import { getConfig, getGoogleIdentityToken } from "../src/utils";
import {
getConfig,
getGoogleIdentityToken,
getPubspecFromString,
getPubspecString,
} from "../src/utils";

vi.mock("google-auth-library");
vi.mock("fs");
vi.mock("yaml");

describe("getConfig", () => {
const config: PluginConfig = {
Expand Down Expand Up @@ -83,3 +92,37 @@ describe("getGoogleIdentityToken", () => {
);
};
});

describe("getPubspecString", () => {
const pubspecPath = "pubspec.yaml";
const fileContent = "fileContent";

afterEach(() => {
vi.restoreAllMocks();
});

test("success", () => {
vi.mocked(readFileSync).mockReturnValue(fileContent);

const actual = getPubspecString();

expect(actual).toEqual(fileContent);
expect(readFileSync).toHaveBeenNthCalledWith(1, pubspecPath, "utf-8");
});
});

describe("getPubspecFromString", () => {
const fileContent = "fileContent";
const pubspec = {
version: "1.0.0",
};

test("success", () => {
vi.mocked(parse).mockReturnValue(pubspec);

const actual = getPubspecFromString(fileContent);

expect(actual).toEqual(pubspec);
expect(parse).toHaveBeenNthCalledWith(1, fileContent);
});
});

0 comments on commit 747035c

Please sign in to comment.