Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle No Local Schema Files #505

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions src/commands/schema/status.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@ts-check

import chalk from "chalk";
import path from "path";

import { container } from "../../cli.mjs";
import {
Expand All @@ -15,33 +16,38 @@ async function doStatus(argv) {
const logger = container.resolve("logger");
const makeFaunaRequest = container.resolve("makeFaunaRequest");

let params = new URLSearchParams({ diff: "summary" });
const secret = await getSecret();
const absoluteDirPath = path.resolve(argv.dir);
const gatherFSL = container.resolve("gatherFSL");
const fsl = reformatFSL(await gatherFSL(argv.dir));

const hasLocalSchema = fsl.entries().next().done === false;
henryfauna marked this conversation as resolved.
Show resolved Hide resolved

const statusParams = new URLSearchParams({ diff: "summary" });
Copy link
Contributor

@ptpaterson ptpaterson Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no diff param documented for /status or /diff endpoints. Maybe that's an old value? Is the format: "summary" the param you want?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can confirm — format is the new alias for diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool - I've updated the command to use format as well as the expectation in the tests

const statusResponse = await makeFaunaRequest({
argv,
path: "/schema/1/staged/status",
params,
params: statusParams,
method: "GET",
secret,
});

params = new URLSearchParams({
diff: "summary",
staged: "true",
version: statusResponse.version,
});

const validationResponse = await makeFaunaRequest({
argv,
path: "/schema/1/diff",
params,
method: "POST",
body: fsl,
secret,
});
let diffResponse = null;
if (hasLocalSchema) {
const diffParams = new URLSearchParams({
diff: "summary",
staged: "true",
version: statusResponse.version,
});
diffResponse = await makeFaunaRequest({
argv,
path: "/schema/1/diff",
params: diffParams,
method: "POST",
body: fsl,
secret,
});
}

logger.stdout(`Staged changes: ${chalk.bold(statusResponse.status)}`);
if (statusResponse.pending_summary !== "") {
Expand All @@ -52,14 +58,18 @@ async function doStatus(argv) {
logger.stdout(statusResponse.diff.split("\n").join("\n "));
}

if (validationResponse.error) {
if (!hasLocalSchema) {
logger.stdout(
`Local changes: ${chalk.bold(`no schema files found in '${absoluteDirPath}'`)}\n`,
);
} else if (diffResponse.error) {
logger.stdout(`Local changes:`);
throw new CommandError(validationResponse.error.message);
} else if (validationResponse.diff === "") {
throw new CommandError(diffResponse.error.message);
} else if (diffResponse.diff === "") {
logger.stdout(`Local changes: ${chalk.bold("none")}\n`);
} else {
logger.stdout(`Local changes:\n`);
logger.stdout(` ${validationResponse.diff.split("\n").join("\n ")}`);
logger.stdout(` ${diffResponse.diff.split("\n").join("\n ")}`);
logger.stdout("(use `fauna schema diff` to display local changes)");
logger.stdout("(use `fauna schema push` to stage local changes)");
}
Expand Down
65 changes: 60 additions & 5 deletions test/schema/status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

import { expect } from "chai";
import chalk from "chalk";
import sinon from "sinon";

import { run } from "../../src/cli.mjs";
import { setupTestContainer as setupContainer } from "../../src/config/setup-test-container.mjs";
import { reformatFSL } from "../../src/lib/schema.mjs";
import { buildUrl, commonFetchParams, f } from "../helpers.mjs";

describe("schema status", function () {
let container, fetch, logger;
let container, fetch, logger, gatherFSL;

let fsl = [
{
name: "collections.fsl",
content: "collection Customer {\n name: String\n email: String\n}\n",
},
];

let summaryDiff =
"\x1B[1;34m* Adding collection `NewCollection`\x1B[0m to collections.fsl:2:1\n" +
Expand Down Expand Up @@ -66,9 +75,52 @@ describe("schema status", function () {
container = setupContainer();
fetch = container.resolve("fetch");
logger = container.resolve("logger");
gatherFSL = container.resolve("gatherFSL");
});

it("notifies the user when no local schema is found", async function () {
gatherFSL.resolves([]);
fetch.onCall(0).resolves(
f({
version: 0,
status: "none",
diff: "Staged schema: none",
pending_summary: "",
text_diff: "",
henryfauna marked this conversation as resolved.
Show resolved Hide resolved
}),
);
fetch.onCall(1).resolves(
f({
version: 0,
diff: "",
}),
);

await run(`schema status --secret "secret"`, container);

expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/staged/status", { diff: "summary", color: "ansi" }),
commonFetchParams,
);
expect(fetch).not.to.have.been.calledWith(
buildUrl("/schema/1/validate", {
diff: "summary",
staged: "true",
version: "0",
color: "ansi",
}),
{ ...commonFetchParams, method: "POST", body: new FormData() },
);
expect(logger.stdout).to.have.been.calledWith(
`Staged changes: ${chalk.bold("none")}`,
);
expect(logger.stdout).to.have.been.calledWith(
sinon.match(/^Local changes: .*no schema files found in.*\n$/),
);
});

it("fetches the current status when there are no changes", async function () {
gatherFSL.resolves(fsl);
fetch.onCall(0).resolves(
f({
version: 0,
Expand Down Expand Up @@ -98,7 +150,7 @@ describe("schema status", function () {
version: "0",
color: "ansi",
}),
{ ...commonFetchParams, method: "POST", body: new FormData() },
{ ...commonFetchParams, method: "POST", body: reformatFSL(fsl) },
);
expect(logger.stdout).to.have.been.calledWith(
`Staged changes: ${chalk.bold("none")}`,
Expand All @@ -109,6 +161,7 @@ describe("schema status", function () {
});

it("fetches the current status when there are only local changes", async function () {
gatherFSL.resolves(fsl);
fetch.onCall(0).resolves(
f({
version: 0,
Expand Down Expand Up @@ -141,7 +194,7 @@ describe("schema status", function () {
version: "0",
color: "ansi",
}),
{ ...commonFetchParams, method: "POST", body: new FormData() },
{ ...commonFetchParams, method: "POST", body: reformatFSL(fsl) },
);
expect(logger.stdout).to.have.been.calledWith(
`Staged changes: ${chalk.bold("none")}`,
Expand All @@ -160,6 +213,7 @@ describe("schema status", function () {
});

it("fetches the current status when there are only staged changes", async function () {
gatherFSL.resolves(fsl);
fetch.onCall(0).resolves(
f({
version: 0,
Expand Down Expand Up @@ -190,7 +244,7 @@ describe("schema status", function () {
version: "0",
color: "ansi",
}),
{ ...commonFetchParams, method: "POST", body: new FormData() },
{ ...commonFetchParams, method: "POST", body: reformatFSL(fsl) },
);
expect(logger.stdout).to.have.been.calledWith(
`Staged changes: ${chalk.bold("ready")}`,
Expand All @@ -205,6 +259,7 @@ describe("schema status", function () {
});

it("fetches the current status when there are both local and staged changes", async function () {
gatherFSL.resolves(fsl);
fetch.onCall(0).resolves(
f({
version: 0,
Expand Down Expand Up @@ -236,7 +291,7 @@ describe("schema status", function () {
version: "0",
color: "ansi",
}),
{ ...commonFetchParams, method: "POST", body: new FormData() },
{ ...commonFetchParams, method: "POST", body: reformatFSL(fsl) },
);
expect(logger.stdout).to.have.been.calledWith(
`Staged changes: ${chalk.bold("ready")}`,
Expand Down
Loading