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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2020 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_fail_on_missing_prop() {
  46. logs = mock(Logs.class);
  47. cli = new Cli(exit, logs);
  48. cli.parse(new String[] {"-D"});
  49. verify(logs).error("Missing argument for option -D/--define");
  50. verify(exit).exit(Exit.INTERNAL_ERROR);
  51. }
  52. @Test
  53. public void should_not_fail_with_errors_option() {
  54. cli.parse(new String[] {"-e"});
  55. }
  56. @Test
  57. public void should_parse_optional_task() {
  58. cli.parse(new String[] {"-D", "foo=bar"});
  59. assertThat(cli.properties().get("sonar.task")).isNull();
  60. cli.parse(new String[] {"views", "-D", "foo=bar"});
  61. assertThat(cli.properties().get("sonar.task")).isEqualTo("views");
  62. }
  63. @Test
  64. public void should_enable_debug_mode() {
  65. cli.parse(new String[] {"-X"});
  66. assertThat(cli.isDebugEnabled()).isTrue();
  67. assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true");
  68. }
  69. @Test
  70. public void should_enable_debug_mode_full() {
  71. cli.parse(new String[] {"--debug"});
  72. assertThat(cli.isDebugEnabled()).isTrue();
  73. assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true");
  74. }
  75. @Test
  76. public void should_show_version() {
  77. cli.parse(new String[] {"-v"});
  78. assertThat(cli.isDisplayVersionOnly()).isTrue();
  79. }
  80. @Test
  81. public void should_show_version_full() {
  82. cli.parse(new String[] {"--version"});
  83. assertThat(cli.isDisplayVersionOnly()).isTrue();
  84. }
  85. @Test
  86. public void should_enable_stacktrace_log() {
  87. cli.parse(new String[] {"-e"});
  88. assertThat(cli.isDebugEnabled()).isFalse();
  89. assertThat(cli.properties().get("sonar.verbose")).isNull();
  90. }
  91. @Test
  92. public void should_enable_stacktrace_log_full() {
  93. cli.parse(new String[] {"--errors"});
  94. assertThat(cli.isDebugEnabled()).isFalse();
  95. assertThat(cli.properties().get("sonar.verbose")).isNull();
  96. }
  97. @Test
  98. public void should_disable_debug_mode_and_stacktrace_log_by_default() {
  99. cli.parse(new String[0]);
  100. assertThat(cli.isDebugEnabled()).isFalse();
  101. assertThat(cli.properties().get("sonar.verbose")).isNull();
  102. }
  103. @Test
  104. public void should_show_usage() {
  105. logs = mock(Logs.class);
  106. cli = new Cli(exit, logs);
  107. cli.parse(new String[] {"-h"});
  108. verify(logs).info("usage: sonar-scanner [options]");
  109. verify(exit).exit(Exit.SUCCESS);
  110. }
  111. @Test
  112. public void should_show_usage_full() {
  113. logs = mock(Logs.class);
  114. cli = new Cli(exit, logs);
  115. cli.parse(new String[] {"--help"});
  116. verify(logs).info("usage: sonar-scanner [options]");
  117. verify(exit).exit(Exit.SUCCESS);
  118. }
  119. @Test
  120. public void should_show_usage_on_bad_syntax() {
  121. logs = mock(Logs.class);
  122. cli = new Cli(exit, logs);
  123. cli.parse(new String[] {"-w"});
  124. verify(logs).error("Unrecognized option: -w");
  125. verify(logs).info("usage: sonar-scanner [options]");
  126. verify(exit).exit(Exit.INTERNAL_ERROR);
  127. }
  128. @Test
  129. public void should_enable_embedded_mode() {
  130. cli.parse(new String[] {"--embedded"});
  131. assertThat(cli.isEmbedded()).isTrue();
  132. }
  133. }