Skip to content

Commit 3c8d710

Browse files
authored
fix(plugin-testops): allow local run for testops plugin (#687)
1 parent f6b51c7 commit 3c8d710

7 files changed

Lines changed: 37 additions & 22 deletions

File tree

packages/ci/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@
99
- 💬 [General Discussion ](https://github.com/orgs/allure-framework/discussions/categories/general-discussion) – engage in casual conversations, share insights and ideas with the community
1010

1111
---
12-
13-
TODO:

packages/ci/src/detect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import { local } from "./detectors/local.js";
1212

1313
/**
1414
* Tries to detect current CI
15-
* Returns CI descriptor if some detected, otherwise undefined
15+
* Returns CI descriptor, otherwise local descriptor
1616
*/
17-
export const detect = (): CiDescriptor | undefined => {
17+
export const detect = (): CiDescriptor => {
1818
return (
1919
[amazon, azure, bitbucket, circle, drone, github, gitlab, jenkins].find((descriptor) => descriptor.detected) ??
2020
local

packages/ci/src/detectors/local.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ export const local: CiDescriptor = {
6262
return "";
6363
},
6464
};
65+
66+
export const isLocalCiDescriptor = (ci: CiDescriptor): ci is Omit<CiDescriptor, "type"> & { type: CiType.Local } =>
67+
ci.type === CiType.Local;

packages/ci/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { detect } from "./detect.js";
2+
export { isLocalCiDescriptor } from "./detectors/local.js";

packages/plugin-testops/src/plugin.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { env } from "node:process";
22

3-
import { detect } from "@allurereport/ci";
4-
import type { CategoryDefinition, CiDescriptor, EnvironmentIdentity, TestStatus } from "@allurereport/core-api";
3+
import { detect, isLocalCiDescriptor } from "@allurereport/ci";
4+
import type { CategoryDefinition, EnvironmentIdentity, TestStatus } from "@allurereport/core-api";
55
import { getWorstStatus } from "@allurereport/core-api";
66
import { type AllureStore, type Plugin, type PluginContext, createPluginSummary } from "@allurereport/plugin-api";
77
import { uniqBy, stubTrue } from "lodash-es";
@@ -24,22 +24,16 @@ const categoryDisplayName = (cat: UploadCategory): string =>
2424

2525
export class TestOpsPlugin implements Plugin {
2626
#logger = new Logger("TestOpsPlugin");
27-
#ci?: CiDescriptor;
27+
#ci = detect();
2828
// @ts-expect-error - if client is not initialized it will not be used
2929
#client: TestOpsClient;
30-
/**
31-
* If the client is configured
32-
*/
33-
#clientConfigured: boolean = false;
3430
#launchName: string = "";
3531
#launchTags: string[] = [];
3632
#uploadedTestResultsIds: Set<string> = new Set();
3733
#autocloseLaunch: boolean = false;
3834

3935
constructor(readonly options: TestOpsPluginOptions) {
40-
this.#ci = detect();
41-
42-
if (!this.#ci || this.#ci.type === "local") {
36+
if (isLocalCiDescriptor(this.#ci) && !this.isOverridenByEnv) {
4337
this.#logger.info(
4438
`plugin is disabled - no CI environment detected. To enable, set ${bold("ALLURE_TESTOPS_ENABLED")}=true or ${bold("CI")}=true.`,
4539
);
@@ -58,7 +52,6 @@ export class TestOpsPlugin implements Plugin {
5852
// don't initialize the client when some options are missing
5953
// we can' throw an error here because it would break the report execution flow
6054
if ([accessToken, endpoint, projectId].every(Boolean)) {
61-
this.#clientConfigured = true;
6255
this.#client = new TestOpsClient({
6356
baseUrl: endpoint,
6457
accessToken,
@@ -102,7 +95,7 @@ export class TestOpsPlugin implements Plugin {
10295
}
10396

10497
get enabled(): boolean {
105-
if (!this.#clientConfigured) {
98+
if (!(this.#client instanceof TestOpsClient)) {
10699
return false;
107100
}
108101

packages/plugin-testops/test/features/quality-gate.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import { beforeEach, describe, expect, test, vi } from "vitest";
77
import { TestOpsPlugin } from "../../src/plugin.js";
88
import { handleBeforeEach, mockAllureStore, mockRequests } from "./helpers.js";
99

10-
vi.mock("@allurereport/ci", () => ({
11-
detect: vi.fn(() => ({ type: "github" }) as CiDescriptor),
12-
}));
10+
vi.mock("@allurereport/ci", async (importOriginal) => {
11+
const actual = await importOriginal<typeof import("@allurereport/ci")>();
12+
13+
return {
14+
...actual,
15+
detect: vi.fn(() => ({ type: "github" }) as CiDescriptor),
16+
};
17+
});
1318

1419
beforeEach(async () => {
1520
await epic("coverage");

packages/plugin-testops/test/plugin.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ import { TestOpsPlugin } from "../src/plugin.js";
1313
import { resolvePluginOptions } from "../src/utils/index.js";
1414
import { AllureStoreMock, TestOpsClientMock } from "./utils.js";
1515

16-
vi.mock("@allurereport/ci", () => ({
17-
detect: vi.fn(),
18-
}));
16+
vi.mock("@allurereport/ci", async (importOriginal) => {
17+
const actual = await importOriginal<typeof import("@allurereport/ci")>();
18+
19+
return {
20+
...actual,
21+
detect: vi.fn(),
22+
};
23+
});
1924

2025
vi.mock("../src/client.js", async () => {
2126
const utils = await import("./utils.js");
@@ -237,6 +242,11 @@ describe("testops plugin", () => {
237242
});
238243

239244
describe("outside ci mode", () => {
245+
beforeEach(() => {
246+
vi.stubEnv("ALLURE_TESTOPS_ENABLED", "");
247+
vi.stubEnv("CI", "");
248+
});
249+
240250
it("should return false from enabled getter when ci is local", () => {
241251
(detect as unknown as Mock).mockReturnValue({ type: "local" } as CiDescriptor);
242252
(resolvePluginOptions as Mock).mockReturnValue({
@@ -1108,6 +1118,11 @@ describe("testops plugin", () => {
11081118
});
11091119

11101120
describe("outside ci mode", () => {
1121+
beforeEach(() => {
1122+
vi.stubEnv("ALLURE_TESTOPS_ENABLED", "");
1123+
vi.stubEnv("CI", "");
1124+
});
1125+
11111126
it("should not stop upload when ci is local", async () => {
11121127
(detect as unknown as Mock).mockReturnValue({ type: "local" } as CiDescriptor);
11131128
(resolvePluginOptions as Mock).mockReturnValue({

0 commit comments

Comments
 (0)