From 6ce2e493a5e850591c810997037439e14bbd27e2 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Fri, 27 Oct 2017 22:17:22 -0400 Subject: [PATCH] feat: replace Q with Bluebird, close #4 --- index.js | 4 +- package.json | 13 ++++-- test/chdir-promise-spec.js | 95 +++++++++++++++++++++++++++----------- test/demo.js | 32 +++++++++++++ 4 files changed, 111 insertions(+), 33 deletions(-) create mode 100644 test/demo.js diff --git a/index.js b/index.js index 67f6cad..666609c 100644 --- a/index.js +++ b/index.js @@ -35,9 +35,7 @@ function comeBack () { } const chdirTo = folderName => { - return Promise.try(() => { - return _to(folderName) - }) + return Promise.try(() => _to(folderName)) } module.exports = { diff --git a/package.json b/package.json index 7516fc3..5018e0f 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,16 @@ "description": "Change working dir and return a promise, a stack of folders is maintained to jump back", "main": "index.js", "scripts": { - "test": "node test/chdir-promise-spec.js", + "test": "echo running unit tests", + "posttest": "mocha test/*-spec.js", + "demo": "DEBUG=chdir-promise node test/demo.js", + "deps": "deps-ok && dependency-check --no-dev .", "semantic-release": "semantic-release", "commit": "commit-wizard", "size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";", - "pretty": "prettier-standard *.js src/*.js", + "pretty": "prettier-standard *.js src/*.js test/*.js", "prelint": "npm run pretty", - "lint": "standard --verbose --fix *.js src/*.js", + "lint": "standard --verbose --fix *.js src/*.js test/*.js", "pretest": "npm run lint", "issues": "git-issues" }, @@ -46,8 +49,11 @@ "spots": "0.5.0" }, "devDependencies": { + "dependency-check": "2.9.1", + "deps-ok": "1.2.1", "git-issues": "^1.3.1", "github-post-release": "1.13.1", + "mocha": "^4.0.1", "pre-git": "3.15.3", "prettier-standard": "7.0.3", "semantic-release": "9.0.0", @@ -60,6 +66,7 @@ "npm test" ], "pre-push": [ + "npm run deps", "npm run size" ], "post-commit": [], diff --git a/test/chdir-promise-spec.js b/test/chdir-promise-spec.js index 59b1239..ac2472c 100644 --- a/test/chdir-promise-spec.js +++ b/test/chdir-promise-spec.js @@ -1,29 +1,70 @@ -var la = require('lazy-ass'); -var check = require('check-more-types'); -var folders = require('..'); -var join = require('path').join; -var parentFolder = join(process.cwd(), '..') - -la(check.object(folders), 'expected folders to be an object', folders); -la(check.fn(folders.to), 'expected folders.to to be a function', folders); - -folders.to(__dirname) - .then(folders.back) - .then(folders.to(__dirname)) - .then(folders.from) - .then(folders.to(__dirname)) - .then(function () { - return 'foo'; +const la = require('lazy-ass') +const is = require('check-more-types') + +/* eslint-env mocha */ +describe('chdir-promise', () => { + const chdir = require('..') + const originalFolder = process.cwd() + + const here = () => + la( + process.cwd() === __dirname, + 'expected to be in', + __dirname, + 'but was in', + process.cwd() + ) + + const inOriginal = () => + la( + process.cwd() === originalFolder, + 'expected to be in', + originalFolder, + 'but was in', + process.cwd() + ) + + it('has to method', () => { + la(is.fn(chdir.to)) + }) + + it('starts in original folder', () => { + inOriginal() }) - .tap(folders.back) - .then(function (result) { - la(result === 'foo', 'incorrect result', result); + + it('changes folders once', () => { + return chdir + .to(__dirname) + .then(here) + .then(chdir.back) + .then(inOriginal) + }) + + it('changes folders twice', () => { + return chdir + .to(__dirname) + .then(here) + .then(chdir.back) + .then(inOriginal) + .then(_ => chdir.to(__dirname)) + .then(here) + .then(chdir.back) + .then(inOriginal) + }) + + context('back', () => { + it('is a function', () => { + la(is.fn(chdir.back)) + }) + + it('returns a promise', () => { + return chdir + .to(__dirname) + .then(here) + .then(_ => chdir.back().then(_ => 'foo')) + .then(value => { + la(value === 'foo', 'got', value) + }) + }) }) - .done(); - -// .back() returns a promise -folders.to(parentFolder) - .then(function () { - return folders.back() - .then(function () {}) - }).done(); +}) diff --git a/test/demo.js b/test/demo.js new file mode 100644 index 0000000..a013c3a --- /dev/null +++ b/test/demo.js @@ -0,0 +1,32 @@ +const la = require('lazy-ass') +const check = require('check-more-types') +const folders = require('..') +// const join = require('path').join +// const parentFolder = join(process.cwd(), '..') + +la(check.object(folders), 'expected folders to be an object', folders) +la(check.fn(folders.to), 'expected folders.to to be a function', folders) + +folders + .to(__dirname) + .then(folders.back) + .then(() => folders.to(__dirname)) + .then(folders.from) + .then(() => folders.to(__dirname)) + .then(function () { + return 'foo' + }) + .tap(folders.back) + .then(function (result) { + la(result === 'foo', 'incorrect result', result) + console.log('result', result) + }) + .done() + +// .back() returns a promise +// folders +// .to(parentFolder) +// .then(function () { +// return folders.back().then(function () {}) +// }) +// .done()