From e1fe9be7afba61bc4fb65e6a8828f54c6e21e552 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 20 Jan 2019 14:02:42 -0800 Subject: [PATCH] Fix minor details in new Bats support --- CHANGELOG.md | 1 + src/ShellCheck/ASTLib.hs | 8 ++++++++ src/ShellCheck/Checker.hs | 1 + src/ShellCheck/Checks/Commands.hs | 2 +- 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69cb66c..7198770 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Since previous release ### Added - Preliminary support for fix suggestions +- Files containing Bats tests can now be checked - SC2246: Warn if a shebang's interpreter ends with / - SC2245: Warn that Ksh ignores all but the first glob result in `[` - SC2243/SC2244: Suggest using explicit -n for `[ $foo ]` diff --git a/src/ShellCheck/ASTLib.hs b/src/ShellCheck/ASTLib.hs index 5f3b68c..66269bc 100644 --- a/src/ShellCheck/ASTLib.hs +++ b/src/ShellCheck/ASTLib.hs @@ -351,6 +351,14 @@ isOnlyRedirection t = isFunction t = case t of T_Function {} -> True; _ -> False +-- Bats tests are functions for the purpose of 'local' and such +isFunctionLike t = + case t of + T_Function {} -> True + T_BatsTest {} -> True + _ -> False + + isBraceExpansion t = case t of T_BraceExpansion {} -> True; _ -> False -- Get the lists of commands from tokens that contain them, such as diff --git a/src/ShellCheck/Checker.hs b/src/ShellCheck/Checker.hs index 2c5eea0..b92cad3 100644 --- a/src/ShellCheck/Checker.hs +++ b/src/ShellCheck/Checker.hs @@ -52,6 +52,7 @@ shellFromFilename filename = foldl mplus Nothing candidates where shellExtensions = [(".ksh", Ksh) ,(".bash", Bash) + ,(".bats", Bash) ,(".dash", Dash)] -- The `.sh` is too generic to determine the shell: -- We fallback to Bash in this case and emit SC2148 if there is no shebang diff --git a/src/ShellCheck/Checks/Commands.hs b/src/ShellCheck/Checks/Commands.hs index 6c82a4f..a121bc7 100644 --- a/src/ShellCheck/Checks/Commands.hs +++ b/src/ShellCheck/Checks/Commands.hs @@ -770,7 +770,7 @@ prop_checkLocalScope2 = verifyNot checkLocalScope "f() { local foo=3; }" checkLocalScope = CommandCheck (Exactly "local") $ \t -> whenShell [Bash, Dash] $ do -- Ksh allows it, Sh doesn't support local path <- getPathM t - unless (any isFunction path) $ + unless (any isFunctionLike path) $ err (getId $ getCommandTokenOrThis t) 2168 "'local' is only valid in functions." prop_checkDeprecatedTempfile1 = verify checkDeprecatedTempfile "var=$(tempfile)"