From 639e945dc03be60c38177b91674632558386da3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Caro?= Date: Mon, 5 Jun 2023 11:26:07 +0200 Subject: [PATCH] VSTS-301 Fix semver check for compatibility with SQ >= 10.0 (#251) --- commonv5/ts/sonarqube/TaskReport.ts | 2 +- .../ts/sonarqube/__tests__/TaskReport-test.ts | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/commonv5/ts/sonarqube/TaskReport.ts b/commonv5/ts/sonarqube/TaskReport.ts index b3b0a46e..4855e5e5 100644 --- a/commonv5/ts/sonarqube/TaskReport.ts +++ b/commonv5/ts/sonarqube/TaskReport.ts @@ -46,7 +46,7 @@ export default class TaskReport { let taskReportGlob: string; let taskReportGlobResult: string[]; - if (endpoint.type === EndpointType.SonarQube && serverVersion < semver.parse("7.2.0")) { + if (endpoint.type === EndpointType.SonarQube && semver.satisfies(serverVersion, "<7.2.0")) { tl.debug( "SonarQube version < 7.2.0 detected, falling back to default location(s) for report-task.txt file." ); diff --git a/commonv5/ts/sonarqube/__tests__/TaskReport-test.ts b/commonv5/ts/sonarqube/__tests__/TaskReport-test.ts index d8778ce3..a4e47f03 100644 --- a/commonv5/ts/sonarqube/__tests__/TaskReport-test.ts +++ b/commonv5/ts/sonarqube/__tests__/TaskReport-test.ts @@ -135,6 +135,37 @@ it("should find report files for SonarQube above 7.2.0", async () => { expect(tl.findMatch).toHaveBeenCalledWith("mock root search path", expectedSearchPath); }); +it.each(["10.0.0", "20.0.0"])( + "should find report files for SonarQube above %p", + async (version) => { + // using spyOn so we can reset the original behaviour + jest.spyOn(tl, "getVariable").mockImplementation(() => "mock root search path"); + jest.spyOn(tl, "findMatch").mockImplementation(() => ["path1", "path2"]); + + const endpoint = new Endpoint(EndpointType.SonarQube, null); + + const reportFiles = await TaskReport.findTaskFileReport(endpoint, new semver.SemVer(version)); + + expect(reportFiles.length).toBe(2); + expect(reportFiles[0]).toBe("path1"); + expect(reportFiles[1]).toBe("path2"); + + expect(tl.getVariable).toHaveBeenCalledTimes(2); + expect(tl.getVariable).toBeCalledWith("Agent.TempDirectory"); + + // Calculate the expected path to take account of different + // path separators in Windows/non-Windows + const expectedSearchPath = path.join( + "sonar", + tl.getVariable("Build.BuildNumber"), + "**", + "report-task.txt" + ); + expect(tl.findMatch).toHaveBeenCalledTimes(1); + expect(tl.findMatch).toHaveBeenCalledWith("mock root search path", expectedSearchPath); + } +); + it("should find report files for SonarQube below 7.2.0", async () => { // using spyOn so we can reset the original behaviour jest.spyOn(tl, "getVariable").mockImplementation(() => "mock root search path");