UNPKG

@villedemontreal/scripting

Version:
129 lines 8.17 kB
"use strict"; /* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable prettier/prettier */ /* eslint-disable @typescript-eslint/no-require-imports */ Object.defineProperty(exports, "__esModule", { value: true }); const chai = require("chai"); const chai_1 = require("chai"); const chai_as_promised_1 = require("chai-as-promised"); const sinon_chai_1 = require("sinon-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'); chai.use(chai_as_promised_1.default); chai.use(sinon_chai_1.default); 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, '"sonar-project.properties" file does not exist!'); (0, chai_1.expect)(loggerRecorder.recordedLogs).to.equal(`info: Script "sonar-init" starting... error: Script "sonar-init" failed after 0 s with: "sonar-project.properties" file does not exist! `); }); validPropertyFiles.forEach((propertyFile) => { (0, mocha_1.describe)(` when using "${propertyFile}" valid property file`, function () { 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.satisfy((s) => s.startsWith('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.satisfy((s) => s.endsWith('info: Script "sonar-init" successful after 0 s\n')); // @ts-ignore (0, chai_1.expect)(shellCommand).to.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.satisfy((s) => s.startsWith('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.satisfy((s) => s.endsWith('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.satisfy((s) => s.startsWith('info: Script "sonar-init" starting...\n')) .and.to.contain("info: Initializing 'my-test-project-key' Sonar project...\n") .and.to.satisfy((s) => s.endsWith('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'); (0, chai_1.expect)(shellCommand).to.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.satisfy((s) => s.startsWith('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.satisfy((s) => s.endsWith('error: Script "sonar-init" failed after 0 s with: Not Found\n')); (0, chai_1.expect)(loggerRecorder.recordedLogs).to.not.contain('warn'); (0, chai_1.expect)(shellCommand).to.not.have.been.called; }); }); }); }); //# sourceMappingURL=sonarInit.test.js.map