use of org.sonar.plugins.python.api.PythonSubscriptionCheck in project sonar-python by SonarSource.
the class PythonSubscriptionCheckTest method scanFileForIssues.
private static List<PreciseIssue> scanFileForIssues(File file, PythonCheck check) {
PythonVisitorContext context = TestPythonVisitorRunner.createContext(file);
check.scanFile(context);
SubscriptionVisitor.analyze(Collections.singletonList((PythonSubscriptionCheck) check), context);
return context.getIssues();
}
use of org.sonar.plugins.python.api.PythonSubscriptionCheck in project sonar-python by SonarSource.
the class SubscriptionVisitor method analyze.
public static void analyze(Collection<PythonSubscriptionCheck> checks, PythonVisitorContext pythonVisitorContext) {
SubscriptionVisitor subscriptionVisitor = new SubscriptionVisitor(checks, pythonVisitorContext);
FileInput rootTree = pythonVisitorContext.rootTree();
if (rootTree != null) {
subscriptionVisitor.scan(rootTree);
checks.forEach(PythonSubscriptionCheck::leaveFile);
}
}
use of org.sonar.plugins.python.api.PythonSubscriptionCheck in project sonar-python by SonarSource.
the class PythonVisitorCheckTest method working_directory.
@Test
public void working_directory() throws IOException {
Path workDir = Files.createTempDirectory("workDir");
PythonSubscriptionCheck check = new PythonSubscriptionCheck() {
@Override
public void initialize(SubscriptionCheck.Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.FILE_INPUT, ctx -> assertThat(ctx.workingDirectory()).isEqualTo(workDir.toFile()));
}
};
File tmpFile = Files.createTempFile("foo", "py").toFile();
PythonVisitorContext context = TestPythonVisitorRunner.createContext(tmpFile, workDir.toFile());
assertThat(context.workingDirectory()).isEqualTo(workDir.toFile());
assertThat(context.pythonFile().uri()).isEqualTo(tmpFile.toURI());
check.scanFile(context);
SubscriptionVisitor.analyze(Collections.singletonList(check), context);
}
use of org.sonar.plugins.python.api.PythonSubscriptionCheck in project sonar-python by SonarSource.
the class PythonVisitorCheckTest method working_directory_null.
@Test
public void working_directory_null() throws IOException {
PythonSubscriptionCheck check = new PythonSubscriptionCheck() {
@Override
public void initialize(SubscriptionCheck.Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.FILE_INPUT, ctx -> assertThat(ctx.workingDirectory()).isNull());
}
};
File tmpFile = Files.createTempFile("foo", "py").toFile();
PythonVisitorContext context = TestPythonVisitorRunner.createContext(tmpFile, null);
assertThat(context.workingDirectory()).isNull();
assertThat(context.pythonFile().uri()).isEqualTo(tmpFile.toURI());
check.scanFile(context);
SubscriptionVisitor.analyze(Collections.singletonList(check), context);
}
use of org.sonar.plugins.python.api.PythonSubscriptionCheck in project sonar-python by SonarSource.
the class SubscriptionVisitorTest method test_regex_cache.
@Test
public void test_regex_cache() {
PythonSubscriptionCheck check = new PythonSubscriptionCheck() {
@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.STRING_ELEMENT, ctx -> {
StringElement stringElement = (StringElement) ctx.syntaxNode();
RegexContext regexCtx = (RegexContext) ctx;
RegexParseResult resultWithNoFlags = regexCtx.regexForStringElement(stringElement, new FlagSet());
RegexParseResult resultWithFlags = regexCtx.regexForStringElement(stringElement, new FlagSet(Pattern.MULTILINE));
assertThat(resultWithNoFlags).isNotSameAs(resultWithFlags);
// When we retrieve them again, it will be the same instance retrieved from the cache.
assertThat(resultWithNoFlags).isSameAs(regexCtx.regexForStringElement(stringElement, new FlagSet()));
assertThat(resultWithFlags).isSameAs(regexCtx.regexForStringElement(stringElement, new FlagSet(Pattern.MULTILINE)));
});
}
};
FileInput fileInput = PythonTestUtils.parse("'.*'");
PythonVisitorContext context = new PythonVisitorContext(fileInput, PythonTestUtils.pythonFile("file"), null, null);
SubscriptionVisitor.analyze(Collections.singleton(check), context);
}
Aggregations