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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * SonarScanner CLI
  3. * Copyright (C) 2011-2022 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.mockito.Mockito.mock;
  24. import static org.mockito.Mockito.verify;
  25. public class CliTest {
  26. private Exit exit = mock(Exit.class);
  27. private Logs logs = new Logs(System.out, System.err);
  28. private Cli cli = new Cli(exit, logs);
  29. @Test
  30. public void should_parse_empty_arguments() {
  31. cli.parse(new String[0]);
  32. assertThat(cli.properties()).isNotEmpty();
  33. assertThat(cli.isDebugEnabled()).isFalse();
  34. assertThat(cli.isDisplayVersionOnly()).isFalse();
  35. assertThat(cli.isEmbedded()).isFalse();
  36. }
  37. @Test
  38. public void should_extract_properties() {
  39. cli.parse(new String[] {"-D", "foo=bar", "--define", "hello=world", "-Dboolean"});
  40. assertThat(cli.properties().get("foo")).isEqualTo("bar");
  41. assertThat(cli.properties().get("hello")).isEqualTo("world");
  42. assertThat(cli.properties().get("boolean")).isEqualTo("true");
  43. }
  44. @Test
  45. public void should_warn_on_duplicate_properties() {
  46. logs = mock(Logs.class);
  47. cli = new Cli(exit, logs);
  48. cli.parse(new String[] {"-D", "foo=bar", "--define", "foo=baz"});
  49. verify(logs).warn("Property 'foo' with value 'bar' is overridden with value 'baz'");
  50. }
  51. @Test
  52. public void should_fail_on_missing_prop() {
  53. logs = mock(Logs.class);
  54. cli = new Cli(exit, logs);
  55. cli.parse(new String[] {"-D"});
  56. verify(logs).error("Missing argument for option -D/--define");
  57. verify(exit).exit(Exit.INTERNAL_ERROR);
  58. }
  59. @Test
  60. public void should_not_fail_with_errors_option() {
  61. cli.parse(new String[] {"-e"});
  62. }
  63. @Test
  64. public void should_parse_optional_task() {
  65. cli.parse(new String[] {"-D", "foo=bar"});
  66. assertThat(cli.properties().get("sonar.task")).isNull();
  67. cli.parse(new String[] {"views", "-D", "foo=bar"});
  68. assertThat(cli.properties().get("sonar.task")).isEqualTo("views");
  69. }
  70. @Test
  71. public void should_enable_debug_mode() {
  72. cli.parse(new String[] {"-X"});
  73. assertThat(cli.isDebugEnabled()).isTrue();
  74. assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true");
  75. }
  76. @Test
  77. public void should_enable_debug_mode_full() {
  78. cli.parse(new String[] {"--debug"});
  79. assertThat(cli.isDebugEnabled()).isTrue();
  80. assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true");
  81. }
  82. @Test
  83. public void should_show_version() {
  84. cli.parse(new String[] {"-v"});
  85. assertThat(cli.isDisplayVersionOnly()).isTrue();
  86. }
  87. @Test
  88. public void should_show_version_full() {
  89. cli.parse(new String[] {"--version"});
  90. assertThat(cli.isDisplayVersionOnly()).isTrue();
  91. }
  92. @Test
  93. public void should_enable_stacktrace_log() {
  94. cli.parse(new String[] {"-e"});
  95. assertThat(cli.isDebugEnabled()).isFalse();
  96. assertThat(cli.properties().get("sonar.verbose")).isNull();
  97. }
  98. @Test
  99. public void should_enable_stacktrace_log_full() {
  100. cli.parse(new String[] {"--errors"});
  101. assertThat(cli.isDebugEnabled()).isFalse();
  102. assertThat(cli.properties().get("sonar.verbose")).isNull();
  103. }
  104. @Test
  105. public void should_parse_from_argument() {
  106. cli.parse(new String[] {"--from=ScannerMSBuild/4.8"});
  107. assertThat(cli.getInvokedFrom()).isNotEmpty();
  108. assertThat(cli.getInvokedFrom()).isEqualTo("ScannerMSBuild/4.8");
  109. }
  110. @Test
  111. public void from_argument_is_only_from_let_value_empty() {
  112. cli.parse(new String[] {"--from="});
  113. assertThat(cli.getInvokedFrom()).isEmpty();
  114. }
  115. @Test
  116. public void should_disable_debug_mode_and_stacktrace_log_by_default() {
  117. cli.parse(new String[0]);
  118. assertThat(cli.isDebugEnabled()).isFalse();
  119. assertThat(cli.properties().get("sonar.verbose")).isNull();
  120. }
  121. @Test
  122. public void should_show_usage() {
  123. logs = mock(Logs.class);
  124. cli = new Cli(exit, logs);
  125. cli.parse(new String[] {"-h"});
  126. verify(logs).info("usage: sonar-scanner [options]");
  127. verify(exit).exit(Exit.SUCCESS);
  128. }
  129. @Test
  130. public void should_show_usage_full() {
  131. logs = mock(Logs.class);
  132. cli = new Cli(exit, logs);
  133. cli.parse(new String[] {"--help"});
  134. verify(logs).info("usage: sonar-scanner [options]");
  135. verify(exit).exit(Exit.SUCCESS);
  136. }
  137. @Test
  138. public void should_show_usage_on_bad_syntax() {
  139. logs = mock(Logs.class);
  140. cli = new Cli(exit, logs);
  141. cli.parse(new String[] {"-w"});
  142. verify(logs).error("Unrecognized option: -w");
  143. verify(logs).info("usage: sonar-scanner [options]");
  144. verify(exit).exit(Exit.INTERNAL_ERROR);
  145. }
  146. @Test
  147. public void should_enable_embedded_mode() {
  148. cli.parse(new String[] {"--embedded"});
  149. assertThat(cli.isEmbedded()).isTrue();
  150. }
  151. }