|
1 |
| -import { expect } from "chai"; |
| 1 | +import * as chai from "chai"; |
| 2 | +import * as path from "path"; |
2 | 3 | import * as fetch from "node-fetch";
|
| 4 | +import 'chai/register-should'; |
| 5 | +import * as sinonChai from 'sinon-chai'; |
| 6 | +import * as sinon from 'sinon'; |
| 7 | +import * as pq from "proxyquire"; |
3 | 8 | import "mocha";
|
| 9 | + |
4 | 10 | import { Version } from "../../cli-version";
|
5 |
| -import { GithubRelease, GithubReleaseAsset, ReleasesApiConsumer, versionCompare } from "../../distribution" |
| 11 | +import { GithubRelease, GithubReleaseAsset, ReleasesApiConsumer, versionCompare } from "../../distribution"; |
| 12 | + |
| 13 | +const proxyquire = pq.noPreserveCache(); |
| 14 | +chai.use(sinonChai); |
| 15 | +const expect = chai.expect; |
6 | 16 |
|
7 | 17 | describe("Releases API consumer", () => {
|
8 | 18 | const owner = "someowner";
|
@@ -176,3 +186,104 @@ describe("Release version ordering", () => {
|
176 | 186 | expect(versionCompare(createVersion(2, 1, 0, "alpha.1", "abcdef0"), createVersion(2, 1, 0, "alpha.1", "bcdef01"))).to.equal(0);
|
177 | 187 | });
|
178 | 188 | });
|
| 189 | + |
| 190 | +describe('Launcher path', () => { |
| 191 | + let sandbox: sinon.SinonSandbox; |
| 192 | + let warnSpy: sinon.SinonSpy; |
| 193 | + let logSpy: sinon.SinonSpy; |
| 194 | + let fsSpy: sinon.SinonSpy; |
| 195 | + let platformSpy: sinon.SinonSpy; |
| 196 | + |
| 197 | + let getExecutableFromDirectory: Function; |
| 198 | + |
| 199 | + let launcherThatExists = ''; |
| 200 | + |
| 201 | + beforeEach(() => { |
| 202 | + getExecutableFromDirectory = createModule().getExecutableFromDirectory; |
| 203 | + }); |
| 204 | + |
| 205 | + beforeEach(() => { |
| 206 | + sandbox.restore(); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should not warn with proper launcher name', async () => { |
| 210 | + launcherThatExists = 'codeql.exe'; |
| 211 | + const result = await getExecutableFromDirectory('abc'); |
| 212 | + expect(fsSpy).to.have.been.calledWith(`abc${path.sep}codeql.exe`); |
| 213 | + |
| 214 | + // correct launcher has been found, so alternate one not looked for |
| 215 | + expect(fsSpy).not.to.have.been.calledWith(`abc${path.sep}codeql.cmd`); |
| 216 | + |
| 217 | + // no warning message |
| 218 | + expect(warnSpy).not.to.have.been.calledWith(sinon.match.string); |
| 219 | + // No log message |
| 220 | + expect(logSpy).not.to.have.been.calledWith(sinon.match.string); |
| 221 | + expect(result).to.equal(`abc${path.sep}codeql.exe`); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should warn when using a hard-coded deprecated launcher name', async () => { |
| 225 | + launcherThatExists = 'codeql.cmd'; |
| 226 | + path.sep; |
| 227 | + const result = await getExecutableFromDirectory('abc'); |
| 228 | + expect(fsSpy).to.have.been.calledWith(`abc${path.sep}codeql.exe`); |
| 229 | + expect(fsSpy).to.have.been.calledWith(`abc${path.sep}codeql.cmd`); |
| 230 | + |
| 231 | + // Should have opened a warning message |
| 232 | + expect(warnSpy).to.have.been.calledWith(sinon.match.string); |
| 233 | + // No log message |
| 234 | + expect(logSpy).not.to.have.been.calledWith(sinon.match.string); |
| 235 | + expect(result).to.equal(`abc${path.sep}codeql.cmd`); |
| 236 | + }); |
| 237 | + |
| 238 | + it('should avoid warn when no launcher is found', async () => { |
| 239 | + launcherThatExists = 'xxx'; |
| 240 | + const result = await getExecutableFromDirectory('abc', false); |
| 241 | + expect(fsSpy).to.have.been.calledWith(`abc${path.sep}codeql.exe`); |
| 242 | + expect(fsSpy).to.have.been.calledWith(`abc${path.sep}codeql.cmd`); |
| 243 | + |
| 244 | + // no warning message |
| 245 | + expect(warnSpy).not.to.have.been.calledWith(sinon.match.string); |
| 246 | + // log message sent out |
| 247 | + expect(logSpy).not.to.have.been.calledWith(sinon.match.string); |
| 248 | + expect(result).to.equal(undefined); |
| 249 | + }); |
| 250 | + |
| 251 | + it('should warn when no launcher is found', async () => { |
| 252 | + launcherThatExists = 'xxx'; |
| 253 | + const result = await getExecutableFromDirectory('abc', true); |
| 254 | + expect(fsSpy).to.have.been.calledWith(`abc${path.sep}codeql.exe`); |
| 255 | + expect(fsSpy).to.have.been.calledWith(`abc${path.sep}codeql.cmd`); |
| 256 | + |
| 257 | + // no warning message |
| 258 | + expect(warnSpy).not.to.have.been.calledWith(sinon.match.string); |
| 259 | + // log message sent out |
| 260 | + expect(logSpy).to.have.been.calledWith(sinon.match.string); |
| 261 | + expect(result).to.equal(undefined); |
| 262 | + }); |
| 263 | + |
| 264 | + function createModule() { |
| 265 | + sandbox = sinon.createSandbox(); |
| 266 | + warnSpy = sandbox.spy(); |
| 267 | + logSpy = sandbox.spy(); |
| 268 | + // pretend that only the .cmd file exists |
| 269 | + fsSpy = sandbox.stub().callsFake(arg => arg.endsWith(launcherThatExists) ? true : false); |
| 270 | + platformSpy = sandbox.stub().returns('win32'); |
| 271 | + |
| 272 | + return proxyquire('../../distribution', { |
| 273 | + './helpers': { |
| 274 | + showAndLogWarningMessage: warnSpy |
| 275 | + }, |
| 276 | + './logging': { |
| 277 | + 'logger': { |
| 278 | + log: logSpy |
| 279 | + } |
| 280 | + }, |
| 281 | + 'fs-extra': { |
| 282 | + pathExists: fsSpy |
| 283 | + }, |
| 284 | + os: { |
| 285 | + platform: platformSpy |
| 286 | + } |
| 287 | + }); |
| 288 | + } |
| 289 | +}); |
0 commit comments