Skip to content

Commit 2441aec

Browse files
committed
Improve code coverage
1 parent 809b609 commit 2441aec

File tree

15 files changed

+124
-16
lines changed

15 files changed

+124
-16
lines changed

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>org.elegoff.plugins</groupId>
6+
<groupId>org.elegoff</groupId>
77
<artifactId>sonar-rust-plugin</artifactId>
88
<packaging>sonar-plugin</packaging>
99
<version>0.0.1</version>
1010

11-
<name>Rust analyzer</name>
12-
<description>Plugin for the Rust programming language</description>
11+
<name>Rust report analyzer</name>
12+
<description>Plugin for importing Rust reports</description>
1313
<url>https://github.com/elegoff/sonar-rust</url>
1414

1515
<properties>
@@ -162,7 +162,7 @@
162162
<version>1.18.0.372</version>
163163
<extensions>true</extensions>
164164
<configuration>
165-
<pluginKey>rustlint</pluginKey>
165+
<pluginKey>rustreport</pluginKey>
166166
<pluginClass>org.elegoff.plugins.rust.RustPlugin</pluginClass>
167167
</configuration>
168168
</plugin>

src/main/java/org/elegoff/plugins/rust/clippy/ClippyRulesDefinition.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@
1616
*/
1717
package org.elegoff.plugins.rust.clippy;
1818

19-
import java.util.Arrays;
20-
import java.util.List;
21-
2219
import org.elegoff.plugins.rust.languages.RustLanguage;
2320
import org.sonar.api.server.rule.RulesDefinition;
2421
import org.sonarsource.analyzer.commons.ExternalRuleLoader;
2522

26-
2723
import static org.elegoff.plugins.rust.clippy.ClippySensor.LINTER_KEY;
2824
import static org.elegoff.plugins.rust.clippy.ClippySensor.LINTER_NAME;
2925

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.elegoff.plugins.rust;
18+
19+
import org.elegoff.plugins.rust.languages.RustLanguage;
20+
import org.sonar.api.batch.fs.InputFile;
21+
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
22+
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
23+
import org.sonar.api.batch.sensor.internal.SensorContextTester;
24+
25+
import java.io.IOException;
26+
import java.nio.charset.StandardCharsets;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
import java.nio.file.Paths;
30+
31+
public class Utils {
32+
public static final String MODULE_KEY = "sonar-rust";
33+
34+
public static final Path BASE_DIR = Paths.get("src", "test", "resources", "org", "elegoff", "plugins", "rust");
35+
36+
37+
private Utils() {
38+
}
39+
40+
41+
public static InputFile getInputFile(String relativePath) throws IOException {
42+
return TestInputFileBuilder.create(MODULE_KEY, BASE_DIR.resolve(relativePath).toString())
43+
.setModuleBaseDir(Paths.get("."))
44+
.setContents(new String(Files.readAllBytes(BASE_DIR.resolve(relativePath))))
45+
.setLanguage(RustLanguage.KEY)
46+
.setCharset(StandardCharsets.UTF_8)
47+
.build();
48+
}
49+
50+
public static SensorContextTester getSensorContext() {
51+
return SensorContextTester.create(BASE_DIR);
52+
}
53+
54+
public static DefaultFileSystem getFileSystem() {
55+
return new DefaultFileSystem(BASE_DIR);
56+
}
57+
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.elegoff.plugins.rust.externalreport.clippy;
17+
package org.elegoff.plugins.rust.clippy;
1818

19-
import org.elegoff.plugins.rust.clippy.ClippyJsonReportReader;
2019
import org.junit.Test;
2120
import org.sonarsource.analyzer.commons.internal.json.simple.JSONArray;
2221
import org.sonarsource.analyzer.commons.internal.json.simple.JSONObject;
@@ -49,7 +48,7 @@ public void invalidReportPathProvided(){
4948

5049
@Test
5150
public void emptyReport() {
52-
File empty = this.getFileFromResources("org/sonar/plugins/rust/clippy/empty-report.txt");
51+
File empty = this.getFileFromResources("org/elegoff/plugins/rust/clippy/empty-report.txt");
5352
InputStream in = null;
5453
try {
5554
in = ClippyJsonReportReader.toJSON(empty);
@@ -64,7 +63,7 @@ public void emptyReport() {
6463

6564
@Test
6665
public void validReport() {
67-
File report = this.getFileFromResources("org/sonar/plugins/rust/clippy/myreport.txt");
66+
File report = this.getFileFromResources("org/elegoff/plugins/rust/clippy/myreport.txt");
6867
InputStream in = null;
6968
try {
7069
in = ClippyJsonReportReader.toJSON(report);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.elegoff.plugins.rust.externalreport.clippy;
17+
package org.elegoff.plugins.rust.clippy;
1818

1919
import org.elegoff.plugins.rust.clippy.ClippyRulesDefinition;
2020
import org.junit.Test;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.elegoff.plugins.rust.externalreport.clippy;
17+
package org.elegoff.plugins.rust.clippy;
1818

1919
import java.io.IOException;
2020
import java.nio.file.Files;
@@ -53,7 +53,7 @@ public class ClippySensorTest{
5353
private static final String CLIPPY_REPORT_TXT = "myreport.txt";
5454
private static final String UNKNOWN_KEY_REPORT = "synreport.txt";
5555

56-
private static final Path PROJECT_DIR = Paths.get("src", "test", "resources", "org", "sonar", "plugins", "rust", "clippy");
56+
private static final Path PROJECT_DIR = Paths.get("src", "test", "resources", "org", "elegoff", "plugins", "rust", "clippy");
5757

5858
private static ClippySensor clippySensor = new ClippySensor();
5959

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.elegoff.plugins.rust.language;
18+
19+
import org.elegoff.plugins.rust.Utils;
20+
import org.elegoff.plugins.rust.languages.RustSourceCode;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.sonar.api.batch.fs.InputFile;
24+
25+
import java.io.IOException;
26+
import java.util.Optional;
27+
28+
import static org.junit.Assert.assertEquals;
29+
30+
public class RustSourceCodeTest {
31+
InputFile inputFile;
32+
RustSourceCode code;
33+
34+
@Before
35+
public void setInputFile() throws IOException {
36+
inputFile = Utils.getInputFile("clippy/main.rs");
37+
code = new RustSourceCode(inputFile, Optional.of(Boolean.FALSE));
38+
}
39+
40+
@Test
41+
public void testGetRustFile() throws IOException {
42+
assertEquals(inputFile, code.getRustFile());
43+
assertEquals("fn main() {\n" +
44+
" println!(\"Checking issues\");\n" +
45+
" absurd_extreme_comparison();\n" +
46+
" println!(\"Done\");\n" +
47+
"}\n" +
48+
"\n" +
49+
"fn absurd_extreme_comparison(){\n" +
50+
" let vec: Vec<isize> = Vec::new();\n" +
51+
" if vec.len() <= 0 {}\n" +
52+
" if 100 > std::i32::MAX {}\n" +
53+
"}", code.getContent());
54+
}
55+
56+
}

0 commit comments

Comments
 (0)