Skip to content

Commit

Permalink
Merge pull request jenkinsci#89 from nottyo/master
Browse files Browse the repository at this point in the history
Add support for Robot Framework Lint
  • Loading branch information
uhafner authored Apr 11, 2017
2 parents 5462088 + ae84931 commit c5db29e
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 2 deletions.
85 changes: 85 additions & 0 deletions src/main/java/hudson/plugins/warnings/parser/RFLintParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package hudson.plugins.warnings.parser;

import hudson.Extension;
import hudson.console.ConsoleNote;
import hudson.plugins.analysis.util.model.FileAnnotation;
import hudson.plugins.analysis.util.model.Priority;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A parser for <a href="http://robotframework.org/">Robot Framework</a>
* Parse output from <a href="https://github.com/boakley/robotframework-lint">robotframework-lint</a>
* To generate rflint file
* cmd$ pip install robotframework-lint
* cmd$ rflint path/to/test.robot
* Created by traitanit on 3/27/2017 AD.
*/
@Extension
public class RFLintParser extends RegexpLineParser {

private static final String RFLINT_ERROR_PATTERN = "([W|E|I]): (\\d+), (\\d+): (.*) \\((.*)\\)";
private static final String RFLINT_FILE_PATTERN = "\\+\\s(.*)";
private String fileName;

public RFLintParser(){
super(Messages._Warnings_RFLint_ParserName(),
Messages._Warnings_RFLint_LinkName(),
Messages._Warnings_RFLint_TrendName(),
RFLINT_ERROR_PATTERN);
}

@Override
public Collection<FileAnnotation> parse(Reader file) throws IOException {
List<FileAnnotation> warnings = new ArrayList<FileAnnotation>();
LineIterator iterator = IOUtils.lineIterator(file);
Pattern filePattern = Pattern.compile(RFLINT_FILE_PATTERN);
try {
while (iterator.hasNext()) {
String line = ConsoleNote.removeNotes(iterator.nextLine());
// check if line contains file name.
Matcher matcher = filePattern.matcher(line);
if (matcher.find()) {
fileName = matcher.group(1);
}
findAnnotations(line, warnings);
}
}
finally {
iterator.close();
}
return warnings;
}

@Override
protected Warning createWarning(Matcher matcher) {
String message = matcher.group(4);
String category = classifyIfEmpty(matcher.group(1), message);
Priority priority = Priority.LOW;
switch (category.charAt(0)){
case 'E':
priority = Priority.HIGH;
category = "ERROR";
break;
case 'W':
priority = Priority.NORMAL;
category = "WARNING";
break;
case 'I':
priority = Priority.LOW;
category = "IGNORE";
break;
default:
break;
}
return createWarning(fileName, getLineNumber(matcher.group(2)), category, message, priority);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,8 @@ Warnings.SphinxBuild.TrendName=Sphinx-build Warnings Trend

Warnings.AnsibleLint.ParserName=Ansible Lint
Warnings.AnsibleLint.LinkName=Ansible Lint Warnings
Warnings.AnsibleLint.TrendName=Ansible Lint Warnings Trend
Warnings.AnsibleLint.TrendName=Ansible Lint Warnings Trend

Warnings.RFLint.ParserName=Robot Framework Lint
Warnings.RFLint.LinkName=Robot Framework Warnings
Warnings.RFLint.TrendName=Robot Framework Warnings Trend
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,7 @@ Warnings.TaskingVXCompiler.TrendName=TASKING VX Compiler Warnungen Trend
Warnings.SphinxBuild.ParserName=Sphinx-build
Warnings.SphinxBuild.LinkName=Sphinx-build Warnungen
Warnings.SphinxBuild.TrendName=Sphinx-build Warnungen Trend

Warnings.RFLint.ParserName=RFLint
Warnings.RFLint.LinkName=RFLint Warnungen
Warnings.RFLint.TrendName=RFLint Warnungen Trend
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
public class ParserRegistryIntegrationTest {
/** If you add a new parser then this value needs to be adapted. */
private static final int NUMBER_OF_AVAILABLE_PARSERS = 65;
private static final int NUMBER_OF_AVAILABLE_PARSERS = 66;
private static final String OLD_ID_ECLIPSE_JAVA_COMPILER = "Eclipse Java Compiler";
private static final String JAVA_WARNINGS_FILE = "deprecations.txt";
private static final String OLD_ID_JAVA_COMPILER = "Java Compiler";
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/hudson/plugins/warnings/parser/RFLintParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package hudson.plugins.warnings.parser;

import hudson.plugins.analysis.util.model.FileAnnotation;
import hudson.plugins.analysis.util.model.Priority;
import org.junit.Test;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

import static org.junit.Assert.assertEquals;

/**
* Tests the class {@link RFLintParser}.
* Created by traitanit on 3/27/2017 AD.
*/
public class RFLintParserTest extends ParserTester {
private static final String WARNING_TYPE = Messages._Warnings_RFLint_ParserName().toString(Locale.ENGLISH);

/**
* Parses a txt file, containing 6 warnings.
*
* @throws IOException
* if the file could not be read
*/
@Test
public void rfLintTest() throws IOException {
Collection<FileAnnotation> warnings = new RFLintParser().parse(openFile());

assertEquals(WRONG_NUMBER_OF_WARNINGS_DETECTED, 6, warnings.size());
Iterator<FileAnnotation> iterator = warnings.iterator();
FileAnnotation warning;

warning = iterator.next();
checkWarning(warning,
25,
"Line is too long (exceeds 100 characters)",
"./Login_to_web.robot",
WARNING_TYPE, "WARNING", Priority.NORMAL);
warning = iterator.next();
checkWarning(warning,
40,
"No keyword documentation",
"./Login_to_web.robot",
WARNING_TYPE, "ERROR", Priority.HIGH);
warning = iterator.next();
checkWarning(warning,
24,
"Line is too long (exceeds 100 characters)",
"./Merchant_Signup.robot",
WARNING_TYPE, "WARNING", Priority.NORMAL);
warning = iterator.next();
checkWarning(warning,
378,
"No keyword documentation",
"./Merchant_Signup.robot",
WARNING_TYPE, "ERROR", Priority.HIGH);
warning = iterator.next();
checkWarning(warning,
73,
"Too few steps (1) in keyword",
"./merchant_common_keyword.txt",
WARNING_TYPE, "WARNING", Priority.NORMAL);
warning = iterator.next();
checkWarning(warning,
123,
"Ignore Error",
"./merchant_common_keyword.txt",
WARNING_TYPE, "IGNORE", Priority.LOW);

}

@Override
protected String getWarningsFile() {
return "rflint.txt";
}
}
10 changes: 10 additions & 0 deletions src/test/resources/hudson/plugins/warnings/parser/rflint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
+ ./Login_to_web.robot
W: 25, 100: Line is too long (exceeds 100 characters) (LineTooLong)
E: 40, 0: No keyword documentation (RequireKeywordDocumentation)
+ ./Merchant_Signup.robot
W: 24, 100: Line is too long (exceeds 100 characters) (LineTooLong)
E: 378, 0: No keyword documentation (RequireKeywordDocumentation)
+ ./merchant_common_keyword.txt
W: 73, 0: Too few steps (1) in keyword (TooFewKeywordSteps)
I: 123, 0: Ignore Error (Ignore Error)
K: 222, 0: Invalid Warning Type (Invalid Warning)

0 comments on commit c5db29e

Please sign in to comment.