Skip to content

Commit

Permalink
Junit test reports (#376)
Browse files Browse the repository at this point in the history
* extend makeFaunaRequest to include request body

* rewrite schema push to use makeFaunaRequest

* fix URL constructors and error handlers

* remove @cloudcmd/stub in favor of sinon

* add test watch mode and junit reporter

* Updated config.yml
  • Loading branch information
echo-bravo-yahoo authored Oct 7, 2024
1 parent eb185a1 commit 36bcb33
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 145 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ commands:
name: Run Tests
command: yarn test

- store_test_results:
path: test-results.xml

- store_artifacts:
path: coverage

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ experiments
*#
.log
coverage
test-results.xml
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
},
"devDependencies": {
"@babel/eslint-parser": "^7.17.0",
"@cloudcmd/stub": "^4.0.1",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.11.1",
"@inquirer/testing": "^2.1.7",
Expand All @@ -57,6 +56,7 @@
"husky": "^7.0.4",
"jest": "^29.7.0",
"mocha": "^10.7.3",
"mocha-junit-reporter": "^2.2.1",
"mock-require": "^3.0.3",
"nock": "^14.0.0-beta.8",
"oclif": "^3.9.2",
Expand Down Expand Up @@ -118,11 +118,12 @@
"prepack": "yarn build && oclif manifest",
"pretest": "yarn fixlint",
"local": "export $(cat .env | xargs); node bin/run",
"local-test": "export $(cat .env | xargs); mocha \"test/**/*.test.{js,ts}\"",
"local-test-old": "export $(cat .env | xargs); mocha \"test/**/*.test.{js,ts}\"",
"local-test": "mocha --watch --recursive ./yargs-test --require ./yargs-test/mocha-root-hooks.mjs --experimental-require-module",
"lint": "eslint .",
"fixlint": "eslint . --fix",
"test-old": "c8 -r html mocha --forbid-only \"test/**/*.test.{js,ts}\"",
"test": "mocha --recursive ./yargs-test --require ./yargs-test/mocha-root-hooks.mjs",
"test": "mocha --recursive ./yargs-test --require ./yargs-test/mocha-root-hooks.mjs --reporter spec --reporter mocha-junit-reporter",
"version": "oclif-dev readme && git add README.md",
"fmt": "prettier -w src"
},
Expand Down
9 changes: 7 additions & 2 deletions src/lib/db.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export async function makeFaunaRequest({
path,
params,
method,
body,
shouldThrow = true,
}) {
const fetch = container.resolve("fetch");
Expand All @@ -21,10 +22,14 @@ export async function makeFaunaRequest({
throw e;
}

const response = await fetch(fullUrl, {
const fetchArgs = {
method,
headers: { AUTHORIZATION: `Bearer ${secret}` },
});
};

if (body) fetchArgs.body = body;

const response = await fetch(fullUrl, fetchArgs);

const obj = await response.json();

Expand Down
13 changes: 5 additions & 8 deletions src/yargs-commands/schema/abandon.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { container } from "../../cli.mjs";
async function doAbandon(argv) {
const makeFaunaRequest = container.resolve("makeFaunaRequest");
const logger = container.resolve("logger");
const exit = container.resolve("exit");

if (argv.force) {
const params = new URLSearchParams({
Expand All @@ -15,7 +14,7 @@ async function doAbandon(argv) {

await makeFaunaRequest({
baseUrl: argv.url,
path: new URL(`/schema/1/staged/abandon?${params}`, argv.url).href,
path: `/schema/1/staged/abandon?${params}`,
secret: argv.secret,
method: "POST",
});
Expand All @@ -27,15 +26,13 @@ async function doAbandon(argv) {

const response = await makeFaunaRequest({
baseUrl: argv.url,
path: new URL(`/schema/1/staged/status?${params}`, argv.url).href,
path: `/schema/1/staged/status?${params}`,
secret: argv.secret,
method: "GET",
});

if (response.status === "none") {
logger.stderr("There is no staged schema to abandon");
exit(1);
}
if (response.status === "none")
throw new Error("There is no staged schema to abandon");

logger.stdout(response.diff);

Expand All @@ -49,7 +46,7 @@ async function doAbandon(argv) {

await makeFaunaRequest({
baseUrl: argv.url,
path: new URL(`/schema/1/staged/abandon?${params}`, argv.url).href,
path: `/schema/1/staged/abandon?${params}`,
secret: argv.secret,
method: "POST",
});
Expand Down
19 changes: 7 additions & 12 deletions src/yargs-commands/schema/commit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { container } from "../../cli.mjs";
async function doCommit(argv) {
const makeFaunaRequest = container.resolve("makeFaunaRequest");
const logger = container.resolve("logger");
const exit = container.resolve("exit");

if (argv.force) {
const params = new URLSearchParams({
Expand All @@ -15,7 +14,7 @@ async function doCommit(argv) {

await makeFaunaRequest({
baseUrl: argv.url,
path: new URL(`/schema/1/staged/commit?${params}`, argv.url).href,
path: `/schema/1/staged/commit?${params}`,
secret: argv.secret,
method: "POST",
});
Expand All @@ -28,22 +27,18 @@ async function doCommit(argv) {

const response = await makeFaunaRequest({
baseUrl: argv.url,
path: new URL(`/schema/1/staged/status?${params}`, argv.url).href,
path: `/schema/1/staged/status?${params}`,
secret: argv.secret,
method: "GET",
});

if (response.status === "none") {
logger.stderr("There is no staged schema to commit");
exit(1);
}
if (response.status === "none")
throw new Error("There is no staged schema to commit");

logger.stdout(response.diff);

if (response.status !== "ready") {
logger.stderr("Schema is not ready to be committed");
exit(1);
}
if (response.status !== "ready")
throw new Error("Schema is not ready to be committed");

const confirmed = await confirm({
message: "Accept and commit these changes?",
Expand All @@ -55,7 +50,7 @@ async function doCommit(argv) {

await makeFaunaRequest({
baseUrl: argv.url,
path: new URL(`/schema/1/staged/commit?${params}`, argv.url).href,
path: `/schema/1/staged/commit?${params}`,
secret: argv.secret,
method: "POST",
});
Expand Down
121 changes: 51 additions & 70 deletions src/yargs-commands/schema/push.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,66 @@ import { commonQueryOptions } from "../../lib/command-helpers.mjs";

async function doPush(argv) {
const logger = container.resolve("logger");
const fetch = container.resolve("fetch");
const makeFaunaRequest = container.resolve("makeFaunaRequest");

const gatherFSL = container.resolve("gatherFSL");
try {
const fsl = await gatherFSL(argv.dir);
if (argv.force) {
const params = new URLSearchParams();
if (argv.force) params.set("force", "true");
if (argv.staged) params.set("staged", "true");
const fsl = await gatherFSL(argv.dir);
if (argv.force) {
const params = new URLSearchParams({
force: argv.force,
staged: argv.staged,
});

const path = new URL(`/schema/1/update?${params}`, argv.url);
const res = await fetch(path.toString(), {
method: "POST",
headers: { AUTHORIZATION: `Bearer ${argv.secret}` },
body: fsl,
// https://github.com/nodejs/node/issues/46221
// https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1483
duplex: "half",
});
await makeFaunaRequest({
baseUrl: argv.url,
path: `/schema/1/update?${params}`,
body: fsl,
secret: argv.secret,
method: "POST",
});
} else {
// Confirm diff, then push it. `force` is set on `validate` so we don't
// need to pass the last known schema version through.
const params = new URLSearchParams({ force: true });
if (argv.color) params.set("color", "ansi");

const json = await res.json();
if (json.error) {
logger.stderr(json.error?.message ?? json.error);
}
} else {
// Confirm diff, then push it. `force` is set on `validate` so we don't
// need to pass the last known schema version through.
const params = new URLSearchParams({ force: true });
if (argv.color) params.set("color", "ansi");
const path = new URL(`/schema/1/validate?${params}`, argv.url);
const res = await fetch(path, {
method: "POST",
headers: { AUTHORIZATION: `Bearer ${argv.secret}` },
body: fsl,
duplex: "half",
});
const response = await makeFaunaRequest({
baseUrl: argv.url,
path: `/schema/1/validate?${params}`,
body: fsl,
secret: argv.secret,
method: "POST",
});

const json = await res.json();
if (json.error) {
logger.stderr(json.error?.message ?? json.error);
}
let message = "Accept and push changes?";
if (response.diff) {
logger.stdout(`Proposed diff:\n`);
logger.stdout(response.diff);
} else {
logger.stdout("No logical changes.");
message = "Push file contents anyway?";
}
const confirmed = await confirm({
message,
default: false,
});

let message = "Accept and push changes?";
if (json.diff) {
logger.stdout(`Proposed diff:\n`);
logger.stdout(json.diff);
} else {
logger.stdout("No logical changes.");
message = "Push file contents anyway?";
}
const confirmed = await confirm({
message,
default: false,
if (confirmed) {
const params = new URLSearchParams({
version: response.version,
staged: argv.staged ? "true" : "false",
});

if (confirmed) {
const params = new URLSearchParams({
version: json.version,
staged: argv.staged ? "true" : "false",
});

const path = new URL(`/schema/1/update?${params}`, argv.url);
const res = await fetch(path, {
method: "POST",
headers: { AUTHORIZATION: `Bearer ${argv.secret}` },
body: fsl,
duplex: "half",
});

const json0 = await res.json();
if (json0.error) {
logger.stderr(json0.error.message);
}
} else {
logger.stdout("Push cancelled");
}
await makeFaunaRequest({
baseUrl: argv.url,
path: `/schema/1/update?${params}`,
body: fsl,
secret: argv.secret,
method: "POST",
});
} else {
logger.stdout("Push cancelled");
}
} catch (err) {
logger.stderr(err);
}
}

Expand Down
7 changes: 3 additions & 4 deletions yargs-test/login.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ import { expect } from "chai";
import { run } from "../src/cli.mjs";
import { setupTestContainer as setupContainer } from "../src/config/setup-test-container.mjs";
import * as awilix from "awilix/lib/awilix.module.mjs";
// TODO: this breaks if we swap the stub implementation to sinon. ugh
import stub from "@cloudcmd/stub";
import { stub, spy } from "sinon";

describe("login", function () {
let container;
const mockOAuth = () => {
let handlers = {};

return {
_receiveAuthCode: stub(async () => {
_receiveAuthCode: spy(async () => {
await handlers.auth_code_received();
}),
start: stub(async () => {
start: spy(async () => {
await handlers.ready();
}),
getOAuthParams: () => {
Expand Down
2 changes: 1 addition & 1 deletion yargs-test/schema/pull.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe("schema pull", function () {
);

const [error] = await tryToCatch(() =>
run(`schema pull --secret "secret" --verbosity 5`, container)
run(`schema pull --secret "secret"`, container)
);
expect(error).to.have.property("code", 1);
expect(container.resolve("gatherFSL")).to.not.have.been.called;
Expand Down
8 changes: 6 additions & 2 deletions yargs-test/schema/push.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import * as awilix from "awilix/lib/awilix.module.mjs";
import { expect } from "chai";
import { run } from "../../src/cli.mjs";
import { setupTestContainer as setupContainer } from "../../src/config/setup-test-container.mjs";
import { makeFaunaRequest } from "../../src/lib/db.mjs";

describe("schema push", function () {
let container;

beforeEach(() => {
container = setupContainer();
container.register({
makeFaunaRequest: awilix.asValue(makeFaunaRequest),
});
});

it("can force push schema", async function () {
Expand All @@ -25,12 +30,11 @@ describe("schema push", function () {
expect(gatherFSL).to.have.been.calledWith(".");

expect(fetch).to.have.been.calledWith(
"https://db.fauna.com/schema/1/update?force=true",
"https://db.fauna.com/schema/1/update?force=true&staged=false",
{
method: "POST",
headers: { AUTHORIZATION: "Bearer secret" },
body: '[{"name":"coll.fsl","content":"collection MyColl {\\n name: String\\n index byName {\\n terms [.name]\\n }\\n}\\n"}]',
duplex: "half",
}
);

Expand Down
Loading

0 comments on commit 36bcb33

Please sign in to comment.