packages feed

haskell-gha-1.0: tests/HaskellGha/Test/Project.hs

module HaskellGha.Test.Project (projectTests) where

import Control.Monad
import Data.Either
import Data.List qualified as L
import Distribution.Version
import System.Directory
import System.FilePath
import System.IO.Temp
import Test.Tasty
import Test.Tasty.HUnit

import HaskellGha.Project
import HaskellGha.Project.Ghc

projectTests :: TestTree
projectTests =
  testGroup
    "Project"
    [ testCase "a single package without cabal.project" test_single
    , testCase "a conditional block" test_conditional
    , testCase "elif and else" test_elifElse
    , testCase "a conditional block inside another" test_nested
    , testCase "import lines" test_imports
    , testCase "globs" test_globs
    , testCase "optional packages" test_optional
    , testCase "exact and series entries" test_exactAndSeries
    , testCase "an exact entry next to a series entry" test_exactNextToSeries
    , testCase "a missing conditional block" test_missingBlock
    , testCase "a tested-with version that the project excludes" test_untestedVersion
    , testCase "the paths of a project in the root" test_rootProject
    , testCase "a condition that includes a part of a series" test_partialCondition
    , testCase "a condition on the first release of a series" test_firstRelease
    , testCase "a flag condition" test_flagCondition
    , testCase "all errors of cabal.project are collected" test_projectFileErrors
    , testCase "a condition that the other side decides" test_decidedCondition
    , testCase "a negated condition" test_not
    , testCase "os and arch conditions" test_osArch
    , testCase "tested-with errors" test_testedWithErrors
    , testCase "no packages for a matrix entry" test_emptyEntry
    , testCase "package location errors" test_locationErrors
    , testCase
        "doctest sources in the package directory and a subdirectory"
        test_doctestRootAndSubdirectory
    , testCase
        "doctest sources with a trailing separator"
        test_doctestTrailingSeparator
    , testCase "a missing project directory" test_missingDirectory
    , testCase "a cabal.project in a parent directory" test_parentProject
    ]

test_parentProject :: Assertion
test_parentProject =
  withSystemTempDirectory "haskell-gha-tests" $ \tmp -> do
    let root = tmp </> "repo"
        write :: FilePath -> String -> IO ()
        write path contents = do
          createDirectoryIfMissing True (takeDirectory (tmp </> path))
          writeFile (tmp </> path) contents
        parentError :: String -> FilePath -> String
        parentError dir file =
          "The project directory "
            ++ show dir
            ++ " has no cabal.project, so cabal uses "
            ++ file
            ++ " of a parent directory, but the tool reads only the packages of the project directory. Give the directory of "
            ++ file
            ++ " to --project-dir, or add a cabal.project to "
            ++ show dir
            ++ "."
    write "cabal.project" "packages: repo/a\n"
    write "repo/a/a.cabal" (cabal "a" "GHC ^>= 9.10" False)
    assertBool "a file above the root" . isRight =<< readProject root "a"
    write "repo/cabal.project" "packages: a\n"
    write "repo/sub/cabal.project" "packages: inner\n"
    write "repo/sub/inner/b.cabal" (cabal "b" "GHC ^>= 9.10" False)
    assertEqual "the root" (Left [parentError "a" "cabal.project"]) =<< readProject root "a"
    assertEqual
      "the nearest parent"
      (Left [parentError "sub/inner" "sub/cabal.project"])
      =<< readProject root "sub/inner"
    createDirectoryLink "sub/inner" (root </> "link")
    assertEqual
      "a symbolic link"
      (Left [parentError "link" "sub/cabal.project"])
      =<< readProject root "link"

test_missingDirectory :: Assertion
test_missingDirectory = do
  result <- readProject "." "tests/does-not-exist"
  assertEqual
    "errors"
    (Left ["The project directory \"tests/does-not-exist\" does not exist."])
    result

test_single :: Assertion
test_single = do
  project <-
    readOk [("example.cabal", cabal "example" "GHC == 9.6.7 || ^>= 9.10 || ^>= 9.12" True)]
  assertEqual "packages" ["example"] (map (.name) project.packages)
  assertEqual "directory" ["."] (map (.directory) project.packages)
  assertEqual "test suite" [True] (map (.hasTestSuite) project.packages)
  assertEqual
    "matrix"
    [(v [9, 6, 7], ["example"]), (s 9 10, ["example"]), (s 9 12, ["example"])]
    (matrixOf project)

test_conditional :: Assertion
test_conditional = do
  project <-
    readOk
      [ ("cabal.project", "packages: core\n\nif impl(ghc >= 9.8)\n  packages: new\n")
      , ("core/core.cabal", cabal "core" "GHC == 9.6.7 || ^>= 9.8 || ^>= 9.10" False)
      , ("new/new.cabal", cabal "new" "GHC ^>= 9.8 || ^>= 9.10" True)
      ]
  assertEqual "packages" ["core", "new"] (map (.name) project.packages)
  assertEqual
    "matrix"
    [ (v [9, 6, 7], ["core"])
    , (s 9 8, ["core", "new"])
    , (s 9 10, ["core", "new"])
    ]
    (matrixOf project)

test_elifElse :: Assertion
test_elifElse = do
  project <-
    readOk
      [
        ( "cabal.project"
        , "if impl(ghc >= 9.12)\n  packages: c\nelif impl(ghc >= 9.8)\n  packages: b\nelse\n  packages: a\n"
        )
      , ("a/a.cabal", cabal "a" "GHC == 9.6.7" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      , ("c/c.cabal", cabal "c" "GHC ^>= 9.12" False)
      ]
  assertEqual
    "matrix"
    [(v [9, 6, 7], ["a"]), (s 9 10, ["b"]), (s 9 12, ["c"])]
    (matrixOf project)

test_nested :: Assertion
test_nested = do
  project <-
    readOk
      [
        ( "cabal.project"
        , "if impl(ghc >= 9.10)\n  if impl(ghc >= 9.12)\n    packages: c\n  else\n    packages: b\nelse\n  packages: a\n"
        )
      , ("a/a.cabal", cabal "a" "GHC == 9.6.7" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      , ("c/c.cabal", cabal "c" "GHC ^>= 9.12" False)
      ]
  assertEqual
    "matrix"
    [(v [9, 6, 7], ["a"]), (s 9 10, ["b"]), (s 9 12, ["c"])]
    (matrixOf project)

test_imports :: Assertion
test_imports = do
  project <-
    readOk
      [
        ( "cabal.project"
        , "packages: a\nimport: base.project\n\nif impl(ghc >= 9.12)\n  import: new.project\n"
        )
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      ]
  assertEqual
    "imports"
    [ Import {location = "PROJECT/cabal.project:2:1: ", target = "base.project"}
    , Import {location = "PROJECT/cabal.project:5:3: ", target = "new.project"}
    ]
    project.imports

test_globs :: Assertion
test_globs = do
  project <-
    readOk
      [ ("cabal.project", "packages: pkgs/*/\n          other/*.cabal, {x,y}\n")
      , ("pkgs/a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      , ("pkgs/a/.#a.cabal", "")
      , ("pkgs/b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      , ("pkgs/docs/index.md", "")
      , ("pkgs/two/c.cabal", cabal "c" "GHC ^>= 9.10" False)
      , ("pkgs/two/d.cabal", cabal "d" "GHC ^>= 9.10" False)
      , ("other/o.cabal", cabal "o" "GHC ^>= 9.10" False)
      , ("x/x.cabal", cabal "x" "GHC ^>= 9.10" False)
      , ("y/y.cabal", cabal "y" "GHC ^>= 9.10" False)
      ]
  assertEqual "packages" ["a", "b", "o", "x", "y"] (L.sort $ map (.name) project.packages)
  assertEqual
    "directories"
    ["other", "pkgs/a", "pkgs/b", "x", "y"]
    (L.sort $ map (.directory) project.packages)
  twoPaths <-
    readOk
      [ ("cabal.project", "packages: b a/../b ./a/\n")
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      ]
  assertEqual "two paths of a package" [(s 9 10, ["b", "a"])] (matrixOf twoPaths)
  -- A wildcard does not match the entries . and .. of a directory.
  dots <-
    readErrors
      [ ("cabal.project", "packages: .*/*.cabal\n")
      , ("up.cabal", cabal "up" "GHC ^>= 9.10" False)
      ]
  assertEqual
    "dot wildcard"
    ["PROJECT/cabal.project:1:11: the package location \".*/*.cabal\" matches no files."]
    dots
  current <-
    readOk
      [ ("cabal.project", "packages: .\n")
      , ("a.cabal", cabal "a" "GHC ^>= 9.10" False)
      ]
  assertEqual "the location ." [(s 9 10, ["a"])] (matrixOf current)

test_optional :: Assertion
test_optional = do
  project <-
    readOk
      [ ("cabal.project", "packages: a\noptional-packages: vendor/*/ docs/\n")
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      , ("docs/index.md", "")
      ]
  assertEqual "packages" ["a"] (map (.name) project.packages)
  errors <-
    readErrors
      [ ("cabal.project", "packages: a\noptional-packages: vendor/* {broken\n")
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      ]
  assertEqual
    "errors"
    ["PROJECT/cabal.project:2:29: the package location \"{broken\" is not a valid glob."]
    errors

test_exactAndSeries :: Assertion
test_exactAndSeries = do
  project <-
    readOk [("a.cabal", cabal "a" "GHC == { 9.6.7 } || ^>= 9.10 || == 9.12.2" False)]
  assertEqual "axis" [v [9, 6, 7], s 9 10, v [9, 12, 2]] (map fst $ matrixOf project)

test_exactNextToSeries :: Assertion
test_exactNextToSeries = do
  errors <-
    readErrors
      [ ("cabal.project", "packages: a b\n")
      , ("a/a.cabal", cabal "a" "GHC == 9.10.3" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      ]
  assertEqual
    "errors"
    [ "Package a lists only some versions of the GHC series 9.10 in tested-with, but another package lists the whole series. A conditional block in cabal.project cannot separate the two. Write the series in the same form in all packages, e.g. ^>= 9.10 or exact versions."
    ]
    errors

test_missingBlock :: Assertion
test_missingBlock = do
  errors <-
    readErrors
      [ ("cabal.project", "packages: core servant-client\n")
      , ("core/core.cabal", cabal "core" "GHC == 9.6.7 || ^>= 9.10 || ^>= 9.12" False)
      ,
        ( "servant-client/servant-client.cabal"
        , cabal "servant-client" "GHC ^>= 9.10 || ^>= 9.12" False
        )
      ]
  assertEqual
    "errors"
    [missingBlock "servant-client" "9.6.7" "^>=9.10 || ^>=9.12" "servant-client"]
    errors
  where
    missingBlock :: String -> String -> String -> FilePath -> String
    missingBlock package version range dir =
      unlines
        [ "Package "
            ++ package
            ++ " does not list GHC "
            ++ version
            ++ " in tested-with, but "
            ++ "PROJECT/cabal.project"
            ++ " includes it for that GHC version. Move the package to a conditional block in cabal.project, e.g.:"
        , ""
        , "if impl(ghc " ++ range ++ ")"
        , "  packages: " ++ dir
        ]

test_untestedVersion :: Assertion
test_untestedVersion = do
  errors <-
    readErrors
      [ ("cabal.project", "packages: a\nif impl(ghc >= 9.10)\n  packages: b\n")
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.8 || ^>= 9.10" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.8 || ^>= 9.10" False)
      ]
  assertEqual
    "errors"
    [ "Package b lists GHC 9.8 in tested-with, but PROJECT/cabal.project does not include the package for that GHC version, so no job tests it. Remove the version from tested-with, or change the conditional block in cabal.project."
    ]
    errors

test_rootProject :: Assertion
test_rootProject =
  withSystemTempDirectory "haskell-gha-tests" $ \root -> do
    let write :: FilePath -> String -> IO ()
        write path contents = do
          createDirectoryIfMissing True (takeDirectory (root </> path))
          writeFile (root </> path) contents
    write "cabal.project" "packages: a\nif flag(dev)\n  packages: b\n"
    write "a/a.cabal" (cabal "a" "GHC ^>= 9.10" False)
    assertEqual
      "errors of the package locations"
      (Left ["cabal.project:3:13: the package location \"b\" does not exist."])
      =<< readProject root "."
    write "b/b.cabal" (cabal "b" "GHC ^>= 9.10" False)
    assertEqual
      "errors of the conditions"
      ( Left
          [ "cabal.project:2:1: the condition flag(dev) is not supported, because the tool does not know the value of the flag."
          ]
      )
      =<< readProject root "."

test_partialCondition :: Assertion
test_partialCondition = do
  errors <-
    readErrors
      [ ("cabal.project", "packages: a\nif impl(ghc >= 9.10.2)\n  packages: b\n")
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      ]
  case errors of
    [e] ->
      assertBool
        e
        ( "the condition impl(ghc >=9.10.2) includes only a part of the GHC versions of the matrix entry 9.10"
            `L.isInfixOf` e
        )
    _ -> assertFailure (unlines errors)

test_firstRelease :: Assertion
test_firstRelease = do
  project <-
    readOk
      [
        ( "cabal.project"
        , "packages: a\nif impl(ghc >= 9.10.1)\n  packages: b\nif impl(ghc < 9.10.1)\n  packages: c\n"
        )
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      , ("c/c.cabal", cabal "c" "GHC ^>= 9.10" False)
      ]
  assertEqual "matrix" [(s 9 10, ["a", "b"])] (matrixOf project)

test_flagCondition :: Assertion
test_flagCondition = do
  errors <-
    readErrors
      [ ("cabal.project", "packages: a\nif flag(dev)\n  packages: b\n")
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      ]
  case errors of
    [e] -> assertBool e ("the condition flag(dev) is not supported" `L.isInfixOf` e)
    _ -> assertFailure (unlines errors)

test_projectFileErrors :: Assertion
test_projectFileErrors = do
  errors <-
    readErrors
      [
        ( "cabal.project"
        , "if foo(bar)\n  packages: a\nelse\n  packages: b\nelse\n  packages: c\n"
        )
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      ]
  case errors of
    [condition, stray] -> do
      assertBool condition ("cabal.project:1:" `L.isInfixOf` condition)
      assertBool stray ("cabal.project:5:1: else without if" `L.isInfixOf` stray)
    _ -> assertFailure (unlines errors)
  elif <-
    readErrors
      [ ("cabal.project", "packages: a\nelif impl(ghc >= 9.10)\n  packages: b\n")
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      ]
  assertEqual "elif without if" ["PROJECT/cabal.project:2:1: elif without if"] elif
  syntax <-
    readErrors
      [ ("cabal.project", "packages: a\n{\n")
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      ]
  assertEqual
    "syntax error"
    ["PROJECT/cabal.project:2:1: expecting indentation of at least 1"]
    syntax
  unfinished <-
    readErrors
      [ ("cabal.project", "if impl(ghc\n  packages: a\n")
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      ]
  -- Before Cabal-syntax 3.18, the parser of a condition does not know its
  -- position, and it gives this error the position 1:1.
  case unfinished of
    [e] ->
      assertBool
        e
        ( "PROJECT/cabal.project:1:" `L.isPrefixOf` e
            && ": unexpected end of input" `L.isSuffixOf` e
            && '\n' `notElem` e
        )
    _ -> assertFailure (unlines unfinished)

test_decidedCondition :: Assertion
test_decidedCondition = do
  project <-
    readOk
      [
        ( "cabal.project"
        , "packages: a\nif os(windows) && impl(ghc >= 9.10.2)\n  packages: b\nif flag(dev) || os(linux)\n  packages: c\n"
        )
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      , ("c/c.cabal", cabal "c" "GHC ^>= 9.10" False)
      ]
  assertEqual "matrix" [(s 9 10, ["a", "c"])] (matrixOf project)

test_not :: Assertion
test_not = do
  project <-
    readOk
      [ ("cabal.project", "packages: a\nif !impl(ghc >= 9.12)\n  packages: b\n")
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10 || ^>= 9.12" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      ]
  assertEqual "matrix" [(s 9 10, ["a", "b"]), (s 9 12, ["a"])] (matrixOf project)

test_osArch :: Assertion
test_osArch = do
  project <-
    readOk
      [
        ( "cabal.project"
        , "packages: a\nif os(linux) && arch(x86_64)\n  packages: b\nif os(windows) || impl(ghcjs)\n  packages: c\n"
        )
      , ("a/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      , ("b/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      , ("c/c.cabal", cabal "c" "GHC ^>= 9.10" False)
      ]
  assertEqual "matrix" [(s 9 10, ["a", "b"])] (matrixOf project)

test_testedWithErrors :: Assertion
test_testedWithErrors = do
  openRange <- readErrors [("a.cabal", cabal "a" "GHC >= 9.10" False)]
  assertEqual
    "open range"
    [ "Package a lists the GHC range >=9.10 in tested-with. The matrix needs a finite list of versions. Write an exact version, e.g. == 9.10.3, or a major series, e.g. ^>= 9.10."
    ]
    openRange
  minorRange <- readErrors [("a.cabal", cabal "a" "GHC ^>= 9.10.2" False)]
  assertEqual
    "minor range"
    [ "Package a lists the GHC range >=9.10.2 && <9.11 in tested-with. A range must be a whole major series with two version parts, e.g. ^>= 9.10 or == 9.10.*. Write the series, or an exact version, e.g. == 9.10.3."
    ]
    minorRange
  shortVersion <- readErrors [("a.cabal", cabal "a" "GHC == 9.10" False)]
  assertEqual
    "short version"
    [ "Package a lists GHC == 9.10 in tested-with. No GHC release has this version. Write an exact version with three parts, e.g. == 9.10.3, or a major series, e.g. ^>= 9.10."
    ]
    shortVersion
  emptyRange <- readErrors [("a.cabal", cabal "a" "GHC == 9.10.1 && == 9.12.1" False)]
  assertEqual
    "empty range"
    [ "Package a lists the GHC range ==9.10.1 && ==9.12.1 in tested-with, which includes no version. Join the versions with ||, e.g. ^>= 9.10 || ^>= 9.12."
    ]
    emptyRange
  noGhc <- readErrors [("a.cabal", cabal "a" "GHCJS == 8.10.7" False)]
  assertEqual "no GHC" ["Package a has no GHC version in tested-with."] noGhc
  oldGhc <- readErrors [("a.cabal", cabal "a" "GHC == 8.8.4 || ^>= 9.0 || ^>= 8.6" False)]
  assertEqual
    "old GHC"
    [ "Package a lists GHC 8.6 in tested-with. The tool supports only GHC 8.10 and later. Remove the version from tested-with."
    , "Package a lists GHC 8.8.4 in tested-with. The tool supports only GHC 8.10 and later. Remove the version from tested-with."
    ]
    oldGhc
  oldestExact <- readOk [("a.cabal", cabal "a" "GHC == 8.10.7" False)]
  assertEqual "oldest exact GHC" [(v [8, 10, 7], ["a"])] (matrixOf oldestExact)
  oldestSeries <- readOk [("a.cabal", cabal "a" "GHC ^>= 8.10" False)]
  assertEqual "oldest GHC series" [(s 8 10, ["a"])] (matrixOf oldestSeries)

test_emptyEntry :: Assertion
test_emptyEntry = do
  errors <-
    readErrors
      [ ("cabal.project", "if impl(ghc >= 9.10)\n  packages: a\n")
      , ("a/a.cabal", cabal "a" "GHC == 9.6.7 || ^>= 9.10" False)
      ]
  case errors of
    [e] -> assertBool e ("cabal.project for GHC 9.6.7." `L.isSuffixOf` e)
    _ -> assertFailure (unlines errors)

test_locationErrors :: Assertion
test_locationErrors = do
  errors <-
    readErrors
      [
        ( "cabal.project"
        , unlines
            [ "packages: missing/ nothing/*.cabal https://example.com/a.tar.gz two docs/*"
            , "  /opt/pkg, ~/pkgs/*/"
            , "  ../../outside ../../*/*.cabal {broken"
            ]
        )
      , ("two/a.cabal", cabal "a" "GHC ^>= 9.10" False)
      , ("two/b.cabal", cabal "b" "GHC ^>= 9.10" False)
      , ("docs/a.md", "")
      , ("docs/pkg.tar.gz", "")
      ]
  assertEqual
    "errors"
    [ "PROJECT/cabal.project:1:11: the package location \"missing/\" does not exist."
    , "PROJECT/cabal.project:1:20: the package location \"nothing/*.cabal\" matches no files."
    , "PROJECT/cabal.project:1:36: the package location \"https://example.com/a.tar.gz\" is a URL. The tool supports only local packages."
    , "PROJECT/cabal.project:1:65: the directory \"two\" contains more than one .cabal file."
    , "PROJECT/cabal.project:1:69: the package location \"docs/pkg.tar.gz\" is a tarball. The tool supports only local packages."
    , "PROJECT/cabal.project:2:3: the package location \"/opt/pkg\" is not a relative path. The tool supports only packages in the repository."
    , "PROJECT/cabal.project:2:13: the package location \"~/pkgs/*/\" is not a relative path. The tool supports only packages in the repository."
    , "PROJECT/cabal.project:3:3: the package location \"../../outside\" is not in the repository. The tool supports only packages in the repository."
    , "PROJECT/cabal.project:3:17: the package location \"../../*/*.cabal\" is not in the repository. The tool supports only packages in the repository."
    , "PROJECT/cabal.project:3:33: the package location \"{broken\" is not a valid glob, and no file or directory has this path."
    ]
    errors
  noCabalFile <- readErrors [("cabal.project", "packages: docs/\n"), ("docs/a.md", "")]
  assertEqual
    "no .cabal file"
    ["PROJECT/cabal.project:1:11: the directory \"docs\" contains no .cabal file."]
    noCabalFile
  strayBrace <-
    readErrors
      [ ("cabal.project", "packages: a}b c\n")
      , ("c/c.cabal", cabal "c" "GHC ^>= 9.10" False)
      ]
  assertEqual
    "a stray brace"
    [ "PROJECT/cabal.project:1:11: the package location \"a}b\" is not a valid glob, and no file or directory has this path."
    ]
    strayBrace
  noPackages <- readErrors [("README", "")]
  assertEqual
    "no packages"
    ["There are no packages in the implicit project of \"PROJECT\"."]
    noPackages

test_doctestRootAndSubdirectory :: Assertion
test_doctestRootAndSubdirectory = do
  project <-
    readOk
      [
        ( "a.cabal"
        , unlines
            [ "cabal-version: 3.0"
            , "name: a"
            , "version: 0"
            , "tested-with: GHC ^>= 9.10"
            , "library"
            , "  hs-source-dirs: . src"
            , "  exposed-modules: Root Inner"
            ]
        )
      , ("Root.hs", "")
      , ("src/Inner.hs", "")
      , ("test/Main.hs", "")
      ]
  assertEqual
    "doctest arguments"
    [[["Root.hs", "src"]]]
    (map (.doctestArgs) project.packages)

test_doctestTrailingSeparator :: Assertion
test_doctestTrailingSeparator = do
  project <-
    readOk
      [
        ( "a.cabal"
        , unlines
            [ "cabal-version: 3.0"
            , "name: a"
            , "version: 0"
            , "tested-with: GHC ^>= 9.10"
            , "library"
            , "  hs-source-dirs: ./ src src/"
            , "  exposed-modules: Root Inner"
            ]
        )
      , ("Root.hs", "")
      , ("src/Inner.hs", "")
      , ("test/Main.hs", "")
      ]
  assertEqual
    "doctest arguments"
    [[["Root.hs", "src"]]]
    (map (.doctestArgs) project.packages)

----------------------------------------
-- Helpers

v :: [Int] -> GhcEntry
v = GhcExact . mkVersion

s :: Int -> Int -> GhcEntry
s = GhcSeries

matrixOf :: Project -> [(GhcEntry, [String])]
matrixOf project = [(e.ghc, map (.name) e.packages) | e <- project.matrix]

-- | The contents of a @.cabal@ file.
cabal :: String -> String -> Bool -> String
cabal name testedWith testSuite =
  unlines $
    [ "cabal-version: 3.0"
    , "name: " ++ name
    , "version: 0"
    , "tested-with: " ++ testedWith
    , "library"
    , "  exposed-modules: M"
    ]
      ++ if testSuite
        then ["test-suite test", "  type: exitcode-stdio-1.0", "  main-is: Main.hs"]
        else []

-- | Write the files of a project to a temporary directory and read it.
readProjectFiles :: [(FilePath, String)] -> IO (Either [String] Project)
readProjectFiles files =
  withSystemTempDirectory "haskell-gha-tests" $ \root -> do
    forM_ files $ \(path, contents) -> do
      createDirectoryIfMissing True (takeDirectory (root </> "PROJECT" </> path))
      writeFile (root </> "PROJECT" </> path) contents
    readProject root "PROJECT"

readOk :: [(FilePath, String)] -> IO Project
readOk files =
  readProjectFiles files >>= \case
    Left errors -> assertFailure (unlines errors)
    Right project -> pure project

readErrors :: [(FilePath, String)] -> IO [String]
readErrors files =
  readProjectFiles files >>= \case
    Left errors -> pure errors
    Right _ -> assertFailure "no errors"