Fix test
This commit is contained in:
parent
95173cd89c
commit
c548b29ecd
31
index.js
31
index.js
|
@ -3,44 +3,42 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const platformNames = {
|
const platformNames = {
|
||||||
win32 : 'win',
|
win32 : 'windows',
|
||||||
linux : 'lin',
|
linux : 'linux',
|
||||||
darwin : 'osx',
|
darwin : 'osx',
|
||||||
};
|
};
|
||||||
|
|
||||||
const platformName = platformNames[process.platform];
|
const platformName = platformNames[process.platform];
|
||||||
|
const isWindows = process.platform === 'win32';
|
||||||
|
|
||||||
if ( ! platformName ) {
|
if ( ! platformName ) {
|
||||||
console.log(`Error: UNKNOWN PLATFORM "${process.platform}"`);
|
console.log(`Error: UNKNOWN PLATFORM "${process.platform}"`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const isWindows = process.platform === 'win32';
|
|
||||||
|
|
||||||
const rootPath = __dirname.replace(/\\/g, '/');
|
const rootPath = __dirname.replace(/\\/g, '/');
|
||||||
|
|
||||||
|
|
||||||
const napiInclude = require('node-addon-api').include.replace(/\\/g, '/');
|
const napiInclude = require('node-addon-api').include.replace(/\\/g, '/');
|
||||||
const thisInclude = `${rootPath}/include`;
|
const thisInclude = `${rootPath}/include`;
|
||||||
|
const includePath = `${napiInclude} ${thisInclude}`;
|
||||||
|
|
||||||
|
|
||||||
const paths = dir => {
|
const paths = dir => {
|
||||||
|
|
||||||
dir = dir.replace(/\\/g, '/');
|
dir = dir.replace(/\\/g, '/');
|
||||||
|
|
||||||
const binPath = `${dir}/${platformName}`;
|
const bin = `${dir}/bin-${platformName}`;
|
||||||
|
const include = `${dir}/include`;
|
||||||
|
|
||||||
if (isWindows) {
|
if (isWindows) {
|
||||||
process.env.path = `${binPath};${process.env.path ? `${process.env.path}` : ''}`;
|
process.env.path = `${bin};${process.env.path ? `${process.env.path}` : ''}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const includePath = `${dir}/include`;
|
|
||||||
|
|
||||||
return { bin, include };
|
return { bin, include };
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const includePath = `${napiInclude} ${thisInclude}`;
|
|
||||||
const binPath = currentDir;
|
|
||||||
|
|
||||||
const mkdirPath = isWindows ? `${rootPath}/bat/mkdir.bat` : 'mkdir';
|
const mkdirPath = isWindows ? `${rootPath}/bat/mkdir.bat` : 'mkdir';
|
||||||
const rmPath = isWindows ? `${rootPath}/bat/rm.bat` : 'rm';
|
const rmPath = isWindows ? `${rootPath}/bat/rm.bat` : 'rm';
|
||||||
const cpPath = isWindows ? `${rootPath}/bat/cp.bat` : 'cp';
|
const cpPath = isWindows ? `${rootPath}/bat/cp.bat` : 'cp';
|
||||||
|
@ -50,11 +48,12 @@ module.exports = {
|
||||||
|
|
||||||
paths,
|
paths,
|
||||||
|
|
||||||
platform: platformName,
|
bin : `bin-${platformName}`,
|
||||||
include: includePath,
|
platform : platformName,
|
||||||
|
include : includePath,
|
||||||
|
|
||||||
mkdir: mkdirPath,
|
mkdir : mkdirPath,
|
||||||
rm: rmPath,
|
rm : rmPath,
|
||||||
cp: cpPath,
|
cp : cpPath,
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
109
test/test.js
109
test/test.js
|
@ -1,19 +1,12 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const path = require('path');
|
|
||||||
const fs = require('fs');
|
|
||||||
|
|
||||||
const { expect } = require('chai');
|
const { expect } = require('chai');
|
||||||
const { stub, spy } = require('sinon');
|
const { stub, spy } = require('sinon');
|
||||||
|
|
||||||
const tools = require('addon-tools-raub');
|
const tools = require('addon-tools-raub');
|
||||||
|
|
||||||
|
|
||||||
const toolsDir = path.dirname(require.resolve('addon-tools-raub')).replace(/\\/g, '/');
|
const PROPS = ['bin', 'platform', 'include', 'mkdir', 'rm', 'cp'];
|
||||||
const allMethods = ['paths', 'bin', 'root', 'include', 'mkdir', 'rm', 'cp'];
|
|
||||||
const ownMethods = allMethods.slice(1);
|
|
||||||
const cmdMethods = allMethods.slice(3);
|
|
||||||
const pathsMethods = ['bin', 'rem', 'include'];
|
|
||||||
|
|
||||||
|
|
||||||
describe('Tools', () => {
|
describe('Tools', () => {
|
||||||
|
@ -27,101 +20,33 @@ describe('Tools', () => {
|
||||||
afterEach(() => stubbed.restore());
|
afterEach(() => stubbed.restore());
|
||||||
|
|
||||||
|
|
||||||
describe('Own Methods', () => {
|
describe('Properties', () => {
|
||||||
|
|
||||||
ownMethods.forEach(
|
PROPS.forEach(
|
||||||
m => it(`#${m}() is available`, () => {
|
m => it(`#${m} is a string`, () => {
|
||||||
expect(tools).to.respondTo(m);
|
expect(tools[m]).to.be.a('string');
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
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('#bin() is correct', () => {
|
|
||||||
tools.bin();
|
|
||||||
expect(log.getCall(0).args[0]).to.equal(path.basename(tools.paths(__dirname).binPath));
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
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 => fs.stat(
|
|
||||||
dir,
|
|
||||||
(err, stat) => err ? res(false) : res(stat.isDirectory())
|
|
||||||
)
|
|
||||||
)));
|
|
||||||
|
|
||||||
dirs.forEach((dir, i) => expect(stats[i], dir).to.be.true);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
describe('Cmd Methods', () => {
|
|
||||||
|
|
||||||
cmdMethods.forEach(
|
|
||||||
m => it(`#${m}() is available`, () => {
|
|
||||||
expect(tools).to.respondTo(m);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
cmdMethods.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');
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#paths()', () => {
|
||||||
describe('Paths', () => {
|
|
||||||
|
|
||||||
it('#paths() returns an object', () => {
|
it('is a function', () => {
|
||||||
|
expect(tools.paths).to.be.a('function');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns an object', () => {
|
||||||
expect(tools.paths(__dirname)).to.be.an('object');
|
expect(tools.paths(__dirname)).to.be.an('object');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('has "include" string', () => {
|
||||||
|
expect(tools.paths(__dirname).include).to.be.a('string');
|
||||||
|
});
|
||||||
|
|
||||||
pathsMethods.forEach(
|
it('has "bin" string', () => {
|
||||||
m => it(`#${m}() is available`, () => {
|
expect(tools.paths(__dirname).include).to.be.a('string');
|
||||||
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');
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue