Search in sources :

Example 1 with PythonSubscriptionCheck

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();
}
Also used : PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) PythonVisitorContext(org.sonar.plugins.python.api.PythonVisitorContext)

Example 2 with PythonSubscriptionCheck

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);
    }
}
Also used : PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) FileInput(org.sonar.plugins.python.api.tree.FileInput)

Example 3 with PythonSubscriptionCheck

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);
}
Also used : Path(java.nio.file.Path) PythonVisitorContext(org.sonar.plugins.python.api.PythonVisitorContext) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) PythonVisitorContext(org.sonar.plugins.python.api.PythonVisitorContext) File(java.io.File) Test(org.junit.Test)

Example 4 with PythonSubscriptionCheck

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);
}
Also used : PythonVisitorContext(org.sonar.plugins.python.api.PythonVisitorContext) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) PythonVisitorContext(org.sonar.plugins.python.api.PythonVisitorContext) File(java.io.File) Test(org.junit.Test)

Example 5 with PythonSubscriptionCheck

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);
}
Also used : PythonVisitorContext(org.sonar.plugins.python.api.PythonVisitorContext) RegexContext(org.sonar.python.regex.RegexContext) FlagSet(org.sonarsource.analyzer.commons.regex.ast.FlagSet) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) RegexContext(org.sonar.python.regex.RegexContext) StringElement(org.sonar.plugins.python.api.tree.StringElement) RegexParseResult(org.sonarsource.analyzer.commons.regex.RegexParseResult) FileInput(org.sonar.plugins.python.api.tree.FileInput) PythonVisitorContext(org.sonar.plugins.python.api.PythonVisitorContext) Test(org.junit.Test)

Aggregations

PythonSubscriptionCheck (org.sonar.plugins.python.api.PythonSubscriptionCheck)7 PythonVisitorContext (org.sonar.plugins.python.api.PythonVisitorContext)5 Test (org.junit.Test)4 FileInput (org.sonar.plugins.python.api.tree.FileInput)3 File (java.io.File)2 AstNode (com.sonar.sslr.api.AstNode)1 RecognitionException (com.sonar.sslr.api.RecognitionException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 PythonCheck (org.sonar.plugins.python.api.PythonCheck)1 PythonFile (org.sonar.plugins.python.api.PythonFile)1 StringElement (org.sonar.plugins.python.api.tree.StringElement)1 RegexContext (org.sonar.python.regex.RegexContext)1 PythonTreeMaker (org.sonar.python.tree.PythonTreeMaker)1 RegexParseResult (org.sonarsource.analyzer.commons.regex.RegexParseResult)1 FlagSet (org.sonarsource.analyzer.commons.regex.ast.FlagSet)1