Skip to content

Commit

Permalink
Merge pull request #54 from filib/pr-52
Browse files Browse the repository at this point in the history
Analyze shellcheck-compatible scripts. (bash, dash, ash, ksh)
  • Loading branch information
Filib committed Sep 25, 2016
2 parents 1d6a28a + 808d576 commit af52397
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,6 +16,7 @@ cabal.sandbox.config
*.hp
.stack-work/
.local/
TAGS

# ruby
.ruby-version
Expand Down
1 change: 1 addition & 0 deletions app/CLI.hs
@@ -1,5 +1,6 @@
module CLI where

import Data.Monoid
import Options.Applicative

--------------------------------------------------------------------------------
Expand Down
26 changes: 20 additions & 6 deletions src/CC/ShellCheck/ShellScript.hs
Expand Up @@ -11,7 +11,9 @@ module CC.ShellCheck.ShellScript (

import Control.Monad.Extra
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as Char8
import Data.List
import Data.Monoid
import Data.Shebang (Shebang(..), Interpretter(..), Argument(..))
import qualified Data.Shebang as Shebang
import System.Directory
Expand All @@ -20,9 +22,24 @@ import System.FilePath.Posix

--------------------------------------------------------------------------------

-- | List of shells the engine should be able to handle.
validShells :: [BS.ByteString]
validShells = ["sh", "ash", "dash", "bash", "ksh"]

--------------------------------------------------------------------------------

-- | List of valid shell file extensions.
validShellExtensions :: [BS.ByteString]
validShellExtensions = ("." <>) <$> validShells

--------------------------------------------------------------------------------

-- | Checks to see if file has correct extension.
hasShellExtension :: FilePath -> Bool
hasShellExtension path = takeExtension path == ".sh"
hasShellExtension path = extension `elem` validShellExtensions
where
extension :: BS.ByteString
extension = Char8.pack $ takeExtension path

--------------------------------------------------------------------------------

Expand All @@ -32,11 +49,8 @@ hasValidInterpretter (Shebang (Interpretter int) maybeArgument) =
if BS.isSuffixOf "env" int
then case maybeArgument of
Nothing -> False
Just (Argument arg) -> any (`BS.isPrefixOf` arg) shellScriptWhitelist
else any (`BS.isSuffixOf` int) shellScriptWhitelist
where
shellScriptWhitelist :: [BS.ByteString]
shellScriptWhitelist = ["sh", "ash", "dash", "bash", "ksh"]
Just (Argument arg) -> any (`BS.isPrefixOf` arg) validShells
else any (`BS.isSuffixOf` int) validShells

--------------------------------------------------------------------------------

Expand Down
20 changes: 20 additions & 0 deletions test/Spec.hs
Expand Up @@ -113,6 +113,26 @@ shellscriptSpecs = describe "Shellscript validation and retrieval" $ do
let result = hasShellExtension subject
result `shouldBe` True

it "should be valid if file has .ash extension" $ do
let subject = "example.ash"
let result = hasShellExtension subject
result `shouldBe` True

it "should be valid if file has .dash extension" $ do
let subject = "example.dash"
let result = hasShellExtension subject
result `shouldBe` True

it "should be valid if file has .bash extension" $ do
let subject = "example.bash"
let result = hasShellExtension subject
result `shouldBe` True

it "should be valid if file has .ksh extension" $ do
let subject = "example.ksh"
let result = hasShellExtension subject
result `shouldBe` True

it "should not be valid if file has no extension" $ do
let subject = "example"
let result = hasShellExtension subject
Expand Down

0 comments on commit af52397

Please sign in to comment.