Skip to content

Commit

Permalink
Support test suite on Windows
Browse files Browse the repository at this point in the history
Based on work by Tyler Waters <tyler.waters@gmail.com> in mochajs#1814.

Changes from mochajs#1814 include:

- Rebasing
- Use process.argv[0] as an authoritative path for Node.js executable
- Support having spaces in path of Node.js executable
- Avoid external dependencies for child_process.spawn()
- Fix symlink tests on Windows. On Windows, creating symlinks can fail
  since it needs additional user permissions

Fixes mochajs#1813.
  • Loading branch information
TimothyGu committed May 22, 2016
1 parent b76989c commit a08d768
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 131 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BROWSERIFY := node_modules/.bin/browserify
ESLINT := node_modules/.bin/eslint
BROWSERIFY := "node_modules/.bin/browserify"
ESLINT := "node_modules/.bin/eslint"

REPORTER ?= spec
TM_BUNDLE = JavaScript\ mocha.tmbundle
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
"growl": "1.9.2",
"jade": "0.26.3",
"mkdirp": "0.5.1",
"os-tmpdir": "1.0.1",
"supports-color": "1.2.0"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions test/acceptance/fs.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
var fs = require('fs');
var path = require('path');
var t = path.join.bind(path, require('os-tmpdir')());

describe('fs.readFile()', function(){
describe('when the file exists', function(){
it('should succeed', function(done){
fs.writeFile('/tmp/mocha', 'wahoo', done)
fs.writeFile(t('mocha'), 'wahoo', done)
})
})

describe('when the file does not exist', function(){
it('should fail', function(done){
// uncomment
// fs.readFile('/tmp/does-not-exist', done);
// fs.readFile(t('does-not-exist'), done);
done();
})
})
Expand Down
62 changes: 43 additions & 19 deletions test/acceptance/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,39 +386,63 @@ describe('lib/utils', function () {
describe('lookupFiles', function () {
var fs = require('fs'),
path = require('path'),
existsSync = fs.existsSync || path.existsSync;
tmpdir = require('os-tmpdir')(),
t = path.join.bind(path, tmpdir);
existsSync = fs.existsSync || path.existsSync,
symlinkSupported = (function () {
var res = false;
fs.writeFileSync(t('mocha-utils.js'), 'yippy skippy ying yang yow');
try {
fs.symlinkSync(t('mocha-utils.js'), t('mocha-utils-link.js'));
res = true;
}
catch (ignored) {}

['mocha-utils.js', 'mocha-utils-link.js', 'bob'].forEach(function (path) {
try {
fs.unlinkSync(t(path));
}
catch (ignored) {}
});

return res;
})();

beforeEach(function () {
fs.writeFileSync('/tmp/mocha-utils.js', 'yippy skippy ying yang yow');
fs.symlinkSync('/tmp/mocha-utils.js', '/tmp/mocha-utils-link.js');
fs.writeFileSync(t('mocha-utils.js'), 'yippy skippy ying yang yow');
if (symlinkSupported) {
fs.symlinkSync(t('mocha-utils.js'), t('mocha-utils-link.js'));
}
});

it('should not choke on symlinks', function () {
utils.lookupFiles('/tmp', ['js'], false)
.should.containEql('/tmp/mocha-utils-link.js')
.and.containEql('/tmp/mocha-utils.js')
(symlinkSupported ? it : it.skip)('should not choke on symlinks', function () {
utils.lookupFiles(tmpdir, ['js'], false)
.should.containEql(t('mocha-utils-link.js'))
.and.containEql(t('mocha-utils.js'))
.and.have.lengthOf(2);
existsSync('/tmp/mocha-utils-link.js').should.be.true();
fs.renameSync('/tmp/mocha-utils.js', '/tmp/bob');
existsSync('/tmp/mocha-utils-link.js').should.be.false();
utils.lookupFiles('/tmp', ['js'], false).should.eql([]);
existsSync(t('mocha-utils-link.js')).should.be.true();
fs.renameSync(t('mocha-utils.js'), t('bob'));
existsSync(t('mocha-utils-link.js')).should.be.false();
utils.lookupFiles(tmpdir, ['js'], false).should.eql([]);
});

it('should accept a glob "path" value', function () {
utils.lookupFiles('/tmp/mocha-utils*', ['js'], false)
.should
.containEql('/tmp/mocha-utils-link.js')
.and
.containEql('/tmp/mocha-utils.js')
var res = utils.lookupFiles(t('mocha-utils*'), ['js'], false)
.map(path.normalize.bind(path))
.should;
if (symlinkSupported) {
res = res.containEql(t('mocha-utils-link.js'));
}
res.containEql(t('mocha-utils.js'))
.and
.have
.lengthOf(2);
.lengthOf(1 + (+symlinkSupported));
});

afterEach(function () {
['/tmp/mocha-utils.js', '/tmp/mocha-utils-link.js', '/tmp/bob'].forEach(function (path) {
['mocha-utils.js', 'mocha-utils-link.js', 'bob'].forEach(function (path) {
try {
fs.unlinkSync(path);
fs.unlinkSync(t(path));
}
catch (ignored) {}
});
Expand Down
5 changes: 3 additions & 2 deletions test/color.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
var assert = require('assert');
var child_process = require('child_process');
var path = require('path');

describe('Mocha', function() {
this.timeout(1000);

it('should not output colors to pipe', function(cb) {
var command = 'bin/mocha --grep missing-test';
child_process.exec(command, function(err, stdout, stderr) {
var command = [path.join('bin', 'mocha'), '--grep', 'missing-test'];
child_process.execFile(process.argv[0], command, function(err, stdout, stderr) {
if (err) return cb(err);

assert(stdout.indexOf('[90m') === -1);
Expand Down
78 changes: 39 additions & 39 deletions test/integration/fixtures/hooks/multiple.hook.async.error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,54 @@ describe('1', function () {
console.log('1 before each');
});

describe('1.1', function () {
describe('1-1', function () {
before(function () {
console.log('1.1 before');
console.log('1-1 before');
});
beforeEach(function (done) {
console.log('1.1 before each');
console.log('1-1 before each');
process.nextTick(function () {
throw new Error('1.1 before each hook failed');
throw new Error('1-1 before each hook failed');
});
});
it('1.1 test 1', function () {
console.log('1.1 test 1');
it('1-1 test 1', function () {
console.log('1-1 test 1');
});
it('1.1 test 2', function () {
console.log('1.1 test 2');
it('1-1 test 2', function () {
console.log('1-1 test 2');
});
afterEach(function () {
console.log('1.1 after each');
console.log('1-1 after each');
});
after(function (done) {
console.log('1.1 after');
console.log('1-1 after');
process.nextTick(function () {
throw new Error('1.1 after hook failed');
throw new Error('1-1 after hook failed');
});
});
});

describe('1.2', function () {
describe('1-2', function () {
before(function () {
console.log('1.2 before');
console.log('1-2 before');
});
beforeEach(function () {
console.log('1.2 before each');
console.log('1-2 before each');
});
it('1.2 test 1', function () {
console.log('1.2 test 1');
it('1-2 test 1', function () {
console.log('1-2 test 1');
});
it('1.2 test 2', function () {
console.log('1.2 test 2');
it('1-2 test 2', function () {
console.log('1-2 test 2');
});
afterEach(function (done) {
console.log('1.2 after each');
console.log('1-2 after each');
process.nextTick(function () {
throw new Error('1.2 after each hook failed');
throw new Error('1-2 after each hook failed');
});
});
after(function () {
console.log('1.2 after');
console.log('1-2 after');
});
});

Expand All @@ -77,45 +77,45 @@ describe('2', function () {
});
});

describe('2.1', function () {
describe('2-1', function () {
before(function () {
console.log('2.1 before');
console.log('2-1 before');
});
beforeEach(function () {
console.log('2.1 before each');
console.log('2-1 before each');
});
it('2.1 test 1', function () {
console.log('2.1 test 1');
it('2-1 test 1', function () {
console.log('2-1 test 1');
});
it('2.1 test 2', function () {
console.log('2.1 test 2');
it('2-1 test 2', function () {
console.log('2-1 test 2');
});
afterEach(function () {
console.log('2.1 after each');
console.log('2-1 after each');
});
after(function () {
console.log('2.1 after');
console.log('2-1 after');
});
});

describe('2.2', function () {
describe('2-2', function () {
before(function () {
console.log('2.2 before');
console.log('2-2 before');
});
beforeEach(function () {
console.log('2.2 before each');
console.log('2-2 before each');
});
it('2.2 test 1', function () {
console.log('2.2 test 1');
it('2-2 test 1', function () {
console.log('2-2 test 1');
});
it('2.2 test 2', function () {
console.log('2.2 test 2');
it('2-2 test 2', function () {
console.log('2-2 test 2');
});
afterEach(function () {
console.log('2.2 after each');
console.log('2-2 after each');
});
after(function () {
console.log('2.2 after');
console.log('2-2 after');
});
});

Expand Down
Loading

0 comments on commit a08d768

Please sign in to comment.