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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2016 SonarSource SA
  4. * mailto:contact 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 org.sonarsource.scanner.cli.Cli;
  23. import org.sonarsource.scanner.cli.Exit;
  24. import org.sonarsource.scanner.cli.Logs;
  25. import static org.fest.assertions.Assertions.assertThat;
  26. import static org.mockito.Mockito.mock;
  27. import static org.mockito.Mockito.verify;
  28. public class CliTest {
  29. Exit exit = mock(Exit.class);
  30. Logs logs = new Logs(System.out, System.err);
  31. Cli cli = new Cli(exit, logs);
  32. @Test
  33. public void should_parse_empty_arguments() {
  34. cli.parse(new String[0]);
  35. assertThat(cli.properties()).isNotEmpty();
  36. assertThat(cli.isDebugMode()).isFalse();
  37. assertThat(cli.isDisplayStackTrace()).isFalse();
  38. assertThat(cli.isDisplayVersionOnly()).isFalse();
  39. }
  40. @Test
  41. public void should_extract_properties() {
  42. cli.parse(new String[] {"-D", "foo=bar", "--define", "hello=world", "-Dboolean"});
  43. assertThat(cli.properties().get("foo")).isEqualTo("bar");
  44. assertThat(cli.properties().get("hello")).isEqualTo("world");
  45. assertThat(cli.properties().get("boolean")).isEqualTo("true");
  46. }
  47. @Test
  48. public void dont_allow_interactive_fork() {
  49. cli.parse(new String[] {"-i", "-DsonarRunner.mode=fork"});
  50. cli.verify();
  51. verify(exit).exit(Exit.SUCCESS);
  52. }
  53. @Test
  54. public void should_parse_optional_task() {
  55. cli.parse(new String[] {"-D", "foo=bar"});
  56. assertThat(cli.properties().get("sonar.task")).isNull();
  57. cli.parse(new String[] {"views", "-D", "foo=bar"});
  58. assertThat(cli.properties().get("sonar.task")).isEqualTo("views");
  59. }
  60. @Test
  61. public void should_enable_debug_mode() {
  62. cli.parse(new String[] {"-X"});
  63. assertThat(cli.isDebugMode()).isTrue();
  64. assertThat(cli.isDisplayStackTrace()).isTrue();
  65. assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true");
  66. }
  67. @Test
  68. public void should_enable_stacktrace_log() {
  69. cli.parse(new String[] {"-e"});
  70. assertThat(cli.isDebugMode()).isFalse();
  71. assertThat(cli.isDisplayStackTrace()).isTrue();
  72. assertThat(cli.properties().get("sonar.verbose")).isNull();
  73. }
  74. @Test
  75. public void should_disable_debug_mode_and_stacktrace_log_by_default() {
  76. cli.parse(new String[0]);
  77. assertThat(cli.isDebugMode()).isFalse();
  78. assertThat(cli.isDisplayStackTrace()).isFalse();
  79. assertThat(cli.properties().get("sonar.verbose")).isNull();
  80. }
  81. }