Fix mocks not being injected into utils tests

This commit is contained in:
Rob Lourens 2015-12-26 22:58:29 -06:00
Родитель f43ae0c43a
Коммит 402464a579
1 изменённых файлов: 35 добавлений и 48 удалений

Просмотреть файл

@ -4,13 +4,15 @@
import * as mockery from 'mockery'; import * as mockery from 'mockery';
import * as assert from 'assert'; import * as assert from 'assert';
import * as path from 'path'; import * as _path from 'path';
import * as testUtils from '../testUtils'; import * as testUtils from '../testUtils';
/** Utilities without mocks - use for type only */ /** Utilities without mocks - use for type only */
import * as _Utilities from '../../webkit/utilities'; import * as _Utilities from '../../webkit/utilities';
let path: typeof _path;
const MODULE_UNDER_TEST = '../../webkit/utilities'; const MODULE_UNDER_TEST = '../../webkit/utilities';
suite('Utilities', () => { suite('Utilities', () => {
function getUtilities(): typeof _Utilities { function getUtilities(): typeof _Utilities {
@ -24,7 +26,8 @@ suite('Utilities', () => {
mockery.registerMock('fs', { statSync: () => { } }); mockery.registerMock('fs', { statSync: () => { } });
mockery.registerMock('http', {}); mockery.registerMock('http', {});
mockery.registerMock('os', { platform: () => 'win32' }); mockery.registerMock('os', { platform: () => 'win32' });
mockery.registerMock('path', path.win32); mockery.registerMock('path', _path.win32);
path = require('path');
mockery.registerAllowables([ mockery.registerAllowables([
'url', MODULE_UNDER_TEST]); 'url', MODULE_UNDER_TEST]);
@ -102,38 +105,34 @@ suite('Utilities', () => {
}); });
suite('reversedArr()', () => { suite('reversedArr()', () => {
const Utilities = getUtilities();
test('it does not modify the input array', () => { test('it does not modify the input array', () => {
let arr = [2, 4, 6]; let arr = [2, 4, 6];
Utilities.reversedArr(arr); getUtilities().reversedArr(arr);
assert.deepEqual(arr, [2, 4, 6]); assert.deepEqual(arr, [2, 4, 6]);
arr = [1]; arr = [1];
Utilities.reversedArr(arr); getUtilities().reversedArr(arr);
assert.deepEqual(arr, [1]); assert.deepEqual(arr, [1]);
}); });
test('it reverses the array', () => { test('it reverses the array', () => {
assert.deepEqual(Utilities.reversedArr([1, 3, 5, 7]), [7, 5, 3, 1]); assert.deepEqual(getUtilities().reversedArr([1, 3, 5, 7]), [7, 5, 3, 1]);
assert.deepEqual( assert.deepEqual(
Utilities.reversedArr([-1, 'hello', null, undefined, [1, 2]]), getUtilities().reversedArr([-1, 'hello', null, undefined, [1, 2]]),
[[1, 2], undefined, null, 'hello', -1]); [[1, 2], undefined, null, 'hello', -1]);
}); });
}); });
suite('promiseTimeout()', () => { suite('promiseTimeout()', () => {
const Utilities = getUtilities();
test('when given a promise it fails if the promise never resolves', () => { test('when given a promise it fails if the promise never resolves', () => {
return Utilities.promiseTimeout(new Promise(() => { }), 5).then( return getUtilities().promiseTimeout(new Promise(() => { }), 5).then(
() => assert.fail('This promise should fail'), () => assert.fail('This promise should fail'),
e => { } e => { }
); );
}); });
test('when given a promise it succeeds if the promise resolves', () => { test('when given a promise it succeeds if the promise resolves', () => {
return Utilities.promiseTimeout(Promise.resolve('test'), 5).then( return getUtilities().promiseTimeout(Promise.resolve('test'), 5).then(
result => { result => {
assert.equal(result, 'test'); assert.equal(result, 'test');
}, },
@ -142,7 +141,7 @@ suite('Utilities', () => {
}); });
test('when not given a promise it resolves', () => { test('when not given a promise it resolves', () => {
return Utilities.promiseTimeout(null, 5).then( return getUtilities().promiseTimeout(null, 5).then(
null, null,
() => assert.fail('This promise should pass') () => assert.fail('This promise should pass')
); );
@ -150,10 +149,8 @@ suite('Utilities', () => {
}); });
suite('retryAsync()', () => { suite('retryAsync()', () => {
const Utilities = getUtilities();
test('when the function passes, it resolves with the value', () => { test('when the function passes, it resolves with the value', () => {
return Utilities.retryAsync(() => Promise.resolve('pass'), /*timeoutMs=*/5).then( return getUtilities().retryAsync(() => Promise.resolve('pass'), /*timeoutMs=*/5).then(
result => { result => {
assert.equal(result, 'pass'); assert.equal(result, 'pass');
}, },
@ -163,7 +160,7 @@ suite('Utilities', () => {
}); });
test('when the function fails, it rejects', () => { test('when the function fails, it rejects', () => {
return Utilities.retryAsync(() => Utilities.errP('fail'), /*timeoutMs=*/5) return getUtilities().retryAsync(() => getUtilities().errP('fail'), /*timeoutMs=*/5)
.then( .then(
() => assert.fail('This promise should fail'), () => assert.fail('This promise should fail'),
e => assert.equal(e.message, 'fail')); e => assert.equal(e.message, 'fail'));
@ -257,20 +254,18 @@ suite('Utilities', () => {
}); });
suite('fixDriveLetterAndSlashes', () => { suite('fixDriveLetterAndSlashes', () => {
const Utilities = getUtilities();
test('works for c:/... cases', () => { test('works for c:/... cases', () => {
assert.equal(Utilities.fixDriveLetterAndSlashes('C:/path/stuff'), 'c:\\path\\stuff'); assert.equal(getUtilities().fixDriveLetterAndSlashes('C:/path/stuff'), 'c:\\path\\stuff');
assert.equal(Utilities.fixDriveLetterAndSlashes('c:/path\\stuff'), 'c:\\path\\stuff'); assert.equal(getUtilities().fixDriveLetterAndSlashes('c:/path\\stuff'), 'c:\\path\\stuff');
assert.equal(Utilities.fixDriveLetterAndSlashes('C:\\path'), 'c:\\path'); assert.equal(getUtilities().fixDriveLetterAndSlashes('C:\\path'), 'c:\\path');
assert.equal(Utilities.fixDriveLetterAndSlashes('C:\\'), 'c:\\'); assert.equal(getUtilities().fixDriveLetterAndSlashes('C:\\'), 'c:\\');
}); });
test('works for file:/// cases', () => { test('works for file:/// cases', () => {
assert.equal(Utilities.fixDriveLetterAndSlashes('file:///C:/path/stuff'), 'file:///c:\\path\\stuff'); assert.equal(getUtilities().fixDriveLetterAndSlashes('file:///C:/path/stuff'), 'file:///c:\\path\\stuff');
assert.equal(Utilities.fixDriveLetterAndSlashes('file:///c:/path\\stuff'), 'file:///c:\\path\\stuff'); assert.equal(getUtilities().fixDriveLetterAndSlashes('file:///c:/path\\stuff'), 'file:///c:\\path\\stuff');
assert.equal(Utilities.fixDriveLetterAndSlashes('file:///C:\\path'), 'file:///c:\\path'); assert.equal(getUtilities().fixDriveLetterAndSlashes('file:///C:\\path'), 'file:///c:\\path');
assert.equal(Utilities.fixDriveLetterAndSlashes('file:///C:\\'), 'file:///c:\\'); assert.equal(getUtilities().fixDriveLetterAndSlashes('file:///C:\\'), 'file:///c:\\');
}); });
}); });
@ -337,18 +332,16 @@ suite('Utilities', () => {
}); });
suite('getWebRoot()', () => { suite('getWebRoot()', () => {
const Utilities = getUtilities();
test('takes absolute webRoot as is', () => { test('takes absolute webRoot as is', () => {
assert.equal(Utilities.getWebRoot({ webRoot: 'c:\\project\\webRoot', cwd: 'c:\\project\\cwd' }), 'c:\\project\\webRoot'); assert.equal(getUtilities().getWebRoot({ webRoot: 'c:\\project\\webRoot', cwd: 'c:\\project\\cwd' }), 'c:\\project\\webRoot');
}); });
test('resolves relative webroot against cwd', () => { test('resolves relative webroot against cwd', () => {
assert.equal(Utilities.getWebRoot({ webRoot: '..\\webRoot', cwd: 'c:\\project\\cwd' }), 'c:\\project\\webRoot'); assert.equal(getUtilities().getWebRoot({ webRoot: '..\\webRoot', cwd: 'c:\\project\\cwd' }), 'c:\\project\\webRoot');
}); });
test('uses cwd when webRoot is missing', () => { test('uses cwd when webRoot is missing', () => {
assert.equal(Utilities.getWebRoot({ webRoot: '', cwd: 'c:\\project\\cwd' }), 'c:\\project\\cwd'); assert.equal(getUtilities().getWebRoot({ webRoot: '', cwd: 'c:\\project\\cwd' }), 'c:\\project\\cwd');
}); });
}); });
@ -404,14 +397,12 @@ suite('Utilities', () => {
}); });
suite('isURL', () => { suite('isURL', () => {
const Utilities = getUtilities();
function assertIsURL(url: string): void { function assertIsURL(url: string): void {
assert(Utilities.isURL(url)); assert(getUtilities().isURL(url));
} }
function assertNotURL(url: string): void { function assertNotURL(url: string): void {
assert(!Utilities.isURL(url)); assert(!getUtilities().isURL(url));
} }
test('returns true for URLs', () => { test('returns true for URLs', () => {
@ -433,27 +424,23 @@ suite('Utilities', () => {
}); });
suite('lstrip', () => { suite('lstrip', () => {
const Utilities = getUtilities();
test('does what it says', () => { test('does what it says', () => {
assert.equal(Utilities.lstrip('test', 'te'), 'st'); assert.equal(getUtilities().lstrip('test', 'te'), 'st');
assert.equal(Utilities.lstrip('asdf', ''), 'asdf'); assert.equal(getUtilities().lstrip('asdf', ''), 'asdf');
assert.equal(Utilities.lstrip('asdf', null), 'asdf'); assert.equal(getUtilities().lstrip('asdf', null), 'asdf');
assert.equal(Utilities.lstrip('asdf', 'asdf'), ''); assert.equal(getUtilities().lstrip('asdf', 'asdf'), '');
assert.equal(Utilities.lstrip('asdf', '123'), 'asdf'); assert.equal(getUtilities().lstrip('asdf', '123'), 'asdf');
assert.equal(Utilities.lstrip('asdf', 'sdf'), 'asdf'); assert.equal(getUtilities().lstrip('asdf', 'sdf'), 'asdf');
}); });
}); });
suite('pathToFileURL', () => { suite('pathToFileURL', () => {
const Utilities = getUtilities();
test('converts windows-style paths', () => { test('converts windows-style paths', () => {
assert.equal(Utilities.pathToFileURL('c:/code/app.js'), 'file:///c:/code/app.js'); assert.equal(getUtilities().pathToFileURL('c:/code/app.js'), 'file:///c:/code/app.js');
}); });
test('converts unix-style paths', () => { test('converts unix-style paths', () => {
assert.equal(Utilities.pathToFileURL('/code/app.js'), 'file:///code/app.js'); assert.equal(getUtilities().pathToFileURL('/code/app.js'), 'file:///code/app.js');
}); });
}); });
}); });