From a2e8e1bcf2fbfcc8cc1b980cca2b93df86e1a7fe Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Mon, 11 Aug 2025 13:53:48 +0300 Subject: Rename tests directory into test --- .gitea/workflows/build.yml | 2 +- slackbuilder.cabal | 2 +- test/SlackBuilder/InfoSpec.hs | 150 +++++++++++++++++++++++++++ test/SlackBuilder/LatestVersionCheckSpec.hs | 53 ++++++++++ test/SlackBuilder/PackageSpec.hs | 26 +++++ test/Spec.hs | 1 + tests/SlackBuilder/InfoSpec.hs | 150 --------------------------- tests/SlackBuilder/LatestVersionCheckSpec.hs | 53 ---------- tests/SlackBuilder/PackageSpec.hs | 26 ----- tests/Spec.hs | 1 - 10 files changed, 232 insertions(+), 232 deletions(-) create mode 100644 test/SlackBuilder/InfoSpec.hs create mode 100644 test/SlackBuilder/LatestVersionCheckSpec.hs create mode 100644 test/SlackBuilder/PackageSpec.hs create mode 100644 test/Spec.hs delete mode 100644 tests/SlackBuilder/InfoSpec.hs delete mode 100644 tests/SlackBuilder/LatestVersionCheckSpec.hs delete mode 100644 tests/SlackBuilder/PackageSpec.hs delete mode 100644 tests/Spec.hs diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 79157a7..07c06e0 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -10,7 +10,7 @@ jobs: runs-on: buildenv steps: - uses: actions/checkout@v4 - - run: hlint src lib tests + - run: hlint src lib test test: runs-on: buildenv diff --git a/slackbuilder.cabal b/slackbuilder.cabal index 43d36de..b71f612 100644 --- a/slackbuilder.cabal +++ b/slackbuilder.cabal @@ -96,7 +96,7 @@ test-suite slackbuilder-test SlackBuilder.InfoSpec SlackBuilder.LatestVersionCheckSpec SlackBuilder.PackageSpec - hs-source-dirs: tests + hs-source-dirs: test build-depends: hspec >= 2.10.9 && < 2.12, hspec-megaparsec ^>= 2.2, diff --git a/test/SlackBuilder/InfoSpec.hs b/test/SlackBuilder/InfoSpec.hs new file mode 100644 index 0000000..769a45e --- /dev/null +++ b/test/SlackBuilder/InfoSpec.hs @@ -0,0 +1,150 @@ +{- 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/. -} + +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 +infoDownload0 = "PRGNAM=\"pkgnam\"\n\ + \VERSION=\"1.2.3\"\n\ + \HOMEPAGE=\"homepage\"\n\ + \DOWNLOAD=\"\"\n\ + \MD5SUM=\"\"\n\ + \DOWNLOAD_x86_64=\"\"\n\ + \MD5SUM_x86_64=\"\"\n\ + \REQUIRES=\"\"\n\ + \MAINTAINER=\"Z\"\n\ + \EMAIL=\"test@example.com\"\n" + +infoDownload1 :: ByteString +infoDownload1 = "PRGNAM=\"pkgnam\"\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" + +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" $ + let expected = ["0102030405060708090a0b0c0d0e0f10"] + condition = (== expected) . fmap show . checksums + in parseInfoFile' infoDownload1 `parseSatisfies` condition + + it "accepts an empty downloads list" $ + parseInfoFile' `shouldSucceedOn` infoDownload0 + + 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 + + describe "generate" $ do + it "generates an .info file without downloads" $ + let given = PackageInfo "pkgnam" "1.2.3" "homepage" [] [] [] [] [] "Z" "test@example.com" + 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 + "pkgnam" "1.2.3" "homepage" downloads' checksums' [] [] [] "Z" "test@example.com" + expected = "PRGNAM=\"pkgnam\"\n\ + \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\ + \ 0102030405060708090a0b0c0d0e0f10\"\n\ + \DOWNLOAD_x86_64=\"\"\n\ + \MD5SUM_x86_64=\"\"\n\ + \REQUIRES=\"\"\n\ + \MAINTAINER=\"Z\"\n\ + \EMAIL=\"test@example.com\"\n" + 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 + "pkgnam" "1.2.3" "homepage" downloads' checksumSample [] [] [] "Z" "test@example.com" + in generate given `shouldBe` Text.decodeUtf8 infoDownload1 diff --git a/test/SlackBuilder/LatestVersionCheckSpec.hs b/test/SlackBuilder/LatestVersionCheckSpec.hs new file mode 100644 index 0000000..54901c1 --- /dev/null +++ b/test/SlackBuilder/LatestVersionCheckSpec.hs @@ -0,0 +1,53 @@ +{- 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/. -} + +module SlackBuilder.LatestVersionCheckSpec + ( spec + ) where + +import SlackBuilder.LatestVersionCheck +import Test.Hspec (Spec, describe, it, shouldBe) + +spec :: Spec +spec = do + describe "match" $ do + it "matches an exact tag prefixed with v" $ + let expected = Just "2.6.0" + actual = match "(v)2.6.0" "v2.6.0" + in actual `shouldBe` expected + + it "matches a glob pattern prefixed with v" $ + let expected = Just "2.6.0" + actual = match "(v)*" "v2.6.0" + in actual `shouldBe` expected + + it "matches digits" $ + let expected = Just "2.6.0" + actual = match "(v)2.6.\\d" "v2.6.0" + in actual `shouldBe` expected + + it "matches digits and dots" $ + let expected = Just "2.6.0" + actual = match "(v)\\." "v2.6.0" + in actual `shouldBe` expected + + it "rejects unexpected suffix" $ + let expected = Nothing + actual = match "(v)\\." "v2.6.0-rc1" + in actual `shouldBe` expected + + it "rejects remaining umatched characters" $ + let expected = Nothing + actual = match "2.6.0-rc1" "2.6.0" + in actual `shouldBe` expected + + it "consumes the last token matching nothing" $ + let expected = Just "abc" + actual = match "abc\\d\\d" "abc" + in actual `shouldBe` expected + + it "matches at least one digit" $ + let expected = Nothing + actual = match "1.\\D.3" "1..3" + in actual `shouldBe` expected diff --git a/test/SlackBuilder/PackageSpec.hs b/test/SlackBuilder/PackageSpec.hs new file mode 100644 index 0000000..79fce72 --- /dev/null +++ b/test/SlackBuilder/PackageSpec.hs @@ -0,0 +1,26 @@ +{- 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/. -} + +module SlackBuilder.PackageSpec + ( spec + ) where + +import SlackBuilder.Package +import Test.Hspec (Spec, describe, it, shouldBe) +import Text.URI.QQ (uri) + +spec :: Spec +spec = do + describe "renderDownloadWithVersion" $ do + it "renders text as URL" $ + let given = DownloadTemplate "https://example.com" + actual = renderDownloadWithVersion given "1.2" + expected = Just [uri|https://example.com|] + in actual `shouldBe` expected + + it "renders the components in order" $ + let given = DownloadTemplate "https://example.com/{version}/segment" + actual = renderDownloadWithVersion given "1.2" + expected = Just [uri|https://example.com/1.2/segment|] + in actual `shouldBe` expected diff --git a/test/Spec.hs b/test/Spec.hs new file mode 100644 index 0000000..a824f8c --- /dev/null +++ b/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-} diff --git a/tests/SlackBuilder/InfoSpec.hs b/tests/SlackBuilder/InfoSpec.hs deleted file mode 100644 index 769a45e..0000000 --- a/tests/SlackBuilder/InfoSpec.hs +++ /dev/null @@ -1,150 +0,0 @@ -{- 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/. -} - -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 -infoDownload0 = "PRGNAM=\"pkgnam\"\n\ - \VERSION=\"1.2.3\"\n\ - \HOMEPAGE=\"homepage\"\n\ - \DOWNLOAD=\"\"\n\ - \MD5SUM=\"\"\n\ - \DOWNLOAD_x86_64=\"\"\n\ - \MD5SUM_x86_64=\"\"\n\ - \REQUIRES=\"\"\n\ - \MAINTAINER=\"Z\"\n\ - \EMAIL=\"test@example.com\"\n" - -infoDownload1 :: ByteString -infoDownload1 = "PRGNAM=\"pkgnam\"\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" - -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" $ - let expected = ["0102030405060708090a0b0c0d0e0f10"] - condition = (== expected) . fmap show . checksums - in parseInfoFile' infoDownload1 `parseSatisfies` condition - - it "accepts an empty downloads list" $ - parseInfoFile' `shouldSucceedOn` infoDownload0 - - 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 - - describe "generate" $ do - it "generates an .info file without downloads" $ - let given = PackageInfo "pkgnam" "1.2.3" "homepage" [] [] [] [] [] "Z" "test@example.com" - 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 - "pkgnam" "1.2.3" "homepage" downloads' checksums' [] [] [] "Z" "test@example.com" - expected = "PRGNAM=\"pkgnam\"\n\ - \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\ - \ 0102030405060708090a0b0c0d0e0f10\"\n\ - \DOWNLOAD_x86_64=\"\"\n\ - \MD5SUM_x86_64=\"\"\n\ - \REQUIRES=\"\"\n\ - \MAINTAINER=\"Z\"\n\ - \EMAIL=\"test@example.com\"\n" - 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 - "pkgnam" "1.2.3" "homepage" downloads' checksumSample [] [] [] "Z" "test@example.com" - in generate given `shouldBe` Text.decodeUtf8 infoDownload1 diff --git a/tests/SlackBuilder/LatestVersionCheckSpec.hs b/tests/SlackBuilder/LatestVersionCheckSpec.hs deleted file mode 100644 index 54901c1..0000000 --- a/tests/SlackBuilder/LatestVersionCheckSpec.hs +++ /dev/null @@ -1,53 +0,0 @@ -{- 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/. -} - -module SlackBuilder.LatestVersionCheckSpec - ( spec - ) where - -import SlackBuilder.LatestVersionCheck -import Test.Hspec (Spec, describe, it, shouldBe) - -spec :: Spec -spec = do - describe "match" $ do - it "matches an exact tag prefixed with v" $ - let expected = Just "2.6.0" - actual = match "(v)2.6.0" "v2.6.0" - in actual `shouldBe` expected - - it "matches a glob pattern prefixed with v" $ - let expected = Just "2.6.0" - actual = match "(v)*" "v2.6.0" - in actual `shouldBe` expected - - it "matches digits" $ - let expected = Just "2.6.0" - actual = match "(v)2.6.\\d" "v2.6.0" - in actual `shouldBe` expected - - it "matches digits and dots" $ - let expected = Just "2.6.0" - actual = match "(v)\\." "v2.6.0" - in actual `shouldBe` expected - - it "rejects unexpected suffix" $ - let expected = Nothing - actual = match "(v)\\." "v2.6.0-rc1" - in actual `shouldBe` expected - - it "rejects remaining umatched characters" $ - let expected = Nothing - actual = match "2.6.0-rc1" "2.6.0" - in actual `shouldBe` expected - - it "consumes the last token matching nothing" $ - let expected = Just "abc" - actual = match "abc\\d\\d" "abc" - in actual `shouldBe` expected - - it "matches at least one digit" $ - let expected = Nothing - actual = match "1.\\D.3" "1..3" - in actual `shouldBe` expected diff --git a/tests/SlackBuilder/PackageSpec.hs b/tests/SlackBuilder/PackageSpec.hs deleted file mode 100644 index 79fce72..0000000 --- a/tests/SlackBuilder/PackageSpec.hs +++ /dev/null @@ -1,26 +0,0 @@ -{- 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/. -} - -module SlackBuilder.PackageSpec - ( spec - ) where - -import SlackBuilder.Package -import Test.Hspec (Spec, describe, it, shouldBe) -import Text.URI.QQ (uri) - -spec :: Spec -spec = do - describe "renderDownloadWithVersion" $ do - it "renders text as URL" $ - let given = DownloadTemplate "https://example.com" - actual = renderDownloadWithVersion given "1.2" - expected = Just [uri|https://example.com|] - in actual `shouldBe` expected - - it "renders the components in order" $ - let given = DownloadTemplate "https://example.com/{version}/segment" - actual = renderDownloadWithVersion given "1.2" - expected = Just [uri|https://example.com/1.2/segment|] - in actual `shouldBe` expected diff --git a/tests/Spec.hs b/tests/Spec.hs deleted file mode 100644 index a824f8c..0000000 --- a/tests/Spec.hs +++ /dev/null @@ -1 +0,0 @@ -{-# OPTIONS_GHC -F -pgmF hspec-discover #-} -- cgit v1.2.3