Skip to content

Commit

Permalink
chore(tests): add jest and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cball committed Apr 18, 2020
1 parent 9c9a8c7 commit fc8b2b1
Show file tree
Hide file tree
Showing 4 changed files with 2,993 additions and 30 deletions.
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -17,10 +17,12 @@
],
"scripts": {
"version": "auto-changelog -p --template keepachangelog && git add CHANGELOG.md",
"prepublishOnly": "git push && git push --tags && gh-release"
"prepublishOnly": "git push && git push --tags && gh-release",
"test": "jest"
},
"devDependencies": {
"auto-changelog": "^2.0.0",
"gh-release": "^3.5.0"
"gh-release": "^3.5.0",
"jest": "^25.3.0"
}
}
30 changes: 18 additions & 12 deletions src/index.js
@@ -1,7 +1,6 @@
const fs = require("fs");
const util = require("util")

const pWriteFile = util.promisify(fs.writeFile)
const fs = require('fs');
const util = require('util');
const pWriteFile = util.promisify(fs.writeFile);

/**
* Overrides an ENV var with a value if it exists
Expand All @@ -16,23 +15,30 @@ function setEnvWithValue(key, contextOrBranch, mode) {
return;
}

console.log(`Setting ${key} to the value from ${envVar}.`);
process.env[key] = process.env[envVar];
console.log(`Exporting ${key}=${process.env[envVar]}.`);

// Renable this once setting process.env is supported in Netlify builds
// See: https://github.com/netlify/build/issues/1129
// process.env[key] = process.env[envVar];
return `${key}=${process.env[envVar]}\n`;
}

module.exports = {
onPreBuild: async ({ inputs }) => {
const context = `${process.env.CONTEXT}`.toUpperCase().replace(/-/g, "_");
const branch = `${process.env.BRANCH}`.toUpperCase().replace(/-/g, "_");
const context = `${process.env.CONTEXT}`.toUpperCase().replace(/-/g, '_');
const branch = `${process.env.BRANCH}`.toUpperCase().replace(/-/g, '_');

const replaced = [].concat(...Object.keys(process.env)
.map((key) => [setEnvWithValue(key, context, inputs.mode), setEnvWithValue(key, branch, inputs.mode)])
).filter(Boolean)
const envOverrides = Object.keys(process.env).map((key) => [
setEnvWithValue(key, context, inputs.mode),
setEnvWithValue(key, branch, inputs.mode),
]);

const replaced = [].concat(...envOverrides).filter(Boolean);

if (replaced.length) {
// Write an env file so we can source it during build
await pWriteFile(".env", replaced.join(""))
await pWriteFile('.env', replaced.join(''));

console.log(`Replaced ${replaced.length} ENVs and wrote .env file`);
} else {
console.log(`Nothing found... keeping default ENVs`);
Expand Down
76 changes: 76 additions & 0 deletions src/index.test.js
@@ -0,0 +1,76 @@
jest.mock('fs');
const mockWriteFile = jest.fn(() => Promise.resolve());

jest.mock('util', () => ({
promisify: () => mockWriteFile,
}));

const onPreBuild = require('./index').onPreBuild;

describe('onPreBuild', () => {
const OLD_ENV = process.env;

beforeEach(() => {
mockWriteFile.mockReset();
jest.resetModules();
process.env = { ...OLD_ENV };
});

afterEach(() => {
process.env = OLD_ENV;
});

describe('with context prefix ENV overrides', () => {
it('writes an env file with updated values', async () => {
process.env.DATABASE_URL = 'https://dev.com';
process.env.STAGING_DATABASE_URL = 'https://stage.com';
process.env.CONTEXT = 'staging';
await onPreBuild({ inputs: { mode: 'prefix' } });

expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n');
});
});

describe('with suffix context prefix ENV overrides', () => {
it('writes an env file with updated values', async () => {
process.env.DATABASE_URL = 'https://dev.com';
process.env.DATABASE_URL_STAGING = 'https://stage.com';
process.env.CONTEXT = 'staging';
await onPreBuild({ inputs: { mode: 'suffix' } });

expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n');
});
});

describe('with branch ENV overrides', () => {
it('writes an env file with updated values', async () => {
process.env.DATABASE_URL = 'https://dev.com';
process.env.HELLO_DATABASE_URL = 'https://stage.com';
process.env.BRANCH = 'hello';
await onPreBuild({ inputs: { mode: 'prefix' } });

expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n');
});
});

describe('with suffix branch ENV overrides', () => {
it('writes an env file with updated values', async () => {
process.env.DATABASE_URL = 'https://dev.com';
process.env.DATABASE_URL_HELLO = 'https://stage.com';
process.env.BRANCH = 'hello';
await onPreBuild({ inputs: { mode: 'suffix' } });

expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n');
});
});

describe('without ENV overrides', () => {
it('writes an env file with updated values', async () => {
process.env.DATABASE_URL = 'https://dev.com';
process.env.BRANCH = 'hello';
await onPreBuild({ inputs: { mode: 'prefix' } });

expect(mockWriteFile).not.toHaveBeenCalled();
});
});
});

0 comments on commit fc8b2b1

Please sign in to comment.