|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2022 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.python.checks.tests; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.Optional; |
| 25 | +import org.sonar.check.Rule; |
| 26 | +import org.sonar.plugins.python.api.IssueLocation; |
| 27 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 28 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 29 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 30 | +import org.sonar.plugins.python.api.tree.ExpressionStatement; |
| 31 | +import org.sonar.plugins.python.api.tree.Name; |
| 32 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 33 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 34 | +import org.sonar.plugins.python.api.tree.Statement; |
| 35 | +import org.sonar.plugins.python.api.tree.Tree; |
| 36 | +import org.sonar.plugins.python.api.tree.WithStatement; |
| 37 | +import org.sonar.python.tests.UnittestUtils; |
| 38 | +import org.sonar.python.tree.TreeUtils; |
| 39 | + |
| 40 | +@Rule(key = "S5915") |
| 41 | +public class AssertAfterRaiseCheck extends PythonSubscriptionCheck { |
| 42 | + private static final String MESSAGE_MULTIPLE_STATEMENT = "Don’t perform an assertion here; An exception is expected to be raised before its execution."; |
| 43 | + private static final String MESSAGE_SINGLE_STATEMENT = "Refactor this test; if this assertion’s argument raises an exception, the assertion will never get executed."; |
| 44 | + private static final String MESSAGE_SECONDARY = "An exception is expected to be raised in this block."; |
| 45 | + |
| 46 | + private static final String ASSERTION_ERROR = "AssertionError"; |
| 47 | + private static final String PYTEST_RAISE_CALL = "pytest.raises"; |
| 48 | + private static final String PYTEST_ARG_EXCEPTION = "expected_exception"; |
| 49 | + private static final String UNITTEST_ARG_EXCEPTION = "exception"; |
| 50 | + |
| 51 | + @Override |
| 52 | + public void initialize(Context context) { |
| 53 | + context.registerSyntaxNodeConsumer(Tree.Kind.WITH_STMT, ctx -> { |
| 54 | + WithStatement withStatement = (WithStatement) ctx.syntaxNode(); |
| 55 | + if (!isWithStatementItemARaise(withStatement)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + List<Statement> statements = withStatement.statements().statements(); |
| 60 | + Statement statement = statements.get(statements.size()-1); |
| 61 | + if (isAnAssert(statement)) { |
| 62 | + ctx.addIssue(statement, statements.size() > 1 ? MESSAGE_MULTIPLE_STATEMENT : MESSAGE_SINGLE_STATEMENT) |
| 63 | + .secondary(IssueLocation.preciseLocation(withStatement.firstToken(), withStatement.colon(), MESSAGE_SECONDARY)); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + public boolean isWithStatementItemARaise(WithStatement withStatement) { |
| 69 | + return withStatement.withItems().stream() |
| 70 | + .filter(withItem -> withItem.test().is(Tree.Kind.CALL_EXPR)) |
| 71 | + .map(withItem -> (CallExpression) withItem.test()) |
| 72 | + .anyMatch(callExpression -> isValidPytestRaise(callExpression) || isValidUnittestRaise(callExpression)); |
| 73 | + } |
| 74 | + |
| 75 | + public boolean isValidPytestRaise(CallExpression callExpression) { |
| 76 | + return Optional.of(callExpression).stream() |
| 77 | + .map(call -> TreeUtils.getSymbolFromTree(call.callee())) |
| 78 | + .filter(Optional::isPresent) |
| 79 | + .map(Optional::get) |
| 80 | + .map(Symbol::fullyQualifiedName) |
| 81 | + .filter(Objects::nonNull) |
| 82 | + .anyMatch(fqn -> fqn.contains(PYTEST_RAISE_CALL)) |
| 83 | + && isNotAssertionErrorArgument(TreeUtils.nthArgumentOrKeyword(0, PYTEST_ARG_EXCEPTION, callExpression.arguments())); |
| 84 | + } |
| 85 | + |
| 86 | + public boolean isValidUnittestRaise(CallExpression callExpression) { |
| 87 | + return Optional.of(callExpression).stream() |
| 88 | + .filter(call -> call.callee().is(Tree.Kind.QUALIFIED_EXPR)) |
| 89 | + .map(call -> (QualifiedExpression) call.callee()) |
| 90 | + .anyMatch( |
| 91 | + callee -> callee.qualifier().is(Tree.Kind.NAME) |
| 92 | + && ((Name) callee.qualifier()).name().equals("self") |
| 93 | + && UnittestUtils.RAISE_METHODS.contains(callee.name().name())) |
| 94 | + && isNotAssertionErrorArgument(TreeUtils.nthArgumentOrKeyword(0, UNITTEST_ARG_EXCEPTION, callExpression.arguments())); |
| 95 | + } |
| 96 | + |
| 97 | + public boolean isNotAssertionErrorArgument(RegularArgument regularArgument) { |
| 98 | + return Optional.ofNullable(regularArgument).stream() |
| 99 | + .filter(Objects::nonNull) |
| 100 | + .map(arg -> TreeUtils.getSymbolFromTree(arg.expression())) |
| 101 | + .anyMatch(optSym -> optSym.isEmpty() || !ASSERTION_ERROR.equals(optSym.get().fullyQualifiedName())); |
| 102 | + } |
| 103 | + |
| 104 | + public boolean isAnAssert(Statement statement) { |
| 105 | + if (statement.is(Tree.Kind.ASSERT_STMT)) { |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + return Optional.of(statement).stream() |
| 110 | + .filter(stat -> stat.is(Tree.Kind.EXPRESSION_STMT)) |
| 111 | + .map(ExpressionStatement.class::cast) |
| 112 | + .map(ExpressionStatement::expressions) |
| 113 | + .anyMatch(expressions -> |
| 114 | + expressions.stream() |
| 115 | + .filter(expression -> expression.is(Tree.Kind.CALL_EXPR)) |
| 116 | + .map(expression -> ((CallExpression) expression).callee()) |
| 117 | + .filter(callee -> callee.is(Tree.Kind.QUALIFIED_EXPR)) |
| 118 | + .map(QualifiedExpression.class::cast) |
| 119 | + .anyMatch(this::isUnittestAssert) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + public boolean isUnittestAssert(QualifiedExpression callee) { |
| 124 | + return callee.qualifier().is(Tree.Kind.NAME) && ((Name) callee.qualifier()).name().equals("self") |
| 125 | + && UnittestUtils.allAssertMethods().contains(callee.name().name()); |
| 126 | + } |
| 127 | +} |
0 commit comments