Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/portfinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ exports.setBasePath = function (path) {
};

internals.getPort = function (options, callback) {
options.port = Number(options.port) || Number(exports.basePort);
options.port = Number(options.port) || Number(options.startPort) || Number(exports.basePort);
options.host = options.host || null;
options.stopPort = Number(options.stopPort) || Number(exports.highestPort);

Expand Down
60 changes: 35 additions & 25 deletions test/port-finder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
const portfinder = require('../lib/portfinder'),
helper = require('./helper');

portfinder.basePort = 32768;
const basePort = 32768;
portfinder.basePort = basePort;

describe('with 5 existing servers', function () {
const servers = [];
Expand Down Expand Up @@ -152,32 +153,41 @@ describe('with no existing servers', function () {
});
});

describe.each([
['getPort()', false, portfinder.getPort],
['getPort()', true, portfinder.getPort],
['getPortPromise()', true, portfinder.getPortPromise],
])(`the %s method (promise: %p)`, function (name, isPromise, method) {
test('with startPort less than or equal to 80', function (done) {
if (isPromise) {
method({ startPort: 80 })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we had this test which was hiding the fact that startPort didn't even work - we thought we set it to port 80 and then tested it went higher but it was the basePort that was making it do that - now its correct

.then(function (port) {
expect(port).toBeGreaterThanOrEqual(80);

describe('with startPort provided', function () {
beforeAll(function () {
portfinder.basePort = 8000;
});
afterAll(function () {
portfinder.basePort = basePort;
});
describe.each([
['getPort()', false, portfinder.getPort],
['getPort()', true, portfinder.getPort],
['getPortPromise()', true, portfinder.getPortPromise],
])(`the %s method (promise: %p)`, function (name, isPromise, method) {
test('with startPort less than or equal to 9050', function (done) {
if (isPromise) {
method({ startPort: 9050 })
.then(function (port) {
expect(port).toBeGreaterThanOrEqual(9050);
done();
})
.catch(function (err) {
done(err);
});
} else {
method({ startPort: 9050 }, function (err, port) {
if (err) {
done(err);
return;
}
expect(err).toBeNull();
expect(port).toBeGreaterThanOrEqual(9050);
done();
})
.catch(function (err) {
done(err);
});
} else {
method({ startPort: 80 }, function (err, port) {
if (err) {
done(err);
return;
}
expect(err).toBeNull();
expect(port).toBeGreaterThanOrEqual(80);
done();
});
}
}
});
});
});

Expand Down