You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CliTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * SonarScanner CLI
  3. * Copyright (C) 2011-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarsource.scanner.cli;
  21. import org.junit.Test;
  22. import static org.assertj.core.api.Assertions.assertThat;
  23. import static org.assertj.core.api.Assertions.assertThatNoException;
  24. import static org.assertj.core.api.Assertions.entry;
  25. import static org.mockito.Mockito.mock;
  26. import static org.mockito.Mockito.verify;
  27. public class CliTest {
  28. private final Exit exit = mock(Exit.class);
  29. private Logs logs = new Logs(System.out, System.err);
  30. private Cli cli = new Cli(exit, logs);
  31. @Test
  32. public void should_parse_empty_arguments() {
  33. cli.parse(new String[0]);
  34. assertThat(cli.properties()).isNotEmpty();
  35. assertThat(cli.isDebugEnabled()).isFalse();
  36. assertThat(cli.isDisplayVersionOnly()).isFalse();
  37. assertThat(cli.isEmbedded()).isFalse();
  38. }
  39. @Test
  40. public void should_extract_properties() {
  41. cli.parse(new String[]{"-D", "foo=bar", "--define", "hello=world", "-Dboolean"});
  42. assertThat(cli.properties()).contains(
  43. entry("foo", "bar"),
  44. entry("hello", "world"),
  45. entry("boolean", "true"));
  46. }
  47. @Test
  48. public void should_warn_on_duplicate_properties() {
  49. logs = mock(Logs.class);
  50. cli = new Cli(exit, logs);
  51. cli.parse(new String[]{"-D", "foo=bar", "--define", "foo=baz"});
  52. verify(logs).warn("Property 'foo' with value 'bar' is overridden with value 'baz'");
  53. }
  54. @Test
  55. public void should_fail_on_missing_prop() {
  56. logs = mock(Logs.class);
  57. cli = new Cli(exit, logs);
  58. cli.parse(new String[]{"-D"});
  59. verify(logs).error("Missing argument for option -D/--define");
  60. verify(exit).exit(Exit.INTERNAL_ERROR);
  61. }
  62. @Test
  63. public void should_not_fail_with_errors_option() {
  64. assertThatNoException().isThrownBy(() -> cli.parse(new String[]{"-e"}));
  65. }
  66. @Test
  67. public void should_enable_debug_mode() {
  68. cli.parse(new String[]{"-X"});
  69. assertThat(cli.isDebugEnabled()).isTrue();
  70. assertThat(cli.properties()).containsEntry("sonar.verbose", "true");
  71. }
  72. @Test
  73. public void should_enable_debug_mode_full() {
  74. cli.parse(new String[]{"--debug"});
  75. assertThat(cli.isDebugEnabled()).isTrue();
  76. assertThat(cli.properties()).containsEntry("sonar.verbose", "true");
  77. }
  78. @Test
  79. public void should_show_version() {
  80. cli.parse(new String[]{"-v"});
  81. assertThat(cli.isDisplayVersionOnly()).isTrue();
  82. }
  83. @Test
  84. public void should_show_version_full() {
  85. cli.parse(new String[]{"--version"});
  86. assertThat(cli.isDisplayVersionOnly()).isTrue();
  87. }
  88. @Test
  89. public void should_enable_stacktrace_log() {
  90. cli.parse(new String[]{"-e"});
  91. assertThat(cli.isDebugEnabled()).isFalse();
  92. assertThat(cli.properties().get("sonar.verbose")).isNull();
  93. }
  94. @Test
  95. public void should_enable_stacktrace_log_full() {
  96. cli.parse(new String[]{"--errors"});
  97. assertThat(cli.isDebugEnabled()).isFalse();
  98. assertThat(cli.properties().get("sonar.verbose")).isNull();
  99. }
  100. @Test
  101. public void should_parse_from_argument() {
  102. cli.parse(new String[]{"--from=ScannerMSBuild/4.8"});
  103. assertThat(cli.getInvokedFrom()).isNotEmpty();
  104. assertThat(cli.getInvokedFrom()).isEqualTo("ScannerMSBuild/4.8");
  105. }
  106. @Test
  107. public void from_argument_is_only_from_let_value_empty() {
  108. cli.parse(new String[]{"--from="});
  109. assertThat(cli.getInvokedFrom()).isEmpty();
  110. }
  111. @Test
  112. public void should_disable_debug_mode_and_stacktrace_log_by_default() {
  113. cli.parse(new String[0]);
  114. assertThat(cli.isDebugEnabled()).isFalse();
  115. assertThat(cli.properties().get("sonar.verbose")).isNull();
  116. }
  117. @Test
  118. public void should_show_usage() {
  119. logs = mock(Logs.class);
  120. cli = new Cli(exit, logs);
  121. cli.parse(new String[]{"-h"});
  122. verify(logs).info("usage: sonar-scanner [options]");
  123. verify(exit).exit(Exit.SUCCESS);
  124. }
  125. @Test
  126. public void should_show_usage_full() {
  127. logs = mock(Logs.class);
  128. cli = new Cli(exit, logs);
  129. cli.parse(new String[]{"--help"});
  130. verify(logs).info("usage: sonar-scanner [options]");
  131. verify(exit).exit(Exit.SUCCESS);
  132. }
  133. @Test
  134. public void should_show_usage_on_bad_syntax() {
  135. logs = mock(Logs.class);
  136. cli = new Cli(exit, logs);
  137. cli.parse(new String[]{"-w"});
  138. verify(logs).error("Unrecognized option: -w");
  139. verify(logs).info("usage: sonar-scanner [options]");
  140. verify(exit).exit(Exit.INTERNAL_ERROR);
  141. }
  142. @Test
  143. public void should_enable_embedded_mode() {
  144. cli.parse(new String[]{"--embedded"});
  145. assertThat(cli.isEmbedded()).isTrue();
  146. }
  147. }