Skip to content

Commit

Permalink
Add stack tests
Browse files Browse the repository at this point in the history
  • Loading branch information
macmv committed Sep 26, 2023
1 parent 40ab92f commit 5cdfc1a
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ const readFile = (fileName: string) => {
return fs.readFileSync(fileName, "utf8");
};

const getRootConfigPath = () => {
export const getRootConfigPath = () => {
return path.join(os.homedir(), ".fauna-shell");
};

Expand Down
221 changes: 221 additions & 0 deletions test/commands/stack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
import fs from "fs";
import { expect, test } from "@oclif/test";
import { getRootConfigPath } from "../../src/lib/config";
import sinon from "sinon";
import * as path from "path";
const ini = require("ini");

// This is the "current" project config. The mocks below will return this
// config from `fs.readFileSync`. do() should be used to update this before a
// command is invoked in each test. If the command writes to the project
// config, this will be updated as well, so this can be used to test the
// result of commands that modify the project config.
let projectConfig: any = {};

before(() => {
const rootConfig = {
"my-endpoint": {
url: "http://localhost:8443",
secret: "secret",
},
};

const readOriginal = fs.readFileSync;
const fsReadStub = sinon.stub(fs, "readFileSync");
fsReadStub.callsFake(readOriginal);

const writeOriginal = fs.writeFileSync;
const fsWriteStub = sinon.stub(fs, "writeFileSync");
fsWriteStub.callsFake(writeOriginal);

const statOriginal = fs.statSync;
const fsStatStub = sinon.stub(fs, "statSync");
fsStatStub.callsFake(statOriginal);

const cwd = process.cwd();

fsReadStub
.withArgs(getRootConfigPath(), "utf8")
.returns(ini.encode(rootConfig));

fsStatStub
.withArgs(path.join(cwd, ".fauna-project"), { throwIfNoEntry: false })
.returns({
isFile: () => true,
} as any);

fsReadStub
.withArgs(path.join(cwd, ".fauna-project"), "utf8")
.callsFake(() => ini.encode(projectConfig));

fsWriteStub
.withArgs(path.join(cwd, ".fauna-project"), sinon.match.any)
.callsFake((_, config) => (projectConfig = ini.decode(config)));
});

describe("stack:add", () => {
test
.stdout()
.do(() => {
projectConfig = {
default: "my-app",
stack: {
"my-app": {
endpoint: "my-endpoint",
database: "my-db",
},
},
};
})
.command([
"stack:add",
"--non-interactive",
"--name",
"foobar",
"--endpoint",
"my-endpoint",
"--database",
"my-db",
])
.it("adds a stack", (ctx) => {
expect(ctx.stdout).to.equal(
"Saved stack foobar to /home/macmv/Desktop/programming/fauna/fauna-shell/.fauna-project\n"
);
expect(projectConfig).to.deep.equal({
default: "my-app",
stack: {
"my-app": {
endpoint: "my-endpoint",
database: "my-db",
},
foobar: {
endpoint: "my-endpoint",
database: "my-db",
},
},
});
});

test
.stdout()
.do(() => {
projectConfig = {
default: "my-app",
stack: {
"my-app": {
endpoint: "my-endpoint",
database: "my-db",
},
},
};
})
.command([
"stack:add",
"--non-interactive",
"--name",
"foobar",
"--endpoint",
"my-endpoint",
"--database",
"my-db",
"--set-default",
])
.it("adds a stack", (ctx) => {
expect(ctx.stdout).to.equal(
"Saved stack foobar to /home/macmv/Desktop/programming/fauna/fauna-shell/.fauna-project\n"
);
expect(projectConfig).to.deep.equal({
default: "foobar",
stack: {
"my-app": {
endpoint: "my-endpoint",
database: "my-db",
},
foobar: {
endpoint: "my-endpoint",
database: "my-db",
},
},
});
});

test
.stdout()
.do(() => {
projectConfig = {
default: "my-app",
stack: {
"my-app": {
endpoint: "my-endpoint",
database: "my-db",
},
},
};
})
.command([
"stack:add",
"--non-interactive",
"--name",
"my-app",
"--endpoint",
"my-endpoint",
"--database",
"my-db",
])
.catch((e) => {
expect(e.message).to.equal("Stack my-app already exists");
})
.it("disallows stacks with the same name", () => {
expect(projectConfig).to.deep.equal({
default: "my-app",
stack: {
"my-app": {
endpoint: "my-endpoint",
database: "my-db",
},
},
});
});

test
.stdout()
.do(() => {
projectConfig = {
default: "my-app",
stack: {
"my-app": {
endpoint: "my-endpoint",
database: "my-db",
},
},
};
})
.command([
"stack:add",
"--non-interactive",
"--name",
"foobar",
"--endpoint",
"doesnt-exist-endpoint",
"--database",
"my-db",
])
.catch((e) => {
expect(e.message).to.equal("No such endpoint 'doesnt-exist-endpoint'");
})
.it("disallows endpoints that don't exist", () => {
expect(projectConfig).to.deep.equal({
default: "my-app",
stack: {
"my-app": {
endpoint: "my-endpoint",
database: "my-db",
},
},
});
});
});

after(() => {
sinon.restore();
});

0 comments on commit 5cdfc1a

Please sign in to comment.