Skip to content

Commit

Permalink
fix: use run params everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
agoldis committed Dec 23, 2022
1 parent a1cf4c3 commit 133d135
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 123 deletions.
17 changes: 8 additions & 9 deletions packages/cypress-cloud/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import("./lib/init");

import { cutInitialOutput, resetCapture } from "./lib/capture";
import { getCurrentsConfig, mergeConfig } from "./lib/config";
import { getConfig } from "./lib/config";
import { setRunId } from "./lib/httpClient";
import {
getFailedDummyResult,
Expand All @@ -23,8 +23,7 @@ import { getPlatformInfo } from "./lib/platform";
import { summaryTable } from "./lib/table";

/**
* Run the Cypress tests.
* You can either pass the options as a parameter, or run the cli.
* Run the Cypress tests and return the results.
*
* @augments RunOptions
* @returns {TestsResult | undefined} The test results, or undefined if no tests were run.
Expand All @@ -35,7 +34,7 @@ export async function run(params: CurrentsRunParameters) {
const { key, projectId, group, parallel, ciBuildId, tags, testingType } =
params;

const config = await mergeConfig(params);
const config = await getConfig(params);
const specPattern = params.spec || config.specPattern;
const specs = await findSpecs({
projectRoot: config.projectRoot,
Expand All @@ -47,13 +46,13 @@ export async function run(params: CurrentsRunParameters) {
});

if (specs.length === 0) {
warn("No spec files found to execute. Used configuration: %O", {
warn("No spec files found to execute. Configuration: %O", {
specPattern,
configSpecPattern: config.specPattern,
excludeSpecPattern: [
...config.excludeSpecPattern,
config.excludeSpecPattern,
config.additionalIgnorePattern,
],
].flat(2),
testingType,
});
return;
Expand Down Expand Up @@ -82,7 +81,7 @@ export async function run(params: CurrentsRunParameters) {
ciBuildId,
projectId,
recordKey: key,
specPattern,
specPattern: [specPattern].flat(2),
tags,
testingType,
});
Expand Down Expand Up @@ -124,7 +123,7 @@ async function runTillDone(
platform,
config,
}: CreateInstancePayload & {
config: ReturnType<typeof getCurrentsConfig>;
config: Awaited<ReturnType<typeof getConfig>>;
},
cypressRunOptions: CurrentsRunParameters
) {
Expand Down
55 changes: 31 additions & 24 deletions packages/cypress-cloud/lib/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import cypress from "cypress";
import cp from "child_process";
import { getBinPath } from "cy2";
import Debug from "debug";
import fs from "fs";
import { chain } from "lodash";
import { customAlphabet } from "nanoid";
import VError from "verror";
import { VError } from "verror";
import { CurrentsRunParameters } from "../types";
import { getStrippedCypressOptions, serializeOptions } from "./cli/cli";
import { createTempFile } from "./fs";
import { error } from "./log";
const debug = Debug("currents:boot");

const debug = Debug("currents:boot");
const getDummySpec = customAlphabet("abcdefghijklmnopqrstuvwxyz", 10);

export const bootCypress = async (
Expand All @@ -18,37 +20,39 @@ export const bootCypress = async (
debug("booting cypress...");
const tempFilePath = await createTempFile();

const serializedOptions = serializeOptions(
getStrippedCypressOptions(params)
).flatMap((arg) => arg.split(" "));
const serializedOptions = chain(getStrippedCypressOptions(params))
.thru((opts) => ({
...opts,
// merge the env with the currents specific env variables
env: {
...(opts.env ?? {}),
currents_temp_file: tempFilePath,
currents_port: port,
currents_debug_enabled: process.env.DEBUG?.includes("currents:")
? "true"
: "false",
},
}))
.thru(serializeOptions)
.flatMap((arg) => arg.split(" "))
.filter(Boolean)
.value();

// it is important to pass the same args in order to get the same config as for the actual run
const args: string[] = [
"run",
"--spec",
getDummySpec(),
"--env",
`currents_port=${port},currents_temp_file=${tempFilePath},currents_debug_enabled=${
process.env.DEBUG?.includes("currents:") ? "true" : "false"
}`,
...serializedOptions,
];

try {
await cypress.run({
...getStrippedCypressOptions(params),
spec: getDummySpec(),
});
} catch (e) {
console.log(e);
}
debug("booting cypress with args: %o", args);
// const cypressBin = await getBinPath(require.resolve("cypress"));
// debug("cypress executable location: %s", cypressBin);
const cypressBin = await getBinPath(require.resolve("cypress"));
debug("cypress executable location: %s", cypressBin);

// const child = cp.spawnSync(cypressBin, args, {
// stdio: "pipe",
// });
const child = cp.spawnSync(cypressBin, args, {
stdio: "pipe",
});

if (!fs.existsSync(tempFilePath)) {
throw new VError(
Expand All @@ -57,8 +61,11 @@ export const bootCypress = async (
);
}
try {
return JSON.parse(fs.readFileSync(tempFilePath, "utf-8"));
const f = fs.readFileSync(tempFilePath, "utf-8");
debug("cypress config '%s': '%s'", tempFilePath, f);
return JSON.parse(f);
} catch (err) {
debug("read config temp file failed: %o", err);
error("Running cypress failed with the following output:");
console.dir({
stdout: child.stdout.toString("utf-8").split("\n"),
Expand Down
102 changes: 40 additions & 62 deletions packages/cypress-cloud/lib/cli/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,75 @@ const getProgram = () =>
});

const p = (args: string[]) =>
parseOptions(getProgram(), ["program", "command", ...args]);
parseOptions(getProgram(), ["program", "command", "--key", "some", ...args]);

const defaults = {
parallel: false,
record: true,
testingType: "e2e",
};
describe("CLI", () => {
it("has defaults", () => expect(p([])).toMatchObject(defaults));
it("parses browser", () => {
expect(p(["--browser", "some"])).toMatchObject({
process.env.CURRENTS_PROJECT_ID = "some";
it("has defaults", async () => expect(await p([])).toMatchObject(defaults));
it("parses browser", async () => {
expect(await p(["--browser", "some"])).toMatchObject({
browser: "some",
});
});

it("parses --ci-build-id", () => {
expect(p(["--ci-build-id", "some"])).toMatchObject({
it("parses --ci-build-id", async () => {
expect(await p(["--ci-build-id", "some"])).toMatchObject({
ciBuildId: "some",
});
});

it("parses spec into an array", async () => {
expect(p(["--spec", "a,b", "--spec", "c"])).toMatchObject({
expect(await p(["--spec", "a,b", "--spec", "c"])).toMatchObject({
spec: ["a", "b", "c"],
});
});

it("parses empty spec into nothing", async () => {
expect(p([])).toMatchObject({
spec: undefined,
});
expect(await p([])).toMatchObject({});
});

it("parses --component", async () => {
expect(p(["--component"])).toMatchObject({
expect(await p(["--component"])).toMatchObject({
testingType: "component",
});
});

it("parses --e2e", async () => {
expect(p(["--e2e"])).toMatchObject({
expect(await p(["--e2e"])).toMatchObject({
testingType: "e2e",
});
});

it("parses --config comma-separated", async () => {
expect(p(["--config", "a=b,c=d"])).toMatchObject({
expect(await p(["--config", "a=b,c=d"])).toMatchObject({
config: {
a: "b",
c: "d",
},
});
});

xit("parses --config json", async () => {
expect(p(["--config", `c={'a': 'b'}`])).toMatchObject({
it("parses --config json", async () => {
expect(
await p(["--config", `{"a": "b", "c": 1, "d": {"nested": true } }`])
).toMatchObject({
config: {
a: "b",
c: 1,
d: {
nested: true,
},
},
});
});

it("parses --env comma-separated", async () => {
expect(p(["--env", "a=b,c=d"])).toMatchObject({
expect(await p(["--env", "a=b,c=d"])).toMatchObject({
env: {
a: "b",
c: "d",
Expand All @@ -83,110 +88,83 @@ describe("CLI", () => {
});

it("parses -C", async () => {
expect(p(["-C", "some"])).toMatchObject({
expect(await p(["-C", "some"])).toMatchObject({
configFile: "some",
});
});

it("parses --group", async () => {
expect(p(["--group", "some"])).toMatchObject({
expect(await p(["--group", "some"])).toMatchObject({
group: "some",
});
});

it("parses --key", async () => {
expect(p(["--key", "some"])).toMatchObject({
expect(await p(["--key", "some"])).toMatchObject({
key: "some",
});
});

it("parses --parallel", async () => {
expect(p(["--parallel"])).toMatchObject({
expect(await p(["--parallel"])).toMatchObject({
parallel: true,
});
});

it("parses no --parallel", async () => {
expect(p([])).toMatchObject({
expect(await p([])).toMatchObject({
parallel: false,
});
});

it("parses --port", async () => {
expect(p(["--port", "8080"])).toMatchObject({
expect(await p(["--port", "8080"])).toMatchObject({
port: 8080,
});
});

it("parses --P", async () => {
expect(p(["-P", "some"])).toMatchObject({
expect(await p(["-P", "some"])).toMatchObject({
project: "some",
});
});

it("parses --record", async () => {
expect(p(["--record"])).toMatchObject({
expect(await p(["--record"])).toMatchObject({
record: true,
});
});

it("parses no --record", async () => {
expect(p([""])).toMatchObject({
expect(await p([""])).toMatchObject({
record: true,
});
});

it("parses tags into an array", async () => {
expect(p(["--tag", "a,b", "--tag", "c", "--key", "a"])).toMatchObject({
tags: ["a", "b", "c"],
});
expect(await p(["--tag", "a,b", "--tag", "c", "--key", "a"])).toMatchObject(
{
tags: ["a", "b", "c"],
}
);
});

it("cannot use --e2e and --component together", async () => {
expect(() =>
parseOptions(getProgram(), [
"program",
"command",
"--component",
"--e2e",
"--key",
"a",
])
).toThrowErrorMatchingInlineSnapshot(
await expect(async () =>
p(["--component", "--e2e"])
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Cannot use both e2e and component options"`
);
});

it("e2e is the default, when no explicit params", async () => {
const parsedOptions = parseOptions(getProgram(), [
"program",
"command",
"--key",
"a",
]);
expect(parsedOptions).toMatchObject({
testingType: "e2e",
});
});

it("using component implies testingType is 'component'", async () => {
expect(
parseOptions(getProgram(), [
"program",
"command",
"--component",
"--key",
"a",
])
).toMatchObject({
expect(await p(["program", "command", "--component"])).toMatchObject({
testingType: "component",
});
});

it("using e2e implies testingType is 'e2e'", async () => {
expect(
parseOptions(getProgram(), ["program", "command", "--e2e", "--key", "a"])
).toMatchObject({
expect(await p(["--e2e"])).toMatchObject({
testingType: "e2e",
});
});
Expand Down
Loading

0 comments on commit 133d135

Please sign in to comment.