Skip to content

Commit

Permalink
v0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenvachon committed Jul 24, 2017
1 parent b684329 commit 29012fb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ A dual file convention is used, consisting of `.env.sample` and `.env`. Both fil

```
POSTGRES_HOST=localhost
POSTGRES_PORT=
POSTGRES_NAME=myapp
POSTGRES_PASSWORD=myapp
POSTGRES_USER=myapp
Expand Down
4 changes: 3 additions & 1 deletion es2017/createdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ const db = async env =>
const host = env.POSTGRES_HOST;
const name = env.POSTGRES_NAME;
const password = env.POSTGRES_PASSWORD;
const port = env.POSTGRES_PORT;
const user = env.POSTGRES_USER;

if (!isset(host) || !isset(name) || !isset(password) || !isset(user))
{
throw new Error("Environmental variable(s) not set");
}

const psql = knex({ client:"pg", connection:{ host } });
const psql = knex({ client:"pg", connection:{ host, port } });

try
{
Expand Down Expand Up @@ -85,6 +86,7 @@ const prompts = async env =>
const promptVars =
[
"POSTGRES_HOST",
"POSTGRES_PORT",
"POSTGRES_NAME",
"POSTGRES_USER",
"POSTGRES_PASSWORD"
Expand Down
3 changes: 2 additions & 1 deletion es2017/dropdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const dropdb = async (envPath=".env") =>
const host = process.env.POSTGRES_HOST;
const name = process.env.POSTGRES_NAME;
const password = process.env.POSTGRES_PASSWORD;
const port = process.env.POSTGRES_PORT;
const user = process.env.POSTGRES_USER;

if (!isset(host) || !isset(name) || !isset(password) || !isset(user))
Expand Down Expand Up @@ -43,7 +44,7 @@ const dropdb = async (envPath=".env") =>
{
// TODO :: prompt for SUPERUSER_NAME and SUPERUSER_PASSWORD (https://github.com/brianc/node-postgres/wiki/Client#new-clientobject-config--client)

const psql = knex({ client:"pg", connection:{ host } });
const psql = knex({ client:"pg", connection:{ host, port } });

try
{
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gres",
"description": "CLI scripts for bootstrapping a PostgreSQL database.",
"version": "0.0.3",
"version": "0.0.4",
"license": "MIT",
"author": "Steven Vachon <contact@svachon.com> (https://www.svachon.com/)",
"repository": "stevenvachon/gres",
Expand All @@ -11,12 +11,12 @@
"dotenv": "^4.0.0",
"edit-dotenv": "^1.0.4",
"enquirer": "^1.0.2",
"fs-extra": "^3.0.1",
"isset": "^1.0.3",
"fs-extra": "^4.0.0",
"isset": "^1.0.4",
"knex": "~0.13.0",
"pg": "^6.4.0",
"pg": "^7.0.2",
"prompt-confirm": "^1.2.0",
"semver": "^5.3.0"
"semver": "^5.4.1"
},
"devDependencies": {
"babel-cli": "^6.24.1",
Expand Down
58 changes: 48 additions & 10 deletions test/createdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const {outputFile, readFile, remove} = require("fs-extra");
const {PassThrough} = require("stream");
const suppose = require("suppose");

const envVarsError = /^Error: Environmental variable\(s\) not set/;



// TODO :: https://github.com/jprichardson/node-suppose/pull/31
Expand Down Expand Up @@ -37,67 +39,89 @@ const createdb = expects =>

const emptyPrompts = () =>
{
it("throws if a prompt is left empty (#1)", function()
it("throws if the prompt for HOST is left empty (#1)", function()
{
return createdb(
[
{ condition:"? Value for POSTGRES_HOST ()", response:"\n" },
{ condition:"? Value for POSTGRES_PORT ()", response:"fake-port\n" },
{ condition:"? Value for POSTGRES_NAME ()", response:"fake-database\n" },
{ condition:"? Value for POSTGRES_USER ()", response:"fake-user\n" },
{ condition:"? Value for POSTGRES_PASSWORD ()", response:"fake-password\n" }
])
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(envVarsError);
});
});

it("does not throw if the prompt for PORT is left empty", function()
{
return createdb(
[
{ condition:"? Value for POSTGRES_HOST ()", response:"fake-host\n" },
{ condition:"? Value for POSTGRES_PORT ()", response:"\n" },
{ condition:"? Value for POSTGRES_NAME ()", response:"fake-database\n" },
{ condition:"? Value for POSTGRES_USER ()", response:"fake-user\n" },
{ condition:"? Value for POSTGRES_PASSWORD ()", response:"fake-password\n" }
])
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(/^Error: Environmental variable\(s\) not set/);
// Vague npmjs.com/pg error stemming from fake credentials
expect(error).to.be.an("error").with.property("message").that.does.not.match(envVarsError);
});
});

it("throws if a prompt is left empty (#2)", function()
it("throws if the prompt for NAME is left empty", function()
{
return createdb(
[
{ condition:"? Value for POSTGRES_HOST ()", response:"fake-host\n" },
{ condition:"? Value for POSTGRES_PORT ()", response:"fake-port\n" },
{ condition:"? Value for POSTGRES_NAME ()", response:"\n" },
{ condition:"? Value for POSTGRES_USER ()", response:"fake-user\n" },
{ condition:"? Value for POSTGRES_PASSWORD ()", response:"fake-password\n" }
])
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.matches(envVarsError);
});
});

it("throws if a prompt is left empty (#3)", function()
it("throws if the prompt for USER is left empty", function()
{
return createdb(
[
{ condition:"? Value for POSTGRES_HOST ()", response:"fake-host\n" },
{ condition:"? Value for POSTGRES_PORT ()", response:"fake-port\n" },
{ condition:"? Value for POSTGRES_NAME ()", response:"fake-database\n" },
{ condition:"? Value for POSTGRES_USER ()", response:"\n" },
{ condition:"? Value for POSTGRES_PASSWORD ()", response:"fake-password\n" }
])
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.matches(envVarsError);
});
});

it("throws if a prompt is left empty (#4)", function()
it("throws if the prompt for PASSWORD is left empty", function()
{
return createdb(
[
{ condition:"? Value for POSTGRES_HOST ()", response:"fake-host\n" },
{ condition:"? Value for POSTGRES_PORT ()", response:"fake-port\n" },
{ condition:"? Value for POSTGRES_NAME ()", response:"fake-database\n" },
{ condition:"? Value for POSTGRES_USER ()", response:"fake-user\n" },
{ condition:"? Value for POSTGRES_PASSWORD ()", response:"\n" }
])
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.matches(envVarsError);
});
});

Expand All @@ -106,14 +130,15 @@ const emptyPrompts = () =>
return createdb(
[
{ condition:"? Value for POSTGRES_HOST ()", response:"\n" },
{ condition:"? Value for POSTGRES_PORT ()", response:"\n" },
{ condition:"? Value for POSTGRES_NAME ()", response:"\n" },
{ condition:"? Value for POSTGRES_USER ()", response:"\n" },
{ condition:"? Value for POSTGRES_PASSWORD ()", response:"\n" }
])
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.matches(envVarsError);
});
});
};
Expand All @@ -127,6 +152,7 @@ const promptsAndWrites = () =>
return createdb(
[
{ condition:"? Value for POSTGRES_HOST ()", response:"fake-host\n" },
{ condition:"? Value for POSTGRES_PORT ()", response:"fake-port\n" },
{ condition:"? Value for POSTGRES_NAME ()", response:"fake-database\n" },
{ condition:"? Value for POSTGRES_USER ()", response:"fake-user\n" },
{ condition:"? Value for POSTGRES_PASSWORD ()", response:"fake-password\n" }
Expand All @@ -135,7 +161,7 @@ const promptsAndWrites = () =>
.then(error =>
{
// Vague npmjs.com/pg error stemming from fake credentials
expect(error).to.be.an("error").with.property("message").that.does.not.match(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.does.not.match(envVarsError);

return readFile(".env", "utf8");
})
Expand All @@ -144,6 +170,7 @@ const promptsAndWrites = () =>
let expected = "";

expected += `POSTGRES_HOST=fake-host${EOL}`;
expected += `POSTGRES_PORT=fake-port${EOL}`;
expected += `POSTGRES_NAME=fake-database${EOL}`;
expected += `POSTGRES_USER=fake-user${EOL}`;
expected += `POSTGRES_PASSWORD=fake-password${EOL}`;
Expand Down Expand Up @@ -175,6 +202,7 @@ describe("createdb", function()
{
let contents = "";
contents += `POSTGRES_HOST=${EOL}`;
contents += `POSTGRES_PORT=${EOL}`;
contents += `POSTGRES_NAME=${EOL}`;
contents += `POSTGRES_USER=${EOL}`;
contents += `POSTGRES_PASSWORD=${EOL}`;
Expand All @@ -187,4 +215,14 @@ describe("createdb", function()
promptsAndWrites();
emptyPrompts();
});



describe("with non-empty .env.sample file", function()
{
it.skip("works", function()
{

});
});
});

0 comments on commit 29012fb

Please sign in to comment.