aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/sonarsource/scanner/cli/CliTest.java
blob: 840f35b46441d14c010e8a718b7a8dcea9e0bf09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * SonarQube Scanner
 * Copyright (C) 2011-2016 SonarSource SA
 * mailto:contact 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 org.sonarsource.scanner.cli;

import org.junit.Test;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class CliTest {
  Exit exit = mock(Exit.class);
  Logs logs = new Logs(System.out, System.err);
  Cli cli = new Cli(exit, logs);

  @Test
  public void should_parse_empty_arguments() {
    cli.parse(new String[0]);
    assertThat(cli.properties()).isNotEmpty();
    assertThat(cli.isDebugEnabled()).isFalse();
    assertThat(cli.isDisplayStackTrace()).isFalse();
    assertThat(cli.isDisplayVersionOnly()).isFalse();
  }

  @Test
  public void should_extract_properties() {
    cli.parse(new String[] {"-D", "foo=bar", "--define", "hello=world", "-Dboolean"});
    assertThat(cli.properties().get("foo")).isEqualTo("bar");
    assertThat(cli.properties().get("hello")).isEqualTo("world");
    assertThat(cli.properties().get("boolean")).isEqualTo("true");
  }

  @Test
  public void should_parse_optional_task() {
    cli.parse(new String[] {"-D", "foo=bar"});
    assertThat(cli.properties().get("sonar.task")).isNull();

    cli.parse(new String[] {"views", "-D", "foo=bar"});
    assertThat(cli.properties().get("sonar.task")).isEqualTo("views");
  }

  @Test
  public void should_enable_debug_mode() {
    cli.parse(new String[] {"-X"});
    assertThat(cli.isDebugEnabled()).isTrue();
    assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true");
  }

  @Test
  public void should_enable_stacktrace_log() {
    cli.parse(new String[] {"-e"});
    assertThat(cli.isDebugEnabled()).isFalse();
    assertThat(cli.isDisplayStackTrace()).isTrue();
    assertThat(cli.properties().get("sonar.verbose")).isNull();
  }

  @Test
  public void should_disable_debug_mode_and_stacktrace_log_by_default() {
    cli.parse(new String[0]);
    assertThat(cli.isDebugEnabled()).isFalse();
    assertThat(cli.isDisplayStackTrace()).isFalse();
    assertThat(cli.properties().get("sonar.verbose")).isNull();
  }

  @Test
  public void should_show_usage() {
    logs = mock(Logs.class);
    cli = new Cli(exit, logs);
    cli.parse(new String[] {"-h"});
    verify(logs).info("usage: sonar-scanner [options]");
    verify(exit).exit(Exit.SUCCESS);
  }

  @Test
  public void should_show_usage_on_bad_syntax() {
    logs = mock(Logs.class);
    cli = new Cli(exit, logs);
    cli.parse(new String[] {"-w"});
    verify(logs).error("Unrecognized option: -w");
    verify(logs).info("usage: sonar-scanner [options]");
    verify(exit).exit(Exit.ERROR);
  }
}