More lenient version check

To also match output such as `version 30GB 2.34 linux amd64`

Fixes #256
This commit is contained in:
Arnout Engelen 2021-03-23 20:45:52 +01:00
parent e3a2d5ed01
commit bcc5172190
No known key found for this signature in database
GPG Key ID: 061107B0F74A6DAA
2 changed files with 48 additions and 8 deletions

View File

@ -6,11 +6,13 @@
module Check
( result,
-- exposed for testing:
hasVersion
)
where
import Control.Applicative (many)
import Data.Char (isSpace)
import Data.Char (isDigit, isLetter)
import Data.Maybe (fromJust)
import qualified Data.Text as T
import Language.Haskell.TH.Env (envQ)
@ -45,12 +47,24 @@ data BinaryCheck = BinaryCheck
versionPresent :: Bool
}
-- | Construct regex: [^\.]*${version}\.*\s*
isWordCharacter :: Char -> Bool
isWordCharacter c = (isDigit c) || (isLetter c)
isNonWordCharacter :: Char -> Bool
isNonWordCharacter c = not (isWordCharacter c)
-- | Construct regex: /.*\b${version}\b.*/s
versionRegex :: Text -> RE' ()
versionRegex version =
(\_ _ _ _ -> ()) <$> many (RE.psym (/= '.')) <*> RE.string version
<*> many (RE.sym '.')
<*> many (RE.psym isSpace)
(\_ -> ()) <$> (
(((many RE.anySym) <* (RE.psym isNonWordCharacter)) <|> (RE.pure ""))
*> (RE.string version) <*
((RE.pure "") <|> ((RE.psym isNonWordCharacter) *> (many RE.anySym)))
)
hasVersion :: Text -> Text -> Bool
hasVersion contents expectedVersion =
isJust $ contents =~ versionRegex expectedVersion
checkTestsBuild :: Text -> IO Bool
checkTestsBuild attrPath =
@ -80,9 +94,8 @@ checkBinary argument expectedVersion program = do
)
case eResult of
Left (_ :: Text) -> return $ BinaryCheck program False False
Right (exitCode, contents) -> do
let hasVersion = isJust $ contents =~ versionRegex expectedVersion
return $ BinaryCheck program (exitCode == ExitSuccess) hasVersion
Right (exitCode, contents) ->
return $ BinaryCheck program (exitCode == ExitSuccess) (hasVersion contents expectedVersion)
checks :: [Version -> FilePath -> IO BinaryCheck]
checks =

27
test/CheckSpec.hs Normal file
View File

@ -0,0 +1,27 @@
module CheckSpec where
import qualified Data.Text as T
import Test.Hspec
import qualified Check
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "version check" do
let seaweedVersion234 = T.pack "version 30GB 2.34 linux amd64"
it "finds the version when present" do
Check.hasVersion (T.pack "The version is 2.34") (T.pack "2.34") `shouldBe` True
Check.hasVersion (T.pack "The version is 2.34.") (T.pack "2.34") `shouldBe` True
Check.hasVersion (T.pack "2.34 is the version") (T.pack "2.34") `shouldBe` True
Check.hasVersion seaweedVersion234 (T.pack "2.34") `shouldBe` True
it "doesn't produce false positives" do
Check.hasVersion (T.pack "The version is 12.34") (T.pack "2.34") `shouldBe` False
Check.hasVersion (T.pack "The version is 2.345") (T.pack "2.34") `shouldBe` False
Check.hasVersion (T.pack "The version is 2.35") (T.pack "2.34") `shouldBe` False
Check.hasVersion (T.pack "2.35 is the version") (T.pack "2.34") `shouldBe` False
Check.hasVersion (T.pack "2.345 is the version") (T.pack "2.34") `shouldBe` False
Check.hasVersion (T.pack "12.34 is the version") (T.pack "2.34") `shouldBe` False
Check.hasVersion seaweedVersion234 (T.pack "2.35") `shouldBe` False