From b0817ca4043e769af98b8059044a6af1ef54e4db Mon Sep 17 00:00:00 2001 From: Martin Schulze Date: Mon, 25 Oct 2021 17:26:34 +0200 Subject: [PATCH] bats: Add check for useless negation (SC2293) --- src/ShellCheck/Analytics.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index 8960d93..7404247 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -199,6 +199,7 @@ nodeChecks = [ ,checkComparisonWithLeadingX ,checkCommandWithTrailingSymbol ,checkUnquotedParameterExpansionPattern + ,checkBatsTestDoesNotUseNegation ] optionalChecks = map fst optionalTreeChecks @@ -4847,6 +4848,24 @@ checkExtraMaskedReturns params t = runNodeAnalysis findMaskingNodes params t hasParent pred t = any (uncurry pred) (parentChildPairs t) +prop_checkBatsTestDoesNotUseNegation1 = verifyNot checkBatsTestDoesNotUseNegation "#!/usr/bin/env bats\n@test \"name\"{ ! true; false; }" +prop_checkBatsTestDoesNotUseNegation2 = verify checkBatsTestDoesNotUseNegation "#!/usr/bin/env bats\n@test \"name\"{ ! true; }" -- using ! on last command in test is okay +prop_checkBatsTestDoesNotUseNegation3 = verify checkBatsTestDoesNotUseNegation "#!/usr/bin/env bats\n@test \"name\"{ run ! true }" +checkBatsTestDoesNotUseNegation params t = + case t of + T_BatsTest _ _ (T_BraceGroup _ commands) -> mapM_ check (dropLast commands) + T_Banged id (T_Pipeline _ _ [T_Redirecting _ _ (T_Condition idCondition _ _)]) -> + err id 2293 + "bats: ! will never fail the test. Fold the `!` into the conditional!" + T_Banged id cmd -> errWithFix id 2293 + "bats: ! will never fail the test." + (fixWith [replaceStart id params 0 "run "]) + _ -> return () + dropLast t = + case t of + [_] -> [] + x:rest -> x : dropLast rest + _ -> [] return [] runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])