Skip to content

Commit af52397

Browse files
authored
Merge pull request wfleming#54 from filib/pr-52
Analyze shellcheck-compatible scripts. (bash, dash, ash, ksh)
2 parents 1d6a28a + 808d576 commit af52397

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cabal.sandbox.config
1616
*.hp
1717
.stack-work/
1818
.local/
19+
TAGS
1920

2021
# ruby
2122
.ruby-version

app/CLI.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module CLI where
22

3+
import Data.Monoid
34
import Options.Applicative
45

56
--------------------------------------------------------------------------------

src/CC/ShellCheck/ShellScript.hs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module CC.ShellCheck.ShellScript (
1111

1212
import Control.Monad.Extra
1313
import qualified Data.ByteString as BS
14+
import qualified Data.ByteString.Char8 as Char8
1415
import Data.List
16+
import Data.Monoid
1517
import Data.Shebang (Shebang(..), Interpretter(..), Argument(..))
1618
import qualified Data.Shebang as Shebang
1719
import System.Directory
@@ -20,9 +22,24 @@ import System.FilePath.Posix
2022

2123
--------------------------------------------------------------------------------
2224

25+
-- | List of shells the engine should be able to handle.
26+
validShells :: [BS.ByteString]
27+
validShells = ["sh", "ash", "dash", "bash", "ksh"]
28+
29+
--------------------------------------------------------------------------------
30+
31+
-- | List of valid shell file extensions.
32+
validShellExtensions :: [BS.ByteString]
33+
validShellExtensions = ("." <>) <$> validShells
34+
35+
--------------------------------------------------------------------------------
36+
2337
-- | Checks to see if file has correct extension.
2438
hasShellExtension :: FilePath -> Bool
25-
hasShellExtension path = takeExtension path == ".sh"
39+
hasShellExtension path = extension `elem` validShellExtensions
40+
where
41+
extension :: BS.ByteString
42+
extension = Char8.pack $ takeExtension path
2643

2744
--------------------------------------------------------------------------------
2845

@@ -32,11 +49,8 @@ hasValidInterpretter (Shebang (Interpretter int) maybeArgument) =
3249
if BS.isSuffixOf "env" int
3350
then case maybeArgument of
3451
Nothing -> False
35-
Just (Argument arg) -> any (`BS.isPrefixOf` arg) shellScriptWhitelist
36-
else any (`BS.isSuffixOf` int) shellScriptWhitelist
37-
where
38-
shellScriptWhitelist :: [BS.ByteString]
39-
shellScriptWhitelist = ["sh", "ash", "dash", "bash", "ksh"]
52+
Just (Argument arg) -> any (`BS.isPrefixOf` arg) validShells
53+
else any (`BS.isSuffixOf` int) validShells
4054

4155
--------------------------------------------------------------------------------
4256

test/Spec.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ shellscriptSpecs = describe "Shellscript validation and retrieval" $ do
113113
let result = hasShellExtension subject
114114
result `shouldBe` True
115115

116+
it "should be valid if file has .ash extension" $ do
117+
let subject = "example.ash"
118+
let result = hasShellExtension subject
119+
result `shouldBe` True
120+
121+
it "should be valid if file has .dash extension" $ do
122+
let subject = "example.dash"
123+
let result = hasShellExtension subject
124+
result `shouldBe` True
125+
126+
it "should be valid if file has .bash extension" $ do
127+
let subject = "example.bash"
128+
let result = hasShellExtension subject
129+
result `shouldBe` True
130+
131+
it "should be valid if file has .ksh extension" $ do
132+
let subject = "example.ksh"
133+
let result = hasShellExtension subject
134+
result `shouldBe` True
135+
116136
it "should not be valid if file has no extension" $ do
117137
let subject = "example"
118138
let result = hasShellExtension subject

0 commit comments

Comments
 (0)