From a88ed19f9847d6b4b142fb31aa110ca9bf4f2753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sat, 30 Oct 2021 20:35:08 -0400 Subject: [PATCH 01/12] test: add unit test on git command error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index 9439245..4bfc879 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -52,6 +52,15 @@ function simulateCurrentGitLocalBranchIs(currentLocalBranch: string) { mySpawn.setDefault(mySpawn.simple(0 /* exit code */, currentLocalBranch /* stdout */)); } +function simulateThereIsNoLocalGitRepository() { + shellCommand.withArgs('git', ['branch', '--show-current'], sinon.match.any).callThrough(); + const mockSpawn = require('mock-spawn'); + const mySpawn = mockSpawn(); + require('child_process').spawn = mySpawn; + const gitOutputMessage = 'fatal: not a git repository (or any of the parent directories): .git'; + mySpawn.setDefault(mySpawn.simple(128 /* exit code */, gitOutputMessage /* stdout */)); +} + describe('sonar script', function () { timeout(this, 30000); @@ -94,6 +103,28 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, nock.cleanAll(); }); + it(` should fail when there is no local git repository`, async () => { + simulateSonarProjectAlreadyExists(); + simulateThereIsNoLocalGitRepository(); + + const loggerRecorder = new LoggerRecorder(); + const sonarScript = getSonarScript(null, loggerRecorder.logger); + + await expect(sonarScript.run()).to.be.rejectedWith( + Error, + 'Expected success codes were "0", but the process exited with "128".' + ); + + expect(loggerRecorder.recordedLogs) + .to.startWith(`info: Script "sonar" starting...\n`) + .and.to.contain('info: Executing: git branch,--show-current\n') + .and.to.endWith('error: Script "sonar" failed after 0 s with: Expected success codes were "0", but the process exited with "128".\n'); + + subScript.should.not.have.been.called; + + shellCommand.should.have.been.calledOnceWith('git', ['branch', '--show-current']); + }); + it(` should successfully analyze code when project already exists in Sonar.`, async () => { simulateSonarProjectAlreadyExists(); simulateCurrentGitLocalBranchIs('current-local-branch'); From 627971947ef9e78e98c7ca26209be19d6606623d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sat, 30 Oct 2021 21:29:59 -0400 Subject: [PATCH 02/12] refactor: review test and make it pass again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 1 - src/scripts/sonar.ts | 14 +++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index 4bfc879..1e14421 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -104,7 +104,6 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, }); it(` should fail when there is no local git repository`, async () => { - simulateSonarProjectAlreadyExists(); simulateThereIsNoLocalGitRepository(); const loggerRecorder = new LoggerRecorder(); diff --git a/src/scripts/sonar.ts b/src/scripts/sonar.ts index 04d6a2d..c9d975b 100644 --- a/src/scripts/sonar.ts +++ b/src/scripts/sonar.ts @@ -30,13 +30,6 @@ export class SonarScript extends SonarBaseScript { protected async main() { const {sonarHostUrl, sonarProjectKey} = this.getSonarProjectInformation(); - if (! await this.sonarProjectAlreadyExists(sonarProjectKey, sonarHostUrl)) { - this.logger.warn(`'${sonarProjectKey}' Sonar project does not yet exist on ${sonarHostUrl} ! Initializing it first...`); - await this.invokeScript(SonarInitScript, {}, {}); - } - - // npx sonar-scanner -Dsonar.branch.name=`git branch --show-current` -Dsonar.branch.target=develop - // TODO Geraud : extract method to determine current git branch let currentBranch = ''; await this.invokeShellCommand('git', ['branch', '--show-current'], { @@ -45,6 +38,13 @@ export class SonarScript extends SonarBaseScript { } }); + if (! await this.sonarProjectAlreadyExists(sonarProjectKey, sonarHostUrl)) { + this.logger.warn(`'${sonarProjectKey}' Sonar project does not yet exist on ${sonarHostUrl} ! Initializing it first...`); + await this.invokeScript(SonarInitScript, {}, {}); + } + + // npx sonar-scanner -Dsonar.branch.name=`git branch --show-current` -Dsonar.branch.target=develop + this.logger.info(`Analyzing current branch "${currentBranch}" source code...`); const args = [`-Dsonar.branch.name=${currentBranch}`]; From bbd94b5e062c1a6e9a0588f9189dba39713ccdd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sun, 31 Oct 2021 16:26:51 -0400 Subject: [PATCH 03/12] test: unit test++ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 61 ++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index 1e14421..f9bc256 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -32,11 +32,16 @@ const shellCommand = sandbox.stub(SonarScript.prototype, 'invokeShellCommand'); const subScript = sandbox.stub(SonarScript.prototype, 'invokeScript'); function getSonarScript(targetBranch: string, logger: {}): SonarScript { + let options = {}; + if (targetBranch) { + options = { + targetBranch + }; + } + return new SonarScript({ args: {}, - options: { - targetBranch - }, + options, program: sinon.stub() as any, command: sinon.stub() as any, ddash: sinon.stub() as any, @@ -124,25 +129,47 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, shellCommand.should.have.been.calledOnceWith('git', ['branch', '--show-current']); }); - it(` should successfully analyze code when project already exists in Sonar.`, async () => { - simulateSonarProjectAlreadyExists(); - simulateCurrentGitLocalBranchIs('current-local-branch'); + describe(' when project already exists in Sonar', () => { + beforeEach(() => { + simulateCurrentGitLocalBranchIs('current-local-branch'); + simulateSonarProjectAlreadyExists(); + }); - const loggerRecorder = new LoggerRecorder(); - const sonarScript = getSonarScript('develop', loggerRecorder.logger); + it(` should succeed when simple code analysis succeeds.`, async () => { + const loggerRecorder = new LoggerRecorder(); + const sonarScript = getSonarScript(undefined, loggerRecorder.logger); - await sonarScript.run(); + await sonarScript.run(); - expect(loggerRecorder.recordedLogs) - .to.startsWith('info: Script "sonar" starting...\n') - .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') - .and.to.endWith('info: Script "sonar" successful after 0 s\n'); + expect(loggerRecorder.recordedLogs) + .to.startsWith('info: Script "sonar" starting...\n') + .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') + .and.to.endWith('info: Script "sonar" successful after 0 s\n'); - subScript.should.not.have.been.called; + subScript.should.not.have.been.called; - shellCommand.should.have.been.calledTwice; - shellCommand.should.have.been.calledWith('git', ['branch', '--show-current']); - shellCommand.should.have.been.calledWithExactly(SONAR_SCANNER, ['-Dsonar.branch.name=current-local-branch', '-Dsonar.branch.target=develop']); + shellCommand.should.have.been.calledTwice; + shellCommand.should.have.been.calledWith('git', ['branch', '--show-current']); + shellCommand.should.have.been.calledWithExactly(SONAR_SCANNER, ['-Dsonar.branch.name=current-local-branch']); + }); + + it(` should succeed when code analysis against a target branch succeeds.`, async () => { + const loggerRecorder = new LoggerRecorder(); + const sonarScript = getSonarScript('develop', loggerRecorder.logger); + + await sonarScript.run(); + + expect(loggerRecorder.recordedLogs) + .to.startsWith('info: Script "sonar" starting...\n') + .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') + .and.to.endWith('info: Script "sonar" successful after 0 s\n'); + + subScript.should.not.have.been.called; + + shellCommand.should.have.been.calledTwice; + shellCommand.should.have.been.calledWith('git', ['branch', '--show-current']); + shellCommand.should.have.been.calledWithExactly(SONAR_SCANNER, ['-Dsonar.branch.name=current-local-branch', '-Dsonar.branch.target=develop']); + }); }); it(` should initialize Sonar project with a warning and then successfully analyze code when project does not yet exist in Sonar.`, async () => { From dedb8151047590dd3b2b50f17944770f36931c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sun, 31 Oct 2021 16:40:11 -0400 Subject: [PATCH 04/12] test: unit test++ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 61 +++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index f9bc256..e5b6bba 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -131,13 +131,13 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, describe(' when project already exists in Sonar', () => { beforeEach(() => { - simulateCurrentGitLocalBranchIs('current-local-branch'); simulateSonarProjectAlreadyExists(); + simulateCurrentGitLocalBranchIs('current-local-branch'); }); it(` should succeed when simple code analysis succeeds.`, async () => { const loggerRecorder = new LoggerRecorder(); - const sonarScript = getSonarScript(undefined, loggerRecorder.logger); + const sonarScript = getSonarScript(null, loggerRecorder.logger); await sonarScript.run(); @@ -172,28 +172,53 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, }); }); - it(` should initialize Sonar project with a warning and then successfully analyze code when project does not yet exist in Sonar.`, async () => { - simulateSonarProjectDoesNotYetExist(); - simulateCurrentGitLocalBranchIs('current-local-branch'); + describe(' when project already exists in Sonar', () => { + beforeEach(() => { + simulateSonarProjectDoesNotYetExist(); + simulateCurrentGitLocalBranchIs('current-local-branch'); + }); - const loggerRecorder = new LoggerRecorder(); - const sonarScript = getSonarScript('develop', loggerRecorder.logger); + it(` should initialize Sonar project with a warning and then successfully analyze code.`, async () => { + const loggerRecorder = new LoggerRecorder(); + const sonarScript = getSonarScript(null, loggerRecorder.logger); - await sonarScript.run(); + await sonarScript.run(); - expect(loggerRecorder.recordedLogs) - .to.startsWith('info: Script "sonar" starting...\n') - .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') - .and.to.endWith('info: Script "sonar" successful after 0 s\n'); + expect(loggerRecorder.recordedLogs) + .to.startsWith('info: Script "sonar" starting...\n') + .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') + .and.to.endWith('info: Script "sonar" successful after 0 s\n'); - expect(loggerRecorder.recordedLogs) - .to.contain("warn: 'my-test-project-key' Sonar project does not yet exist on https://example.com/sonar/ ! Initializing it first...\n"); + expect(loggerRecorder.recordedLogs) + .to.contain("warn: 'my-test-project-key' Sonar project does not yet exist on https://example.com/sonar/ ! Initializing it first...\n"); + + subScript.should.have.been.calledOnceWithExactly(SonarInitScript, {}, {}); + + shellCommand.should.have.been.calledTwice; + shellCommand.should.have.been.calledWith('git', ['branch', '--show-current']); + shellCommand.should.have.been.calledWithExactly(SONAR_SCANNER, ['-Dsonar.branch.name=current-local-branch']); + }); + + it(` should initialize Sonar project with a warning and then successfully analyze code against a target branch.`, async () => { + const loggerRecorder = new LoggerRecorder(); + const sonarScript = getSonarScript('develop', loggerRecorder.logger); - subScript.should.have.been.calledOnceWithExactly(SonarInitScript, {}, {}); + await sonarScript.run(); - shellCommand.should.have.been.calledTwice; - shellCommand.should.have.been.calledWith('git', ['branch', '--show-current']); - shellCommand.should.have.been.calledWithExactly(SONAR_SCANNER, ['-Dsonar.branch.name=current-local-branch', '-Dsonar.branch.target=develop']); + expect(loggerRecorder.recordedLogs) + .to.startsWith('info: Script "sonar" starting...\n') + .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') + .and.to.endWith('info: Script "sonar" successful after 0 s\n'); + + expect(loggerRecorder.recordedLogs) + .to.contain("warn: 'my-test-project-key' Sonar project does not yet exist on https://example.com/sonar/ ! Initializing it first...\n"); + + subScript.should.have.been.calledOnceWithExactly(SonarInitScript, {}, {}); + + shellCommand.should.have.been.calledTwice; + shellCommand.should.have.been.calledWith('git', ['branch', '--show-current']); + shellCommand.should.have.been.calledWithExactly(SONAR_SCANNER, ['-Dsonar.branch.name=current-local-branch', '-Dsonar.branch.target=develop']); + }); }); // TODO Geraud : add more test cases: From cee8c9e4aed28697eea4f549449a650551c8c0b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sun, 31 Oct 2021 16:50:48 -0400 Subject: [PATCH 05/12] test: unit test++ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index e5b6bba..f7c2a77 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -170,6 +170,29 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, shellCommand.should.have.been.calledWith('git', ['branch', '--show-current']); shellCommand.should.have.been.calledWithExactly(SONAR_SCANNER, ['-Dsonar.branch.name=current-local-branch', '-Dsonar.branch.target=develop']); }); + + it(` should fail when simple code analysis fails.`, async () => { + const loggerRecorder = new LoggerRecorder(); + const sonarScript = getSonarScript(null, loggerRecorder.logger); + + shellCommand.withArgs(SONAR_SCANNER).rejects(new Error(`An error occurred while calling ${SONAR_SCANNER}.`)) + + await expect(sonarScript.run()).to.be.rejectedWith( + Error, + `An error occurred while calling ${SONAR_SCANNER}.` + ); + + expect(loggerRecorder.recordedLogs) + .to.startsWith('info: Script "sonar" starting...\n') + .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') + .and.to.endWith(`error: Script "sonar" failed after 0 s with: An error occurred while calling ${SONAR_SCANNER}.\n`); + + subScript.should.not.have.been.called; + + shellCommand.should.have.been.calledTwice; + shellCommand.should.have.been.calledWith('git', ['branch', '--show-current']); + shellCommand.should.have.been.calledWithExactly(SONAR_SCANNER, ['-Dsonar.branch.name=current-local-branch']); + }); }); describe(' when project already exists in Sonar', () => { From 9bcc30c03b030bcf726f13180995186a421dfd51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sun, 31 Oct 2021 16:58:24 -0400 Subject: [PATCH 06/12] test: make sonar-scanner call succeed in several unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index f7c2a77..04982bf 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -139,6 +139,8 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, const loggerRecorder = new LoggerRecorder(); const sonarScript = getSonarScript(null, loggerRecorder.logger); + shellCommand.withArgs(SONAR_SCANNER).returns(0); + await sonarScript.run(); expect(loggerRecorder.recordedLogs) @@ -157,6 +159,8 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, const loggerRecorder = new LoggerRecorder(); const sonarScript = getSonarScript('develop', loggerRecorder.logger); + shellCommand.withArgs(SONAR_SCANNER).returns(0); + await sonarScript.run(); expect(loggerRecorder.recordedLogs) @@ -205,6 +209,8 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, const loggerRecorder = new LoggerRecorder(); const sonarScript = getSonarScript(null, loggerRecorder.logger); + shellCommand.withArgs(SONAR_SCANNER).returns(0); + await sonarScript.run(); expect(loggerRecorder.recordedLogs) @@ -226,6 +232,8 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, const loggerRecorder = new LoggerRecorder(); const sonarScript = getSonarScript('develop', loggerRecorder.logger); + shellCommand.withArgs(SONAR_SCANNER).returns(0); + await sonarScript.run(); expect(loggerRecorder.recordedLogs) From 725b7fd0978c5dde8c05dc9d2916115a0047aac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sun, 31 Oct 2021 17:18:17 -0400 Subject: [PATCH 07/12] test: regroup some test assertions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index 04982bf..daf67ef 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -215,12 +215,10 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, expect(loggerRecorder.recordedLogs) .to.startsWith('info: Script "sonar" starting...\n') + .and.to.contain("warn: 'my-test-project-key' Sonar project does not yet exist on https://example.com/sonar/ ! Initializing it first...\n") .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') .and.to.endWith('info: Script "sonar" successful after 0 s\n'); - expect(loggerRecorder.recordedLogs) - .to.contain("warn: 'my-test-project-key' Sonar project does not yet exist on https://example.com/sonar/ ! Initializing it first...\n"); - subScript.should.have.been.calledOnceWithExactly(SonarInitScript, {}, {}); shellCommand.should.have.been.calledTwice; @@ -238,12 +236,10 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, expect(loggerRecorder.recordedLogs) .to.startsWith('info: Script "sonar" starting...\n') + .and.to.contain("warn: 'my-test-project-key' Sonar project does not yet exist on https://example.com/sonar/ ! Initializing it first...\n") .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') .and.to.endWith('info: Script "sonar" successful after 0 s\n'); - expect(loggerRecorder.recordedLogs) - .to.contain("warn: 'my-test-project-key' Sonar project does not yet exist on https://example.com/sonar/ ! Initializing it first...\n"); - subScript.should.have.been.calledOnceWithExactly(SonarInitScript, {}, {}); shellCommand.should.have.been.calledTwice; From bce3bb156b0b5431630030b38f388c56653d702c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sun, 31 Oct 2021 17:18:29 -0400 Subject: [PATCH 08/12] test: unit test++ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index daf67ef..f65c351 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -246,6 +246,28 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, shellCommand.should.have.been.calledWith('git', ['branch', '--show-current']); shellCommand.should.have.been.calledWithExactly(SONAR_SCANNER, ['-Dsonar.branch.name=current-local-branch', '-Dsonar.branch.target=develop']); }); + + it(` should fail when Sonar project initialization fails.`, async () => { + const loggerRecorder = new LoggerRecorder(); + const sonarScript = getSonarScript(null, loggerRecorder.logger); + + subScript.withArgs(SonarInitScript).rejects(new Error('An error occurred while calling sonar-init sub-script.')) + + await expect(sonarScript.run()).to.be.rejectedWith( + Error, + 'An error occurred while calling sonar-init sub-script.' + ); + + expect(loggerRecorder.recordedLogs) + .to.startsWith('info: Script "sonar" starting...\n') + .and.to.contain("warn: 'my-test-project-key' Sonar project does not yet exist on https://example.com/sonar/ ! Initializing it first...\n") + .and.to.endWith('error: Script "sonar" failed after 0 s with: An error occurred while calling sonar-init sub-script.\n') + .and.to.not.contain('info: Analyzing current branch "current-local-branch" source code...\n') + + subScript.should.have.been.calledOnceWithExactly(SonarInitScript, {}, {}); + + shellCommand.should.have.been.calledOnceWith('git', ['branch', '--show-current']); + }); }); // TODO Geraud : add more test cases: From c5faf2fd0974b4eeb63a921207e4734aac7d7278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sun, 31 Oct 2021 17:34:40 -0400 Subject: [PATCH 09/12] test: unit test++ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index f65c351..b0f4f46 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -268,6 +268,31 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, shellCommand.should.have.been.calledOnceWith('git', ['branch', '--show-current']); }); + + it(` should fail when code analysis fails after project initialization.`, async () => { + const loggerRecorder = new LoggerRecorder(); + const sonarScript = getSonarScript(null, loggerRecorder.logger); + + subScript.withArgs(SonarInitScript).returns(0); + shellCommand.withArgs(SONAR_SCANNER).rejects(new Error('An error occurred while analyzing source code.')); + + await expect(sonarScript.run()).to.be.rejectedWith( + Error, + 'An error occurred while analyzing source code.' + ); + + expect(loggerRecorder.recordedLogs) + .to.startsWith('info: Script "sonar" starting...\n') + .and.to.contain("warn: 'my-test-project-key' Sonar project does not yet exist on https://example.com/sonar/ ! Initializing it first...\n") + .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') + .and.to.endWith('error: Script "sonar" failed after 0 s with: An error occurred while analyzing source code.\n') + + subScript.should.have.been.calledOnceWithExactly(SonarInitScript, {}, {}); + + shellCommand.should.have.been.calledTwice; + shellCommand.should.have.been.calledWith('git', ['branch', '--show-current']); + shellCommand.should.have.been.calledWithExactly(SONAR_SCANNER, ['-Dsonar.branch.name=current-local-branch']); + }); }); // TODO Geraud : add more test cases: From d0b3bd88f6ae235e0f692b01d085b249ee721d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sun, 31 Oct 2021 17:35:19 -0400 Subject: [PATCH 10/12] test: review unit test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index b0f4f46..4e2a932 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -179,17 +179,17 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, const loggerRecorder = new LoggerRecorder(); const sonarScript = getSonarScript(null, loggerRecorder.logger); - shellCommand.withArgs(SONAR_SCANNER).rejects(new Error(`An error occurred while calling ${SONAR_SCANNER}.`)) + shellCommand.withArgs(SONAR_SCANNER).rejects(new Error('An error occurred while analyzing source code.')) await expect(sonarScript.run()).to.be.rejectedWith( Error, - `An error occurred while calling ${SONAR_SCANNER}.` + 'An error occurred while analyzing source code.' ); expect(loggerRecorder.recordedLogs) .to.startsWith('info: Script "sonar" starting...\n') .and.to.contain('info: Analyzing current branch "current-local-branch" source code...\n') - .and.to.endWith(`error: Script "sonar" failed after 0 s with: An error occurred while calling ${SONAR_SCANNER}.\n`); + .and.to.endWith(`error: Script "sonar" failed after 0 s with: An error occurred while analyzing source code.\n`); subScript.should.not.have.been.called; From fff189c90286fb526ddda0dfc81c7655e3e0d24e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sun, 31 Oct 2021 17:35:42 -0400 Subject: [PATCH 11/12] test: fix unit test group description MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index 4e2a932..1b4020b 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -199,7 +199,7 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, }); }); - describe(' when project already exists in Sonar', () => { + describe(' when project does not yet exist in Sonar', () => { beforeEach(() => { simulateSonarProjectDoesNotYetExist(); simulateCurrentGitLocalBranchIs('current-local-branch'); From c9d7eb3d1cc19f3e35b648034974cf7feffd8bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9raud=20Lac?= Date: Sun, 31 Oct 2021 17:36:23 -0400 Subject: [PATCH 12/12] test: todo-- MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Géraud Lac --- src/scripts/sonar.test.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/scripts/sonar.test.ts b/src/scripts/sonar.test.ts index 1b4020b..79a1bde 100644 --- a/src/scripts/sonar.test.ts +++ b/src/scripts/sonar.test.ts @@ -295,11 +295,6 @@ error: Script "sonar" failed after 0 s with: ENOENT: no such file or directory, }); }); - // TODO Geraud : add more test cases: - // - when git branch fails - // - when passing no target branch - // - when sonar project does not yet exists - }); });