-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from pbrisbin/pb-fingerprint
Emit content-based fingerprints
- Loading branch information
Showing
6 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
module CC.ShellCheck.Fingerprint | ||
( issueFingerprint | ||
) where | ||
|
||
import Data.Char (isSpace) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Text (Text) | ||
import Data.Text.Lazy (fromStrict) | ||
import Data.Text.Lazy.Encoding (encodeUtf8) | ||
import ShellCheck.Interface | ||
( Comment(..) | ||
, Position(..) | ||
, PositionedComment(..) | ||
) | ||
|
||
import qualified Data.Digest.Pure.MD5 as MD5 | ||
import qualified Data.Text as T | ||
|
||
-- | Given a positioned comment and the file's contents, generate a fingerprint | ||
-- unique to that issue | ||
issueFingerprint :: PositionedComment -> Text -> Text | ||
issueFingerprint (PositionedComment Position{..} (Comment _ code _)) script = | ||
md5 $ T.intercalate "|" | ||
[ T.pack $ posFile | ||
, T.pack $ show code | ||
, T.filter (not . isSpace) $ fetchLine (fromIntegral posLine) script | ||
] | ||
|
||
md5 :: Text -> Text | ||
md5 = T.pack . show . MD5.md5 . encodeUtf8 . fromStrict | ||
|
||
fetchLine :: Int -> Text -> Text | ||
fetchLine idx = fromMaybe "" . safeIndex (idx - 1) . T.lines | ||
|
||
safeIndex :: Int -> [a] -> Maybe a | ||
safeIndex idx xs | ||
| idx >= 0 && idx < length xs = Just $ xs !! idx | ||
| otherwise = Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module CC.ShellCheck.FingerprintSpec | ||
( main | ||
, fingerprintSpecs | ||
) where | ||
|
||
import CC.ShellCheck.Fingerprint | ||
|
||
import Data.Text (Text) | ||
import ShellCheck.Interface | ||
( Code | ||
, Comment(..) | ||
, Position(..) | ||
, PositionedComment(..) | ||
, Severity(..) | ||
) | ||
import Test.Hspec | ||
|
||
import qualified Data.Text as T | ||
|
||
main :: IO () | ||
main = hspec fingerprintSpecs | ||
|
||
fingerprintSpecs :: Spec | ||
fingerprintSpecs = describe "issueFingerprint" $ do | ||
it "uniquely identifies an issue" $ do | ||
let content = T.unlines | ||
[ "#!/bin/sh" | ||
, "" | ||
, "foo = $*" | ||
, "" | ||
, "bar = $*" | ||
, "" | ||
] | ||
fp1 = fingerprint 3 123 content | ||
fp2 = fingerprint 5 123 content | ||
fp3 = fingerprint 5 456 content | ||
|
||
fp1 `shouldNotBe` fp2 | ||
fp2 `shouldNotBe` fp3 | ||
|
||
it "is robust against the issue moving" $ do | ||
let fp1 = fingerprint 3 123 $ T.unlines | ||
[ "#!/bin/sh" | ||
, "" | ||
, "foo = $*" | ||
] | ||
fp2 = fingerprint 5 123 $ T.unlines | ||
[ "#!/bin/sh" | ||
, "" | ||
, "" | ||
, "" | ||
, "foo = $*" | ||
] | ||
|
||
fp1 `shouldBe` fp2 | ||
|
||
it "is robust against whitespace" $ do | ||
let fp1 = fingerprint 3 123 $ T.unlines | ||
[ "#!/bin/sh" | ||
, "" | ||
, "foo = $*" | ||
] | ||
fp2 = fingerprint 3 123 $ T.unlines | ||
[ "#!/bin/sh" | ||
, "" | ||
, "foo = $*" | ||
] | ||
|
||
fp1 `shouldBe` fp2 | ||
|
||
fingerprint :: Integer -> Code -> Text -> Text | ||
fingerprint ln code = issueFingerprint $ PositionedComment (position ln) (comment code) | ||
|
||
position :: Integer -> Position | ||
position ln = Position | ||
{ posFile = "foo.sh" | ||
, posLine = ln | ||
, posColumn = 0 | ||
} | ||
|
||
comment :: Code -> Comment | ||
comment code = Comment ErrorC code "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters