diff --git a/lib/LocalBinary.js b/lib/LocalBinary.js index 632d88b..eb94cfc 100644 --- a/lib/LocalBinary.js +++ b/lib/LocalBinary.js @@ -17,6 +17,8 @@ const packageName = 'browserstack-local-nodejs'; function LocalBinary(){ this.hostOS = process.platform; + /* Set here, not only in getBinaryFilename(): binaryPath() reads it before any download. */ + this.windows = /mswin|msys|mingw|cygwin|bccwin|wince|emc|win32/i.test(this.hostOS); this.is64bits = process.arch == 'x64'; this.isArm64 = process.arch == 'arm64'; this.baseRetries = 9; diff --git a/test/local_binary_busy_download.js b/test/local_binary_busy_download.js index d7a70fb..81063b9 100644 --- a/test/local_binary_busy_download.js +++ b/test/local_binary_busy_download.js @@ -130,6 +130,48 @@ describe('LocalBinary busy-binary download handling', function () { }); }); + // SDK-7713: binaryPath() read this.windows before anything had set it, so on + // Windows it looked for "BrowserStackLocal" without ".exe", never found it, and + // re-downloaded on every start -- colliding with any other run executing it. + describe('existing binary on Windows', function () { + var dir, exe; + + function windowsBinary() { + var platform = Object.getOwnPropertyDescriptor(process, 'platform'); + Object.defineProperty(process, 'platform', { value: 'win32' }); + try { + var binary = new LocalBinary(); + } finally { + Object.defineProperty(process, 'platform', platform); + } + binary.orderedPaths = [dir]; + binary.download = binary.downloadSync = function () { throw new Error('should not re-download'); }; + return binary; + } + + beforeEach(function () { + dir = fs.mkdtempSync(path.join(os.tmpdir(), 'bs-local-')); + exe = path.join(dir, 'BrowserStackLocal.exe'); + fs.writeFileSync(exe, 'binary', { mode: 0o755 }); + }); + + afterEach(function () { + fs.unlinkSync(exe); + fs.rmdirSync(dir); + }); + + it('reuses BrowserStackLocal.exe instead of re-downloading (sync)', function () { + expect(windowsBinary().binaryPath({}, null, 'key', 9)).to.equal(exe); + }); + + it('reuses BrowserStackLocal.exe instead of re-downloading (async)', function (done) { + windowsBinary().binaryPath({}, null, 'key', 9, function (binaryPath) { + expect(binaryPath).to.equal(exe); + done(); + }); + }); + }); + describe('isBinaryBusy', function () { it('reports a readable file as free', function () { var binary = new LocalBinary(),