Skip to content

Commit

Permalink
Merge branch 'v3' into v3-no-strip-ansi
Browse files Browse the repository at this point in the history
  • Loading branch information
ecooper authored Dec 11, 2024
2 parents f75bb2b + 69a1217 commit 13293f8
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 40 deletions.
69 changes: 43 additions & 26 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 { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs";
Expand All @@ -13,34 +14,40 @@ 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 fslFiles = await gatherFSL(argv.dir);
const hasLocalSchema = fslFiles.length > 0;
const fsl = reformatFSL(fslFiles);

const statusParams = new URLSearchParams({ format: "summary" });
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({
staged: "true",
format: "summary",
version: statusResponse.version,
});
diffResponse = await makeFaunaRequest({
argv,
path: "/schema/1/diff",
params: diffParams,
method: "POST",
body: fsl,
secret,
});
}

// Output the status response
logger.stdout(`Staged changes: ${chalk.bold(statusResponse.status)}`);
if (statusResponse.pending_summary !== "") {
logger.stdout(statusResponse.pending_summary);
Expand All @@ -50,17 +57,27 @@ async function doStatus(argv) {
logger.stdout(statusResponse.diff.split("\n").join("\n "));
}

if (validationResponse.error) {
logger.stdout(`Local changes:`);
throw new CommandError(validationResponse.error.message);
} else if (validationResponse.diff === "") {
// Output the diff response
if (!hasLocalSchema) {
logger.stdout(
`Local changes: ${chalk.bold(`no schema files found in '${absoluteDirPath}'`)}\n`,
);
return;
}

if (diffResponse.error) {
throw new CommandError(diffResponse.error.message);
}

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("(use `fauna schema diff` to display local changes)");
logger.stdout("(use `fauna schema push` to stage local changes)");
return;
}

logger.stdout(`Local changes:\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)");
}

function buildStatusCommand(yargs) {
Expand Down
82 changes: 68 additions & 14 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,51 @@ 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: "",
}),
);
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", { format: "summary", color: "ansi" }),
commonFetchParams,
);
expect(fetch).not.to.have.been.calledWith(
buildUrl("/schema/1/validate", {
format: "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 All @@ -88,17 +139,17 @@ describe("schema status", function () {
await run(`schema status --secret "secret"`, container);

expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/staged/status", { diff: "summary", color: "ansi" }),
buildUrl("/schema/1/staged/status", { format: "summary", color: "ansi" }),
commonFetchParams,
);
expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/diff", {
diff: "summary",
format: "summary",
staged: "true",
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 +160,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 All @@ -131,17 +183,17 @@ describe("schema status", function () {
await run(`schema status --secret "secret"`, container);

expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/staged/status", { diff: "summary", color: "ansi" }),
buildUrl("/schema/1/staged/status", { format: "summary", color: "ansi" }),
commonFetchParams,
);
expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/diff", {
diff: "summary",
format: "summary",
staged: "true",
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 +212,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 All @@ -179,18 +232,18 @@ describe("schema status", function () {
await run(`schema status --secret "secret"`, container);

expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/staged/status", { diff: "summary", color: "ansi" }),
buildUrl("/schema/1/staged/status", { format: "summary", color: "ansi" }),
commonFetchParams,
);

expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/diff", {
diff: "summary",
format: "summary",
staged: "true",
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 +258,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 All @@ -226,17 +280,17 @@ describe("schema status", function () {
await run(`schema status --secret "secret"`, container);

expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/staged/status", { diff: "summary", color: "ansi" }),
buildUrl("/schema/1/staged/status", { format: "summary", color: "ansi" }),
commonFetchParams,
);
expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/diff", {
diff: "summary",
format: "summary",
staged: "true",
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 Expand Up @@ -270,7 +324,7 @@ describe("schema status", function () {
await run(`schema status --no-color --secret "secret"`, container);

expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/staged/status", { diff: "summary" }),
buildUrl("/schema/1/staged/status", { format: "summary" }),
commonFetchParams,
);
expect(logger.stderr).not.to.have.been.called;
Expand Down

0 comments on commit 13293f8

Please sign in to comment.