Skip to content

Commit a43da9f

Browse files
committed
Remove global Promise checking and workarounds
Now that this package depends on Node >=4, Promise is always defined globally. Remove the checks and workarounds. Signed-off-by: Kevin Locke <[email protected]>
1 parent 17f862f commit a43da9f

File tree

5 files changed

+91
-146
lines changed

5 files changed

+91
-146
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"commander": "^2.9.0"
4646
},
4747
"devDependencies": {
48-
"bluebird": "^3.3.0",
4948
"codecov": "^3.0.0",
5049
"coveralls": "^3.0.0",
5150
"eslint": "^4.6.1",

test-lib/git.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55

66
'use strict';
77

8-
const BBPromise = require('bluebird').Promise;
9-
// eslint-disable-next-line no-undef
10-
const PPromise = typeof Promise === 'function' ? Promise : BBPromise;
118
const execFile = require('child_process').execFile;
129
const pify = require('pify');
1310

14-
const execFileP = pify(execFile, PPromise, {multiArgs: true});
11+
const execFileP = pify(execFile, {multiArgs: true});
1512

1613
/**
1714
* Run git with given arguments and options.

test/git-branch-is-cmd.js

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
'use strict';
77

88

9-
const BBPromise = require('bluebird').Promise;
10-
// eslint-disable-next-line no-undef
11-
const PPromise = typeof Promise === 'function' ? Promise : BBPromise;
129
const assert = require('assert');
1310
const execFile = require('child_process').execFile;
1411
const path = require('path');
@@ -300,60 +297,38 @@ describe('git-branch-is', () => {
300297
});
301298
});
302299

303-
describe('with global Promise', () => {
304-
let hadPromise, oldPromise;
305-
306-
before('ensure global Promise', () => {
307-
if (global.Promise !== PPromise) {
308-
hadPromise = hasOwnProperty.call(global, 'Promise');
309-
oldPromise = global.Promise;
310-
global.Promise = PPromise;
311-
}
312-
});
313-
314-
after('restore global Promise', () => {
315-
if (hadPromise === true) {
316-
global.Promise = oldPromise;
317-
} else if (hadPromise === false) {
318-
delete global.Promise;
319-
}
320-
});
321-
322-
it('returns a Promise with the result', () => {
323-
const promise = gitBranchIsCmd(ARGS.concat(BRANCH_CURRENT));
324-
assert(promise instanceof global.Promise);
325-
return promise.then((result) => {
326-
assert.strictEqual(result.code, 0);
327-
assert(!result.stdout);
328-
assert(!result.stderr);
329-
});
300+
it('returns a Promise with the result', () => {
301+
const promise = gitBranchIsCmd(ARGS.concat(BRANCH_CURRENT));
302+
assert(promise instanceof global.Promise);
303+
return promise.then((result) => {
304+
assert.strictEqual(result.code, 0);
305+
assert(!result.stdout);
306+
assert(!result.stderr);
330307
});
308+
});
331309

332-
it('rejects the Promise with an Error', () => {
333-
const promise = gitBranchIsCmd(ARGS.concat(
334-
'-C',
335-
'invalid',
336-
BRANCH_CURRENT
337-
));
338-
assert(promise instanceof global.Promise);
339-
return promise.then(
340-
(result) => { throw new Error('expecting Error'); },
341-
(err) => { assert(err instanceof Error); }
342-
);
343-
});
310+
it('rejects the Promise with an Error', () => {
311+
const promise = gitBranchIsCmd(ARGS.concat(
312+
'-C',
313+
'invalid',
314+
BRANCH_CURRENT
315+
));
316+
assert(promise instanceof global.Promise);
317+
return promise.then(
318+
(result) => { throw new Error('expecting Error'); },
319+
(err) => { assert(err instanceof Error); }
320+
);
344321
});
345322

346323
describe('without global Promise', () => {
347324
let hadPromise, oldPromise;
348325

349326
before('remove global Promise', () => {
350-
if (global.Promise) {
351-
hadPromise = hasOwnProperty.call(global, 'Promise');
352-
oldPromise = global.Promise;
353-
// Note: Deleting triggers Mocha's global leak detection.
354-
// Also wouldn't work if global scope had a prototype chain.
355-
global.Promise = undefined;
356-
}
327+
hadPromise = hasOwnProperty.call(global, 'Promise');
328+
oldPromise = global.Promise;
329+
// Note: Deleting triggers Mocha's global leak detection.
330+
// Also wouldn't work if global scope had a prototype chain.
331+
global.Promise = undefined;
357332
});
358333

359334
after('restore global Promise', () => {

test/git-branch-is.js

Lines changed: 64 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
const gitBranchIs = require('..');
99

10-
const BBPromise = require('bluebird').Promise;
11-
// eslint-disable-next-line no-undef
12-
const PPromise = typeof Promise === 'function' ? Promise : BBPromise;
1310
const assert = require('assert');
1411
const path = require('path');
1512

@@ -201,43 +198,82 @@ describe('gitBranchIs', () => {
201198
});
202199
});
203200

204-
describe('with global Promise', () => {
205-
let hadPromise, oldPromise;
201+
it('Promise resolves true for same branch name', () => {
202+
const promise = gitBranchIs(BRANCH_CURRENT);
203+
assert(promise instanceof global.Promise);
204+
return promise.then((result) => {
205+
assert.strictEqual(result, true);
206+
});
207+
});
206208

207-
before('ensure global Promise', () => {
208-
if (global.Promise !== PPromise) {
209-
hadPromise = hasOwnProperty.call(global, 'Promise');
210-
oldPromise = global.Promise;
211-
global.Promise = PPromise;
212-
}
209+
it('Promise resolves false for different branch name', () => {
210+
const promise = gitBranchIs('invalid');
211+
assert(promise instanceof global.Promise);
212+
return promise.then((result) => {
213+
assert.strictEqual(result, false);
213214
});
215+
});
214216

215-
after('restore global Promise', () => {
216-
if (hadPromise === true) {
217-
global.Promise = oldPromise;
218-
} else if (hadPromise === false) {
219-
delete global.Promise;
217+
it('Promise rejects on Error', () => {
218+
const promise = gitBranchIs(BRANCH_CURRENT, 'opts');
219+
assert(promise instanceof global.Promise);
220+
return promise.then(
221+
(result) => { throw new Error('expecting Error'); },
222+
(err) => {
223+
assert(err instanceof TypeError);
224+
assertMatch(err.message, /\boptions\b/);
220225
}
221-
});
226+
);
227+
});
222228

223-
it('Promise resolves true for same branch name', () => {
224-
const promise = gitBranchIs(BRANCH_CURRENT);
225-
assert(promise instanceof global.Promise);
226-
return promise.then((result) => {
227-
assert.strictEqual(result, true);
228-
});
229+
it('Promise flattens for function returning Promise', () => {
230+
function checkBranchName(branchName) {
231+
return global.Promise.resolve(branchName === BRANCH_CURRENT);
232+
}
233+
const promise = gitBranchIs(checkBranchName);
234+
assert(promise instanceof global.Promise);
235+
return promise.then((result) => {
236+
assert.strictEqual(result, true);
229237
});
238+
});
239+
240+
it('Promise rejects for function returning Promise', () => {
241+
// Note: reject with non-Error to ensure no special handling
242+
function checkBranchName(branchName) {
243+
return global.Promise.reject(branchName === BRANCH_CURRENT);
244+
}
245+
const promise = gitBranchIs(checkBranchName);
246+
assert(promise instanceof global.Promise);
247+
return promise.then(
248+
(result) => { throw new Error('expecting rejection'); },
249+
(err) => {
250+
assert.strictEqual(err, true);
251+
}
252+
);
253+
});
254+
255+
it('Promise rejects for function throwing Error', () => {
256+
const errTest = new Error('test');
257+
function checkBranchName(branchName) { throw errTest; }
258+
const promise = gitBranchIs(checkBranchName);
259+
assert(promise instanceof global.Promise);
260+
return promise.then(
261+
(result) => { throw new Error('expecting rejection'); },
262+
(err) => { assert.strictEqual(err, errTest); }
263+
);
264+
});
230265

231-
it('Promise resolves false for different branch name', () => {
232-
const promise = gitBranchIs('invalid');
266+
describe('.getBranch()', () => {
267+
it('resolves to the branch name', () => {
268+
const promise = gitBranchIs.getBranch();
233269
assert(promise instanceof global.Promise);
234270
return promise.then((result) => {
235-
assert.strictEqual(result, false);
271+
assert.strictEqual(result, BRANCH_CURRENT);
236272
});
237273
});
238274

239-
it('Promise rejects on Error', () => {
240-
const promise = gitBranchIs(BRANCH_CURRENT, 'opts');
275+
it('rejects on Error', () => {
276+
const promise = gitBranchIs.getBranch(BRANCH_CURRENT);
241277
assert(promise instanceof global.Promise);
242278
return promise.then(
243279
(result) => { throw new Error('expecting Error'); },
@@ -247,64 +283,5 @@ describe('gitBranchIs', () => {
247283
}
248284
);
249285
});
250-
251-
it('Promise flattens for function returning Promise', () => {
252-
function checkBranchName(branchName) {
253-
return global.Promise.resolve(branchName === BRANCH_CURRENT);
254-
}
255-
const promise = gitBranchIs(checkBranchName);
256-
assert(promise instanceof global.Promise);
257-
return promise.then((result) => {
258-
assert.strictEqual(result, true);
259-
});
260-
});
261-
262-
it('Promise rejects for function returning Promise', () => {
263-
// Note: reject with non-Error to ensure no special handling
264-
function checkBranchName(branchName) {
265-
return global.Promise.reject(branchName === BRANCH_CURRENT);
266-
}
267-
const promise = gitBranchIs(checkBranchName);
268-
assert(promise instanceof global.Promise);
269-
return promise.then(
270-
(result) => { throw new Error('expecting rejection'); },
271-
(err) => {
272-
assert.strictEqual(err, true);
273-
}
274-
);
275-
});
276-
277-
it('Promise rejects for function throwing Error', () => {
278-
const errTest = new Error('test');
279-
function checkBranchName(branchName) { throw errTest; }
280-
const promise = gitBranchIs(checkBranchName);
281-
assert(promise instanceof global.Promise);
282-
return promise.then(
283-
(result) => { throw new Error('expecting rejection'); },
284-
(err) => { assert.strictEqual(err, errTest); }
285-
);
286-
});
287-
288-
describe('.getBranch()', () => {
289-
it('resolves to the branch name', () => {
290-
const promise = gitBranchIs.getBranch();
291-
assert(promise instanceof global.Promise);
292-
return promise.then((result) => {
293-
assert.strictEqual(result, BRANCH_CURRENT);
294-
});
295-
});
296-
297-
it('rejects on Error', () => {
298-
const promise = gitBranchIs.getBranch(BRANCH_CURRENT);
299-
assert(promise instanceof global.Promise);
300-
return promise.then(
301-
(result) => { throw new Error('expecting Error'); },
302-
(err) => {
303-
assert(err instanceof TypeError);
304-
assertMatch(err.message, /\boptions\b/);
305-
}
306-
);
307-
});
308-
});
309286
});
310287
});

test/global-hooks.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
'use strict';
77

8-
const BBPromise = require('bluebird').Promise;
9-
// eslint-disable-next-line no-undef
10-
const PPromise = typeof Promise === 'function' ? Promise : BBPromise;
118
const fs = require('fs');
129
const path = require('path');
1310
const pify = require('pify');
@@ -16,8 +13,8 @@ const rimraf = require('rimraf');
1613
const git = require('../test-lib/git');
1714
const constants = require('../test-lib/constants');
1815

19-
const fsP = pify(fs, PPromise);
20-
const rimrafP = pify(rimraf, PPromise);
16+
const fsP = pify(fs);
17+
const rimrafP = pify(rimraf);
2118

2219
// Local copy of shared constants
2320
const BRANCH_CURRENT = constants.BRANCH_CURRENT;

0 commit comments

Comments
 (0)