Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/cli/e2e/upload-ignore-failures.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";

import { run } from "./utils";

// A malformed token makes the upload fail on config validation, before any
// network call, so these tests neither touch the API nor wait on its retries.
const failingEnv: NodeJS.ProcessEnv = {
...process.env,
ARGOS_TOKEN: "invalid",
ARGOS_IGNORE_UPLOAD_FAILURES: undefined,
};

const args = ["upload", "../../__fixtures__/screenshots/nested"];

describe("upload failures", () => {
it("fail the command by default", () => {
expect(() => run(args, failingEnv)).toThrow(
expect.objectContaining({ status: 1 }),
);
});

it("are ignored with --ignore-upload-failures", () => {
const result = run([...args, "--ignore-upload-failures"], failingEnv);
expect(result.combined).toContain("Build failed");
expect(result.combined).toContain(
"Upload failure ignored due to --ignore-upload-failures",
);
});

it("are ignored with ARGOS_IGNORE_UPLOAD_FAILURES", () => {
expect(() =>
run(args, { ...failingEnv, ARGOS_IGNORE_UPLOAD_FAILURES: "true" }),
).not.toThrow();
});
});
33 changes: 33 additions & 0 deletions packages/cli/src/commands/upload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, it } from "vitest";
import { getIgnoreUploadFailuresSource } from "./upload";

describe("getIgnoreUploadFailuresSource", () => {
it("returns null by default", () => {
expect(getIgnoreUploadFailuresSource({}, {})).toBeNull();
});

it("prefers the flag over the environment variable", () => {
expect(
getIgnoreUploadFailuresSource(
{ ignoreUploadFailures: true },
{ ARGOS_IGNORE_UPLOAD_FAILURES: "false" },
),
).toBe("--ignore-upload-failures");
});

it.each([
["true", "ARGOS_IGNORE_UPLOAD_FAILURES"],
["TRUE", "ARGOS_IGNORE_UPLOAD_FAILURES"],
["1", "ARGOS_IGNORE_UPLOAD_FAILURES"],
["false", null],
["False", null],
["", null],
])("reads ARGOS_IGNORE_UPLOAD_FAILURES=%j as %j", (value, expected) => {
expect(
getIgnoreUploadFailuresSource(
{},
{ ARGOS_IGNORE_UPLOAD_FAILURES: value },
),
).toBe(expected);
});
});
34 changes: 34 additions & 0 deletions packages/cli/src/commands/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,29 @@ type UploadOptions = BuildNameOption &
referenceCommit?: string | undefined;
threshold?: number | undefined;
subset?: boolean | undefined;
ignoreUploadFailures?: boolean | undefined;
};

/**
* Where `--ignore-upload-failures` comes from, or `null` when upload failures
* should fail the command.
*/
export function getIgnoreUploadFailuresSource(
options: Pick<UploadOptions, "ignoreUploadFailures">,
env: NodeJS.ProcessEnv = process.env,
): string | null {
if (options.ignoreUploadFailures) {
return "--ignore-upload-failures";
}
// Read like the core reads its boolean variables, such as `ARGOS_SUBSET`:
// any value but an empty one or "false".
const envValue = env.ARGOS_IGNORE_UPLOAD_FAILURES;
if (!!envValue && envValue.toLowerCase() !== "false") {
return "ARGOS_IGNORE_UPLOAD_FAILURES";
}
return null;
}

export function uploadCommand(program: Command) {
program
.command("upload")
Expand Down Expand Up @@ -97,6 +118,12 @@ export function uploadCommand(program: Command) {
"Whether this build contains only a subset of screenshots.\nThis is useful when a build is created from an incomplete test suite where some tests are skipped.\nCan also be set via the ARGOS_SUBSET environment variable.",
),
)
.addOption(
new Option(
"--ignore-upload-failures",
"Exit successfully when the upload fails, after logging the error.\nThis is useful to keep CI green when Argos can't be reached or the project isn't set up yet.\nCan also be set via the ARGOS_IGNORE_UPLOAD_FAILURES environment variable.",
),
)
.action(async (directory: string, options: UploadOptions) => {
const spinner = ora("Uploading screenshots").start();
try {
Expand Down Expand Up @@ -137,6 +164,13 @@ export function uploadCommand(program: Command) {
if (error instanceof Error) {
spinner.fail(`Build failed: ${error.message}`);
console.error(error.stack);
} else {
spinner.fail("Build failed");
}
const ignoreSource = getIgnoreUploadFailuresSource(options);
if (ignoreSource) {
console.warn(`⚠️ Upload failure ignored due to ${ignoreSource}`);
return;
}
process.exit(1);
}
Expand Down
64 changes: 43 additions & 21 deletions packages/cypress/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export type RegisterArgosTaskOptions = Omit<
* @default true
*/
uploadToArgos?: boolean;
/**
* Do not fail the Cypress run when the upload to Argos fails. The error is
* still logged.
* @default false
*/
ignoreUploadFailures?: boolean;
};

function checkIsCypressFailedResult(
Expand Down Expand Up @@ -49,9 +55,12 @@ async function createDirectory(directory: string): Promise<void> {
function getArgosConfigFromOptions(
options: RegisterArgosTaskOptions | undefined,
) {
return {
uploadToArgos: options?.uploadToArgos ?? true,
};
const {
uploadToArgos = true,
ignoreUploadFailures,
...uploadParameters
} = options ?? {};
return { uploadToArgos, ignoreUploadFailures, uploadParameters };
}

/**
Expand Down Expand Up @@ -120,32 +129,45 @@ export async function argosAfterRun(
| CypressCommandLine.CypressFailedRunResult,
options?: RegisterArgosTaskOptions,
) {
const { uploadToArgos } = getArgosConfigFromOptions(options);
const { uploadToArgos, ignoreUploadFailures, uploadParameters } =
getArgosConfigFromOptions(options);

if (!uploadToArgos) {
return;
}

const argosScreenshotsDir = await getScreenshotsDirectory();

const res = await upload({
...options,
files: ["**/*.png"],
root: argosScreenshotsDir,
metadata: {
testReport: checkIsCypressFailedResult(results)
? { status: "failed" }
: {
status: "passed",
stats: {
startTime: results.startedTestsAt,
duration: results.totalDuration,
try {
const res = await upload({
...uploadParameters,
files: ["**/*.png"],
root: argosScreenshotsDir,
metadata: {
testReport: checkIsCypressFailedResult(results)
? { status: "failed" }
: {
status: "passed",
stats: {
startTime: results.startedTestsAt,
duration: results.totalDuration,
},
},
},
},
});

console.log(`✅ Argos build created: ${res.build.url}`);
},
});

console.log(`✅ Argos build created: ${res.build.url}`);
} catch (error) {
// Fails the Cypress run.
if (!ignoreUploadFailures) {
throw error;
}
console.error(`❌ Error while creating the Argos build`);
console.error(error);
console.warn(
"⚠️ Upload failure ignored due to the `ignoreUploadFailures` option",
);
}
}

/**
Expand Down
Loading