Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse settings from Azure Devops Extension #1246

Merged
merged 13 commits into from Jun 9, 2022
Expand Up @@ -227,28 +227,28 @@ public void testExcludedAndTest_AnalyzeTestProject() throws Exception {
int expectedTestProjectIssues = isTestProjectSupported() ? 1 : 0;
String token = TestUtils.getNewToken(ORCHESTRATOR);
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Path projectDir = TestUtils.projectDir(temp, "ExcludedTest");
ScannerForMSBuild build = createScannerForMSBuild(projectDir, token)
ScannerForMSBuild build = TestUtils.newScannerBegin(ORCHESTRATOR, "ExcludedTest_False", projectDir, token, ScannerClassifier.NET_FRAMEWORK_46)
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
// don't exclude test projects
.setProperty("sonar.dotnet.excludeTestProjects", "false");
testExcludedAndTest(build, projectDir, token, expectedTestProjectIssues, false);
testExcludedAndTest(build, "ExcludedTest_False", projectDir, token, expectedTestProjectIssues, false);
}

@Test
public void testExcludedAndTest_ExcludeTestProject() throws Exception {
String token = TestUtils.getNewToken(ORCHESTRATOR);
Path projectDir = TestUtils.projectDir(temp, "ExcludedTest");
ScannerForMSBuild build = createScannerForMSBuild(projectDir, token)
ScannerForMSBuild build = TestUtils.newScannerBegin(ORCHESTRATOR, "ExcludedTest_True", projectDir, token, ScannerClassifier.NET_FRAMEWORK_46)
// exclude test projects
.setProperty("sonar.dotnet.excludeTestProjects", "true");
testExcludedAndTest(build, projectDir, token, 0, false);
testExcludedAndTest(build, "ExcludedTest_True", projectDir, token, 0, false);
}

@Test
csaba-sagi-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
public void testExcludedAndTest_simulateAzureDevopsEnvironmentSetting_ExcludeTestProject() throws Exception {
String token = TestUtils.getNewToken(ORCHESTRATOR);
Path projectDir = TestUtils.projectDir(temp, "ExcludedTest");
ScannerForMSBuild build = createScannerForMSBuild(projectDir, token);
testExcludedAndTest(build, projectDir, token, 0, true);
ScannerForMSBuild build = TestUtils.newScannerBegin(ORCHESTRATOR, "ExcludedTest_True_FromAzureDevOps", projectDir, token, ScannerClassifier.NET_FRAMEWORK_46);
testExcludedAndTest(build, "ExcludedTest_True_FromAzureDevOps", projectDir, token, 0, true);
}

@Test
Expand Down Expand Up @@ -949,16 +949,6 @@ private BuildResult runBeginBuildAndEndForStandardProject(Path projectDir, Strin
return TestUtils.executeEndStepAndDumpResults(ORCHESTRATOR, projectDir, folderName, token);
}

private ScannerForMSBuild createScannerForMSBuild(Path projectDir, String token) {
return TestUtils.newScanner(ORCHESTRATOR, projectDir)
.addArgument("begin")
.setProjectKey(PROJECT_KEY)
.setProjectName("excludedAndTest")
.setProjectVersion("1.0")
.setProperty("sonar.projectBaseDir", projectDir.toAbsolutePath().toString())
.setProperty("sonar.login", token);
}

private void validateRazorProject(String projectName) throws IOException {
String localProjectKey = PROJECT_KEY + projectName;
ORCHESTRATOR.getServer().provisionProject(localProjectKey, projectName);
Expand Down Expand Up @@ -991,19 +981,23 @@ private void validateRazorProject(String projectName) throws IOException {
assertThat(TestUtils.getMeasureAsInteger(localProjectKey, "files", ORCHESTRATOR)).isEqualTo(2);
}

private void testExcludedAndTest(ScannerForMSBuild build, Path projectDir, String token, int expectedTestProjectIssues, boolean simulateAzureDevopsEnvironment) {
String normalProjectKey = TestUtils.hasModules(ORCHESTRATOR) ? "my.project:my.project:B93B287C-47DB-4406-9EAB-653BCF7D20DC" : "my.project:Normal";
String testProjectKey = TestUtils.hasModules(ORCHESTRATOR) ? "my.project:my.project:2DC588FC-16FB-42F8-9FDA-193852E538AF" : "my.project:Test";
private void testExcludedAndTest(ScannerForMSBuild build, String projectKeyName, Path projectDir, String token, int expectedTestProjectIssues, boolean simulateAzureDevopsEnvironment) {
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
String normalProjectKey = TestUtils.hasModules(ORCHESTRATOR)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #1248 to remove hasModules()

outside the scope of this PR

? String.format("%1$s:%1$s:B93B287C-47DB-4406-9EAB-653BCF7D20DC", projectKeyName)
: String.format("%1$s:Normal", projectKeyName);
String testProjectKey = TestUtils.hasModules(ORCHESTRATOR)
? String.format("%1$s:%1$s:2DC588FC-16FB-42F8-9FDA-193852E538AF", projectKeyName)
: String.format("%1$s:Test", projectKeyName);

ORCHESTRATOR.getServer().restoreProfile(FileLocation.of("projects/ProjectUnderTest/TestQualityProfile.xml"));
ORCHESTRATOR.getServer().provisionProject(PROJECT_KEY, "excludedAndTest");
ORCHESTRATOR.getServer().associateProjectToQualityProfile(PROJECT_KEY, "cs", "ProfileForTest");
ORCHESTRATOR.getServer().provisionProject(projectKeyName, projectKeyName);
ORCHESTRATOR.getServer().associateProjectToQualityProfile(projectKeyName, "cs", "ProfileForTest");

ORCHESTRATOR.executeBuild(build);

TestUtils.runMSBuild(ORCHESTRATOR, projectDir, simulateAzureDevopsEnvironment, "/t:Rebuild");

BuildResult result = TestUtils.executeEndStepAndDumpResults(ORCHESTRATOR, projectDir, PROJECT_KEY, token);
BuildResult result = TestUtils.executeEndStepAndDumpResults(ORCHESTRATOR, projectDir, projectKeyName, token);
assertTrue(result.isSuccess());

// Dump debug info
Expand All @@ -1022,7 +1016,7 @@ private void testExcludedAndTest(ScannerForMSBuild build, Path projectDir, Strin

// excluded project doesn't exist in SonarQube

assertThat(TestUtils.getMeasureAsInteger(PROJECT_KEY, "ncloc", ORCHESTRATOR)).isEqualTo(45);
assertThat(TestUtils.getMeasureAsInteger(projectKeyName, "ncloc", ORCHESTRATOR)).isEqualTo(45);
assertThat(TestUtils.getMeasureAsInteger(normalProjectKey, "ncloc", ORCHESTRATOR)).isEqualTo(45);
assertThat(TestUtils.getMeasureAsInteger(testProjectKey, "ncloc", ORCHESTRATOR)).isNull();
}
Expand Down
19 changes: 14 additions & 5 deletions its/src/test/java/com/sonar/it/scanner/msbuild/TestUtils.java
Expand Up @@ -97,6 +97,16 @@ public static ScannerForMSBuild newScanner(Orchestrator orchestrator, Path proje
return newScanner(orchestrator, projectDir, ScannerClassifier.NET_FRAMEWORK_46);
}

public static ScannerForMSBuild newScannerBegin(Orchestrator orchestrator, String projectKeyName, Path projectDir, String token, ScannerClassifier classifier) {
return TestUtils.newScanner(orchestrator, projectDir, classifier)
.addArgument("begin")
.setProjectKey(projectKeyName)
.setProjectName(projectKeyName)
.setProjectVersion("1.0")
.setProperty("sonar.projectBaseDir", projectDir.toAbsolutePath().toString())
.setProperty("sonar.login", token);
}

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

Expand Down Expand Up @@ -153,9 +163,9 @@ public static Path getCustomRoslynPlugin() {
throw new IllegalStateException(e);
}
if (jars.isEmpty()) {
throw new IllegalStateException("No jars found in " + customPluginDir.toString());
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalStateException("No jars found in " + customPluginDir);
} else if (jars.size() > 1) {
throw new IllegalStateException("Several jars found in " + customPluginDir.toString());
throw new IllegalStateException("Several jars found in " + customPluginDir);
}

LOG.info("TEST SETUP: custom plugin path = " + jars.get(0));
Expand Down Expand Up @@ -257,8 +267,7 @@ private static Path getNuGetPath(Orchestrator orch) {
String nugetPathStr = orch.getConfiguration().getString(NUGET_PATH, toolsFolder);
Path nugetPath = Paths.get(nugetPathStr).toAbsolutePath();
if (!Files.exists(nugetPath)) {
throw new IllegalStateException("Unable to find NuGet at '" + nugetPath.toString() +
"'. Please configure property '" + NUGET_PATH + "'");
throw new IllegalStateException("Unable to find NuGet at '" + nugetPath + "'. Please configure property '" + NUGET_PATH + "'");
}

LOG.info("TEST SETUP: nuget.exe path = " + nugetPath);
Expand Down Expand Up @@ -302,7 +311,7 @@ static Path getMsBuildPath(Orchestrator orch) {
+ "Studio\\2017\\Enterprise\\MSBuild\\15.0\\Bin\\MSBuild.exe"));
Path msBuildPath = Paths.get(msBuildPathStr).toAbsolutePath();
if (!Files.exists(msBuildPath)) {
throw new IllegalStateException("Unable to find MSBuild at " + msBuildPath.toString()
throw new IllegalStateException("Unable to find MSBuild at " + msBuildPath
+ ". Please configure property 'msbuild.path' or 'MSBUILD_PATH' environment variable to the full path to MSBuild.exe.");
}
return msBuildPath;
Expand Down