aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/sonarsource/scanner/cli/CliTest.java
blob: 961f7d10ba7ebd38fd16b68b9bf30d286a014614 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * SonarScanner CLI
 * Copyright (C) 2011-2024 SonarSource SA
 * mailto:info 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.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class CliTest {
  private Exit exit = mock(Exit.class);
  private Logs logs = new Logs(System.out, System.err);
  private 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.isDisplayVersionOnly()).isFalse();
    assertThat(cli.isEmbedded()).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_warn_on_duplicate_properties() {
    logs = mock(Logs.class);
    cli = new Cli(exit, logs);
    cli.parse(new String[] {"-D", "foo=bar", "--define", "foo=baz"});
    verify(logs).warn("Property 'foo' with value 'bar' is overridden with value 'baz'");
  }

  @Test
  public void should_fail_on_missing_prop() {
    logs = mock(Logs.class);
    cli = new Cli(exit, logs);
    cli.parse(new String[] {"-D"});
    verify(logs).error("Missing argument for option -D/--define");
    verify(exit).exit(Exit.INTERNAL_ERROR);
  }

  @Test
  public void should_not_fail_with_errors_option() {
    cli.parse(new String[] {"-e"});
  }

  @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_debug_mode_full() {
    cli.parse(new String[] {"--debug"});
    assertThat(cli.isDebugEnabled()).isTrue();
    assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true");
  }

  @Test
  public void should_show_version() {
    cli.parse(new String[] {"-v"});
    assertThat(cli.isDisplayVersionOnly()).isTrue();
  }

  @Test
  public void should_show_version_full() {
    cli.parse(new String[] {"--version"});
    assertThat(cli.isDisplayVersionOnly()).isTrue();
  }

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

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

  @Test
  public void should_parse_from_argument() {
    cli.parse(new String[] {"--from=ScannerMSBuild/4.8"});
    assertThat(cli.getInvokedFrom()).isNotEmpty();
    assertThat(cli.getInvokedFrom()).isEqualTo("ScannerMSBuild/4.8");
  }

  @Test
  public void from_argument_is_only_from_let_value_empty() {
    cli.parse(new String[] {"--from="});
    assertThat(cli.getInvokedFrom()).isEmpty();
  }

  @Test
  public void should_disable_debug_mode_and_stacktrace_log_by_default() {
    cli.parse(new String[0]);
    assertThat(cli.isDebugEnabled()).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_full() {
    logs = mock(Logs.class);
    cli = new Cli(exit, logs);
    cli.parse(new String[] {"--help"});
    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.INTERNAL_ERROR);
  }

  @Test
  public void should_enable_embedded_mode() {
    cli.parse(new String[] {"--embedded"});
    assertThat(cli.isEmbedded()).isTrue();
  }
}