UNPKG

@villedemontreal/scripting

Version:
133 lines 8.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable @typescript-eslint/no-misused-promises */ /* eslint-disable @typescript-eslint/ban-types */ /* eslint-disable no-console */ /* eslint-disable max-lines-per-function */ /* eslint-disable prettier/prettier */ const chai_1 = require("chai"); const fs = require("fs-extra"); const mocha_1 = require("mocha"); const sinon = require("sinon"); const sonarTestUtils_1 = require("../../src/utils/sonarTestUtils"); const testingUtils_1 = require("../../src/utils/testingUtils"); const sonar_1 = require("./sonar"); const sonarInit_1 = require("./sonarInit"); const nock = require('nock'); const chai = require('chai'); chai.should(); chai.use(require('chai-as-promised')); chai.use(require('sinon-chai')); chai.use(require('chai-string')); const sandbox = sinon.createSandbox(); function getSonarInitScript(logger) { return new sonarInit_1.SonarInitScript({ args: {}, options: {}, program: sinon.stub(), command: sinon.stub(), ddash: sinon.stub(), logger: logger, }); } const validPropertyFiles = [ './src/utils/test-sonar-project_url-with-trailing-slash.properties', './src/utils/test-sonar-project_url-without-trailing-slash.properties', ]; (0, mocha_1.describe)('sonar-init script', function () { (0, testingUtils_1.timeout)(this, 30000); before(() => { (0, testingUtils_1.setTestingConfigs)(); }); (0, mocha_1.it)(` should fail when sonar-project.properties is missing`, async () => { const loggerRecorder = new sonarTestUtils_1.LoggerRecorder(); const sonarInitScript = getSonarInitScript(loggerRecorder.logger); await (0, chai_1.expect)(sonarInitScript.run()).to.be.rejectedWith(Error, "ENOENT: no such file or directory, open 'sonar-project.properties'"); (0, chai_1.expect)(loggerRecorder.recordedLogs).to.equal(`info: Script "sonar-init" starting... error: Script "sonar-init" failed after 0 s with: ENOENT: no such file or directory, open 'sonar-project.properties' `); }); validPropertyFiles.forEach((propertyFile) => { // eslint-disable-next-line @typescript-eslint/require-await (0, mocha_1.describe)(` when using "${propertyFile}" valid property file`, async () => { before(async () => { await fs.copyFile(propertyFile, './sonar-project.properties'); }); after(async () => { await fs.unlink('./sonar-project.properties'); }); afterEach(() => { nock.cleanAll(); sandbox.restore(); }); (0, mocha_1.it)(` should skip sonar project initialization with a warning when it does already exist.`, async () => { (0, sonarTestUtils_1.simulateSonarProjectAlreadyExists)(); // @ts-ignore const shellCommand = sandbox.spy(sonarInit_1.SonarInitScript.prototype, 'invokeShellCommand'); const loggerRecorder = new sonarTestUtils_1.LoggerRecorder(); const sonarInitScript = getSonarInitScript(loggerRecorder.logger); await sonarInitScript.run(); chai_1.assert.isTrue(nock.isDone(), `There are remaining expected HTTP calls: ${nock.pendingMocks().toString()}`); (0, chai_1.expect)(loggerRecorder.recordedLogs) .to.startWith('info: Script "sonar-init" starting...\n') .and.to.contain("info: Initializing 'my-test-project-key' Sonar project...\n") .and.to.contain("warn: 'my-test-project-key' Sonar project already exists at https://example.com/sonar/dashboard?id=my-test-project-key ! Skipping sonar initialization...\n") .and.to.endWith('info: Script "sonar-init" successful after 0 s\n'); // @ts-ignore shellCommand.should.not.have.been.called; }); (0, mocha_1.it)(` should initialize sonar project when it does not yet exist.`, async () => { (0, sonarTestUtils_1.simulateSonarProjectDoesNotYetExist)(); const loggerRecorder = new sonarTestUtils_1.LoggerRecorder(); const sonarInitScript = getSonarInitScript(loggerRecorder.logger); await sonarInitScript.run(); chai_1.assert.isTrue(nock.isDone(), `There are remaining expected HTTP calls: ${nock.pendingMocks().toString()}`); (0, chai_1.expect)(loggerRecorder.recordedLogs) .to.startWith('info: Script "sonar-init" starting...\n') .and.to.contain("info: Initializing 'my-test-project-key' Sonar project...\n") .and.to.contain("info: 'my-test-project-key' Sonar project successfully initialized, and available at https://example.com/sonar/dashboard?id=my-test-project-key\n") .and.to.endWith('info: Script "sonar-init" successful after 0 s\n'); (0, chai_1.expect)(loggerRecorder.recordedLogs).to.not.contain('warn'); }); (0, mocha_1.it)(` should fail when sonar project initialization fails.`, async () => { (0, sonarTestUtils_1.simulateSonarProjectDoesNotYetExist)(); // @ts-ignore const shellCommand = sandbox.stub(sonarInit_1.SonarInitScript.prototype, 'invokeShellCommand'); shellCommand .withArgs(sonar_1.SONAR_SCANNER) .rejects(new Error('An error occured while analyzing code.')); const loggerRecorder = new sonarTestUtils_1.LoggerRecorder(); const sonarInitScript = getSonarInitScript(loggerRecorder.logger); await (0, chai_1.expect)(sonarInitScript.run()).to.be.rejectedWith(Error, 'An error occured while analyzing code.'); chai_1.assert.isTrue(nock.isDone(), `There are remaining expected HTTP calls: ${nock.pendingMocks().toString()}`); (0, chai_1.expect)(loggerRecorder.recordedLogs) .to.startWith('info: Script "sonar-init" starting...\n') .and.to.contain("info: Initializing 'my-test-project-key' Sonar project...\n") .and.to.endWith('error: Script "sonar-init" failed after 0 s with: An error occured while analyzing code.\n'); (0, chai_1.expect)(loggerRecorder.recordedLogs).to.not.contain('warn'); shellCommand.should.have.been.calledOnceWithExactly(sonar_1.SONAR_SCANNER, []); }); (0, mocha_1.it)(` should fail when sonar server is not found.`, async () => { (0, sonarTestUtils_1.simulateSonarServerIsNotFound)(); // @ts-ignore const shellCommand = sandbox.spy(sonarInit_1.SonarInitScript.prototype, 'invokeShellCommand'); const loggerRecorder = new sonarTestUtils_1.LoggerRecorder(); const sonarInitScript = getSonarInitScript(loggerRecorder.logger); await (0, chai_1.expect)(sonarInitScript.run()).to.be.rejectedWith(Error, 'Not Found'); chai_1.assert.isTrue(nock.isDone(), `There are remaining expected HTTP calls: ${nock.pendingMocks().toString()}`); (0, chai_1.expect)(loggerRecorder.recordedLogs) .to.startWith('info: Script "sonar-init" starting...\n') .and.to.contain("info: Initializing 'my-test-project-key' Sonar project...\n") .and.to.contain.oneOf([ 'error: "https://example.com/sonar/" Sonar server is not reachable.', 'error: "https://example.com/sonar" Sonar server is not reachable.', ]) .and.to.endWith('error: Script "sonar-init" failed after 0 s with: Not Found\n'); (0, chai_1.expect)(loggerRecorder.recordedLogs).to.not.contain('warn'); shellCommand.should.not.have.been.called; }); }); }); }); //# sourceMappingURL=sonarInit.test.js.map