diff --git a/src/commands/schema/schema.mjs b/src/commands/schema/schema.mjs index 2ff8be98..e2d22dda 100644 --- a/src/commands/schema/schema.mjs +++ b/src/commands/schema/schema.mjs @@ -1,5 +1,6 @@ //@ts-check +import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs"; import abandonCommand from "./abandon.mjs"; import commitCommand from "./commit.mjs"; import diffCommand from "./diff.mjs"; @@ -25,6 +26,7 @@ function buildSchema(yargs) { .command(pushCommand) .command(pullCommand) .command(statusCommand) + .check(validateDatabaseOrSecret) .demandCommand(); } diff --git a/src/commands/schema/status.mjs b/src/commands/schema/status.mjs index 48943a29..dc7822e5 100644 --- a/src/commands/schema/status.mjs +++ b/src/commands/schema/status.mjs @@ -9,6 +9,7 @@ import { } from "../../lib/command-helpers.mjs"; import { getSecret } from "../../lib/fauna-client.mjs"; import { reformatFSL } from "../../lib/schema.mjs"; +import { localSchemaOptions } from "./schema.mjs"; async function doStatus(argv) { const logger = container.resolve("logger"); @@ -32,6 +33,7 @@ async function doStatus(argv) { staged: "true", version: statusResponse.version, }); + const validationResponse = await makeFaunaRequest({ argv, path: "/schema/1/validate", @@ -64,16 +66,18 @@ async function doStatus(argv) { } function buildStatusCommand(yargs) { - return yargsWithCommonQueryOptions(yargs).example([ - [ - "$0 schema status --database us/example", - "Get the staged schema status for the 'us/example' database.", - ], - [ - "$0 schema status --secret my-secret", - "Get the staged schema status for the database scoped to a secret.", - ], - ]); + return yargsWithCommonQueryOptions(yargs) + .options(localSchemaOptions) + .example([ + [ + "$0 schema status --database us/example", + "Get the staged schema status for the 'us/example' database.", + ], + [ + "$0 schema status --secret my-secret", + "Get the staged schema status for the database scoped to a secret.", + ], + ]); } export default { diff --git a/test/schema/schema.mjs b/test/schema/schema.mjs new file mode 100644 index 00000000..92dc8d48 --- /dev/null +++ b/test/schema/schema.mjs @@ -0,0 +1,35 @@ +//@ts-check + +import { expect } from "chai"; +import chalk from "chalk"; + +import { builtYargs, run } from "../../src/cli.mjs"; +import { setupTestContainer as setupContainer } from "../../src/config/setup-test-container.mjs"; + +describe("schema", function () { + let container, logger; + beforeEach(() => { + container = setupContainer(); + logger = container.resolve("logger"); + }); + + [ + { command: "schema status" }, + { command: "schema push" }, + { command: "schema abandon" }, + { command: "schema diff" }, + { command: "schema pull" }, + { command: "schema commit" }, + ].forEach(({ command }) => { + it(`requires a database or secret to call: ${command}`, async function () { + try { + await run(command, container); + } catch (e) {} + + expect(logger.stderr).to.have.been.calledWith( + `${chalk.reset(await builtYargs.getHelp())}\n\n${chalk.red("No database or secret specified. Please use either --database, --secret, or --local to connect to your desired Fauna database.")}`, + ); + expect(container.resolve("parseYargs")).to.have.been.calledOnce; + }); + }); +});