Skip to content

Commit

Permalink
feat: add remote debug capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
agoldis committed Sep 26, 2023
1 parent e44347c commit 2cd73fb
Show file tree
Hide file tree
Showing 27 changed files with 222 additions and 63 deletions.
2 changes: 1 addition & 1 deletion packages/cypress-cloud/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { run } from "../lib/run";
import { parseCLIOptions, program } from "./lib";

async function main() {
return run(parseCLIOptions());
return run(await parseCLIOptions());
}

main()
Expand Down
6 changes: 4 additions & 2 deletions packages/cypress-cloud/bin/lib/cli.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { CurrentsRunParameters, TestingType } from "cypress-cloud/types";
import Debug from "debug";
import { activateDebug } from "../../lib/debug";
import { Debug, initRemoteDebug } from "../../lib/remote-debug";
import { sanitizeAndConvertNestedArgs } from "./parser";
import { program } from "./program";

const debug = Debug("currents:cli");

export function parseCLIOptions(
export async function parseCLIOptions(
_program: typeof program = program,
...args: Parameters<typeof program.parse>
) {
const opts = _program.parse(...args).opts();

await initRemoteDebug(opts, "cli");
activateDebug(opts.cloudDebug);

debug("parsed CLI flags %o", opts);

const { e2e, component } = opts;
Expand Down
14 changes: 13 additions & 1 deletion packages/cypress-cloud/bin/lib/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ ${getLegalNotice()}
)
.addOption(
new Option(
`--cloud-debug [true | string]`,
`--cloud-debug [true | ${Object.values(DebugMode).join(" | ")}]`,
`Enable debug mode for cypress-cloud, this will print out logs for troubleshooting. Values: [true | ${Object.values(
DebugMode
).join(
Expand All @@ -122,6 +122,18 @@ ${getLegalNotice()}
.argParser(parseCommaSeparatedList)
.default(undefined)
)
.addOption(
new Option(
`--cloud-debug-remote`,
`Enable sending debug logs to remote storage for troubleshooting`
).default(undefined)
)
.addOption(
new Option(
`--cloud-debug-silent`,
`Supress printing debug logs to stdout, this is useful when you want to send the logs to remote servers for troubleshooting without printing them to stdout`
).default(undefined)
)
.addOption(
new Option(
`--experimental-coverage-recording [bool]`,
Expand Down
10 changes: 6 additions & 4 deletions packages/cypress-cloud/lib/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,22 @@ export const updateInstanceStdout = (instanceId: string, stdout: string) =>
});

export const getDebugUrl = ({
recordKey,
runId,
type,
}: {
recordKey: string;
runId: string;
type: string;
}) => {
return makeRequest<
{ uploadUrl: string; readUrl: string },
{ recordKey: string; runId: string }
{ runId: string; type: string }
>({
// comment for local
baseURL: "https://cy.currents.dev",
method: "POST",
url: `runs/debug-logs`,
data: {
recordKey,
type,
runId,
},
}).then((result) => result.data);
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress-cloud/lib/bootstrap/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getBinPath } from "cy2";
import { ValidatedCurrentsParameters } from "cypress-cloud/types";
import Debug from "debug";
import execa, { ExecaError } from "execa";
import fs from "fs";
import { ValidationError } from "../errors";
import { createTempFile } from "../fs";
import { bold, info } from "../log";
import { Debug } from "../remote-debug";
import { require } from "../require";
import { getBootstrapArgs } from "./serializer";

Expand Down
2 changes: 1 addition & 1 deletion packages/cypress-cloud/lib/bootstrap/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {
CurrentsRunParameters,
CypressRunParameters,
} from "cypress-cloud/types";
import Debug from "debug";
import _ from "lodash";
import { getCypressRunAPIParams } from "../config";
import { shouldEnablePluginDebug } from "../debug";
import { sortObjectKeys } from "../lang";
import { getRandomString } from "../nano";
import { Debug } from "../remote-debug";
const debug = Debug("currents:boot");

export function getBootstrapArgs({
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress-cloud/lib/capture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Debug from "debug";
import { Debug } from "./remote-debug";
const debug = Debug("currents:capture");

const _write = process.stdout.write;
Expand Down
7 changes: 4 additions & 3 deletions packages/cypress-cloud/lib/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Debug from "debug";
import { Debug } from "../remote-debug";

import _ from "lodash";
import { P, match } from "ts-pattern";
import { DetectedBrowser, ValidatedCurrentsParameters } from "../../types";
import { bootCypress } from "../bootstrap";
Expand Down Expand Up @@ -93,7 +94,7 @@ export async function getMergedConfig(params: ValidatedCurrentsParameters) {
})
| undefined = await bootCypress(params);

debug("cypress resolvedConfig: %O", cypressResolvedConfig);
debug("cypress resolvedConfig: %o", cypressResolvedConfig);

// @ts-ignore
const rawE2EPattern = cypressResolvedConfig.rawJson?.e2e?.specPattern;
Expand All @@ -116,6 +117,6 @@ export async function getMergedConfig(params: ValidatedCurrentsParameters) {
resolved: cypressResolvedConfig,
experimentalCoverageRecording: params.experimentalCoverageRecording,
};
debug("merged config: %O", result);
debug("merged config: %o", _.omit(result, ["resolved"]));
return result;
}
4 changes: 3 additions & 1 deletion packages/cypress-cloud/lib/config/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
CypressRunParameters,
ValidatedCurrentsParameters,
} from "cypress-cloud/types";
import Debug from "debug";
import _ from "lodash";
import { shouldEnablePluginDebug } from "../debug";
import { ValidationError } from "../errors";
import { error } from "../log";
import { Debug } from "../remote-debug";
import { getCurrentsConfig } from "./config";
const debug = Debug("currents:validateParams");

Expand Down Expand Up @@ -171,6 +171,8 @@ export function getCypressRunAPIParams(
..._.pickBy(
_.omit(params, [
"cloudDebug",
"cloudDebugRemote",
"cloudDebugSilent",
"cloudConfigFile",
"autoCancelAfterFailures",
"cloudServiceUrl",
Expand Down
20 changes: 20 additions & 0 deletions packages/cypress-cloud/lib/debug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ export function shouldEnablePluginDebug(
)
.otherwise(() => false);
}

export function shouldEnableRemoteDebug(params: CurrentsRunParameters) {
if (!params["cloudDebugRemote"]) {
return false;
}

return match(params.cloudDebug)
.with(P.nullish, () => false)
.with(DebugMode.None, () => false)
.with(true, () => true)
.with(DebugMode.All, () => true)
.with(DebugMode.Currents, () => true)

.with(
P.array(P.string),
(v) => v.includes(DebugMode.All) || v.includes(DebugMode.Currents)
)
.otherwise(() => false);
}

export function activateDebug(mode: CurrentsRunParameters["cloudDebug"]) {
match(mode)
.with(P.instanceOf(Array), (i) => i.forEach(setDebugMode))
Expand Down
7 changes: 7 additions & 0 deletions packages/cypress-cloud/lib/httpClient/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export async function getClient() {
};

debug("network request: %o", {
baseUrl: req.baseURL,
..._.pick(req, "method", "url", "headers"),
data: Buffer.isBuffer(req.data) ? "buffer" : redactData(req.data),
});
Expand Down Expand Up @@ -106,6 +107,12 @@ function redactData(data: Record<string, any>) {
if (key === "stdout") {
return "<redacted stdout>";
}
if (key === "platform") {
return {
...value,
osCpus: "<omitted osCpus>",
};
}
return value;
});
}
Expand Down
13 changes: 11 additions & 2 deletions packages/cypress-cloud/lib/listener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Debug from "debug";
import { Debug } from "./remote-debug";

import { CypressTypes } from "./cypress.types";
import { Event, allEvents, getPubSub } from "./pubsub";
Expand All @@ -21,6 +21,9 @@ export function listenToEvents(
executionState: ExecutionState,
experimentalCoverageRecording: boolean = false
) {
getPubSub().on(Event.DEBUG, (payload: string) => {
debug("%s: %o", Event.DEBUG, payload);
});
getPubSub().on(
Event.RUN_RESULT,
({
Expand All @@ -38,7 +41,13 @@ export function listenToEvents(
// getSpecShortName(specRelative),
// "runResult"
// );
debug("%s %s: %o", Event.RUN_RESULT, instanceId, runResult);
debug(
"%s %s %s: %o",
Event.RUN_RESULT,
instanceId,
specRelative,
runResult
);
executionState.setInstanceResult(
instanceId,
ModuleAPIResults.getStandardResult(runResult, executionState)
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress-cloud/lib/platform/browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Debug from "debug";
import { DetectedBrowser, Platform } from "../../types";
import { warn } from "../log";
import { Debug } from "../remote-debug";

const debug = Debug("currents:browser");

Expand Down
6 changes: 3 additions & 3 deletions packages/cypress-cloud/lib/platform/platform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Debug from "debug";
import getos from "getos";
import { cpus, freemem, platform, release, totalmem } from "os";
import { freemem, platform, release, totalmem } from "os";
import { promisify } from "util";
import { Debug } from "../remote-debug";

const debug = Debug("currents:platform");

Expand All @@ -26,7 +26,7 @@ export const getPlatformInfo = async () => {
const result = {
osName: platform(),
osVersion,
osCpus: cpus(),
osCpus: [],
osMemory: {
free: freemem(),
total: totalmem(),
Expand Down
2 changes: 2 additions & 0 deletions packages/cypress-cloud/lib/pubsub/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ export enum Event {
TEST_BEFORE_RUN = "test:before:run",
AFTER_SCREENSHOT = "after:screenshot",
AFTER_SPEC = "after:spec",
BEFORE_SPEC = "before:spec",
DEBUG = "currents:debug",
}
export const allEvents = Object.values(Event);
Loading

0 comments on commit 2cd73fb

Please sign in to comment.