-
Notifications
You must be signed in to change notification settings - Fork 176
/
TypeScriptAnalysisTest.java
220 lines (184 loc) · 9.05 KB
/
TypeScriptAnalysisTest.java
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2012-2020 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 com.sonar.javascript.it.plugin;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.SonarScanner;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonarqube.ws.Issues.Issue;
import static com.sonar.javascript.it.plugin.Tests.getIssues;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
public class TypeScriptAnalysisTest {
@ClassRule
public static final Orchestrator orchestrator = Tests.ORCHESTRATOR;
private static final File PROJECT_DIR = TestUtils.projectDir("tsproject");
@BeforeClass
public static void startServer() throws Exception {
orchestrator.resetData();
TestUtils.npmInstall(PROJECT_DIR);
}
@Test
public void test() throws Exception {
String projectKey = "tsproject";
SonarScanner build = SonarScanner.create()
.setProjectKey(projectKey)
.setSourceEncoding("UTF-8")
.setSourceDirs(".")
.setProjectDir(PROJECT_DIR);
Tests.setProfile(projectKey, "eslint-based-rules-profile", "ts");
BuildResult result = orchestrator.executeBuild(build);
String sampleFileKey = projectKey + ":sample.lint.ts";
List<Issue> issuesList = getIssues(sampleFileKey);
assertThat(issuesList).hasSize(1);
assertThat(issuesList.get(0).getLine()).isEqualTo(4);
assertThat(Tests.getMeasureAsInt(sampleFileKey, "ncloc")).isEqualTo(7);
assertThat(Tests.getMeasureAsInt(sampleFileKey, "classes")).isEqualTo(0);
assertThat(Tests.getMeasureAsInt(sampleFileKey, "functions")).isEqualTo(1);
assertThat(Tests.getMeasureAsInt(sampleFileKey, "statements")).isEqualTo(3);
assertThat(Tests.getMeasureAsInt(sampleFileKey, "comment_lines")).isEqualTo(1);
assertThat(Tests.getMeasureAsInt(sampleFileKey, "complexity")).isEqualTo(2);
assertThat(Tests.getMeasureAsInt(sampleFileKey, "cognitive_complexity")).isEqualTo(2);
assertThat(Tests.getMeasureAsDouble(projectKey, "duplicated_lines")).isEqualTo(111.0);
assertThat(Tests.getMeasureAsDouble(projectKey, "duplicated_blocks")).isEqualTo(2.0);
assertThat(Tests.getMeasureAsDouble(projectKey, "duplicated_files")).isEqualTo(1.0);
issuesList = getIssues(projectKey + ":nosonar.lint.ts");
assertThat(issuesList).hasSize(1);
assertThat(result.getLogsLines(log -> log.contains("Found 1 tsconfig.json file(s)"))).hasSize(1);
}
@Test
public void should_use_custom_tsconfig() throws Exception {
String projectKey = "tsproject-custom";
SonarScanner build = SonarScanner.create()
.setProjectKey(projectKey)
.setSourceEncoding("UTF-8")
.setSourceDirs(".")
.setProjectDir(PROJECT_DIR)
.setProperty("sonar.typescript.tsconfigPath", "custom.tsconfig.json");
Tests.setProfile(projectKey, "eslint-based-rules-profile", "ts");
BuildResult result = orchestrator.executeBuild(build);
List<Issue> issuesList = getIssues(projectKey);
assertThat(issuesList).hasSize(1);
Issue issue = issuesList.get(0);
assertThat(issue.getLine()).isEqualTo(2);
assertThat(issue.getComponent()).isEqualTo(projectKey + ":fileUsedInCustomTsConfig.ts");
Path tsconfig = PROJECT_DIR.toPath().resolve("custom.tsconfig.json").toAbsolutePath();
assertThat(result.getLogsLines(l -> l.contains("Using " + tsconfig + " from sonar.typescript.tsconfigPath property"))).hasSize(1);
}
@Test
public void test_missing_typescript() throws Exception {
File dir = TestUtils.projectDir("tsproject-no-typescript");
File node_modules = new File(dir, "node_modules");
if (node_modules.exists()) {
FileUtils.deleteDirectory(node_modules);
}
String projectKey = "tsproject-no-typescript";
SonarScanner build = SonarScanner.create()
.setProjectKey(projectKey)
.setSourceEncoding("UTF-8")
.setSourceDirs(".")
.setProjectDir(dir);
Tests.setProfile(projectKey, "eslint-based-rules-profile", "ts");
BuildResult result = orchestrator.executeBuild(build);
assertThat(result.isSuccess()).isTrue();
assertThat(result.getLogsLines(l -> l.contains("TypeScript dependency was not found inside project directory, " +
"Node.js will search TypeScript using module resolution algorithm; analysis will fail without TypeScript."))).hasSize(1);
assertThat(result.getLogsLines(l -> l.contains("TypeScript dependency was not found and it is required for analysis."))).hasSize(1);
assertThat(result.getLogsLines(l -> l.contains("Install TypeScript in the project directory or use NODE_PATH env. " +
"variable to set TypeScript location, if it's located outside of project directory."))).hasSize(1);
}
@Test
public void test_incompatible_typescript() throws Exception {
File dir = TestUtils.projectDir("tsproject-no-typescript");
TestUtils.npmInstall(dir, "typescript@2.6.2", "--no-save");
String projectKey = "tsproject-old-typescript";
SonarScanner build = SonarScanner.create()
.setProjectKey(projectKey)
.setSourceEncoding("UTF-8")
.setSourceDirs(".")
.setProjectDir(dir);
Tests.setProfile(projectKey, "eslint-based-rules-profile", "ts");
BuildResult result = orchestrator.executeBuild(build);
assertThat(result.isSuccess()).isTrue();
assertThat(result.getLogsLines(l -> l.contains("You are using version of TypeScript 2.6.2 which is not supported; supported versions >=3.3.1"))).hasSize(1);
}
@Test
public void test_new_typescript() throws Exception {
File dir = TestUtils.projectDir("tsproject-no-typescript");
String tsVersion = "4.0.0-beta";
TestUtils.npmInstall(dir, "typescript@" + tsVersion, "--no-save");
String projectKey = "tsproject-new-typescript";
SonarScanner build = SonarScanner.create()
.setProjectKey(projectKey)
.setSourceEncoding("UTF-8")
.setSourceDirs(".")
.setProjectDir(dir);
Tests.setProfile(projectKey, "eslint-based-rules-profile", "ts");
BuildResult result = orchestrator.executeBuild(build);
assertThat(result.isSuccess()).isTrue();
assertThat(result.getLogsLines(l -> l.contains("You are using version of TypeScript " + tsVersion + " which is not officially supported; supported versions >=3.3.1 <3.10.0"))).hasSize(1);
}
@Test
public void should_analyze_without_tsconfig() throws Exception {
File dir = TestUtils.projectDir("missing-tsconfig");
TestUtils.npmInstall(dir);
String projectKey = "missing-tsconfig";
SonarScanner build = SonarScanner.create()
.setProjectKey(projectKey)
.setSourceEncoding("UTF-8")
.setSourceDirs(".")
.setProjectDir(dir)
.setDebugLogs(true);
Tests.setProfile(projectKey, "eslint-based-rules-profile", "ts");
BuildResult result = orchestrator.executeBuild(build);
List<Issue> issuesList = getIssues(projectKey);
assertThat(issuesList).extracting(Issue::getLine, Issue::getRule, Issue::getComponent).containsExactly(
tuple(2, "typescript:S4325", "missing-tsconfig:src/main.ts")
);
assertThat(result.getLogsLines(l -> l.contains("Using generated tsconfig.json file"))).hasSize(1);
}
@Test
public void should_exclude_from_extended_tsconfig() throws Exception {
File dir = TestUtils.projectDir("tsproject-extended");
TestUtils.npmInstall(dir);
String projectKey = "tsproject-extended";
SonarScanner build = SonarScanner.create()
.setProjectKey(projectKey)
.setSourceEncoding("UTF-8")
.setSourceDirs(".")
.setProjectDir(dir)
.setDebugLogs(true);
Tests.setProfile(projectKey, "eslint-based-rules-profile", "ts");
BuildResult result = orchestrator.executeBuild(build);
List<Issue> issuesList = getIssues(projectKey);
assertThat(issuesList).extracting(Issue::getLine, Issue::getRule, Issue::getComponent).containsExactly(
tuple(2, "typescript:S3923", "tsproject-extended:dir/file.ts")
);
// Limitation of our analyzer, exclusions from extended tsconfig are for some reason are not considered
// in perfect world we should not even try to analyze this excluded file
assertThat(result.getLogsLines(l -> l.contains("Failed to analyze file [dir/file.excluded.ts]"))).hasSize(1);
}
}