Skip to content

Commit

Permalink
100% test coverage, new silent option
Browse files Browse the repository at this point in the history
  • Loading branch information
bchr02 committed Jun 2, 2016
1 parent 73e8be3 commit f545dd6
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 32 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# node-pre-gyp-github
##### A node-pre-gyp module which provides the ability to publish to GitHub releases.

[![Coverage Status](https://coveralls.io/repos/github/bchr02/node-pre-gyp-github/badge.svg?branch=master)](https://coveralls.io/github/bchr02/node-pre-gyp-github?branch=master)
[![Join the chat at https://gitter.im/bchr02/node-pre-gyp-github](https://badges.gitter.im/bchr02/node-pre-gyp-github.svg)](https://gitter.im/bchr02/node-pre-gyp-github?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

## Usage
Instead of ```node-pre-gyp publish``` use **```node-pre-gyp-github publish```**

## Options
## Options for publish command
* --silent : Turns verbose messages off.
* --release : Publish the GitHub Release immediately instead of creating a Draft.

ex. ```node-pre-gyp-github publish --release```
For Ex. ```node-pre-gyp-github publish --release```

## Install
```javascript
Expand Down
2 changes: 2 additions & 0 deletions bin/node-pre-gyp-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ program
.command('publish [options]')
.description('publishes the contents of .\\build\\stage\\{version} to the current version\'s GitHub release')
.option("-r, --release", "publish immediately, do not create draft")
.option("-s, --silent", "turns verbose messages off")
.action(function(cmd, options){
var opts = {},
x = new module();
opts.draft = options.release ? false : true;
opts.verbose = options.silent ? false : true;
x.publish(opts);
});

Expand Down
29 changes: 18 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"use strict";

var path = require("path");
var fs = require('fs');
var GitHubApi = require("github");
var cwd = process.cwd();
var path = require("path");
var fs = require('fs');
var GitHubApi = require("github");
var cwd = process.cwd();

var verbose;
var consoleLog = function(x){
return (verbose) ? consoleLog(x) : false;
};

function NodePreGypGithub() {}

Expand Down Expand Up @@ -77,11 +82,11 @@ NodePreGypGithub.prototype.createRelease = function(args, callback) {
};

Object.keys(args).forEach(function(key) {
if(args.hasOwnProperty(key)) {
if(args.hasOwnProperty(key) && options.hasOwnProperty(key)) {
options[key] = args[key];
}
});

this.github.authenticate(this.authenticate_settings());
this.github.releases.createRelease(options, callback);
};
Expand All @@ -96,13 +101,13 @@ NodePreGypGithub.prototype.uploadAsset = function(cfg){
filePath: cfg.filePath
}, function(err){
if(err) throw err;
console.log('Staged file ' + cfg.fileName + ' saved to ' + this.owner + '/' + this.repo + ' release ' + this.release.tag_name + ' successfully.');
consoleLog('Staged file ' + cfg.fileName + ' saved to ' + this.owner + '/' + this.repo + ' release ' + this.release.tag_name + ' successfully.');
}.bind(this));
};

NodePreGypGithub.prototype.uploadAssets = function(){
var asset;
console.log("Stage directory path: " + path.join(this.stage_dir));
consoleLog("Stage directory path: " + path.join(this.stage_dir));
fs.readdir(path.join(this.stage_dir), function(err, files){
if(err) throw err;

Expand All @@ -116,7 +121,7 @@ NodePreGypGithub.prototype.uploadAssets = function(){
throw new Error("Staged file " + file + " found but it already exists in release " + this.release.tag_name + ". If you would like to replace it, you must first manually delete it within GitHub.");
}
else {
console.log("Staged file " + file + " found. Proceeding to upload it.");
consoleLog("Staged file " + file + " found. Proceeding to upload it.");
this.uploadAsset({
fileName: file,
filePath: path.join(this.stage_dir, file)
Expand All @@ -127,6 +132,8 @@ NodePreGypGithub.prototype.uploadAssets = function(){
};

NodePreGypGithub.prototype.publish = function(options) {
options = (typeof options === 'undefined') ? {} : options;
verbose = (typeof options.verbose === 'undefined' || options.verbose) ? true : false;
this.init();
this.github.authenticate(this.authenticate_settings());
this.github.releases.listReleases({
Expand Down Expand Up @@ -164,10 +171,10 @@ NodePreGypGithub.prototype.publish = function(options) {

this.release = release;
if (release.draft) {
console.log('Release ' + release.tag_name + " not found, so a draft release was created. YOU MUST MANUALLY PUBLISH THIS DRAFT WITHIN GITHUB FOR IT TO BE ACCESSIBLE.");
consoleLog('Release ' + release.tag_name + " not found, so a draft release was created. YOU MUST MANUALLY PUBLISH THIS DRAFT WITHIN GITHUB FOR IT TO BE ACCESSIBLE.");
}
else {
console.log('Release ' + release.tag_name + " not found, so a new release was created and published.");
consoleLog('Release ' + release.tag_name + " not found, so a new release was created and published.");
}
this.uploadAssets();
}.bind(this));
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "node-pre-gyp-github",
"version": "1.2.2",
"version": "1.3.0",
"description": "A node-pre-gyp module which provides the ability to publish to GitHub releases.",
"bin": "./bin/node-pre-gyp-github.js",
"main": "index.js",
"scripts": {
"test": "mocha test",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec"
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha --report text-lcov -- -R spec | node ./node_modules/coveralls/bin/coveralls.js"
},
"repository": {
"type": "git",
Expand All @@ -30,6 +30,7 @@
},
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"istanbul": "^0.4.3",
"mocha": "^2.5.3"
},
Expand Down
77 changes: 60 additions & 17 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var reset_index = function(index_string_ref) {
return require(index_string_ref);
};
var reset_mocks = function() {
process.env['NODE_PRE_GYP_GITHUB_TOKEN'] = "secret";
process.env.NODE_PRE_GYP_GITHUB_TOKEN = "secret";
fs = reset_index('fs');
fs.readFileSync = function(){return '{"name":"test","version":"0.0.1","repository": {"url":"git+https://github.com/test/test.git"},"binary":{"host":"https://github.com/test/test/releases/download/","remote_path":"{version}"}}';};
index.stage_dir = stage_dir;
Expand All @@ -19,54 +19,89 @@ var reset_mocks = function() {
cb(null, [{"tag_name":"0.0.0","assets":[{"name":"filename"}]}]);
};
index.github.releases.createRelease = function(options, cb){
cb(null,{"tag_name":"0.0.1","assets":[{}]});
cb(null,{"tag_name":"0.0.1","draft":true,"assets":[{}]});
};
index.github.releases.uploadAsset = function(cfg,cb){cb();};
};

if(!process.env.COVERALLS_SERVICE_NAME) console.log('To post to coveralls.io, be sure to set COVERALLS_SERVICE_NAME environment variable');
if(!process.env.COVERALLS_REPO_TOKEN) console.log('To post to coveralls.io, be sure to set COVERALLS_REPO_TOKEN environment variable');

describe("Publishes packages to GitHub Releases", function() {
describe("Throws an Error when node-pre-gyp-github is not configured properly", function() {

describe("Publishes without an error under all options", function() {

it("should publish a non-draft release without an error", function() {
var options = {'draft': false, 'verbose': false};
reset_mocks();
fs.readdir = function(filename, cb) {
cb(null,["filename"]);
};
index.github.releases.createRelease = function(options, cb){
cb(null,{"tag_name":"0.0.1","draft":false,"assets":[{}]});
};
expect(function(){ index.publish(options); }).to.not.throw();
});

it("should publish a draft release without an error", function() {
var options = {'draft': true, 'verbose': false};
reset_mocks();
fs.readdir = function(filename, cb) {
cb(null,["filename"]);
};
expect(function(){ index.publish(options); }).to.not.throw();
});

});

describe("Throws an error when node-pre-gyp-github is not configured properly", function() {

it("should throw an error when missing repository.url in package.json", function() {
var options = {'draft': true, 'verbose': false};
reset_mocks();
fs.readFileSync = function(){return '{}';};
expect(function(){ index.publish(); }).to.throw("Missing repository.url in package.json");
expect(function(){ index.publish(options); }).to.throw("Missing repository.url in package.json");
});

it("should throw an error when a correctly formatted GitHub repository.url is not found in package.json", function() {
var options = {'draft': true, 'verbose': false};
reset_mocks();
fs.readFileSync = function(){return '{"repository": {"url":"bad_format_url"}}';};
expect(function(){ index.publish(); }).to.throw("A correctly formatted GitHub repository.url was not found within package.json");
expect(function(){ index.publish(options); }).to.throw("A correctly formatted GitHub repository.url was not found within package.json");
});

it("should throw an error when missing binary.host in package.json", function() {
var options = {'draft': true, 'verbose': false};
reset_mocks();
fs.readFileSync = function(){return '{"repository": {"url":"git+https://github.com/test/test.git"}}';};
expect(function(){ index.publish(); }).to.throw("Missing binary.host in package.json");
expect(function(){ index.publish(options); }).to.throw("Missing binary.host in package.json");
});

it("should throw an error when binary.host does not begin with the correct url", function() {
var options = {'draft': true, 'verbose': false};
reset_mocks();
fs.readFileSync = function(){return '{"repository": {"url":"git+https://github.com/test/test.git"},"binary":{"host":"bad_format_binary"}}';};
expect(function(){ index.publish(); }).to.throw(/^binary.host in package.json should begin with:/i);
expect(function(){ index.publish(options); }).to.throw(/^binary.host in package.json should begin with:/i);
});

it("should throw an error when the NODE_PRE_GYP_GITHUB_TOKEN environment variable is not found", function() {
var options = {'draft': true, 'verbose': false};
reset_mocks();
process.env['NODE_PRE_GYP_GITHUB_TOKEN'] = "";
expect(function(){ index.publish(); }).to.throw("NODE_PRE_GYP_GITHUB_TOKEN environment variable not found");
process.env.NODE_PRE_GYP_GITHUB_TOKEN = "";
expect(function(){ index.publish(options); }).to.throw("NODE_PRE_GYP_GITHUB_TOKEN environment variable not found");
});

it("should throw an error when github.releases.listReleases returns an error", function() {
var options = {'draft': true, 'verbose': false};
reset_mocks();
index.github.releases.listReleases = function(options, cb){
cb(new Error('listReleases error'));
};
expect(function(){ index.publish(); }).to.throw('listReleases error');
expect(function(){ index.publish(options); }).to.throw('listReleases error');
});

it("should throw an error when github.releases.createRelease returns an error", function() {
var options = {'draft': true};
var options = {'draft': true, 'verbose': false};
reset_mocks();
index.github.releases.listReleases = function(options, cb){
cb(null,null);
Expand All @@ -78,7 +113,7 @@ describe("Publishes packages to GitHub Releases", function() {
});

it("should throw an error when the stage directory structure is missing", function() {
var options = {'draft': true};
var options = {'draft': true, 'verbose': false};
reset_mocks();
fs.readdir = function(filename, cb) {
cb(new Error('readdir Error'));
Expand All @@ -87,7 +122,7 @@ describe("Publishes packages to GitHub Releases", function() {
});

it("should throw an error when there are no files found within the stage directory", function() {
var options = {'draft': true};
var options = {'draft': true, 'verbose': false};
reset_mocks();
fs.readdir = function(filename, cb) {
cb(null,[]);
Expand All @@ -96,7 +131,7 @@ describe("Publishes packages to GitHub Releases", function() {
});

it("should throw an error when a staged file already exists in the current release", function() {
var options = {'draft': true};
var options = {'draft': true, 'verbose': false};
reset_mocks();
fs.readdir = function(filename, cb) {
cb(null,["filename"]);
Expand All @@ -108,7 +143,7 @@ describe("Publishes packages to GitHub Releases", function() {
});

it("should throw an error when github.releases.uploadAsset returns an error", function() {
var options = {'draft': true};
var options = {'draft': true, 'verbose': false};
reset_mocks();
fs.readdir = function(filename, cb) {
cb(null,["filename"]);
Expand All @@ -119,12 +154,20 @@ describe("Publishes packages to GitHub Releases", function() {
expect(function(){ index.publish(options); }).to.throw("uploadAsset error");
});

it("should publish without an error", function() {
var options = {'draft': true};
});

describe("Verify backwords compatible with any breaking changes made within the same MINOR version.", function() {

it("should publish even when package.json's binary.remote_path property is not provided and instead the version is hard coded within binary.host", function() {
var options = {'draft': false, 'verbose': false};
reset_mocks();
fs.readFileSync = function(){return '{"name":"test","version":"0.0.1","repository": {"url":"git+https://github.com/test/test.git"},"binary":{"host":"https://github.com/test/test/releases/download/0.0.1"}}';};
fs.readdir = function(filename, cb) {
cb(null,["filename"]);
};
index.github.releases.createRelease = function(options, cb){
cb(null,{"tag_name":"0.0.1","draft":false,"assets":[{}]});
};
expect(function(){ index.publish(options); }).to.not.throw();
});

Expand Down

0 comments on commit f545dd6

Please sign in to comment.