diff --git a/.npmignore b/.npmignore index e1d2f47..5c51d45 100644 --- a/.npmignore +++ b/.npmignore @@ -8,3 +8,4 @@ node_modules package-lock.json *.log .gitignore +test diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..5d5b4df --- /dev/null +++ b/test/package.json @@ -0,0 +1,38 @@ +{ + "name": "TEST", + "author": "Luis Blanco ", + "description": "TEST", + "version": "0.0.1", + "main": "mocha", + "scripts": { + "test": "mocha", + "start": "mocha" + }, + "keywords": [ + "node", + "addon", + "header", + "include", + "platform", + "build", + "paths" + ], + "maintainers": [ + { + "name": "Luis Blanco", + "email": "raubtierxxx@gmail.com", + "skype": "rauber666" + } + ], + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/raub/node-addon-tools.git" + }, + "dependencies": { + "addon-tools-raub": "https://github.com/raub/node-addon-tools.git", + "chai": "^4.1.2", + "mocha": "^5.0.0", + "sinon": "^4.2.2" + } +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..0aa167a --- /dev/null +++ b/test/test.js @@ -0,0 +1,101 @@ +'use strict'; + +const path = require('path'); +const fs = require('fs'); + +const { expect } = require('chai'); +const { stub, spy } = require('sinon'); + +const tools = require('addon-tools-raub'); + + +const toolsDir = path.dirname(require.resolve('addon-tools-raub')).replace(/\\/g, '/') +const allMethods = ['paths', 'root', 'include', 'mkdir', 'rm', 'cp']; +const ownMethods = allMethods.slice(1); +const cmdMethods = allMethods.slice(3); +const pathsMethods = ['bin', 'rem', 'include']; + + +describe('Tools', () => { + + let log; + let stubbed; + beforeEach(() => { + log = spy(); + stubbed = stub(console, 'log').callsFake(log) + }); + afterEach(() => stubbed.restore()); + + + describe('Own Methods', () => { + + allMethods.forEach( + m => it(`#${m}() is available`, () => { + expect(tools).to.respondTo(m); + }) + ); + + + ownMethods.forEach( + m => it(`#${m}() writes stdout`, () => { + tools[m](); + expect(log.getCall(0), 'called').to.exist; + expect(log.getCall(0).args[0], 'has args').to.exist; + expect(log.getCall(0).args[0], 'writes string').to.be.a('string'); + }) + ); + + + it(`#root() is correct`, () => { + tools.root(); + expect(log.getCall(0).args[0]).to.equal(toolsDir); + }); + + + it(`#include() is correct`, async () => { + + tools.include(); + const dirs = log.getCall(0).args[0].split(' '); + + const stats = await Promise.all(dirs.map(dir => new Promise( + (res, rej) => fs.stat( + dir, + (err, stat) => err ? res(false) : res(stat.isDirectory()) + ) + ))); + + dirs.forEach((dir, i) => expect(stats[i], dir).to.be.true); + + }); + + }); + + + describe('Paths', () => { + + it(`#paths() returns an object`, () => { + expect(tools.paths(__dirname)).to.be.an('object'); + }); + + + pathsMethods.forEach( + m => it(`#${m}() is available`, () => { + const paths = tools.paths(__dirname); + expect(paths).to.respondTo(m); + }) + ); + + + pathsMethods.forEach( + m => it(`#${m}() writes stdout`, () => { + const paths = tools.paths(__dirname); + paths[m](); + expect(log.getCall(0), 'called').to.exist; + expect(log.getCall(0).args[0], 'has args').to.exist; + expect(log.getCall(0).args[0], 'writes string').to.be.a('string'); + }) + ); + + }); + +});