Skip to content

Instantly share code, notes, and snippets.

@bmatusiak
Forked from draeder/1243.js
Last active May 31, 2022 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmatusiak/b892f3a5497990074bab055a895ea52a to your computer and use it in GitHub Desktop.
Save bmatusiak/b892f3a5497990074bab055a895ea52a to your computer and use it in GitHub Desktop.
Gun Heroku one-click deploy testing
/**
* NOTE: Does not work with npm installed heroku-cli
*
* Heroku CLI REQUIRED! Uninstall with: npm uninstall heroku -g
* Install Heroku with: curl https://cli-assets.heroku.com/install.sh | sh
* Login Heroku with: heroku login
*
* after login you can run test
* like: $ mocha test/bug/1243.js
*
*/
const child_process = require('child_process');
const expect = require('../expect');
const http = require("https");
const consoleLog = false;
const NPM_CONFIG_PRODUCTION = true;
function request(hostname, done) {
http.get('https://' + hostname + '/', {
timeout: 1000 * 60 * 5
}, (res) => {
done(res.statusCode);
});
}
function spawn(cmd, done) {
const spawn = child_process.spawn;
const args = cmd.split(" ");
cmd = args.shift();
const s = spawn(cmd, args);
let stderrOUT = "";
s.stdout.on('data', (data) => {
saveData(data.toString());
});
s.stderr.on('data', (data) => {
saveData(data.toString());
});
function saveData(out) {
stderrOUT += out.toString();
if (consoleLog) console.log(out.toString());
}
s.on('exit', (code) => {
done(stderrOUT);
});
}
function makeid(length) {
let result = '';
const characters = 'abcdefghijklmnopqrstuvwxyz';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
describe('Heroku deploy', function() {
const heroku_name = 'test-gun-' + makeid(5);
it('create herokuapp ' + heroku_name, function(done) {
this.timeout(1000 * 15); //15 sec
const c = 'heroku create ' + heroku_name;
spawn(c, (stdout) => {
if (stdout.indexOf("done") > -1) { done(); }
});
});
it('add git remote', function(done) {
this.timeout(1000 * 15); //15 sec
const c = 'heroku git:remote -a ' + heroku_name;
spawn(c, (stdout) => {
if (stdout.indexOf("set git") > -1) { done(); }
});
});
if (NPM_CONFIG_PRODUCTION)
it('set heroku config to NPM_CONFIG_PRODUCTION=' + NPM_CONFIG_PRODUCTION, function(done) {
this.timeout(1000 * 15); //15 sec
const c = 'heroku config:set NPM_CONFIG_PRODUCTION=' + NPM_CONFIG_PRODUCTION + ' -a ' + heroku_name;
spawn(c, (stdout) => {
if (stdout.indexOf("NPM_CONFIG_PRODUCTION:") > -1) { done(); }
});
});
it('git push heroku', function(done) {
this.timeout(1000 * 60 * 2); // 2 min
const c = 'git push heroku master --force';
spawn(c, (stdout) => {
if (stdout.indexOf("deployed to Heroku") > -1) { done(); }
});
});
it('fetch heroku app https', function(done) {
this.timeout(1000 * 60 * 5); // 5 min
const c = heroku_name + ".herokuapp.com";
setTimeout(() => {
request(c, (statusCode) => {
expect(statusCode).to.be(200);
done();
});
}, 1000 * 5);
});
it('destroy herokuapp ' + heroku_name, function(done) {
this.timeout(1000 * 15); //15 sec
const c = 'heroku apps:destroy ' + heroku_name + ' --confirm=' + heroku_name;
spawn(c, (stdout) => {
if (stdout.indexOf("done") > -1) { done(); }
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment