Skip to content

Commit 809b609

Browse files
committed
Improve code coverage
1 parent ce4b0e0 commit 809b609

File tree

9 files changed

+163
-5
lines changed

9 files changed

+163
-5
lines changed

src/main/java/org/elegoff/plugins/rust/RustPlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public void define(Context context) {
5454
context.addExtension(ClippySensor.class);
5555
context.addExtensions(
5656
PropertyDefinition.builder(ClippySensor.REPORT_PROPERTY_KEY)
57-
.name("Bandit Report Files")
58-
.description("Paths (absolute or relative) to json files with Bandit issues.")
57+
.name("Clippy Report Files")
58+
.description("Paths (absolute or relative) to json files with Clippy issues.")
5959
.category(EXTERNAL_ANALYZERS_CATEGORY)
6060
.subCategory(RUST_SUBCATEGORY)
6161
.onQualifiers(Qualifiers.PROJECT)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private void onResult(JSONObject result) {
8989
clippyIssue.filePath = (String) span.get("file_name");
9090
clippyIssue.message = (String) message.get(MESSAGE);
9191
JSONArray children = (JSONArray) message.get("children");
92-
if ((children != null) && !children.isEmpty()){
92+
if ((clippyIssue.message != null)&&(children != null) && !children.isEmpty()){
9393
addHelpDetails(clippyIssue, children);
9494
}
9595
clippyIssue.lineNumberStart = toInteger(span.get("line_start"));

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public class ClippyRulesDefinition implements RulesDefinition {
3131

3232
private static final String RULES_JSON = "org/elegoff/l10n/rust/rules/rust/clippy/rules.json";
3333
private static final String RULE_REPOSITORY_LANGUAGE = RustLanguage.KEY;
34-
private static final List<String> TEXT_FILE_EXTENSIONS = Arrays.asList(".rs");
3534
static final ExternalRuleLoader RULE_LOADER = new ExternalRuleLoader(LINTER_KEY, LINTER_NAME, RULES_JSON, RULE_REPOSITORY_LANGUAGE);
3635

3736
@Override
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 junit.framework.TestCase;
20+
import org.elegoff.plugins.rust.clippy.ClippyRulesDefinition;
21+
import org.junit.Test;
22+
import org.sonar.api.SonarEdition;
23+
import org.sonar.api.SonarRuntime;
24+
import org.sonar.api.utils.Version;
25+
import org.sonar.api.Plugin;
26+
import org.sonar.api.SonarQubeSide;
27+
import org.sonar.api.internal.SonarRuntimeImpl;
28+
29+
import java.util.List;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
public class RustPluginTest extends TestCase {
34+
@Test
35+
public void testGetExtensions() {
36+
Version v79 = Version.create(7, 9);
37+
SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(v79, SonarQubeSide.SERVER, SonarEdition.DEVELOPER);
38+
assertThat(extensions(runtime)).hasSize(7);
39+
assertThat(extensions(runtime)).contains(ClippyRulesDefinition.class);
40+
assertThat(extensions(SonarRuntimeImpl.forSonarLint(v79))).hasSize(7);
41+
}
42+
43+
private static List extensions(SonarRuntime runtime) {
44+
Plugin.Context context = new Plugin.Context(runtime);
45+
new RustPlugin().define(context);
46+
return context.getExtensions();
47+
}
48+
}

src/test/java/org/elegoff/plugins/rust/externalreport/clippy/ClippySensorTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,26 @@ public void noIssuesWithInvalidReportPath() throws IOException {
119119
.startsWith("No issues information will be saved as the report file '")
120120
.contains("invalid-path.txt' can't be read.");
121121
}
122+
/*
123+
@Test
124+
public void issuesWhenClippyFileHasErrors() throws IOException {
125+
List<ExternalIssue> externalIssues = executeSensorImporting(7, 9, "wrongpaths.txt");
126+
assertThat(externalIssues).hasSize(1);
122127
128+
ExternalIssue first = externalIssues.get(0);
129+
assertThat(first.primaryLocation().inputComponent().key()).isEqualTo(CLIPPY_FILE);
130+
assertThat(first.ruleKey().toString()).isEqualTo(CLIPPY_AEC);
131+
assertThat(first.type()).isEqualTo(RuleType.CODE_SMELL);
132+
assertThat(first.severity()).isEqualTo(Severity.MAJOR);
133+
assertThat(first.primaryLocation().message()).isEqualTo("A message");
134+
assertThat(first.primaryLocation().textRange()).isNull();
135+
136+
assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
137+
assertThat(onlyOneLogElement(logTester.logs(LoggerLevel.WARN)))
138+
.isEqualTo("Failed to resolve 1 file path(s) in Clippy report. No issues imported related to file(s): clippy/wrong.rs");
139+
assertThat(logTester.logs(LoggerLevel.DEBUG)).containsExactly("Missing information for ruleKey:'clippy::absurd_extreme_comparisons', filePath:'clippy/wrong.rs', message:'null'");
140+
}
141+
*/
123142
@Test
124143
public void noIssuesWithEmptyClippyReport() throws IOException {
125144
List<ExternalIssue> externalIssues = executeSensorImporting(7, 9, "empty-report.txt");
@@ -128,7 +147,7 @@ public void noIssuesWithEmptyClippyReport() throws IOException {
128147
}
129148

130149
@Test
131-
public void clippyReportWithUnknownRuleKey() throws IOException {
150+
public void clippyReportWithSuggestedChanges() throws IOException {
132151
List<ExternalIssue> externalIssues = executeSensorImporting(7, 9, UNKNOWN_KEY_REPORT);
133152
assertThat(externalIssues).hasSize(4);
134153

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.RustPlugin;
20+
import org.elegoff.plugins.rust.languages.RustLanguage;
21+
import org.elegoff.plugins.rust.settings.RustLanguageSettings;
22+
import org.junit.Test;
23+
import org.sonar.api.config.internal.ConfigurationBridge;
24+
import org.sonar.api.config.internal.MapSettings;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
public class RustLanguageTest {
29+
@Test
30+
public void test() {
31+
RustLanguage language = new RustLanguage(new ConfigurationBridge(new MapSettings()));
32+
assertThat(language.getKey()).isEqualTo("rust");
33+
assertThat(language.getName()).isEqualTo("Rust");
34+
assertThat(language.getFileSuffixes()).hasSize(1).contains(".rs");
35+
}
36+
37+
@Test
38+
public void custom_file_suffixes() {
39+
MapSettings settings = new MapSettings();
40+
settings.setProperty(RustLanguageSettings.FILE_SUFFIXES_KEY, "rs");
41+
42+
RustLanguage language = new RustLanguage(new ConfigurationBridge(settings));
43+
assertThat(language.getFileSuffixes()).hasSize(1).contains("rs");
44+
}
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.languages.RustQualityProfile;
20+
import junit.framework.TestCase;
21+
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
22+
23+
24+
25+
public class RustQualityProfileTest extends TestCase{
26+
public void testDefine() {
27+
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
28+
RustQualityProfile qp = new RustQualityProfile();
29+
qp.define(context);
30+
BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = context.profile("rust", "RUST Rules");
31+
assertNotNull(profile);
32+
assertTrue(profile.isDefault());
33+
assertEquals(0, profile.rules().size());
34+
}
35+
36+
37+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Checking clippy-issues v0.1.0 (/Users/ericlegoff/lab/clippy-issues)
2+
{"reason":"compiler-message","package_id":"clippy-issues 0.1.0 (path+file:///Users/ericlegoff/lab/clippy-issues)","target":{"kind":["bin"],"crate_types":["bin"],"name":"clippy-issues","src_path":"/Users/ericlegoff/lab/clippy-issues/src/main.rs","edition":"2018","doctest":false},"message":{"rendered":"error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false\n --> clippy/main.rs:9:8\n |\n9 | if vec.len() <= 0 {}\n | ^^^^^^^^^^^^^^\n |\n = note: `#[deny(clippy::absurd_extreme_comparisons)]` on by default\n = help: because `0` is the minimum value for this type, the case where the two sides are not equal never occurs, consider using `vec.len() == 0` instead\n = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons\n\n","children":[{"children":[],"code":null,"level":"note","message":"`#[deny(clippy::absurd_extreme_comparisons)]` on by default","rendered":null,"spans":[]},{"children":[],"code":null,"level":"help","message":"","rendered":null,"spans":[]},{"children":[],"code":null,"level":"help","message":"for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons","rendered":null,"spans":[]}],"code":{"code":"clippy::absurd_extreme_comparisons","explanation":null},"level":"error","message":"A message","spans":[{"byte_end":194,"byte_start":180,"column_end":22,"column_start":8,"expansion":null,"file_name":"clippy/main.rs","is_primary":true,"label":null,"line_end":null,"line_start":null,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":22,"highlight_start":8,"text":" if vec.len() <= 0 {}"}]}]}}
3+
{"reason":"compiler-message","package_id":"clippy-issues 0.1.0 (path+file:///Users/ericlegoff/lab/clippy-issues)","target":{"kind":["bin"],"crate_types":["bin"],"name":"clippy-issues","src_path":"/Users/ericlegoff/lab/clippy-issues/src/main.rs","edition":"2018","doctest":false},"message":{"rendered":"error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false\n --> clippy/main.rs:10:8\n |\n10 | if 100 > std::i32::MAX {}\n | ^^^^^^^^^^^^^^^^^^^\n |\n = help: because `std::i32::MAX` is the maximum value for this type, this comparison is always false\n = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons\n\n","children":[{"children":[],"code":null,"level":"help","message":"because `std::i32::MAX` is the maximum value for this type, this comparison is always false","rendered":null,"spans":[]},{"children":[],"code":null,"level":"help","message":"for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons","rendered":null,"spans":[]}],"code":{"code":"clippy::absurd_extreme_comparisons","explanation":null},"level":"error","message":"this comparison involving the minimum or maximum element for this type contains a case that is always true or always false","spans":[{"byte_end":224,"byte_start":205,"column_end":27,"column_start":8,"expansion":null,"file_name":"clippy/wrong.rs","is_primary":true,"label":null,"line_end":10,"line_start":10,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":27,"highlight_start":8,"text":" if 100 > std::i32::MAX {}"}]}]}}
4+
{"reason":"compiler-message","package_id":"clippy-issues 0.1.0 (path+file:///Users/ericlegoff/lab/clippy-issues)","target":{"kind":["bin"],"crate_types":["bin"],"name":"clippy-issues","src_path":"/Users/ericlegoff/lab/clippy-issues/src/main.rs","edition":"2018","doctest":false},"message":{"rendered":"error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false\n --> clippy/main.rs:10:8\n |\n10 | if 100 > std::i32::MAX {}\n | ^^^^^^^^^^^^^^^^^^^\n |\n = help: because `std::i32::MAX` is the maximum value for this type, this comparison is always false\n = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons\n\n","children":[{"children":[],"code":null,"level":"help","message":"because `std::i32::MAX` is the maximum value for this type, this comparison is always false","rendered":null,"spans":[]},{"children":[],"code":null,"level":"help","message":"for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons","rendered":null,"spans":[]}],"code":{"code":"clippy::absurd_extreme_comparisons","explanation":null},"level":"error","spans":[{"byte_end":224,"byte_start":205,"column_end":27,"column_start":8,"expansion":null,"file_name":"clippy/wrong.rs","is_primary":true,"label":null,"line_end":10,"line_start":10,"suggested_replacement":null,"suggestion_applicability":null,"text":[{"highlight_end":27,"highlight_start":8,"text":" if 100 > std::i32::MAX {}"}]}]}}
5+
6+
{"reason":"compiler-message","package_id":"clippy-issues 0.1.0 (path+file:///Users/ericlegoff/lab/clippy-issues)","target":{"kind":["bin"],"crate_types":["bin"],"name":"clippy-issues","src_path":"/Users/ericlegoff/lab/clippy-issues/src/main.rs","edition":"2018","doctest":false},"message":{"rendered":"error: aborting due to 2 previous errors\n\n","children":[],"code":null,"level":"error","message":"aborting due to 2 previous errors","spans":[]}}
7+
error: could not compile `clippy-issues`.
8+
9+
To learn more, run the command again with --verbose.

0 commit comments

Comments
 (0)