-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
222 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |