slackbuilder/tests/SlackBuilder/InfoSpec.hs

151 lines
6.3 KiB
Haskell
Raw Normal View History

2024-03-19 11:34:19 +01:00
{- This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at https://mozilla.org/MPL/2.0/. -}
2023-10-03 18:53:41 +02:00
module SlackBuilder.InfoSpec
( spec
) where
import Crypto.Hash (Digest, MD5, digestFromByteString)
import qualified Data.ByteString as ByteString
import Data.ByteString.Char8 (ByteString)
import Data.Maybe (maybeToList)
import qualified Data.Text.Encoding as Text
import Data.Void (Void)
import SlackBuilder.Info
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Megaparsec (parseSatisfies, shouldSucceedOn)
import Text.Megaparsec (parse)
import Text.Megaparsec.Error (ParseErrorBundle)
import Text.URI (mkURI)
parseInfoFile'
:: ByteString
-> Either (ParseErrorBundle ByteString Void) PackageInfo
parseInfoFile' = parse parseInfoFile ""
infoDownload0 :: ByteString
2023-10-08 12:28:46 +02:00
infoDownload0 = "PRGNAM=\"pkgnam\"\n\
2023-10-03 18:53:41 +02:00
\VERSION=\"1.2.3\"\n\
\HOMEPAGE=\"homepage\"\n\
\DOWNLOAD=\"\"\n\
2023-10-08 12:28:46 +02:00
\MD5SUM=\"\"\n\
\DOWNLOAD_x86_64=\"\"\n\
\MD5SUM_x86_64=\"\"\n\
\REQUIRES=\"\"\n\
\MAINTAINER=\"Z\"\n\
\EMAIL=\"test@example.com\"\n"
2023-10-03 18:53:41 +02:00
infoDownload1 :: ByteString
2023-10-08 12:28:46 +02:00
infoDownload1 = "PRGNAM=\"pkgnam\"\n\
2023-10-03 18:53:41 +02:00
\VERSION=\"1.2.3\"\n\
\HOMEPAGE=\"homepage\"\n\
\DOWNLOAD=\"https://dlackware.com/download.tar.gz\"\n\
2023-10-08 12:28:46 +02:00
\MD5SUM=\"0102030405060708090a0b0c0d0e0f10\"\n\
\DOWNLOAD_x86_64=\"\"\n\
\MD5SUM_x86_64=\"\"\n\
\REQUIRES=\"\"\n\
\MAINTAINER=\"Z\"\n\
\EMAIL=\"test@example.com\"\n"
2023-10-03 18:53:41 +02:00
maybeToDoubleList :: forall a. Maybe a -> [a]
maybeToDoubleList xs = [y | x <- maybeToList xs, y <- [x, x]]
checksumSample :: [Digest MD5]
checksumSample = maybeToList $ digestFromByteString (ByteString.pack [1 .. 16])
spec :: Spec
spec = do
describe "parseInfoFile" $ do
it "returns package on a valid input" $
parseInfoFile' `shouldSucceedOn` infoDownload1
it "returns an array with one element if one download is given" $
let condition = (== 1) . length . checksums
in parseInfoFile' infoDownload1 `parseSatisfies` condition
it "translates checksum characters into the binary format" $
2024-08-11 13:00:01 +02:00
let expected = ["0102030405060708090a0b0c0d0e0f10"]
condition = (== expected) . fmap show . checksums
2023-10-03 18:53:41 +02:00
in parseInfoFile' infoDownload1 `parseSatisfies` condition
it "accepts an empty downloads list" $
parseInfoFile' `shouldSucceedOn` infoDownload0
2024-11-27 22:41:03 +01:00
it "parses a package name with a dot" $
let given =
"PRGNAM=\"pkgnam.yaml\"\n\
\VERSION=\"1.2.3\"\n\
\HOMEPAGE=\"homepage\"\n\
\DOWNLOAD=\"https://dlackware.com/download.tar.gz\"\n\
\MD5SUM=\"0102030405060708090a0b0c0d0e0f10\"\n\
\DOWNLOAD_x86_64=\"\"\n\
\MD5SUM_x86_64=\"\"\n\
\REQUIRES=\"\"\n\
\MAINTAINER=\"Z\"\n\
\EMAIL=\"test@example.com\"\n"
in parseInfoFile' `shouldSucceedOn` given
it "parses to downloads in a single line" $
let given =
"PRGNAM=\"pkgnam.yaml\"\n\
\VERSION=\"1.2.3\"\n\
\HOMEPAGE=\"homepage\"\n\
\DOWNLOAD=\"https://dlackware.com/download1.tar.gz https://dlackware.com/download2.tar.gz\"\n\
\MD5SUM=\"0102030405060708090a0b0c0d0e0f10 0102030405060708090a0b0c0d0e0f11\"\n\
\DOWNLOAD_x86_64=\"\"\n\
\MD5SUM_x86_64=\"\"\n\
\REQUIRES=\"\"\n\
\MAINTAINER=\"Z\"\n\
\EMAIL=\"test@example.com\"\n"
in parseInfoFile' `shouldSucceedOn` given
it "parses downloads continuing on the next line" $
let given =
"PRGNAM=\"pkgnam.yaml\"\n\
\VERSION=\"1.2.3\"\n\
\HOMEPAGE=\"homepage\"\n\
\DOWNLOAD=\"https://dlackware.com/download1.tar.gz \\\n\
\ https://dlackware.com/download2.tar.gz\"\n\
\MD5SUM=\"0102030405060708090a0b0c0d0e0f10 \\\n\
\ 0102030405060708090a0b0c0d0e0f11\"\n\
\DOWNLOAD_x86_64=\"\"\n\
\MD5SUM_x86_64=\"\"\n\
\REQUIRES=\"\"\n\
\MAINTAINER=\"Z\"\n\
\EMAIL=\"test@example.com\"\n"
in parseInfoFile' `shouldSucceedOn` given
2023-10-03 18:53:41 +02:00
describe "generate" $ do
it "generates an .info file without downloads" $
2023-10-08 12:28:46 +02:00
let given = PackageInfo "pkgnam" "1.2.3" "homepage" [] [] [] [] [] "Z" "test@example.com"
2023-10-03 18:53:41 +02:00
in generate given `shouldBe` Text.decodeUtf8 infoDownload0
it "splits multiple downloads into multiple lines" $
let downloads' = maybeToDoubleList
$ mkURI "https://dlackware.com/download.tar.gz"
checksums' = maybeToDoubleList
$ digestFromByteString (ByteString.pack [1.. 16])
given = PackageInfo
2023-10-08 12:28:46 +02:00
"pkgnam" "1.2.3" "homepage" downloads' checksums' [] [] [] "Z" "test@example.com"
expected = "PRGNAM=\"pkgnam\"\n\
2023-10-03 18:53:41 +02:00
\VERSION=\"1.2.3\"\n\
\HOMEPAGE=\"homepage\"\n\
\DOWNLOAD=\"https://dlackware.com/download.tar.gz \\\n\
\ https://dlackware.com/download.tar.gz\"\n\
\MD5SUM=\"0102030405060708090a0b0c0d0e0f10 \\\n\
2023-10-08 12:28:46 +02:00
\ 0102030405060708090a0b0c0d0e0f10\"\n\
\DOWNLOAD_x86_64=\"\"\n\
\MD5SUM_x86_64=\"\"\n\
\REQUIRES=\"\"\n\
\MAINTAINER=\"Z\"\n\
\EMAIL=\"test@example.com\"\n"
2023-10-03 18:53:41 +02:00
in generate given `shouldBe` expected
it "prints the checksum as a sequence of hexadecimal numbers" $
let downloads' = maybeToList
$ mkURI "https://dlackware.com/download.tar.gz"
given = PackageInfo
2023-10-08 12:28:46 +02:00
"pkgnam" "1.2.3" "homepage" downloads' checksumSample [] [] [] "Z" "test@example.com"
2023-10-03 18:53:41 +02:00
in generate given `shouldBe` Text.decodeUtf8 infoDownload1