diff options
author | Janos Gyerik <janos.gyerik@sonarsource.com> | 2017-08-15 17:06:22 +0200 |
---|---|---|
committer | Janos Gyerik <janos.gyerik@sonarsource.com> | 2017-09-12 10:59:56 +0200 |
commit | cecf0eb983899311c4f302e90b4425272df40971 (patch) | |
tree | b246e47e0a408db6a0ea61919c6bafe23ed24741 | |
parent | 6533563053cceb42e926466b8d4755d5a5ec6293 (diff) | |
download | sonarqube-cecf0eb983899311c4f302e90b4425272df40971.tar.gz sonarqube-cecf0eb983899311c4f302e90b4425272df40971.zip |
Add medium test for branches (#2359)
3 files changed, 243 insertions, 96 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java index e23ba7b4b49..641b75de6af 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java @@ -37,6 +37,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.function.Consumer; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.io.FileUtils; import org.junit.rules.ExternalResource; @@ -72,8 +73,6 @@ import org.sonar.scanner.scan.BranchConfigurationLoader; import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; import org.sonarqube.ws.Rules.ListResponse.Rule; -import static org.mockito.Mockito.mock; - /** * Main utility class for writing scanner medium tests. * @@ -83,7 +82,8 @@ public class ScannerMediumTester extends ExternalResource { private static Path userHome = null; private Map<String, String> globalProperties = new HashMap<>(); private final FakeMetricsRepositoryLoader globalRefProvider = new FakeMetricsRepositoryLoader(); - private final FakeBranchConfigurationLoader projectBranchesProvider = new FakeBranchConfigurationLoader(); + private final FakeBranchConfigurationLoader branchConfigurationLoader = new FakeBranchConfigurationLoader(); + private final FakeBranchConfiguration branchConfiguration = new FakeBranchConfiguration(); private final FakeProjectRepositoriesLoader projectRefProvider = new FakeProjectRepositoriesLoader(); private final FakePluginInstaller pluginInstaller = new FakePluginInstaller(); private final FakeServerIssuesLoader serverIssues = new FakeServerIssuesLoader(); @@ -294,7 +294,7 @@ public class ScannerMediumTester extends ExternalResource { tester.globalRefProvider, tester.qualityProfiles, tester.rulesLoader, - tester.projectBranchesProvider, + tester.branchConfigurationLoader, tester.projectRefProvider, tester.activeRules, tester.serverIssues, @@ -389,10 +389,49 @@ public class ScannerMediumTester extends ExternalResource { } - private static class FakeBranchConfigurationLoader implements BranchConfigurationLoader { + private static class FakeBranchConfiguration implements BranchConfiguration { + + private BranchType branchType = BranchType.LONG; + private String branchName = ""; + private String branchTarget = ""; + + @Override + public BranchType branchType() { + return branchType; + } + + @CheckForNull + @Override + public String branchTarget() { + return branchTarget; + } + + @CheckForNull + @Override + public String branchName() { + return branchName; + } + } + + public ScannerMediumTester setBranchType(BranchConfiguration.BranchType branchType) { + branchConfiguration.branchType = branchType; + return this; + } + + public ScannerMediumTester setBranchName(String branchName) { + this.branchConfiguration.branchName = branchName; + return this; + } + + public ScannerMediumTester setBranchTarget(String branchTarget) { + this.branchConfiguration.branchTarget = branchTarget; + return this; + } + + private class FakeBranchConfigurationLoader implements BranchConfigurationLoader { @Override public BranchConfiguration load(String projectKey, GlobalConfiguration settings) { - return mock(BranchConfiguration.class); + return branchConfiguration; } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java index 88020f9eb5d..bf4b0f4f310 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java @@ -22,18 +22,19 @@ package org.sonar.scanner.mediumtest.branch; import com.google.common.collect.ImmutableMap; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Map; -import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.fs.internal.FileMetadata; import org.sonar.scanner.mediumtest.ScannerMediumTester; import org.sonar.scanner.mediumtest.TaskResult; +import org.sonar.scanner.protocol.output.ScannerReport; +import org.sonar.scanner.repository.FileData; +import org.sonar.scanner.scan.BranchConfiguration; import org.sonar.xoo.XooPlugin; import org.sonar.xoo.rule.XooRulesDefinition; @@ -41,111 +42,67 @@ import static org.assertj.core.api.Assertions.assertThat; public class BranchMediumTest { - @Rule - public TemporaryFolder temp = new TemporaryFolder(); + private File baseDir; + private final String relativePath = "HelloJava.xoo"; @Rule - public ExpectedException thrown = ExpectedException.none(); + public TemporaryFolder temp = new TemporaryFolder(); @Rule public ScannerMediumTester tester = new ScannerMediumTester() .registerPlugin("xoo", new XooPlugin()) + .addDefaultQProfile("xoo", "Sonar Way") .addRules(new XooRulesDefinition()) - // active a rule just to be sure that xoo files are published - .addActiveRule("xoo", "xoo:OneIssuePerFile", null, "One Issue Per File", null, null, null) - .addDefaultQProfile("xoo", "Sonar Way"); - - private File baseDir; - - private Map<String, String> commonProps; + .addActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo"); @Before public void prepare() throws IOException { - baseDir = temp.getRoot(); - - commonProps = ImmutableMap.<String, String>builder() - .put("sonar.task", "scan") - .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) - .put("sonar.projectKey", "com.foo.project") - .put("sonar.projectName", "Foo Project") - .put("sonar.projectVersion", "1.0-SNAPSHOT") - .put("sonar.projectDescription", "Description of Foo Project") - .put("sonar.sources", "src") - .build(); + baseDir = temp.newFolder(); + Path filepath = baseDir.toPath().resolve(relativePath); + Files.write(filepath, "xoooo".getBytes()); + + String md5sum = new FileMetadata() + .readMetadata(Files.newInputStream(filepath), StandardCharsets.UTF_8, relativePath) + .hash(); + tester.addFileData("sample", relativePath, new FileData(md5sum, null)); } @Test - public void scanProjectWithBranch() throws IOException { - File srcDir = new File(baseDir, "src"); - srcDir.mkdir(); - - File xooFile = new File(srcDir, "sample.xoo"); - FileUtils.write(xooFile, "Sample xoo\ncontent"); - - TaskResult result = tester.newTask() - .properties(ImmutableMap.<String, String>builder() - .putAll(commonProps) - .put("sonar.branch", "branch") - .build()) - .execute(); - - assertThat(result.inputFiles()).hasSize(1); - assertThat(result.inputFile("src/sample.xoo").key()).isEqualTo("com.foo.project:src/sample.xoo"); - - DefaultInputFile inputfile = (DefaultInputFile) result.inputFile("src/sample.xoo"); - assertThat(result.getReportReader().readComponent(inputfile.batchId()).getPath()).isEqualTo("src/sample.xoo"); - - assertThat(result.getReportReader().readMetadata().getDeprecatedBranch()).isEqualTo("branch"); - - result = tester.newTask() - .properties(ImmutableMap.<String, String>builder() - .putAll(commonProps) - .put("sonar.branch", "") - .build()) - .execute(); - - assertThat(result.inputFiles()).hasSize(1); - assertThat(result.inputFile("src/sample.xoo").key()).isEqualTo("com.foo.project:src/sample.xoo"); + public void should_skip_report_for_unchanged_files_in_short_branch() { + // sanity check, normally report gets generated + TaskResult result = getResult(tester); + assertThat(getResult(tester).getReportComponent(result.inputFile(relativePath).key())).isNotNull(); + + // file is skipped for short branches (no report, no coverage, no duplications) + TaskResult result2 = getResult(tester.setBranchType(BranchConfiguration.BranchType.SHORT)); + assertThat(result2.getReportComponent(result2.inputFile(relativePath).key())).isNull(); } @Test - public void scanMultiModuleWithBranch() throws IOException { - Path srcDir = baseDir.toPath().resolve("moduleA").resolve("src"); - Files.createDirectories(srcDir); - - File xooFile = new File(srcDir.toFile(), "sample.xoo"); - FileUtils.write(xooFile, "Sample xoo\ncontent"); - - TaskResult result = tester.newTask() - .properties(ImmutableMap.<String, String>builder() - .putAll(commonProps) - .put("sonar.branch", "branch") - .put("sonar.modules", "moduleA") - .build()) - .execute(); - - assertThat(result.inputFiles()).hasSize(1); - assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo"); - - // no branch in the report - DefaultInputFile inputfile = (DefaultInputFile) result.inputFile("moduleA/src/sample.xoo"); - assertThat(result.getReportReader().readComponent(inputfile.batchId()).getPath()).isEqualTo("src/sample.xoo"); - - // no branch in InputModule's key or in report - assertThat(result.getReportComponent("com.foo.project:moduleA").getKey()).isEqualTo("com.foo.project:moduleA"); - - assertThat(result.getReportReader().readMetadata().getDeprecatedBranch()).isEqualTo("branch"); + public void verify_metadata() { + String branchName = "feature"; + String branchTarget = "branch-1.x"; + + TaskResult result = getResult(tester + .setBranchName(branchName) + .setBranchTarget(branchTarget) + .setBranchType(BranchConfiguration.BranchType.SHORT)); + + ScannerReport.Metadata metadata = result.getReportReader().readMetadata(); + assertThat(metadata.getBranchName()).isEqualTo(branchName); + assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.SHORT); + assertThat(metadata.getMergeBranchName()).isEqualTo(branchTarget); + } - result = tester.newTask() + private TaskResult getResult(ScannerMediumTester tester) { + return tester + .newTask() .properties(ImmutableMap.<String, String>builder() - .putAll(commonProps) - .put("sonar.branch", "") - .put("sonar.modules", "moduleA") + .put("sonar.task", "scan") + .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) + .put("sonar.projectKey", "sample") + .put("sonar.sources", ".") .build()) .execute(); - - assertThat(result.inputFiles()).hasSize(1); - assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo"); } - } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/DeprecatedBranchMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/DeprecatedBranchMediumTest.java new file mode 100644 index 00000000000..bedf74bb43a --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/DeprecatedBranchMediumTest.java @@ -0,0 +1,151 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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 org.sonar.scanner.mediumtest.branch; + +import com.google.common.collect.ImmutableMap; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.scanner.mediumtest.ScannerMediumTester; +import org.sonar.scanner.mediumtest.TaskResult; +import org.sonar.xoo.XooPlugin; +import org.sonar.xoo.rule.XooRulesDefinition; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DeprecatedBranchMediumTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Rule + public ScannerMediumTester tester = new ScannerMediumTester() + .registerPlugin("xoo", new XooPlugin()) + .addRules(new XooRulesDefinition()) + // active a rule just to be sure that xoo files are published + .addActiveRule("xoo", "xoo:OneIssuePerFile", null, "One Issue Per File", null, null, null) + .addDefaultQProfile("xoo", "Sonar Way"); + + private File baseDir; + + private Map<String, String> commonProps; + + @Before + public void prepare() throws IOException { + baseDir = temp.getRoot(); + + commonProps = ImmutableMap.<String, String>builder() + .put("sonar.task", "scan") + .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) + .put("sonar.projectKey", "com.foo.project") + .put("sonar.projectName", "Foo Project") + .put("sonar.projectVersion", "1.0-SNAPSHOT") + .put("sonar.projectDescription", "Description of Foo Project") + .put("sonar.sources", "src") + .build(); + } + + @Test + public void scanProjectWithBranch() throws IOException { + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFile = new File(srcDir, "sample.xoo"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + + TaskResult result = tester.newTask() + .properties(ImmutableMap.<String, String>builder() + .putAll(commonProps) + .put("sonar.branch", "branch") + .build()) + .execute(); + + assertThat(result.inputFiles()).hasSize(1); + assertThat(result.inputFile("src/sample.xoo").key()).isEqualTo("com.foo.project:src/sample.xoo"); + + DefaultInputFile inputfile = (DefaultInputFile) result.inputFile("src/sample.xoo"); + assertThat(result.getReportReader().readComponent(inputfile.batchId()).getPath()).isEqualTo("src/sample.xoo"); + + assertThat(result.getReportReader().readMetadata().getDeprecatedBranch()).isEqualTo("branch"); + + result = tester.newTask() + .properties(ImmutableMap.<String, String>builder() + .putAll(commonProps) + .put("sonar.branch", "") + .build()) + .execute(); + + assertThat(result.inputFiles()).hasSize(1); + assertThat(result.inputFile("src/sample.xoo").key()).isEqualTo("com.foo.project:src/sample.xoo"); + } + + @Test + public void scanMultiModuleWithBranch() throws IOException { + Path srcDir = baseDir.toPath().resolve("moduleA").resolve("src"); + Files.createDirectories(srcDir); + + File xooFile = new File(srcDir.toFile(), "sample.xoo"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + + TaskResult result = tester.newTask() + .properties(ImmutableMap.<String, String>builder() + .putAll(commonProps) + .put("sonar.branch", "branch") + .put("sonar.modules", "moduleA") + .build()) + .execute(); + + assertThat(result.inputFiles()).hasSize(1); + assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo"); + + // no branch in the report + DefaultInputFile inputfile = (DefaultInputFile) result.inputFile("moduleA/src/sample.xoo"); + assertThat(result.getReportReader().readComponent(inputfile.batchId()).getPath()).isEqualTo("src/sample.xoo"); + + // no branch in InputModule's key or in report + assertThat(result.getReportComponent("com.foo.project:moduleA").getKey()).isEqualTo("com.foo.project:moduleA"); + + assertThat(result.getReportReader().readMetadata().getDeprecatedBranch()).isEqualTo("branch"); + + result = tester.newTask() + .properties(ImmutableMap.<String, String>builder() + .putAll(commonProps) + .put("sonar.branch", "") + .put("sonar.modules", "moduleA") + .build()) + .execute(); + + assertThat(result.inputFiles()).hasSize(1); + assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo"); + } + +} |