From: Julien HENRY Date: Fri, 28 Jul 2017 10:36:45 +0000 (+0200) Subject: SQSCANNER-43 Remove deprecated sonar-runner bat/sh scripts X-Git-Tag: 3.1.0.1141~19 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b0583c0fbddafdd013bd9289d0385beb1c299a17;p=sonar-scanner-cli.git SQSCANNER-43 Remove deprecated sonar-runner bat/sh scripts --- diff --git a/it/src/test/java/com/sonar/runner/it/DistributionTest.java b/it/src/test/java/com/sonar/runner/it/DistributionTest.java deleted file mode 100644 index 6bd2651..0000000 --- a/it/src/test/java/com/sonar/runner/it/DistributionTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SonarSource :: IT :: SonarQube Scanner - * 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 com.sonar.runner.it; - -import com.sonar.orchestrator.build.BuildFailureException; -import com.sonar.orchestrator.build.SonarScanner; -import com.sonar.orchestrator.locator.ResourceLocation; -import java.io.File; -import java.io.IOException; -import java.util.Map; -import org.junit.After; -import org.junit.Test; -import org.sonarqube.ws.WsMeasures.Measure; - -import static java.lang.Integer.parseInt; -import static org.assertj.core.api.Assertions.assertThat; - -public class DistributionTest extends ScannerTestCase { - - @After - public void cleanup() { - orchestrator.resetData(); - } - - @Test - public void should_succeed_with_self_contained_jre_despite_rubbish_java_home() throws IOException, InterruptedException { - String projectKey = "java:basedir-with-source"; - orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); - orchestrator.getServer().provisionProject(projectKey, "Basedir with source"); - orchestrator.getServer().associateProjectToQualityProfile(projectKey, "java", "sonar-way"); - - File projectDir = new File("projects/basedir-with-source"); - SonarScanner build = newScanner(projectDir, "sonar.projectKey", projectKey) - .setEnvironmentVariable("JAVA_HOME", "nonexistent") - .useNative(); - orchestrator.executeBuild(build, true); - - Map projectMeasures = getMeasures(projectKey, "files", "ncloc"); - assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1); - assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1); - } - - @Test(expected = BuildFailureException.class) - public void should_fail_without_self_contained_jre_when_rubbish_java_home() throws IOException, InterruptedException { - String projectKey = "java:basedir-with-source"; - orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); - orchestrator.getServer().provisionProject(projectKey, "Basedir with source"); - orchestrator.getServer().associateProjectToQualityProfile(projectKey, "java", "sonar-way"); - - File projectDir = new File("projects/basedir-with-source"); - SonarScanner build = newScanner(projectDir, "sonar.projectKey", projectKey) - .setEnvironmentVariable("JAVA_HOME", "nonexistent"); - orchestrator.executeBuild(build, true); - } -} diff --git a/it/src/test/java/com/sonar/runner/it/JavaTest.java b/it/src/test/java/com/sonar/runner/it/JavaTest.java deleted file mode 100644 index 7337757..0000000 --- a/it/src/test/java/com/sonar/runner/it/JavaTest.java +++ /dev/null @@ -1,317 +0,0 @@ -/* - * SonarSource :: IT :: SonarQube Scanner - * 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 com.sonar.runner.it; - -import com.sonar.orchestrator.build.BuildResult; -import com.sonar.orchestrator.build.SonarScanner; -import com.sonar.orchestrator.locator.ResourceLocation; -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import org.apache.commons.lang.SystemUtils; -import org.junit.After; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.wsclient.issue.Issue; -import org.sonar.wsclient.issue.IssueQuery; -import org.sonarqube.ws.WsComponents.Component; -import org.sonarqube.ws.WsMeasures.Measure; - -import static java.lang.Integer.parseInt; -import static org.fest.assertions.Assertions.assertThat; - -public class JavaTest extends ScannerTestCase { - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @After - public void cleanup() { - orchestrator.resetData(); - } - - /** - * No bytecode, only sources - */ - @Test - public void scan_java_sources() { - orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); - orchestrator.getServer().provisionProject("java:sample", "Java Sample, with comma"); - orchestrator.getServer().associateProjectToQualityProfile("java:sample", "java", "sonar-way"); - - SonarScanner build = newScanner(new File("projects/java-sample")) - .setProperty("sonar.verbose", "true"); - // SONARPLUGINS-3061 - // Add a trailing slash - build.setProperty("sonar.host.url", orchestrator.getServer().getUrl() + "/"); - orchestrator.executeBuild(build); - - Component project = getComponent("java:sample"); - assertThat(project.getName()).isEqualTo("Java Sample, with comma"); - assertThat(project.getDescription()).isEqualTo("This is a Java sample"); - - Map projectMeasures = getMeasures("java:sample", "files", "ncloc", "classes", "violations"); - // SONARPLUGINS-2399 - assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(2); - assertThat(parseInt(projectMeasures.get("classes").getValue())).isEqualTo(2); - assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(10); - assertThat(parseInt(projectMeasures.get("violations").getValue())).isGreaterThan(0); - - Component file = getComponent("java:sample:src/basic/Hello.java"); - assertThat(file.getName()).isEqualTo("Hello.java"); - - Map fileMeasures = getMeasures("java:sample:src/basic/Hello.java", "files", "ncloc", "classes", "violations"); - assertThat(parseInt(fileMeasures.get("ncloc").getValue())).isEqualTo(7); - assertThat(parseInt(fileMeasures.get("violations").getValue())).isGreaterThan(0); - } - - /** - * Only tests, no sources - */ - @Test - public void scan_java_tests() { - orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); - orchestrator.getServer().provisionProject("java:sampletest", "Java Sample"); - orchestrator.getServer().associateProjectToQualityProfile("java:sampletest", "java", "sonar-way"); - - SonarScanner build = newScanner(new File("projects/java-sample")) - .setProperty("sonar.projectKey", "java:sampletest") - .setProperty("sonar.tests", "src") - .setProperty("sonar.sources", ""); - orchestrator.executeBuild(build); - - Component file = getComponent("java:sampletest:src/basic/Hello.java"); - assertThat(file.getName()).isEqualTo("Hello.java"); - assertThat(file.getQualifier()).isEqualTo("UTS"); - } - - @Test - public void scan_java_sources_and_bytecode() { - orchestrator.getServer().restoreProfile(ResourceLocation.create("/requires-bytecode-profile.xml")); - orchestrator.getServer().provisionProject("java:bytecode", "Java Bytecode Sample"); - orchestrator.getServer().associateProjectToQualityProfile("java:bytecode", "java", "requires-bytecode"); - - SonarScanner build = newScanner(new File("projects/java-bytecode")); - orchestrator.executeBuild(build); - - Component project = getComponent("java:bytecode"); - assertThat(project.getName()).isEqualTo("Java Bytecode Sample"); - - Map projectMeasures = getMeasures("java:bytecode", "violations"); - // the squid rules enabled in sonar-way-profile do not exist in SQ 3.0 - assertThat(parseInt(projectMeasures.get("violations").getValue())).isGreaterThan(0); - - assertThat(getMeasureAsInteger("java:bytecode:src/HasFindbugsViolation.java", "violations")).isGreaterThan(0); - - // findbugs is executed on bytecode - List issues = orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().componentRoots("java:bytecode").rules("squid:S1147")).list(); - assertThat(issues).hasSize(1); - assertThat(issues.get(0).ruleKey()).isEqualTo("squid:S1147"); - - // Squid performs analysis of dependencies - issues = orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().componentRoots("java:bytecode").rules("squid:CallToDeprecatedMethod")).list(); - assertThat(issues).hasSize(1); - assertThat(issues.get(0).ruleKey()).isEqualTo("squid:CallToDeprecatedMethod"); - } - - @Test - public void basedir_contains_java_sources() { - orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); - orchestrator.getServer().provisionProject("java:basedir-with-source", "Basedir with source"); - orchestrator.getServer().associateProjectToQualityProfile("java:basedir-with-source", "java", "sonar-way"); - - SonarScanner build = newScanner(new File("projects/basedir-with-source")); - orchestrator.executeBuild(build); - - Map projectMeasures = getMeasures("java:basedir-with-source", "files", "ncloc"); - assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1); - assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1); - } - - /** - * Replace the maven format groupId:artifactId by a single key - */ - @Test - public void should_support_simple_project_keys() { - orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); - orchestrator.getServer().provisionProject("SAMPLE", "Java Sample, with comma"); - orchestrator.getServer().associateProjectToQualityProfile("SAMPLE", "java", "sonar-way"); - - SonarScanner build = newScanner(new File("projects/java-sample")) - .setProjectKey("SAMPLE"); - orchestrator.executeBuild(build); - - Map projectMeasures = getMeasures("SAMPLE", "files", "ncloc"); - assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(2); - assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1); - } - - /** - * SONARPLUGINS-1230 - */ - @Test - public void should_override_working_dir_with_relative_path() { - SonarScanner build = newScanner(new File("projects/override-working-dir")) - .setProperty("sonar.working.directory", ".overridden-relative-sonar"); - orchestrator.executeBuild(build); - - assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist(); - assertThat(new File("projects/override-working-dir/.overridden-relative-sonar")).exists().isDirectory(); - } - - /** - * SONARPLUGINS-1230 - */ - @Test - public void should_override_working_dir_with_absolute_path() { - File projectHome = new File("projects/override-working-dir"); - SonarScanner build = newScanner(projectHome) - .setProperty("sonar.working.directory", new File(projectHome, ".overridden-absolute-sonar").getAbsolutePath()); - orchestrator.executeBuild(build); - - assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist(); - assertThat(new File("projects/override-working-dir/.overridden-absolute-sonar")).exists().isDirectory(); - } - - /** - * SONARPLUGINS-1856 - */ - @Test - public void should_fail_if_source_dir_does_not_exist() { - SonarScanner build = newScanner(new File("projects/bad-source-dirs")); - - BuildResult result = orchestrator.executeBuildQuietly(build); - assertThat(result.getStatus()).isNotEqualTo(0); - // with the following message - assertThat(result.getLogs()).contains("Invalid value of sonar.sources for bad-source-dirs"); - } - - /** - * SONARPLUGINS-2256 - */ - @Test - public void should_warn_when_analysis_is_platform_dependent() { - SonarScanner build = newScanner(new File("projects/java-sample")) - // ORCH-243 - .setSourceEncoding(""); - String log = orchestrator.executeBuild(build).getLogs(); - - // Note: we can't really check the locale value and the charset because the ones used during the Sonar analysis may not be the ones - // used to launch the tests. But we can check that the analysis is platform dependent (i.e. "sonar.sourceEncoding" hasn't been set). - assertThat(log).contains("Default locale:"); - assertThat(log).contains(", source code encoding:"); - assertThat(log).contains("(analysis is platform dependent)"); - } - - /** - * SONARUNNER-153 - */ - @Test - public void should_enable_verbose() { - // this line should appear in all versions (LTS-DEV) in debug only - String expectedLog = "Available languages:"; - SonarScanner build = newScanner(new File("projects/java-sample")) - .setProperty("sonar.verbose", "true"); - String logs = orchestrator.executeBuild(build).getLogs(); - assertThat(logs).contains(expectedLog); - } - - @Test - public void should_use_environment_props() { - SonarScanner build = newScanner(new File("projects/java-sample-no-properties")) - .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{" - + "\"sonar.projectKey\" : \"java:sample\"," + - "\"sonar.projectName\" : \"Java Sample, with comma\"," + - "\"sonar.projectDescription\" : \"This is a Java sample\"," + - "\"sonar.projectVersion\" : \"1.2.3\"," + - "\"sonar.sources\" : \"src\" }"); - orchestrator.executeBuild(build); - } - - @Test - public void should_skip_analysis() { - SonarScanner build = newScanner(new File("projects/java-sample")) - .setProperty("sonar.host.url", "http://foo") - .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{ \"sonar.scanner.skip\":\"true\" }"); - - BuildResult result = orchestrator.executeBuild(build); - assertThat(result.getLogs()).contains("SonarQube Scanner analysis skipped"); - } - - @Test - public void should_fail_if_unable_to_connect() { - SonarScanner build = newScanner(new File("projects/java-sample")) - .setProperty("sonar.host.url", "http://foo"); - - BuildResult result = orchestrator.executeBuildQuietly(build); - // expect build failure - assertThat(result.getStatus()).isNotEqualTo(0); - // with the following message - assertThat(result.getLogs()).contains("SonarQube server [http://foo] can not be reached"); - } - - // SONARPLUGINS-3574 - @Test - public void run_from_external_location() throws IOException { - File tempDir = temp.newFolder(); - SonarScanner build = newScanner(tempDir) - .setProperty("sonar.projectBaseDir", new File("projects/java-sample").getAbsolutePath()) - .addArguments("-e"); - orchestrator.executeBuild(build); - - assertThat(getComponent("java:sample").getDescription()).isEqualTo("This is a Java sample"); - Map projectMeasures = getMeasures("java:sample", "files", "ncloc", "classes", "violations"); - assertThat(projectMeasures.values().stream().filter(measure -> measure.getValue() != null).collect(Collectors.toList())).hasSize(4); - } - - @Test - public void use_old_script_and_old_env_variable() { - SonarScanner build = newScanner(new File("projects/java-sample")) - .setUseOldSonarRunnerScript(true) - .setEnvironmentVariable("SONAR_RUNNER_OPTS", "-Xmx2m"); - BuildResult executeBuild = orchestrator.executeBuildQuietly(build); - assertThat(executeBuild.getStatus()).isNotEqualTo(0); - String logs = executeBuild.getLogs(); - if (SystemUtils.IS_OS_WINDOWS) { - assertThat(logs).contains("WARN: sonar-runner.bat script is deprecated. Please use sonar-scanner.bat instead."); - assertThat(logs).contains("WARN: SONAR_RUNNER_OPTS is deprecated. Please use SONAR_SCANNER_OPTS instead."); - } else { - assertThat(logs).contains("WARN: sonar-runner script is deprecated. Please use sonar-scanner instead."); - assertThat(logs).contains("WARN: $SONAR_RUNNER_OPTS is deprecated. Please use $SONAR_SCANNER_OPTS instead."); - } - assertThat(logs).contains("java.lang.OutOfMemoryError"); - } - - @Test - public void use_new_script_and_new_env_variable() { - SonarScanner build = newScanner(new File("projects/java-sample")) - .setEnvironmentVariable("SONAR_SCANNER_OPTS", "-Xmx2m"); - BuildResult executeBuild = orchestrator.executeBuildQuietly(build); - assertThat(executeBuild.getStatus()).isNotEqualTo(0); - String logs = executeBuild.getLogs(); - assertThat(logs).doesNotContain("sonar-runner"); - assertThat(logs).doesNotContain("SONAR_RUNNER_OPTS"); - assertThat(logs).contains("java.lang.OutOfMemoryError"); - } - -} diff --git a/it/src/test/java/com/sonar/runner/it/MultimoduleTest.java b/it/src/test/java/com/sonar/runner/it/MultimoduleTest.java deleted file mode 100644 index 9486dec..0000000 --- a/it/src/test/java/com/sonar/runner/it/MultimoduleTest.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * SonarSource :: IT :: SonarQube Scanner - * 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 com.sonar.runner.it; - -import com.sonar.orchestrator.build.BuildResult; -import com.sonar.orchestrator.build.SonarScanner; -import java.io.File; -import org.junit.After; -import org.junit.Test; -import org.sonarqube.ws.WsComponents.Component; - -import static org.fest.assertions.Assertions.assertThat; - -public class MultimoduleTest extends ScannerTestCase { - - @After - public void cleanup() { - orchestrator.resetData(); - } - - /** - * SONARPLUGINS-2202 - */ - @Test - public void test_simplest_with_props_on_root() { - SonarScanner build = newScanner(new File("projects/multi-module/simplest/simplest-with-props-on-root")); - - orchestrator.executeBuild(build); - - assertThat(getComponent("simplest-with-props-on-root").getName()).isEqualTo("Simplest multi-module project with all properties set on the root project"); - - // Verify that we have the modules - assertThat(getComponent("simplest-with-props-on-root:module1").getName()).isEqualTo("module1"); - - assertThat(getComponent("simplest-with-props-on-root:module2").getName()).isEqualTo("module2"); - - // And verify that the working directories are all located in the root folder - File workDir = new File("projects/multi-module/simplest/simplest-with-props-on-root/.scannerwork"); - assertThat(workDir).exists(); - assertThat(new File(workDir, "simplest-with-props-on-root_module1")).exists(); - assertThat(new File(workDir, "simplest-with-props-on-root_module2")).exists(); - assertThat(new File("projects/multi-module/simplest/simplest-with-props-on-root/module1/.scannerwork")).doesNotExist(); - assertThat(new File("projects/multi-module/simplest/simplest-with-props-on-root/module2/.scannerwork")).doesNotExist(); - } - - /** - * SONARPLUGINS-2421 - */ - @Test - public void test_multi_language_with_same_projectdir() { - SonarScanner build = newScanner(new File("projects/multi-module/multi-language")); - - orchestrator.executeBuild(build); - - assertThat(getComponent("multi-language").getName()).isEqualTo("Simplest multi-language project"); - - // Verify that we have the modules - assertThat(getComponent("multi-language:java-module").getName()).isEqualTo("java-module"); - - assertThat(getComponent("multi-language:js-module").getName()).isEqualTo("js-module"); - } - - /** - * SONARPLUGINS-2202 - */ - @Test - public void test_simplest_with_props_on_each_module() { - SonarScanner build = newScanner(new File("projects/multi-module/simplest/simplest-with-props-on-each-module")); - - orchestrator.executeBuild(build); - - assertThat(getComponent("simplest-with-props-each-module").getName()).isEqualTo("Simplest multi-module project with properties set on each module"); - - // Verify that we have the modules - assertThat(getComponent("simplest-with-props-each-module:module1").getName()).isEqualTo("module1"); - - assertThat(getComponent("simplest-with-props-each-module:overridden-key-for-module2").getName()).isEqualTo("Module 2"); - } - - /** - * SONARPLUGINS-2202 - */ - @Test - public void test_deep_path_for_modules() { - SonarScanner build = newScanner(new File("projects/multi-module/customization/deep-path-for-modules")); - - orchestrator.executeBuild(build); - - assertThat(getComponent("deep-path-for-modules").getName()).isEqualTo("Project with deep path for modules"); - - // Verify that we have the modules - assertThat(getComponent("deep-path-for-modules:mod1").getName()).isEqualTo("Module 1"); - - assertThat(getComponent("deep-path-for-modules:mod2").getName()).isEqualTo("Module 2"); - } - - /** - * SONARPLUGINS-2202 - */ - @Test - public void test_module_path_with_space() { - SonarScanner build = newScanner(new File("projects/multi-module/customization/module-path-with-space")); - - orchestrator.executeBuild(build); - - assertThat(getComponent("module-path-with-space").getName()).isEqualTo("Project with module path that contain spaces"); - - // Verify that we have the modules - assertThat(getComponent("module-path-with-space:module1").getName()).isEqualTo("Module 1"); - - assertThat(getComponent("module-path-with-space:module2").getName()).isEqualTo("Module 2"); - } - - /** - * SONARPLUGINS-2202 - */ - @Test - public void test_overwriting_parent_properties() { - SonarScanner build = newScanner(new File("projects/multi-module/customization/overwriting-parent-properties")); - - orchestrator.executeBuild(build); - - Component rootProject = getComponent("overwriting-parent-properties"); - assertThat(rootProject.getName()).isEqualTo("Project with modules that overwrite properties"); - assertThat(rootProject.getDescription()).isEqualTo("Description of root project"); - - // Verify that we have the modules - Component module1 = getComponent("overwriting-parent-properties:module1-new-key"); - assertThat(module1.getName()).isEqualTo("Module 1"); - assertThat(module1.getDescription()).isEqualTo("Description of module 1"); - - Component module2 = getComponent("overwriting-parent-properties:module2-new-key"); - assertThat(module2.getName()).isEqualTo("Module 2"); - assertThat(module2.getDescription()).isEqualTo("Description of module 2"); - } - - /** - * SONARPLUGINS-2202 - */ - @Test - public void test_using_config_file_property() { - SonarScanner build = newScanner(new File("projects/multi-module/advanced/using-config-file-prop")); - - orchestrator.executeBuild(build); - - assertThat(getComponent("using-config-file-prop").getName()).isEqualTo("Advanced use case - mostly used by the Ant task"); - - // Verify that we have the modules - assertThat(getComponent("using-config-file-prop:module1").getName()).isEqualTo("Module 1"); - - assertThat(getComponent("using-config-file-prop:module2").getName()).isEqualTo("Module 2"); - } - - /** - * SONARPLUGINS-2202 - */ - @Test - public void should_fail_if_unexisting_base_dir() { - SonarScanner build = newScanner(new File("projects/multi-module/failures/unexisting-base-dir")); - - BuildResult result = orchestrator.executeBuildQuietly(build); - // expect build failure - assertThat(result.getStatus()).isNotEqualTo(0); - // with the following message - assertThat(result.getLogs()).contains("The base directory of the module 'module3' does not exist"); - - } - - /** - * SONARPLUGINS-2202 - */ - @Test - public void should_fail_if_unexisting_config_file() { - SonarScanner build = newScanner(new File("projects/multi-module/failures/unexisting-config-file")); - - BuildResult result = orchestrator.executeBuildQuietly(build); - // expect build failure - assertThat(result.getStatus()).isNotEqualTo(0); - // with the following message - assertThat(result.getLogs()).contains("The properties file of the module 'module1' does not exist"); - } - -} diff --git a/it/src/test/java/com/sonar/runner/it/ScannerTestCase.java b/it/src/test/java/com/sonar/runner/it/ScannerTestCase.java deleted file mode 100644 index 4f32725..0000000 --- a/it/src/test/java/com/sonar/runner/it/ScannerTestCase.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * SonarSource :: IT :: SonarQube Scanner - * 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 com.sonar.runner.it; - -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.build.SonarScanner; -import com.sonar.orchestrator.version.Version; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.function.Function; -import java.util.stream.Collectors; -import javax.annotation.CheckForNull; -import org.apache.commons.lang.StringUtils; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.rules.ExpectedException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonarqube.ws.WsComponents; -import org.sonarqube.ws.WsComponents.Component; -import org.sonarqube.ws.WsMeasures; -import org.sonarqube.ws.WsMeasures.Measure; -import org.sonarqube.ws.client.HttpConnector; -import org.sonarqube.ws.client.WsClient; -import org.sonarqube.ws.client.WsClientFactories; -import org.sonarqube.ws.client.component.ShowWsRequest; -import org.sonarqube.ws.client.measure.ComponentWsRequest; - -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; - -public abstract class ScannerTestCase { - - private static final Logger LOG = LoggerFactory.getLogger(ScannerTestCase.class); - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @ClassRule - public static Orchestrator orchestrator = SonarScannerTestSuite.ORCHESTRATOR; - - private static Version artifactVersion; - - private static Version artifactVersion() { - if (artifactVersion == null) { - String scannerVersion = System.getProperty("scanner.version"); - if (StringUtils.isNotBlank(scannerVersion)) { - LOG.info("Use provided Scanner version: " + scannerVersion); - artifactVersion = Version.create(scannerVersion); - } else { - try (FileInputStream fis = new FileInputStream(new File("../target/maven-archiver/pom.properties"))) { - Properties props = new Properties(); - props.load(fis); - artifactVersion = Version.create(props.getProperty("version")); - return artifactVersion; - } catch (IOException e) { - throw new IllegalStateException(e); - } - } - } - return artifactVersion; - } - - SonarScanner newScanner(File baseDir, String... keyValueProperties) { - SonarScanner scannerCli = SonarScanner.create(baseDir, keyValueProperties); - scannerCli.setScannerVersion(artifactVersion().toString()); - return scannerCli; - } - - @CheckForNull - static Map getMeasures(String componentKey, String... metricKeys) { - return newWsClient().measures().component(new ComponentWsRequest() - .setComponentKey(componentKey) - .setMetricKeys(asList(metricKeys))) - .getComponent().getMeasuresList() - .stream() - .collect(Collectors.toMap(Measure::getMetric, Function.identity())); - } - - @CheckForNull - static Measure getMeasure(String componentKey, String metricKey) { - WsMeasures.ComponentWsResponse response = newWsClient().measures().component(new ComponentWsRequest() - .setComponentKey(componentKey) - .setMetricKeys(singletonList(metricKey))); - List measures = response.getComponent().getMeasuresList(); - return measures.size() == 1 ? measures.get(0) : null; - } - - @CheckForNull - static Integer getMeasureAsInteger(String componentKey, String metricKey) { - Measure measure = getMeasure(componentKey, metricKey); - return (measure == null) ? null : Integer.parseInt(measure.getValue()); - } - - @CheckForNull - static Double getMeasureAsDouble(String componentKey, String metricKey) { - Measure measure = getMeasure(componentKey, metricKey); - return (measure == null) ? null : Double.parseDouble(measure.getValue()); - } - - @CheckForNull - static Component getComponent(String componentKey) { - return newWsClient().components().show(new ShowWsRequest().setKey(componentKey)).getComponent(); - } - - static WsClient newWsClient() { - return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder() - .url(orchestrator.getServer().getUrl()) - .build()); - } - -} diff --git a/it/src/test/java/com/sonar/runner/it/SonarScannerTestSuite.java b/it/src/test/java/com/sonar/runner/it/SonarScannerTestSuite.java deleted file mode 100644 index f36120b..0000000 --- a/it/src/test/java/com/sonar/runner/it/SonarScannerTestSuite.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * SonarSource :: IT :: SonarQube Scanner - * 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 com.sonar.runner.it; - -import com.sonar.orchestrator.Orchestrator; -import org.junit.ClassRule; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -@RunWith(Suite.class) -@SuiteClasses({JavaTest.class, MultimoduleTest.class, DistributionTest.class}) -public class SonarScannerTestSuite { - - @ClassRule - public static final Orchestrator ORCHESTRATOR = Orchestrator.builderEnv() - .setOrchestratorProperty("javaVersion", "LATEST_RELEASE") - .addPlugin("java") - .setOrchestratorProperty("javascriptVersion", "LATEST_RELEASE") - .addPlugin("javascript") - .build(); - -} diff --git a/it/src/test/java/com/sonarsource/scanner/it/DistributionTest.java b/it/src/test/java/com/sonarsource/scanner/it/DistributionTest.java new file mode 100644 index 0000000..310ffba --- /dev/null +++ b/it/src/test/java/com/sonarsource/scanner/it/DistributionTest.java @@ -0,0 +1,72 @@ +/* + * SonarSource :: IT :: SonarQube Scanner + * 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 com.sonarsource.scanner.it; + +import com.sonar.orchestrator.build.BuildFailureException; +import com.sonar.orchestrator.build.SonarScanner; +import com.sonar.orchestrator.locator.ResourceLocation; +import java.io.File; +import java.io.IOException; +import java.util.Map; +import org.junit.After; +import org.junit.Test; +import org.sonarqube.ws.WsMeasures.Measure; + +import static java.lang.Integer.parseInt; +import static org.assertj.core.api.Assertions.assertThat; + +public class DistributionTest extends ScannerTestCase { + + @After + public void cleanup() { + orchestrator.resetData(); + } + + @Test + public void should_succeed_with_self_contained_jre_despite_rubbish_java_home() throws IOException, InterruptedException { + String projectKey = "java:basedir-with-source"; + orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); + orchestrator.getServer().provisionProject(projectKey, "Basedir with source"); + orchestrator.getServer().associateProjectToQualityProfile(projectKey, "java", "sonar-way"); + + File projectDir = new File("projects/basedir-with-source"); + SonarScanner build = newScanner(projectDir, "sonar.projectKey", projectKey) + .setEnvironmentVariable("JAVA_HOME", "nonexistent") + .useNative(); + orchestrator.executeBuild(build, true); + + Map projectMeasures = getMeasures(projectKey, "files", "ncloc"); + assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1); + assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1); + } + + @Test(expected = BuildFailureException.class) + public void should_fail_without_self_contained_jre_when_rubbish_java_home() throws IOException, InterruptedException { + String projectKey = "java:basedir-with-source"; + orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); + orchestrator.getServer().provisionProject(projectKey, "Basedir with source"); + orchestrator.getServer().associateProjectToQualityProfile(projectKey, "java", "sonar-way"); + + File projectDir = new File("projects/basedir-with-source"); + SonarScanner build = newScanner(projectDir, "sonar.projectKey", projectKey) + .setEnvironmentVariable("JAVA_HOME", "nonexistent"); + orchestrator.executeBuild(build, true); + } +} diff --git a/it/src/test/java/com/sonarsource/scanner/it/JavaTest.java b/it/src/test/java/com/sonarsource/scanner/it/JavaTest.java new file mode 100644 index 0000000..1f53a88 --- /dev/null +++ b/it/src/test/java/com/sonarsource/scanner/it/JavaTest.java @@ -0,0 +1,296 @@ +/* + * SonarSource :: IT :: SonarQube Scanner + * 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 com.sonarsource.scanner.it; + +import com.sonar.orchestrator.build.BuildResult; +import com.sonar.orchestrator.build.SonarScanner; +import com.sonar.orchestrator.locator.ResourceLocation; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.junit.After; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.wsclient.issue.Issue; +import org.sonar.wsclient.issue.IssueQuery; +import org.sonarqube.ws.WsComponents.Component; +import org.sonarqube.ws.WsMeasures.Measure; + +import static java.lang.Integer.parseInt; +import static org.fest.assertions.Assertions.assertThat; + +public class JavaTest extends ScannerTestCase { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @After + public void cleanup() { + orchestrator.resetData(); + } + + /** + * No bytecode, only sources + */ + @Test + public void scan_java_sources() { + orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); + orchestrator.getServer().provisionProject("java:sample", "Java Sample, with comma"); + orchestrator.getServer().associateProjectToQualityProfile("java:sample", "java", "sonar-way"); + + SonarScanner build = newScanner(new File("projects/java-sample")) + .setProperty("sonar.verbose", "true"); + // SONARPLUGINS-3061 + // Add a trailing slash + build.setProperty("sonar.host.url", orchestrator.getServer().getUrl() + "/"); + orchestrator.executeBuild(build); + + Component project = getComponent("java:sample"); + assertThat(project.getName()).isEqualTo("Java Sample, with comma"); + assertThat(project.getDescription()).isEqualTo("This is a Java sample"); + + Map projectMeasures = getMeasures("java:sample", "files", "ncloc", "classes", "violations"); + // SONARPLUGINS-2399 + assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(2); + assertThat(parseInt(projectMeasures.get("classes").getValue())).isEqualTo(2); + assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(10); + assertThat(parseInt(projectMeasures.get("violations").getValue())).isGreaterThan(0); + + Component file = getComponent("java:sample:src/basic/Hello.java"); + assertThat(file.getName()).isEqualTo("Hello.java"); + + Map fileMeasures = getMeasures("java:sample:src/basic/Hello.java", "files", "ncloc", "classes", "violations"); + assertThat(parseInt(fileMeasures.get("ncloc").getValue())).isEqualTo(7); + assertThat(parseInt(fileMeasures.get("violations").getValue())).isGreaterThan(0); + } + + /** + * Only tests, no sources + */ + @Test + public void scan_java_tests() { + orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); + orchestrator.getServer().provisionProject("java:sampletest", "Java Sample"); + orchestrator.getServer().associateProjectToQualityProfile("java:sampletest", "java", "sonar-way"); + + SonarScanner build = newScanner(new File("projects/java-sample")) + .setProperty("sonar.projectKey", "java:sampletest") + .setProperty("sonar.tests", "src") + .setProperty("sonar.sources", ""); + orchestrator.executeBuild(build); + + Component file = getComponent("java:sampletest:src/basic/Hello.java"); + assertThat(file.getName()).isEqualTo("Hello.java"); + assertThat(file.getQualifier()).isEqualTo("UTS"); + } + + @Test + public void scan_java_sources_and_bytecode() { + orchestrator.getServer().restoreProfile(ResourceLocation.create("/requires-bytecode-profile.xml")); + orchestrator.getServer().provisionProject("java:bytecode", "Java Bytecode Sample"); + orchestrator.getServer().associateProjectToQualityProfile("java:bytecode", "java", "requires-bytecode"); + + SonarScanner build = newScanner(new File("projects/java-bytecode")); + orchestrator.executeBuild(build); + + Component project = getComponent("java:bytecode"); + assertThat(project.getName()).isEqualTo("Java Bytecode Sample"); + + Map projectMeasures = getMeasures("java:bytecode", "violations"); + // the squid rules enabled in sonar-way-profile do not exist in SQ 3.0 + assertThat(parseInt(projectMeasures.get("violations").getValue())).isGreaterThan(0); + + assertThat(getMeasureAsInteger("java:bytecode:src/HasFindbugsViolation.java", "violations")).isGreaterThan(0); + + // findbugs is executed on bytecode + List issues = orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().componentRoots("java:bytecode").rules("squid:S1147")).list(); + assertThat(issues).hasSize(1); + assertThat(issues.get(0).ruleKey()).isEqualTo("squid:S1147"); + + // Squid performs analysis of dependencies + issues = orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().componentRoots("java:bytecode").rules("squid:CallToDeprecatedMethod")).list(); + assertThat(issues).hasSize(1); + assertThat(issues.get(0).ruleKey()).isEqualTo("squid:CallToDeprecatedMethod"); + } + + @Test + public void basedir_contains_java_sources() { + orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); + orchestrator.getServer().provisionProject("java:basedir-with-source", "Basedir with source"); + orchestrator.getServer().associateProjectToQualityProfile("java:basedir-with-source", "java", "sonar-way"); + + SonarScanner build = newScanner(new File("projects/basedir-with-source")); + orchestrator.executeBuild(build); + + Map projectMeasures = getMeasures("java:basedir-with-source", "files", "ncloc"); + assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1); + assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1); + } + + /** + * Replace the maven format groupId:artifactId by a single key + */ + @Test + public void should_support_simple_project_keys() { + orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml")); + orchestrator.getServer().provisionProject("SAMPLE", "Java Sample, with comma"); + orchestrator.getServer().associateProjectToQualityProfile("SAMPLE", "java", "sonar-way"); + + SonarScanner build = newScanner(new File("projects/java-sample")) + .setProjectKey("SAMPLE"); + orchestrator.executeBuild(build); + + Map projectMeasures = getMeasures("SAMPLE", "files", "ncloc"); + assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(2); + assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1); + } + + /** + * SONARPLUGINS-1230 + */ + @Test + public void should_override_working_dir_with_relative_path() { + SonarScanner build = newScanner(new File("projects/override-working-dir")) + .setProperty("sonar.working.directory", ".overridden-relative-sonar"); + orchestrator.executeBuild(build); + + assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist(); + assertThat(new File("projects/override-working-dir/.overridden-relative-sonar")).exists().isDirectory(); + } + + /** + * SONARPLUGINS-1230 + */ + @Test + public void should_override_working_dir_with_absolute_path() { + File projectHome = new File("projects/override-working-dir"); + SonarScanner build = newScanner(projectHome) + .setProperty("sonar.working.directory", new File(projectHome, ".overridden-absolute-sonar").getAbsolutePath()); + orchestrator.executeBuild(build); + + assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist(); + assertThat(new File("projects/override-working-dir/.overridden-absolute-sonar")).exists().isDirectory(); + } + + /** + * SONARPLUGINS-1856 + */ + @Test + public void should_fail_if_source_dir_does_not_exist() { + SonarScanner build = newScanner(new File("projects/bad-source-dirs")); + + BuildResult result = orchestrator.executeBuildQuietly(build); + assertThat(result.getStatus()).isNotEqualTo(0); + // with the following message + assertThat(result.getLogs()).contains("Invalid value of sonar.sources for bad-source-dirs"); + } + + /** + * SONARPLUGINS-2256 + */ + @Test + public void should_warn_when_analysis_is_platform_dependent() { + SonarScanner build = newScanner(new File("projects/java-sample")) + // ORCH-243 + .setSourceEncoding(""); + String log = orchestrator.executeBuild(build).getLogs(); + + // Note: we can't really check the locale value and the charset because the ones used during the Sonar analysis may not be the ones + // used to launch the tests. But we can check that the analysis is platform dependent (i.e. "sonar.sourceEncoding" hasn't been set). + assertThat(log).contains("Default locale:"); + assertThat(log).contains(", source code encoding:"); + assertThat(log).contains("(analysis is platform dependent)"); + } + + /** + * SONARUNNER-153 + */ + @Test + public void should_enable_verbose() { + // this line should appear in all versions (LTS-DEV) in debug only + String expectedLog = "Available languages:"; + SonarScanner build = newScanner(new File("projects/java-sample")) + .setProperty("sonar.verbose", "true"); + String logs = orchestrator.executeBuild(build).getLogs(); + assertThat(logs).contains(expectedLog); + } + + @Test + public void should_use_environment_props() { + SonarScanner build = newScanner(new File("projects/java-sample-no-properties")) + .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{" + + "\"sonar.projectKey\" : \"java:sample\"," + + "\"sonar.projectName\" : \"Java Sample, with comma\"," + + "\"sonar.projectDescription\" : \"This is a Java sample\"," + + "\"sonar.projectVersion\" : \"1.2.3\"," + + "\"sonar.sources\" : \"src\" }"); + orchestrator.executeBuild(build); + } + + @Test + public void should_skip_analysis() { + SonarScanner build = newScanner(new File("projects/java-sample")) + .setProperty("sonar.host.url", "http://foo") + .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{ \"sonar.scanner.skip\":\"true\" }"); + + BuildResult result = orchestrator.executeBuild(build); + assertThat(result.getLogs()).contains("SonarQube Scanner analysis skipped"); + } + + @Test + public void should_fail_if_unable_to_connect() { + SonarScanner build = newScanner(new File("projects/java-sample")) + .setProperty("sonar.host.url", "http://foo"); + + BuildResult result = orchestrator.executeBuildQuietly(build); + // expect build failure + assertThat(result.getStatus()).isNotEqualTo(0); + // with the following message + assertThat(result.getLogs()).contains("SonarQube server [http://foo] can not be reached"); + } + + // SONARPLUGINS-3574 + @Test + public void run_from_external_location() throws IOException { + File tempDir = temp.newFolder(); + SonarScanner build = newScanner(tempDir) + .setProperty("sonar.projectBaseDir", new File("projects/java-sample").getAbsolutePath()) + .addArguments("-e"); + orchestrator.executeBuild(build); + + assertThat(getComponent("java:sample").getDescription()).isEqualTo("This is a Java sample"); + Map projectMeasures = getMeasures("java:sample", "files", "ncloc", "classes", "violations"); + assertThat(projectMeasures.values().stream().filter(measure -> measure.getValue() != null).collect(Collectors.toList())).hasSize(4); + } + + @Test + public void verify_env_variable() { + SonarScanner build = newScanner(new File("projects/java-sample")) + .setEnvironmentVariable("SONAR_SCANNER_OPTS", "-Xmx2m"); + BuildResult executeBuild = orchestrator.executeBuildQuietly(build); + assertThat(executeBuild.getStatus()).isNotEqualTo(0); + String logs = executeBuild.getLogs(); + assertThat(logs).contains("java.lang.OutOfMemoryError"); + } + +} diff --git a/it/src/test/java/com/sonarsource/scanner/it/MultimoduleTest.java b/it/src/test/java/com/sonarsource/scanner/it/MultimoduleTest.java new file mode 100644 index 0000000..52931ee --- /dev/null +++ b/it/src/test/java/com/sonarsource/scanner/it/MultimoduleTest.java @@ -0,0 +1,200 @@ +/* + * SonarSource :: IT :: SonarQube Scanner + * 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 com.sonarsource.scanner.it; + +import com.sonar.orchestrator.build.BuildResult; +import com.sonar.orchestrator.build.SonarScanner; +import java.io.File; +import org.junit.After; +import org.junit.Test; +import org.sonarqube.ws.WsComponents.Component; + +import static org.fest.assertions.Assertions.assertThat; + +public class MultimoduleTest extends ScannerTestCase { + + @After + public void cleanup() { + orchestrator.resetData(); + } + + /** + * SONARPLUGINS-2202 + */ + @Test + public void test_simplest_with_props_on_root() { + SonarScanner build = newScanner(new File("projects/multi-module/simplest/simplest-with-props-on-root")); + + orchestrator.executeBuild(build); + + assertThat(getComponent("simplest-with-props-on-root").getName()).isEqualTo("Simplest multi-module project with all properties set on the root project"); + + // Verify that we have the modules + assertThat(getComponent("simplest-with-props-on-root:module1").getName()).isEqualTo("module1"); + + assertThat(getComponent("simplest-with-props-on-root:module2").getName()).isEqualTo("module2"); + + // And verify that the working directories are all located in the root folder + File workDir = new File("projects/multi-module/simplest/simplest-with-props-on-root/.scannerwork"); + assertThat(workDir).exists(); + assertThat(new File(workDir, "simplest-with-props-on-root_module1")).exists(); + assertThat(new File(workDir, "simplest-with-props-on-root_module2")).exists(); + assertThat(new File("projects/multi-module/simplest/simplest-with-props-on-root/module1/.scannerwork")).doesNotExist(); + assertThat(new File("projects/multi-module/simplest/simplest-with-props-on-root/module2/.scannerwork")).doesNotExist(); + } + + /** + * SONARPLUGINS-2421 + */ + @Test + public void test_multi_language_with_same_projectdir() { + SonarScanner build = newScanner(new File("projects/multi-module/multi-language")); + + orchestrator.executeBuild(build); + + assertThat(getComponent("multi-language").getName()).isEqualTo("Simplest multi-language project"); + + // Verify that we have the modules + assertThat(getComponent("multi-language:java-module").getName()).isEqualTo("java-module"); + + assertThat(getComponent("multi-language:js-module").getName()).isEqualTo("js-module"); + } + + /** + * SONARPLUGINS-2202 + */ + @Test + public void test_simplest_with_props_on_each_module() { + SonarScanner build = newScanner(new File("projects/multi-module/simplest/simplest-with-props-on-each-module")); + + orchestrator.executeBuild(build); + + assertThat(getComponent("simplest-with-props-each-module").getName()).isEqualTo("Simplest multi-module project with properties set on each module"); + + // Verify that we have the modules + assertThat(getComponent("simplest-with-props-each-module:module1").getName()).isEqualTo("module1"); + + assertThat(getComponent("simplest-with-props-each-module:overridden-key-for-module2").getName()).isEqualTo("Module 2"); + } + + /** + * SONARPLUGINS-2202 + */ + @Test + public void test_deep_path_for_modules() { + SonarScanner build = newScanner(new File("projects/multi-module/customization/deep-path-for-modules")); + + orchestrator.executeBuild(build); + + assertThat(getComponent("deep-path-for-modules").getName()).isEqualTo("Project with deep path for modules"); + + // Verify that we have the modules + assertThat(getComponent("deep-path-for-modules:mod1").getName()).isEqualTo("Module 1"); + + assertThat(getComponent("deep-path-for-modules:mod2").getName()).isEqualTo("Module 2"); + } + + /** + * SONARPLUGINS-2202 + */ + @Test + public void test_module_path_with_space() { + SonarScanner build = newScanner(new File("projects/multi-module/customization/module-path-with-space")); + + orchestrator.executeBuild(build); + + assertThat(getComponent("module-path-with-space").getName()).isEqualTo("Project with module path that contain spaces"); + + // Verify that we have the modules + assertThat(getComponent("module-path-with-space:module1").getName()).isEqualTo("Module 1"); + + assertThat(getComponent("module-path-with-space:module2").getName()).isEqualTo("Module 2"); + } + + /** + * SONARPLUGINS-2202 + */ + @Test + public void test_overwriting_parent_properties() { + SonarScanner build = newScanner(new File("projects/multi-module/customization/overwriting-parent-properties")); + + orchestrator.executeBuild(build); + + Component rootProject = getComponent("overwriting-parent-properties"); + assertThat(rootProject.getName()).isEqualTo("Project with modules that overwrite properties"); + assertThat(rootProject.getDescription()).isEqualTo("Description of root project"); + + // Verify that we have the modules + Component module1 = getComponent("overwriting-parent-properties:module1-new-key"); + assertThat(module1.getName()).isEqualTo("Module 1"); + assertThat(module1.getDescription()).isEqualTo("Description of module 1"); + + Component module2 = getComponent("overwriting-parent-properties:module2-new-key"); + assertThat(module2.getName()).isEqualTo("Module 2"); + assertThat(module2.getDescription()).isEqualTo("Description of module 2"); + } + + /** + * SONARPLUGINS-2202 + */ + @Test + public void test_using_config_file_property() { + SonarScanner build = newScanner(new File("projects/multi-module/advanced/using-config-file-prop")); + + orchestrator.executeBuild(build); + + assertThat(getComponent("using-config-file-prop").getName()).isEqualTo("Advanced use case - mostly used by the Ant task"); + + // Verify that we have the modules + assertThat(getComponent("using-config-file-prop:module1").getName()).isEqualTo("Module 1"); + + assertThat(getComponent("using-config-file-prop:module2").getName()).isEqualTo("Module 2"); + } + + /** + * SONARPLUGINS-2202 + */ + @Test + public void should_fail_if_unexisting_base_dir() { + SonarScanner build = newScanner(new File("projects/multi-module/failures/unexisting-base-dir")); + + BuildResult result = orchestrator.executeBuildQuietly(build); + // expect build failure + assertThat(result.getStatus()).isNotEqualTo(0); + // with the following message + assertThat(result.getLogs()).contains("The base directory of the module 'module3' does not exist"); + + } + + /** + * SONARPLUGINS-2202 + */ + @Test + public void should_fail_if_unexisting_config_file() { + SonarScanner build = newScanner(new File("projects/multi-module/failures/unexisting-config-file")); + + BuildResult result = orchestrator.executeBuildQuietly(build); + // expect build failure + assertThat(result.getStatus()).isNotEqualTo(0); + // with the following message + assertThat(result.getLogs()).contains("The properties file of the module 'module1' does not exist"); + } + +} diff --git a/it/src/test/java/com/sonarsource/scanner/it/ScannerTestCase.java b/it/src/test/java/com/sonarsource/scanner/it/ScannerTestCase.java new file mode 100644 index 0000000..be19ea9 --- /dev/null +++ b/it/src/test/java/com/sonarsource/scanner/it/ScannerTestCase.java @@ -0,0 +1,133 @@ +/* + * SonarSource :: IT :: SonarQube Scanner + * 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 com.sonarsource.scanner.it; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.SonarScanner; +import com.sonar.orchestrator.version.Version; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.function.Function; +import java.util.stream.Collectors; +import javax.annotation.CheckForNull; +import org.apache.commons.lang.StringUtils; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.rules.ExpectedException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonarqube.ws.WsComponents; +import org.sonarqube.ws.WsComponents.Component; +import org.sonarqube.ws.WsMeasures; +import org.sonarqube.ws.WsMeasures.Measure; +import org.sonarqube.ws.client.HttpConnector; +import org.sonarqube.ws.client.WsClient; +import org.sonarqube.ws.client.WsClientFactories; +import org.sonarqube.ws.client.component.ShowWsRequest; +import org.sonarqube.ws.client.measure.ComponentWsRequest; + +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; + +public abstract class ScannerTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(ScannerTestCase.class); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @ClassRule + public static Orchestrator orchestrator = SonarScannerTestSuite.ORCHESTRATOR; + + private static Version artifactVersion; + + private static Version artifactVersion() { + if (artifactVersion == null) { + String scannerVersion = System.getProperty("scanner.version"); + if (StringUtils.isNotBlank(scannerVersion)) { + LOG.info("Use provided Scanner version: " + scannerVersion); + artifactVersion = Version.create(scannerVersion); + } else { + try (FileInputStream fis = new FileInputStream(new File("../target/maven-archiver/pom.properties"))) { + Properties props = new Properties(); + props.load(fis); + artifactVersion = Version.create(props.getProperty("version")); + return artifactVersion; + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + } + return artifactVersion; + } + + SonarScanner newScanner(File baseDir, String... keyValueProperties) { + SonarScanner scannerCli = SonarScanner.create(baseDir, keyValueProperties); + scannerCli.setScannerVersion(artifactVersion().toString()); + return scannerCli; + } + + @CheckForNull + static Map getMeasures(String componentKey, String... metricKeys) { + return newWsClient().measures().component(new ComponentWsRequest() + .setComponentKey(componentKey) + .setMetricKeys(asList(metricKeys))) + .getComponent().getMeasuresList() + .stream() + .collect(Collectors.toMap(Measure::getMetric, Function.identity())); + } + + @CheckForNull + static Measure getMeasure(String componentKey, String metricKey) { + WsMeasures.ComponentWsResponse response = newWsClient().measures().component(new ComponentWsRequest() + .setComponentKey(componentKey) + .setMetricKeys(singletonList(metricKey))); + List measures = response.getComponent().getMeasuresList(); + return measures.size() == 1 ? measures.get(0) : null; + } + + @CheckForNull + static Integer getMeasureAsInteger(String componentKey, String metricKey) { + Measure measure = getMeasure(componentKey, metricKey); + return (measure == null) ? null : Integer.parseInt(measure.getValue()); + } + + @CheckForNull + static Double getMeasureAsDouble(String componentKey, String metricKey) { + Measure measure = getMeasure(componentKey, metricKey); + return (measure == null) ? null : Double.parseDouble(measure.getValue()); + } + + @CheckForNull + static Component getComponent(String componentKey) { + return newWsClient().components().show(new ShowWsRequest().setKey(componentKey)).getComponent(); + } + + static WsClient newWsClient() { + return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder() + .url(orchestrator.getServer().getUrl()) + .build()); + } + +} diff --git a/it/src/test/java/com/sonarsource/scanner/it/SonarScannerTestSuite.java b/it/src/test/java/com/sonarsource/scanner/it/SonarScannerTestSuite.java new file mode 100644 index 0000000..6b157b2 --- /dev/null +++ b/it/src/test/java/com/sonarsource/scanner/it/SonarScannerTestSuite.java @@ -0,0 +1,40 @@ +/* + * SonarSource :: IT :: SonarQube Scanner + * 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 com.sonarsource.scanner.it; + +import com.sonar.orchestrator.Orchestrator; +import org.junit.ClassRule; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({JavaTest.class, MultimoduleTest.class, DistributionTest.class}) +public class SonarScannerTestSuite { + + @ClassRule + public static final Orchestrator ORCHESTRATOR = Orchestrator.builderEnv() + .setOrchestratorProperty("javaVersion", "LATEST_RELEASE") + .addPlugin("java") + .setOrchestratorProperty("javascriptVersion", "LATEST_RELEASE") + .addPlugin("javascript") + .build(); + +} diff --git a/src/main/assembly/bin/sonar-runner b/src/main/assembly/bin/sonar-runner deleted file mode 100755 index a116ea4..0000000 --- a/src/main/assembly/bin/sonar-runner +++ /dev/null @@ -1,77 +0,0 @@ -#!/bin/sh -# -# SonarQube Runner Startup Script for Unix -# -# Optional ENV vars: -# SONAR_RUNNER_OPTS - Parameters passed to the Java VM when running Sonar -# JAVA_HOME - Location of Java's installation - -real_path () { - target=$1 - - ( - while true; do - cd "$(dirname "$target")" - target=$(basename "$target") - link=$(readlink "$target") - test "$link" || break - target=$link - done - - echo "$(pwd -P)/$target" - ) -} - -echo WARN: 'sonar-runner' script is deprecated. Please use 'sonar-scanner' instead. - -script_path="$0" - -if [ -h "$script_path" ] ; then - # resolve recursively symlinks - script_path=$(real_path "$script_path") -fi - -sonar_runner_home=$(dirname "$script_path")/.. - -# make it fully qualified -sonar_runner_home=$(cd "$sonar_runner_home" && pwd) - -jar_file=$sonar_runner_home/lib/sonar-scanner-cli-${project.version}.jar - -# check that sonar_runner_home has been correctly set -if [ ! -f "$jar_file" ] ; then - echo "File does not exist: $jar_file" - echo "'$sonar_runner_home' does not point to a valid installation directory: $sonar_runner_home" - exit 1 -fi - -if [ -n "$JAVA_HOME" ] -then - java_cmd="$JAVA_HOME/bin/java" -else - java_cmd="$(which java)" -fi - -if [ -n "$SONAR_RUNNER_OPTS" ] ; -then - echo WARN: '$SONAR_RUNNER_OPTS' is deprecated. Please use '$SONAR_SCANNER_OPTS' instead. - if [ -z "$SONAR_SCANNER_OPTS" ] ; then - SONAR_SCANNER_OPTS=$SONAR_RUNNER_OPTS - fi -fi - -project_home=$(pwd) - -#echo "Info: Using sonar-runner at $sonar_runner_home" -#echo "Info: Using java at $java_cmd" -#echo "Info: Using classpath $jar_file" -#echo "Info: Using project $project_home" - -exec "$java_cmd" \ - -Djava.awt.headless=true \ - $SONAR_SCANNER_OPTS \ - -classpath "$jar_file" \ - -Dscanner.home="$sonar_runner_home" \ - -Dproject.home="$project_home" \ - org.sonarsource.scanner.cli.Main "$@" - diff --git a/src/main/assembly/bin/sonar-runner.bat b/src/main/assembly/bin/sonar-runner.bat deleted file mode 100644 index 0c5fdb2..0000000 --- a/src/main/assembly/bin/sonar-runner.bat +++ /dev/null @@ -1,89 +0,0 @@ -@REM SonarQube Runner Startup Script for Windows -@REM -@REM Required ENV vars: -@REM JAVA_HOME - location of a JDK home dir -@REM -@REM Optional ENV vars: -@REM SONAR_RUNNER_OPTS - parameters passed to the Java VM when running Sonar - -@echo off - -set ERROR_CODE=0 - -@REM set local scope for the variables with windows NT shell -@setlocal - -echo WARN: sonar-runner.bat script is deprecated. Please use sonar-scanner.bat instead. - -@REM ==== START VALIDATION ==== -@REM *** JAVA EXEC VALIDATION *** -if not "%JAVA_HOME%" == "" goto foundJavaHome - -for %%i in (java.exe) do set JAVA_EXEC=%%~$PATH:i - -if not "%JAVA_EXEC%" == "" ( - set JAVA_EXEC="%JAVA_EXEC%" - goto OkJava -) - -if not "%JAVA_EXEC%" == "" goto OkJava - -echo. -echo ERROR: JAVA_HOME not found in your environment, and no Java -echo executable present in the PATH. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation, or add "java.exe" to the PATH -echo. -goto error - -:foundJavaHome -if EXIST "%JAVA_HOME%\bin\java.exe" goto foundJavaExeFromJavaHome - -echo. -echo ERROR: JAVA_HOME exists but does not point to a valid Java home -echo folder. No "\bin\java.exe" file can be found there. -echo. -goto error - -:foundJavaExeFromJavaHome -set JAVA_EXEC="%JAVA_HOME%\bin\java.exe" - -:OkJava -set SONAR_RUNNER_HOME=%~dp0.. -goto sonarRunnerOpts - -@REM ==== HANDLE DEPRECATED SONAR_RUNNER_OPTS ==== -:sonarRunnerOpts -if "%SONAR_RUNNER_OPTS%" == "" ( - goto run -) else ( - echo WARN: SONAR_RUNNER_OPTS is deprecated. Please use SONAR_SCANNER_OPTS instead. - if "%SONAR_SCANNER_OPTS%" == "" (set SONAR_SCANNER_OPTS=%SONAR_RUNNER_OPTS%) -) - -@REM ==== START RUN ==== -:run - -set PROJECT_HOME=%CD% - -%JAVA_EXEC% -Djava.awt.headless=true %SONAR_SCANNER_OPTS% -cp "%SONAR_RUNNER_HOME%\lib\sonar-scanner-cli-${project.version}.jar" "-Dscanner.home=%SONAR_RUNNER_HOME%" "-Dproject.home=%PROJECT_HOME%" org.sonarsource.scanner.cli.Main %* -if ERRORLEVEL 1 goto error -goto end - -:error -set ERROR_CODE=1 - -@REM ==== END EXECUTION ==== - -:end -@REM set local scope for the variables with windows NT shell -@endlocal & set ERROR_CODE=%ERROR_CODE% - -@REM see http://code-bear.com/bearlog/2007/06/01/getting-the-exit-code-from-a-batch-file-that-is-run-from-a-python-program/ -goto exit - -:returncode -exit /B %1 - -:exit -call :returncode %ERROR_CODE% diff --git a/src/main/assembly/bin/sonar-scanner b/src/main/assembly/bin/sonar-scanner index 985de89..d6ffd57 100755 --- a/src/main/assembly/bin/sonar-scanner +++ b/src/main/assembly/bin/sonar-scanner @@ -3,7 +3,7 @@ # SonarQube Scanner Startup Script for Unix # # Optional ENV vars: -# SONAR_SCANNER_OPTS - Parameters passed to the Java VM when running SonarQube Scanner +# SONAR_SCANNER_OPTS - Parameters passed to the Java VM when running the SonarQube Scanner # SONAR_SCANNER_DEBUG_OPTS - Extra parameters passed to the Java VM for debugging # JAVA_HOME - Location of Java's installation diff --git a/src/main/assembly/bin/sonar-scanner-debug b/src/main/assembly/bin/sonar-scanner-debug index 257835e..d43b036 100755 --- a/src/main/assembly/bin/sonar-scanner-debug +++ b/src/main/assembly/bin/sonar-scanner-debug @@ -3,7 +3,7 @@ # SonarQube Scanner Startup Script for Unix # # Optional ENV vars: -# SONAR_SCANNER_OPTS - parameters passed to the Java VM when running SonarQube Scanner +# SONAR_SCANNER_OPTS - parameters passed to the Java VM when running the SonarQube Scanner # JAVA_HOME - Location of Java's installation SONAR_SCANNER_DEBUG_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000" diff --git a/src/main/assembly/bin/sonar-scanner-debug.bat b/src/main/assembly/bin/sonar-scanner-debug.bat index e0942c8..1ebc42c 100644 --- a/src/main/assembly/bin/sonar-scanner-debug.bat +++ b/src/main/assembly/bin/sonar-scanner-debug.bat @@ -4,8 +4,7 @@ @REM JAVA_HOME - location of a JDK home dir @REM @REM Optional ENV vars: -@REM SONAR_SCANNER_HOME - location of runner's installed home dir -@REM SONAR_SCANNER_OPTS - parameters passed to the Java VM when running Sonar +@REM SONAR_SCANNER_OPTS - parameters passed to the Java VM when running the SonarQube Scanner @setlocal @set SONAR_SCANNER_DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 diff --git a/src/main/assembly/bin/sonar-scanner.bat b/src/main/assembly/bin/sonar-scanner.bat index c75e28e..970af4b 100644 --- a/src/main/assembly/bin/sonar-scanner.bat +++ b/src/main/assembly/bin/sonar-scanner.bat @@ -4,7 +4,7 @@ @REM JAVA_HOME - location of a JDK home dir @REM @REM Optional ENV vars: -@REM SONAR_SCANNER_OPTS - parameters passed to the Java VM when running Sonar +@REM SONAR_SCANNER_OPTS - parameters passed to the Java VM when running the SonarQube Scanner @echo off diff --git a/src/main/assembly/conf/sonar-scanner.properties b/src/main/assembly/conf/sonar-scanner.properties index 69a52a2..b30e841 100644 --- a/src/main/assembly/conf/sonar-scanner.properties +++ b/src/main/assembly/conf/sonar-scanner.properties @@ -1,4 +1,4 @@ -#Configure here general information about the environment, such as SonarQube DB details for example +#Configure here general information about the environment, such as SonarQube server connection details for example #No information about specific project should appear here #----- Default SonarQube server diff --git a/src/main/assembly/noarch.xml b/src/main/assembly/noarch.xml index cce6955..e226a7a 100644 --- a/src/main/assembly/noarch.xml +++ b/src/main/assembly/noarch.xml @@ -12,7 +12,6 @@ src/main/assembly/bin bin - sonar-runner.bat sonar-scanner.bat sonar-scanner-debug.bat @@ -23,7 +22,6 @@ src/main/assembly/bin bin - sonar-runner sonar-scanner sonar-scanner-debug