Skip to content

Commit

Permalink
test updates
Browse files Browse the repository at this point in the history
test updates
  • Loading branch information
jmcriffey committed Jun 29, 2016
1 parent aa79b79 commit bbe3bc4
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 208 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
language: node_js

node_js:
- "0.12"
- "4.4"
- "5.12"
- "6.2"

sudo: false

script:
- npm run test
- cat coverage/lcov.info | node_modules/.bin/coveralls || echo "Coveralls upload failed"
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
[![Travis Status][trav_img]][trav_site]
[![Coverage Status][cov_img]][cov_site]
[![NPM Package][npm_img]][npm_site]

# Publishr

A tool for harmonious publishing of git and npm packages.


[trav_img]: https://img.shields.io/travis/FormidableLabs/publishr.svg
[trav_site]: https://travis-ci.org/FormidableLabs/publishr
[cov_img]: https://img.shields.io/coveralls/FormidableLabs/publishr.svg
[cov_site]: https://coveralls.io/r/FormidableLabs/publishr
[npm_img]: https://img.shields.io/npm/v/publishr.svg
[npm_site]: https://www.npmjs.org/package/publishr
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"scripts": {
"build": "rimraf lib && babel src/ -d lib/",
"check-coverage": "babel-istanbul check-coverage",
"cover": "NODE_PATH=./src babel-node node_modules/.bin/babel-istanbul cover node_modules/.bin/_mocha -- --require test/main.js --recursive test",
"cover": "rimraf coverage && NODE_PATH=src babel-node node_modules/.bin/babel-istanbul cover node_modules/.bin/_mocha -- --require test/main.js --recursive test",
"lint": "eslint src test",
"test": "npm run lint && npm run cover && npm run check-coverage",
"postpublish": "node lib/cli.js postpublish",
"postversion": "npm run build && node lib/cli.js postversion",
"unit": "NODE_PATH=./src babel-node node_modules/.bin/_mocha --require test/main.js --recursive test"
"unit": "NODE_PATH=src babel-node node_modules/.bin/_mocha --require test/main.js --recursive test"
},
"keywords": [
"git",
Expand All @@ -34,10 +34,12 @@
"babel-eslint": "^6.0.4",
"babel-istanbul": "^0.8.0",
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"eslint": "^1.0.0",
"eslint-config-defaults": "^9.0.0",
"eslint-plugin-filenames": "^0.2.0",
"mocha": "^2.5.3",
"mock-fs": "^3.9.0",
"proxyquire": "^1.7.9",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0"
Expand Down
22 changes: 17 additions & 5 deletions src/file-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ const fileUtils = {

readPackage() {
return new Promise((resolve, reject) => {
fs.readFile("package.json", "utf8", (err, contents) => {
if (err) {
return reject(err);
fs.readFile("package.json", "utf8", (readErr, contents) => {
if (readErr) {
return reject(readErr);
}

return resolve(JSON.parse(contents));
try {
return resolve(JSON.parse(contents));
} catch (parseErr) {
return reject(parseErr);
}
});
});
},
Expand Down Expand Up @@ -92,7 +96,15 @@ const fileUtils = {

writePackage(json) {
return new Promise((resolve, reject) => {
fs.writeFile("package.json", JSON.stringify(json, null, 2), "utf8", (err) => {
let contents;

try {
contents = JSON.stringify(json, null, 2);
} catch (err) {
return reject(err);
}

fs.writeFile("package.json", contents, "utf8", (err) => {
if (err) {
return reject(err);
}
Expand Down
132 changes: 63 additions & 69 deletions test/spec/file-handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import sinon from "sinon";


describe("fileHandler", () => {
let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});

describe("fixFiles", () => {
it("should checkout files", () => {
sinon.stub(fileUtils, "checkoutFile");
sinon.stub(fileUtils, "removeFile");
sandbox.stub(fileUtils, "checkoutFile");
sandbox.stub(fileUtils, "removeFile");

fileHandler.fixFiles({
_publishr: [{
Expand All @@ -18,16 +28,14 @@ describe("fileHandler", () => {
}]
});
expect(fileUtils.removeFile).to.have.callCount(0);
expect(fileUtils.checkoutFile).to.have.callCount(1);
expect(fileUtils.checkoutFile).to.have.been.calledWith("checkout.js");

fileUtils.checkoutFile.restore();
fileUtils.removeFile.restore();
expect(fileUtils.checkoutFile)
.to.have.callCount(1).and
.to.have.been.calledWith("checkout.js");
});

it("should remove files", () => {
sinon.stub(fileUtils, "checkoutFile");
sinon.stub(fileUtils, "removeFile");
sandbox.stub(fileUtils, "checkoutFile");
sandbox.stub(fileUtils, "removeFile");

fileHandler.fixFiles({
_publishr: [{
Expand All @@ -36,16 +44,14 @@ describe("fileHandler", () => {
}]
});
expect(fileUtils.checkoutFile).to.have.callCount(0);
expect(fileUtils.removeFile).to.have.callCount(1);
expect(fileUtils.removeFile).to.have.been.calledWith("remove.js");

fileUtils.checkoutFile.restore();
fileUtils.removeFile.restore();
expect(fileUtils.removeFile)
.to.have.callCount(1).and
.to.have.been.calledWith("remove.js");
});

it("should handle multiple files", () => {
sinon.stub(fileUtils, "checkoutFile");
sinon.stub(fileUtils, "removeFile");
sandbox.stub(fileUtils, "checkoutFile");
sandbox.stub(fileUtils, "removeFile");

fileHandler.fixFiles({
_publishr: [{
Expand All @@ -62,26 +68,25 @@ describe("fileHandler", () => {
path: "remove2.js"
}]
});
expect(fileUtils.checkoutFile).to.have.callCount(2);
expect(fileUtils.removeFile).to.have.callCount(2);
expect(fileUtils.checkoutFile).to.have.been.calledWith("checkout1.js");
expect(fileUtils.checkoutFile).to.have.been.calledWith("checkout2.js");
expect(fileUtils.removeFile).to.have.been.calledWith("remove1.js");
expect(fileUtils.removeFile).to.have.been.calledWith("remove2.js");

fileUtils.checkoutFile.restore();
fileUtils.removeFile.restore();
expect(fileUtils.checkoutFile)
.to.have.callCount(2).and
.to.have.been.calledWith("checkout1.js").and
.to.have.been.calledWith("checkout2.js");
expect(fileUtils.removeFile)
.to.have.callCount(2).and
.to.have.been.calledWith("remove1.js").and
.to.have.been.calledWith("remove2.js");
});
});

describe("overwriteFiles", () => {
it("should overwrite files", () => {
const handler = (files) => Promise.resolve(files);

sinon.stub(fileUtils, "statFiles", handler);
sinon.stub(fileUtils, "readFiles", handler);
sinon.stub(fileUtils, "writeFiles", handler);
sinon.stub(fileHandler, "overwritePackage", handler);
sandbox.stub(fileUtils, "statFiles", handler);
sandbox.stub(fileUtils, "readFiles", handler);
sandbox.stub(fileUtils, "writeFiles", handler);
sandbox.stub(fileHandler, "overwritePackage", handler);

return fileHandler.overwriteFiles({
publishr: {
Expand All @@ -91,17 +96,18 @@ describe("fileHandler", () => {
}
}
}).then((json) => {
expect(fileUtils.statFiles).to.have.callCount(1);
expect(fileUtils.statFiles)
.to.have.callCount(1).and
.to.have.been.calledWith([{
newPath: "first.js",
oldPath: "first.js.publishr"
}, {
newPath: "second.js",
oldPath: "second.js.publishr"
}]);
expect(fileUtils.readFiles).to.have.callCount(1);
expect(fileUtils.writeFiles).to.have.callCount(1);
expect(fileHandler.overwritePackage).to.have.callCount(1);
expect(fileUtils.statFiles).to.have.been.calledWith([{
newPath: "first.js",
oldPath: "first.js.publishr"
}, {
newPath: "second.js",
oldPath: "second.js.publishr"
}]);
expect(json).to.deep.equal({
publishr: {
files: {
Expand All @@ -110,22 +116,13 @@ describe("fileHandler", () => {
}
}
});

fileUtils.statFiles.restore();
fileUtils.readFiles.restore();
fileUtils.writeFiles.restore();
fileHandler.overwritePackage.restore();
});
});

it("should reject on an error", () => {
const mockErr = new Error("Something bad happend!");

sinon.stub(fileUtils, "statFiles", () => {
return new Promise((resolve, reject) => {
reject(mockErr);
});
});
sandbox.stub(fileUtils, "statFiles", () => Promise.reject(mockErr));

return fileHandler.overwriteFiles({
publishr: {
Expand All @@ -135,8 +132,6 @@ describe("fileHandler", () => {
}
}).catch((err) => {
expect(err).to.equal(mockErr);

fileUtils.statFiles.restore();
});
});
});
Expand All @@ -156,28 +151,27 @@ describe("fileHandler", () => {
path: ".npmignore"
}];

sinon.stub(fileUtils, "writePackage", () => Promise.resolve());
sinon.stub(packageUtils, "updateDependencies");
sinon.stub(packageUtils, "updateMeta");
sandbox.stub(fileUtils, "writePackage", () => Promise.resolve());
sandbox.stub(packageUtils, "updateDependencies");
sandbox.stub(packageUtils, "updateMeta");

return fileHandler.overwritePackage(packageJSON, files).then(() => {
expect(packageUtils.updateDependencies).to.have.callCount(1);
expect(packageUtils.updateMeta).to.have.callCount(1);
expect(fileUtils.writePackage).to.have.callCount(1);
expect(packageUtils.updateDependencies).to.have.been.calledWith(packageJSON);
expect(packageUtils.updateMeta).to.have.been.calledWith(packageJSON, files);
expect(fileUtils.writePackage).to.have.been.calledWith({
dependencies: {
babel: "1.0.0"
},
devDependencies: {
eslint: "1.0.0"
}
});

fileUtils.writePackage.restore();
packageUtils.updateDependencies.restore();
packageUtils.updateMeta.restore();
expect(packageUtils.updateDependencies)
.to.have.callCount(1).and
.to.have.been.calledWith(packageJSON);
expect(packageUtils.updateMeta)
.to.have.callCount(1).and
.to.have.been.calledWith(packageJSON, files);
expect(fileUtils.writePackage)
.to.have.callCount(1).and
.to.have.been.calledWith({
dependencies: {
babel: "1.0.0"
},
devDependencies: {
eslint: "1.0.0"
}
});
});
});
});
Expand Down

0 comments on commit bbe3bc4

Please sign in to comment.