Skip to content

Commit

Permalink
Add ITs for S4NET netcore versions (#1233)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-epure-sonarsource authored May 30, 2022
1 parent c8ab09f commit 3ee16c0
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* SonarScanner for .NET
* Copyright (C) 2016-2022 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package com.sonar.it.scanner.msbuild;

import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.locator.Location;

import java.nio.file.Paths;

public enum ScannerClassifier {
NETCORE_2_1("netcoreapp2.0"),
NETCORE_3_1("netcoreapp3.0"),
NET_5("net5.0"),
NET_FRAMEWORK_46("net46");

private final String classifier;

ScannerClassifier(String classifier) {
this.classifier = classifier;
}

public String toString() {
return classifier;
}

public String toZipName() {
return "sonarscanner-msbuild-" + classifier + ".zip";
}

public Location toLocation(String scannerLocation) {
return FileLocation.of(Paths.get(scannerLocation, toZipName()).toFile());
}

public boolean isDotNetCore() {
return !classifier.equals(NET_FRAMEWORK_46.classifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.ScannerForMSBuild;
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.util.Command;
import com.sonar.orchestrator.util.CommandExecutor;
import com.sonar.orchestrator.util.NetworkUtils;
import java.io.IOException;
import java.nio.file.LinkOption;
Expand Down Expand Up @@ -700,6 +702,35 @@ public void testCSharpSdk2() throws IOException {
validateCSharpSdk("CSharp.SDK.2.1");
}

@Test
public void testScannerNetCore21HasAnalysisWarning() throws IOException {
assumeFalse(TestUtils.getMsBuildPath(ORCHESTRATOR).toString().contains("2017")); // We can't run .NET Core SDK under VS 2017 CI context
Path projectDir = TestUtils.projectDir(temp, "CSharp.SDK.2.1");
BuildResult buildResult = runNetCoreBeginBuildAndEnd(projectDir, ScannerClassifier.NETCORE_2_1);

assertAnalysisWarning(buildResult, "From the 6th of July 2022, we will no longer release new Scanner for .NET versions that target .NET Core 2.1." +
" If you are using the .NET Core Global Tool you will need to use a supported .NET runtime environment." +
" For more information see https://community.sonarsource.com/t/54684");
}

@Test
public void testScannerNetCore31NoAnalysisWarning() throws IOException {
assumeFalse(TestUtils.getMsBuildPath(ORCHESTRATOR).toString().contains("2017")); // We can't run .NET Core SDK under VS 2017 CI context
Path projectDir = TestUtils.projectDir(temp, "CSharp.SDK.3.1");
BuildResult buildResult = runNetCoreBeginBuildAndEnd(projectDir, ScannerClassifier.NETCORE_3_1);

assertNoWarnings(buildResult);
}

@Test
public void testScannerNet5NoAnalysisWarnings() throws IOException {
assumeFalse(TestUtils.getMsBuildPath(ORCHESTRATOR).toString().contains("2017")); // We can't run .NET Core SDK under VS 2017 CI context
Path projectDir = TestUtils.projectDir(temp, "CSharp.SDK.5");
BuildResult buildResult = runNetCoreBeginBuildAndEnd(projectDir, ScannerClassifier.NET_5);

assertNoWarnings(buildResult);
}

@Test
public void testCSharpSdk3() throws IOException {
validateCSharpSdk("CSharp.SDK.3.1");
Expand Down Expand Up @@ -785,6 +816,13 @@ private void assertNoWarnings(BuildResult buildResult) {
assertThat(task.getWarningsList()).isEmpty();
}

// Verify an AnalysisWarning is raised inside the SQ GUI (on the project dashboard)
private void assertAnalysisWarning(BuildResult buildResult, String message) {
Ce.Task task = TestUtils.getAnalysisWarningsTask(ORCHESTRATOR, buildResult);
assertThat(task.getStatus()).isEqualTo(Ce.TaskStatus.SUCCESS);
assertThat(task.getWarningsList()).containsExactly(message);
}

private void runCSharpSharedFileWithOneProjectUsingProjectBaseDir(Function<Path, String> getProjectBaseDir)
throws IOException {
String folderName = "CSharpSharedFileWithOneProject";
Expand Down Expand Up @@ -828,6 +866,36 @@ private BuildResult runBeginBuildAndEndForStandardProject(String folderName, Str
return runBeginBuildAndEndForStandardProject(projectDir, projectName, setProjectBaseDirExplicitly, useNuGet);
}

private BuildResult runNetCoreBeginBuildAndEnd(Path projectDir, ScannerClassifier classifier) {
String token = TestUtils.getNewToken(ORCHESTRATOR);
String folderName = projectDir.getFileName().toString();
ScannerForMSBuild scanner = TestUtils.newScanner(ORCHESTRATOR, projectDir, classifier)
.addArgument("begin")
.setUseDotNetCore(Boolean.TRUE)
.setScannerVersion(TestUtils.developmentScannerVersion())
.setProjectKey(folderName)
.setProjectName(folderName)
.setProjectVersion("1.0")
.setProperty("sonar.sourceEncoding", "UTF-8")
.setProperty("sonar.login", token);

ORCHESTRATOR.executeBuild(scanner);

// build project
String[] arguments = new String[]{"build", folderName + ".sln"};
int status = CommandExecutor.create().execute(Command.create("dotnet")
.addArguments(arguments)
// verbosity level: change 'm' to 'd' for detailed logs
.addArguments("-v:m")
.addArgument("/warnaserror:AD0001")
.setDirectory(projectDir.toFile()), 5 * 60 * 1000);

assertThat(status).isZero();

TestUtils.runMSBuild(ORCHESTRATOR, projectDir, "/t:Restore,Rebuild", folderName + ".sln");
return TestUtils.executeEndStepAndDumpResults(ORCHESTRATOR, projectDir, folderName, token, classifier);
}

private BuildResult runBeginBuildAndEndForStandardProject(Path projectDir, String projectName, Boolean setProjectBaseDirExplicitly, Boolean useNuGet) throws IOException {
String token = TestUtils.getNewToken(ORCHESTRATOR);
String folderName = projectDir.getFileName().toString();
Expand Down
39 changes: 23 additions & 16 deletions its/src/test/java/com/sonar/it/scanner/msbuild/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.ScannerForMSBuild;
import com.sonar.orchestrator.http.HttpMethod;
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.locator.Location;
import com.sonar.orchestrator.locator.MavenLocation;
import com.sonar.orchestrator.util.Command;
Expand Down Expand Up @@ -76,51 +75,53 @@ public static String getScannerVersion(Orchestrator orchestrator) {
return orchestrator.getConfiguration().getString("scannerForMSBuild.version");
}

private static MavenLocation getScannerMavenLocation(String scannerVersion) {
private static MavenLocation getScannerMavenLocation(String scannerVersion, ScannerClassifier classifier) {
String groupId = "org.sonarsource.scanner.msbuild";
String artifactId = "sonar-scanner-msbuild";
return MavenLocation.builder()
.setGroupId(groupId)
.setArtifactId(artifactId)
.setVersion(scannerVersion)
.setClassifier("net46")
.setClassifier(classifier.toString())
.withPackaging("zip")
.build();
}

// https://github.com/SonarSource/sonar-scanner-msbuild/issues/1235
public static String developmentScannerVersion() {
// dummy version, needed by Orchestrator to use the dotnet core versions of the scanner
return "99";
}

public static ScannerForMSBuild newScanner(Orchestrator orchestrator, Path projectDir) {
return newScanner(orchestrator, projectDir, ScannerClassifier.NET_FRAMEWORK_46);
}

public static ScannerForMSBuild newScanner(Orchestrator orchestrator, Path projectDir, ScannerClassifier classifier) {
String scannerVersion = getScannerVersion(orchestrator);

Location scannerLocation;
if (scannerVersion != null) {
LOG.info("Using Scanner for MSBuild " + scannerVersion);
scannerLocation = getScannerMavenLocation(scannerVersion);
scannerLocation = getScannerMavenLocation(scannerVersion, classifier);
}
else {
String scannerLocationEnv = System.getenv("SCANNER_LOCATION");
if(scannerLocationEnv != null) {
LOG.info("Using Scanner for MSBuild specified by %SCANNER_LOCATION%: " + scannerLocationEnv);
Path scannerPath = Paths.get(scannerLocationEnv, "sonarscanner-msbuild-net46.zip");
scannerLocation = FileLocation.of(scannerPath.toFile());
scannerLocation = classifier.toLocation(scannerLocationEnv);
}
else {
// run locally
LOG.info("Using Scanner for MSBuild from the local build");
scannerLocation = FindScannerZip("../build");
scannerLocation = classifier.toLocation("../build");
}
}
LOG.info("Scanner location: " + scannerLocation);
return ScannerForMSBuild.create(projectDir.toFile())
.setScannerLocation(scannerLocation);
}

private static Location FindScannerZip(String folderPath){
Path root = Paths.get(folderPath);
Path scannerZip = Paths.get(folderPath + "/sonarscanner-msbuild-net46.zip");
Location scannerLocation = FileLocation.of(scannerZip.toFile());
return scannerLocation;
}

public static void reset(Orchestrator orchestrator) {
// We add one day to ensure that today's entries are deleted.
Instant instant = Instant.now().plus(1, ChronoUnit.DAYS);
Expand Down Expand Up @@ -322,8 +323,14 @@ static void dumpAllIssues(Orchestrator orchestrator) {
}
}

static BuildResult executeEndStepAndDumpResults(Orchestrator orchestrator, Path projectDir, String projectKey, String token){
BuildResult result = orchestrator.executeBuild(TestUtils.newScanner(orchestrator, projectDir)
static BuildResult executeEndStepAndDumpResults(Orchestrator orchestrator, Path projectDir, String projectKey, String token) {
return executeEndStepAndDumpResults(orchestrator, projectDir, projectKey, token, ScannerClassifier.NET_FRAMEWORK_46);
}

static BuildResult executeEndStepAndDumpResults(Orchestrator orchestrator, Path projectDir, String projectKey, String token, ScannerClassifier classifier){
BuildResult result = orchestrator.executeBuild(TestUtils.newScanner(orchestrator, projectDir, classifier)
.setUseDotNetCore(classifier.isDotNetCore())
.setScannerVersion(developmentScannerVersion())
.addArgument("end")
.setProperty("sonar.login", token));

Expand Down

0 comments on commit 3ee16c0

Please sign in to comment.