From afe59b42e646d8c3d94f17b853d4756d1058f9e6 Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Tue, 10 Dec 2024 09:00:56 -0800 Subject: [PATCH 1/4] require a database or secret --- src/commands/schema/schema.mjs | 2 ++ src/commands/schema/status.mjs | 28 +++++++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/commands/schema/schema.mjs b/src/commands/schema/schema.mjs index 2ff8be98..47693e29 100644 --- a/src/commands/schema/schema.mjs +++ b/src/commands/schema/schema.mjs @@ -6,6 +6,7 @@ import diffCommand from "./diff.mjs"; import pullCommand from "./pull.mjs"; import pushCommand from "./push.mjs"; import statusCommand from "./status.mjs"; +import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs"; export const localSchemaOptions = { "project-directory": { @@ -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..1814b80e 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"); @@ -17,7 +18,9 @@ async function doStatus(argv) { let params = new URLSearchParams({ diff: "summary" }); const secret = await getSecret(); const gatherFSL = container.resolve("gatherFSL"); - const fsl = reformatFSL(await gatherFSL(argv.dir)); + + const files = await gatherFSL(argv.dir); + const fsl = reformatFSL(files); const statusResponse = await makeFaunaRequest({ argv, @@ -32,6 +35,7 @@ async function doStatus(argv) { staged: "true", version: statusResponse.version, }); + const validationResponse = await makeFaunaRequest({ argv, path: "/schema/1/validate", @@ -64,16 +68,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 { From 26eb68fc33c7e9f3e5e1c4698565abde9c8cbe3c Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Tue, 10 Dec 2024 09:20:41 -0800 Subject: [PATCH 2/4] add a test --- src/commands/schema/schema.mjs | 2 +- src/commands/schema/status.mjs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/commands/schema/schema.mjs b/src/commands/schema/schema.mjs index 47693e29..e2d22dda 100644 --- a/src/commands/schema/schema.mjs +++ b/src/commands/schema/schema.mjs @@ -1,12 +1,12 @@ //@ts-check +import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs"; import abandonCommand from "./abandon.mjs"; import commitCommand from "./commit.mjs"; import diffCommand from "./diff.mjs"; import pullCommand from "./pull.mjs"; import pushCommand from "./push.mjs"; import statusCommand from "./status.mjs"; -import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs"; export const localSchemaOptions = { "project-directory": { diff --git a/src/commands/schema/status.mjs b/src/commands/schema/status.mjs index 1814b80e..dc7822e5 100644 --- a/src/commands/schema/status.mjs +++ b/src/commands/schema/status.mjs @@ -18,9 +18,7 @@ async function doStatus(argv) { let params = new URLSearchParams({ diff: "summary" }); const secret = await getSecret(); const gatherFSL = container.resolve("gatherFSL"); - - const files = await gatherFSL(argv.dir); - const fsl = reformatFSL(files); + const fsl = reformatFSL(await gatherFSL(argv.dir)); const statusResponse = await makeFaunaRequest({ argv, From d1de3f5533b9cec42392118a1d609ab6b14c1893 Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Tue, 10 Dec 2024 09:21:16 -0800 Subject: [PATCH 3/4] add a test for real --- test/schema/schema.mjs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test/schema/schema.mjs diff --git a/test/schema/schema.mjs b/test/schema/schema.mjs new file mode 100644 index 00000000..ba10009e --- /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.only("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; + }); + }); +}); From 86585f0178cf682c406295f92a9b12de42803e8c Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Tue, 10 Dec 2024 09:24:04 -0800 Subject: [PATCH 4/4] remove .only from test --- test/schema/schema.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/schema/schema.mjs b/test/schema/schema.mjs index ba10009e..92dc8d48 100644 --- a/test/schema/schema.mjs +++ b/test/schema/schema.mjs @@ -6,7 +6,7 @@ import chalk from "chalk"; import { builtYargs, run } from "../../src/cli.mjs"; import { setupTestContainer as setupContainer } from "../../src/config/setup-test-container.mjs"; -describe.only("schema", function () { +describe("schema", function () { let container, logger; beforeEach(() => { container = setupContainer();