forked from jenkinsci/warnings-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jenkinsci#89 from nottyo/master
Add support for Robot Framework Lint
- Loading branch information
Showing
6 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/main/java/hudson/plugins/warnings/parser/RFLintParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/test/java/hudson/plugins/warnings/parser/RFLintParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/test/resources/hudson/plugins/warnings/parser/rflint.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |