Skip to content

Commit

Permalink
feat: add 'config' command
Browse files Browse the repository at this point in the history
  • Loading branch information
KuznetsovRoman committed Jan 13, 2025
1 parent 189c8d2 commit f0313d4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/cli/commands/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Testplane } from "../../../testplane";
import { CliCommands } from "../../constants";
import logger from "../../../utils/logger";

const { CONFIG: commandName } = CliCommands;

export const registerCmd = (cliTool: typeof commander, testplane: Testplane): void => {
cliTool
.command(commandName)
.description("Lists all browsers from the config")
.option("-c, --config <path>", "path to configuration file")
.option("--space <count>", "white spaces count to insert into the JSON output", Number, 0)
.action(async (options: typeof commander) => {
const { space } = options;

try {
console.info(JSON.stringify(testplane.config, null, space));

process.exit(0);
} catch (err) {
logger.error((err as Error).stack || err);
process.exit(1);
}
});
};
1 change: 1 addition & 0 deletions src/cli/commands/install-deps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const registerCmd = (cliTool: typeof commander, testplane: Testplane): vo
cliTool
.command(commandName)
.description("Install browsers to run locally with 'gridUrl': 'local' or '--local' cli argument")
.option("-c, --config <path>", "path to configuration file")
.arguments("[browsers...]")
.action(async (browsers: string[]) => {
try {
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/list-browsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const registerCmd = (cliTool: typeof commander, testplane: Testplane): vo
cliTool
.command(commandName)
.description("Lists all browsers from the config")
.option("-c, --config <path>", "path to configuration file")
.option(
"--type [type]",
"return browsers in specified type ('tags': browserName and browserVersion, 'ids': browserId from config)",
Expand Down
1 change: 1 addition & 0 deletions src/cli/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const CliCommands = {
CONFIG: "config",
LIST_TESTS: "list-tests",
LIST_BROWSERS: "list-browsers",
INSTALL_DEPS: "install-deps",
Expand Down
55 changes: 55 additions & 0 deletions test/src/cli/commands/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Command } from "@gemini-testing/commander";
import sinon, { SinonStub, SinonSpy } from "sinon";

import { Testplane } from "../../../../../src/testplane";
import * as testplaneCli from "../../../../../src/cli";

describe("cli/commands/config", () => {
const sandbox = sinon.createSandbox();

let testplaneStub: Testplane;
let consoleInfoStub: SinonStub;
let jsonStringifyStub: SinonSpy;

const config_ = async (options: string[] = [], cli: { run: VoidFunction } = testplaneCli): Promise<void> => {
process.argv = ["foo/bar/node", "foo/bar/script", "config", ...options];
cli.run();

await (Command.prototype.action as SinonStub).lastCall.returnValue;
};

beforeEach(() => {
testplaneStub = Object.create(Testplane.prototype);

sandbox.stub(Testplane, "create").returns(testplaneStub);

consoleInfoStub = sandbox.stub(console, "info");
jsonStringifyStub = sandbox.spy(JSON, "stringify");

sandbox.stub(process, "exit");

sandbox.spy(Command.prototype, "action");
});

afterEach(() => sandbox.restore());

it("should exit with code 0", async () => {
await config_();

assert.calledOnceWith(process.exit as unknown as SinonStub, 0);
});

it("should output testplane config", async () => {
await config_();

assert.calledWith(jsonStringifyStub, testplaneStub.config, null, 0);
assert.calledOnceWith(consoleInfoStub, JSON.stringify(testplaneStub.config, null, 0));
});

it("should output testplane config with spacing", async () => {
await config_(["--space", "4"]);

assert.calledWith(jsonStringifyStub, testplaneStub.config, null, 4);
assert.calledOnceWith(consoleInfoStub, JSON.stringify(testplaneStub.config, null, 4));
});
});

0 comments on commit f0313d4

Please sign in to comment.