fix: Switch to biome & vitest (#726)

This commit is contained in:
Nerivec
2025-03-23 21:30:14 +01:00
committed by GitHub
parent 44c5bbcc1c
commit 94b76a910a
48 changed files with 1762 additions and 4075 deletions

View File

@@ -1,35 +1,37 @@
import type CoreApi from '@actions/core';
import type {Context} from '@actions/github/lib/context';
import type {Octokit} from '@octokit/rest';
import type CoreApi from "@actions/core";
import type {Context} from "@actions/github/lib/context";
import type {Octokit} from "@octokit/rest";
import type {RepoImageMeta} from '../src/types';
import type {RepoImageMeta} from "../src/types";
import {rmSync} from 'fs';
import {rmSync} from "node:fs";
import * as common from '../src/common';
import {updateManifests} from '../src/ghw_update_manifests';
import {type MockInstance, afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi} from "vitest";
import * as common from "../src/common";
import {updateManifests} from "../src/ghw_update_manifests";
import {
BASE_IMAGES_TEST_DIR_PATH,
getAdjustedContent,
IMAGE_V13_1,
IMAGE_V14_1,
IMAGE_V14_1_METAS,
PREV_IMAGES_TEST_DIR_PATH,
getAdjustedContent,
useImage,
withExtraMetas,
} from './data.test';
} from "./data.test";
const github = {
rest: {
pulls: {
get: jest.fn<ReturnType<Octokit['rest']['pulls']['get']>, Parameters<Octokit['rest']['pulls']['get']>, unknown>(),
get: vi.fn<(...args: Parameters<Octokit["rest"]["pulls"]["get"]>) => ReturnType<Octokit["rest"]["pulls"]["get"]>>(),
},
repos: {
compareCommitsWithBasehead: jest.fn<
ReturnType<Octokit['rest']['repos']['compareCommitsWithBasehead']>,
Parameters<Octokit['rest']['repos']['compareCommitsWithBasehead']>,
unknown
>(),
compareCommitsWithBasehead:
vi.fn<
(
...args: Parameters<Octokit["rest"]["repos"]["compareCommitsWithBasehead"]>
) => ReturnType<Octokit["rest"]["repos"]["compareCommitsWithBasehead"]>
>(),
},
},
};
@@ -39,40 +41,42 @@ const core: Partial<typeof CoreApi> = {
warning: console.warn,
error: console.error,
notice: console.log,
startGroup: jest.fn(),
endGroup: jest.fn(),
startGroup: vi.fn(),
endGroup: vi.fn(),
};
const context: Partial<Context> = {
eventName: 'push',
eventName: "push",
payload: {
head_commit: {
message: 'push from pr (#213)',
message: "push from pr (#213)",
},
},
repo: {
owner: 'Koenkk',
repo: 'zigbee-OTA',
owner: "Koenkk",
repo: "zigbee-OTA",
},
};
describe('Github Workflow: Update manifests', () => {
describe("Github Workflow: Update manifests", () => {
let baseManifest: RepoImageMeta[];
let prevManifest: RepoImageMeta[];
let readManifestSpy: jest.SpyInstance;
let writeManifestSpy: jest.SpyInstance;
let addImageToBaseSpy: jest.SpyInstance;
let addImageToPrevSpy: jest.SpyInstance;
let readManifestSpy: MockInstance;
let writeManifestSpy: MockInstance;
let addImageToBaseSpy: MockInstance;
let addImageToPrevSpy: MockInstance;
let filePaths: ReturnType<typeof useImage>[] = [];
let prBody: string | undefined;
const getManifest = (fileName: string): RepoImageMeta[] => {
if (fileName === common.BASE_INDEX_MANIFEST_FILENAME) {
return baseManifest;
} else if (fileName === common.PREV_INDEX_MANIFEST_FILENAME) {
return prevManifest;
} else {
throw new Error(`${fileName} not supported`);
}
if (fileName === common.PREV_INDEX_MANIFEST_FILENAME) {
return prevManifest;
}
throw new Error(`${fileName} not supported`);
};
const setManifest = (fileName: string, content: RepoImageMeta[]): void => {
@@ -92,7 +96,7 @@ describe('Github Workflow: Update manifests', () => {
prevManifest = [];
};
const expectNoChanges = (noReadManifest: boolean = false): void => {
const expectNoChanges = (noReadManifest = false): void => {
if (noReadManifest) {
expect(readManifestSpy).toHaveBeenCalledTimes(0);
} else {
@@ -119,10 +123,10 @@ describe('Github Workflow: Update manifests', () => {
resetManifests();
filePaths = [];
readManifestSpy = jest.spyOn(common, 'readManifest').mockImplementation(getManifest);
writeManifestSpy = jest.spyOn(common, 'writeManifest').mockImplementation(setManifest);
addImageToBaseSpy = jest.spyOn(common, 'addImageToBase');
addImageToPrevSpy = jest.spyOn(common, 'addImageToPrev');
readManifestSpy = vi.spyOn(common, "readManifest").mockImplementation(getManifest);
writeManifestSpy = vi.spyOn(common, "writeManifest").mockImplementation(setManifest);
addImageToBaseSpy = vi.spyOn(common, "addImageToBase");
addImageToPrevSpy = vi.spyOn(common, "addImageToPrev");
github.rest.pulls.get.mockImplementation(
// @ts-expect-error mock
() => ({data: {body: prBody}}),
@@ -139,29 +143,29 @@ describe('Github Workflow: Update manifests', () => {
rmSync(common.PR_ARTIFACT_DIR, {recursive: true, force: true});
});
it('hard failure from outside push context', async () => {
it("hard failure from outside push context", async () => {
filePaths = [useImage(IMAGE_V14_1)];
await expect(async () => {
// @ts-expect-error mock
await updateManifests(github, core, {payload: {}});
}).rejects.toThrow(`Not a push`);
}).rejects.toThrow("Not a push");
expectNoChanges(true);
});
it('failure with file outside of images directory', async () => {
it("failure with file outside of images directory", async () => {
filePaths = [useImage(IMAGE_V13_1, PREV_IMAGES_TEST_DIR_PATH), useImage(IMAGE_V14_1)];
await expect(async () => {
// @ts-expect-error mock
await updateManifests(github, core, context);
}).rejects.toThrow(expect.objectContaining({message: expect.stringContaining(`Cannot run with files outside`)}));
}).rejects.toThrow(expect.objectContaining({message: expect.stringContaining("Cannot run with files outside")}));
expectNoChanges(true);
});
it('success into base', async () => {
it("success into base", async () => {
filePaths = [useImage(IMAGE_V14_1)];
// @ts-expect-error mock
@@ -175,7 +179,7 @@ describe('Github Workflow: Update manifests', () => {
expect(writeManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME, [IMAGE_V14_1_METAS]);
});
it('success with extra metas', async () => {
it("success with extra metas", async () => {
filePaths = [useImage(IMAGE_V14_1)];
prBody = `Text before start tag \`\`\`json {"manufacturerName": ["lixee"]} \`\`\` Text after end tag`;
@@ -188,18 +192,18 @@ describe('Github Workflow: Update manifests', () => {
expect(addImageToPrevSpy).toHaveBeenCalledTimes(0);
expect(writeManifestSpy).toHaveBeenCalledTimes(2);
expect(writeManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME, [
withExtraMetas(IMAGE_V14_1_METAS, {manufacturerName: ['lixee']}),
withExtraMetas(IMAGE_V14_1_METAS, {manufacturerName: ["lixee"]}),
]);
});
it('fails to get PR for extra metas', async () => {
it("fails to get PR for extra metas", async () => {
filePaths = [useImage(IMAGE_V14_1)];
github.rest.pulls.get.mockRejectedValueOnce('403');
github.rest.pulls.get.mockRejectedValueOnce("403");
await expect(async () => {
// @ts-expect-error mock
await updateManifests(github, core, context);
}).rejects.toThrow(expect.objectContaining({message: `Failed to get PR#213 for extra metas: 403`}));
}).rejects.toThrow(expect.objectContaining({message: "Failed to get PR#213 for extra metas: 403"}));
expectNoChanges(false);
});