我正在尝试为我的项目编写一些SONARQUBE自定义规则。 阅读以下文件后- https://docs.sonarqube.org/display/PLUG/Writing+Custom+Java+Rules+101 和 https://github.com/SonarSource/sonar-custom-rules-examples, 我在下面创建了类似这些类的自定义规则-
规则文件:
@Rule(key = "MyAssertionRule")
public class FirstSonarCustomRule extends BaseTreeVisitor implements JavaFileScanner {
private static final String DEFAULT_VALUE = "Inject";
private JavaFileScannerContext context;
/**
* Name of the annotation to avoid. Value can be set by users in Quality
* profiles. The key
*/
@RuleProperty(defaultValue = DEFAULT_VALUE, description = "Name of the annotation to avoid, without the prefix @, for instance 'Override'")
protected String name;
@Override
public void scanFile(JavaFileScannerContext context) {
this.context = context;
System.out.println(PrinterVisitor.print(context.getTree()));
scan(context.getTree());
}
@Override
public void visitMethod(MethodTree tree) {
List<StatementTree> statements = tree.block().body();
for (StatementTree statement : statements) {
System.out.println("KIND IS " + statement.kind());
if (statement.is(Kind.EXPRESSION_STATEMENT)) {
if (statement.firstToken().text().equals("Assert")) {
System.out.println("ERROR");
}
}
}
}
}
测试类:
public class FirstSonarCustomRuleTest {
@Test
public void verify() {
FirstSonarCustomRule f = new FirstSonarCustomRule();
f.name = "ASSERTION";
JavaCheckVerifier.verify("src/test/files/FirstSonarCustom.java", f);
}
}
最后-Test
文件:
class FirstSonarCustom {
int aField;
public void methodToUseTestNgAssertions() {
Assert.assertTrue(true);
}
}
上述测试文件以后将成为我项目的源代码。 根据SONAR文档-//不合规是我的测试文件中的强制性注释。因此,我的第一个问题是我是否也应该在源代码中的任何地方添加此注释? 如果是的话-有什么办法可以避免添加此注释,因为我不想整个添加代码重构工作。
有人可以建议我在这里做什么吗?
我正在使用SONARQUBE 6.3。
答案 0 :(得分:0)
此注释仅由测试框架(new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
screw_ie8: true,
conditionals: true,
unused: true,
comparisons: true,
sequences: true,
dead_code: true,
evaluate: true,
if_return: true,
join_vars: true,
},
comments: false,
sourceMap: true,
minimize: true,
exclude: [/\.min\.js$/gi],
}),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.optimize.ModuleConcatenationPlugin(),
类)用于测试规则的实现。无论如何,它都不是强制性的,并且可以确保您在实际代码中不需要它。