installAndTest
  :: [PackageIdentifier]
  -- ^ Constraints
  -> UTCTime
  -> FilePath
  -- ^ Path to compiler
  -> FilePath
  -- ^ Path to ghc-pkg
  -> FilePath
  -- ^ Path to cabal executable
  -> [(String, [String])]
  -- ^ How to test the package
  -> IO InstallAndTestResult
installAndTest cs date ghc pkg cabal test =
  withTempDirectory verbose "." "sunlight" $ \relDir -> do
    dir <- canonicalizePath relDir
    let setup = dir ++ "/Setup"
        distDeps = dir ++ "/distDeps"
        distPkg = dir ++ "/distPkg"
        db = dir ++ "/db"
        pfx = dir ++ "/prefix"
    ghcVer <- tee ghc ["--version"]
    pkgVer <- tee pkg ["--version"]
    cblVer <- tee "cabal" ["--version"]
    rSetup <- tee ghc ["--make", "-outputdir", dir,
      "-o", setup, "Setup.hs"]
    rDistDeps <- tee setup
      ["sdist", "--output-directory=" ++ distDeps ]
    rDistPkg <- tee setup
      ["sdist", "--output-directory=" ++ distPkg ]
    createDirectory pfx
    rInit <- tee pkg ["init", db]
    rDeps <- inDirectory distDeps $
      installDeps cs ghc pkg cabal db pfx
    rInst <- inDirectory distPkg
      $ installPackage ghc pkg db pfx
    rTest <- inDirectory distPkg $ testPackage test
    return $ InstallAndTestResult date ghcVer pkgVer cblVer
      rSetup rDistDeps rDistPkg
      rInit rDeps rInst rTest

-- | Gets a list of PackageIdentifier with the lowest possible
-- versions.  Fails if a package has a dependency range with no
-- minimum.
installAndTest
  :: [PackageIdentifier]
  -- ^ Constraints
  -> UTCTime
  -> FilePath
  -- ^ Path to compiler
  -> FilePath
  -- ^ Path to ghc-pkg
  -> FilePath
  -- ^ Path to cabal executable
  -> [(String, [String])]
  -- ^ How to test the package
  -> IO InstallAndTestResult
installAndTest cs date ghc pkg cabal test =
  withTempDirectory verbose "." "sunlight" $ \relDir -> do
    dir <- canonicalizePath relDir
    let setup = dir ++ "/Setup"
        distDeps = dir ++ "/distDeps"
        distPkg = dir ++ "/distPkg"
        db = dir ++ "/db"
        pfx = dir ++ "/prefix"
    ghcVer <- tee ghc ["--version"]
    pkgVer <- tee pkg ["--version"]
    cblVer <- tee "cabal" ["--version"]
    rSetup <- tee ghc ["--make", "-outputdir", dir,
      "-o", setup, "Setup.hs"]
    rDistDeps <- tee setup
      ["sdist", "--output-directory=" ++ distDeps ]
    rDistPkg <- tee setup
      ["sdist", "--output-directory=" ++ distPkg ]
    createDirectory pfx
    rInit <- tee pkg ["init", db]
    rDeps <- inDirectory distDeps $
      installDeps cs ghc pkg cabal db pfx
    rInst <- inDirectory distPkg
      $ installPackage ghc pkg db pfx
    rTest <- inDirectory distPkg $ testPackage test
    return $ InstallAndTestResult date ghcVer pkgVer cblVer
      rSetup rDistDeps rDistPkg
      rInit rDeps rInst rTest

-- | Gets a list of PackageIdentifier with the lowest possible
-- versions.  Fails if a package has a dependency range with no
-- minimum.
installAndTest cs date ghc pkg cabal test =
  withTempDirectory verbose "." "sunlight" $ \relDir -> do
    dir <- canonicalizePath relDir
    let setup = dir ++ "/Setup"
        distDeps = dir ++ "/distDeps"
        distPkg = dir ++ "/distPkg"
        db = dir ++ "/db"
        pfx = dir ++ "/prefix"
    ghcVer <- tee ghc ["--version"]
    pkgVer <- tee pkg ["--version"]
    cblVer <- tee "cabal" ["--version"]
    rSetup <- tee ghc ["--make", "-outputdir", dir,
      "-o", setup, "Setup.hs"]
    rDistDeps <- tee setup
      ["sdist", "--output-directory=" ++ distDeps ]
    rDistPkg <- tee setup
      ["sdist", "--output-directory=" ++ distPkg ]
    createDirectory pfx
    rInit <- tee pkg ["init", db]
    rDeps <- inDirectory distDeps $
      installDeps cs ghc pkg cabal db pfx
    rInst <- inDirectory distPkg
      $ installPackage ghc pkg db pfx
    rTest <- inDirectory distPkg $ testPackage test
    return $ InstallAndTestResult date ghcVer pkgVer cblVer
      rSetup rDistDeps rDistPkg
      rInit rDeps rInst rTest

-- | Gets a list of PackageIdentifier with the lowest possible
-- versions.  Fails if a package has a dependency range with no
-- minimum.