Skip to content

Commit 29e4173

Browse files
authored
Merge pull request #177 from http-party/fix-start-port-external-api
if `startPort` is provided by user, actually use it
2 parents 7c622b6 + 8523a9c commit 29e4173

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

lib/portfinder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ exports.setBasePath = function (path) {
124124
};
125125

126126
internals.getPort = function (options, callback) {
127-
options.port = Number(options.port) || Number(exports.basePort);
127+
options.port = Number(options.port) || Number(options.startPort) || Number(exports.basePort);
128128
options.host = options.host || null;
129129
options.stopPort = Number(options.stopPort) || Number(exports.highestPort);
130130

test/port-finder.test.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
const portfinder = require('../lib/portfinder'),
1111
helper = require('./helper');
1212

13-
portfinder.basePort = 32768;
13+
const basePort = 32768;
14+
portfinder.basePort = basePort;
1415

1516
describe('with 5 existing servers', function () {
1617
const servers = [];
@@ -152,32 +153,41 @@ describe('with no existing servers', function () {
152153
});
153154
});
154155

155-
describe.each([
156-
['getPort()', false, portfinder.getPort],
157-
['getPort()', true, portfinder.getPort],
158-
['getPortPromise()', true, portfinder.getPortPromise],
159-
])(`the %s method (promise: %p)`, function (name, isPromise, method) {
160-
test('with startPort less than or equal to 80', function (done) {
161-
if (isPromise) {
162-
method({ startPort: 80 })
163-
.then(function (port) {
164-
expect(port).toBeGreaterThanOrEqual(80);
156+
157+
describe('with startPort provided', function () {
158+
beforeAll(function () {
159+
portfinder.basePort = 8000;
160+
});
161+
afterAll(function () {
162+
portfinder.basePort = basePort;
163+
});
164+
describe.each([
165+
['getPort()', false, portfinder.getPort],
166+
['getPort()', true, portfinder.getPort],
167+
['getPortPromise()', true, portfinder.getPortPromise],
168+
])(`the %s method (promise: %p)`, function (name, isPromise, method) {
169+
test('with startPort less than or equal to 9050', function (done) {
170+
if (isPromise) {
171+
method({ startPort: 9050 })
172+
.then(function (port) {
173+
expect(port).toBeGreaterThanOrEqual(9050);
174+
done();
175+
})
176+
.catch(function (err) {
177+
done(err);
178+
});
179+
} else {
180+
method({ startPort: 9050 }, function (err, port) {
181+
if (err) {
182+
done(err);
183+
return;
184+
}
185+
expect(err).toBeNull();
186+
expect(port).toBeGreaterThanOrEqual(9050);
165187
done();
166-
})
167-
.catch(function (err) {
168-
done(err);
169188
});
170-
} else {
171-
method({ startPort: 80 }, function (err, port) {
172-
if (err) {
173-
done(err);
174-
return;
175-
}
176-
expect(err).toBeNull();
177-
expect(port).toBeGreaterThanOrEqual(80);
178-
done();
179-
});
180-
}
189+
}
190+
});
181191
});
182192
});
183193

0 commit comments

Comments
 (0)