Skip to content

Commit

Permalink
Merge pull request #9505 from mpickering/wip/refactor-testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikolaj committed Apr 19, 2024
2 parents eb0f95b + 2a2d0b3 commit 774ff04
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 98 deletions.
2 changes: 1 addition & 1 deletion Cabal-tests/Cabal-tests.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ source-repository head
library
hs-source-dirs: lib
exposed-modules: Test.Utils.TempTestDir
build-depends: base, directory, Cabal, filepath
build-depends: base, directory, Cabal, filepath, exceptions

-- Small, fast running tests.
test-suite unit-tests
Expand Down
12 changes: 7 additions & 5 deletions Cabal-tests/lib/Test/Utils/TempTestDir.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import Distribution.Simple.Utils (warn)
import Distribution.Verbosity

import Control.Concurrent (threadDelay)
import Control.Exception (bracket, throwIO, try)
import Control.Exception (throwIO, try)
import Control.Monad (when)
import Control.Monad.Catch ( bracket, MonadMask)
import Control.Monad.IO.Class

import System.Directory
import System.IO.Error
Expand All @@ -20,13 +22,13 @@ import qualified System.Info (os)

-- | Much like 'withTemporaryDirectory' but with a number of hacks to make
-- sure on windows that we can clean up the directory at the end.
withTestDir :: Verbosity -> String -> (FilePath -> IO a) -> IO a
withTestDir :: (MonadIO m, MonadMask m) => Verbosity -> String -> (FilePath -> m a) -> m a
withTestDir verbosity template action = do
systmpdir <- getTemporaryDirectory
systmpdir <- liftIO getTemporaryDirectory
bracket
( do { tmpRelDir <- createTempDirectory systmpdir template
( do { tmpRelDir <- liftIO $ createTempDirectory systmpdir template
; return $ systmpdir </> tmpRelDir } )
(removeDirectoryRecursiveHack verbosity)
(liftIO . removeDirectoryRecursiveHack verbosity)
action

-- | On Windows, file locks held by programs we run (in this case VCSs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module UnitTests.Distribution.PackageDescription.Check (tests) where

import Distribution.Compat.Prelude.Internal
import Distribution.Compat.Prelude
import Prelude ()

import Distribution.PackageDescription.Check
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Test.Cabal.Prelude
main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest $ do
main = cabalTest $ withShorterPathForNewBuildStore $ do
skipUnlessGhcVersion ">= 8.1"
skipIfWindows -- TODO: https://github.com/haskell/cabal/issues/6271
withProjectFile "cabal.repo.project" $ do
withRepo "repo" $ do
cabalG ["--store-dir=" ++ storeDir] "v2-build" ["exe"]
cabal "v2-build" ["exe"]
6 changes: 3 additions & 3 deletions cabal-testsuite/PackageTests/Backpack/T6385/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Test.Cabal.Prelude
main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest $ do
main =
cabalTest $ withShorterPathForNewBuildStore $ do
skipUnlessGhcVersion ">= 8.1"
skipIfWindows -- TODO: https://github.com/haskell/cabal/issues/6271
withRepo "repo" $ do
cabalG ["--store-dir=" ++ storeDir] "v2-build" ["T6385"]
cabal "v2-build" ["T6385"]
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import Test.Cabal.Prelude
-- qualifier as pkg, even though they are both build targets of the project.
-- The solution must use --independent-goals to give pkg and setup-dep different
-- qualifiers.
main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest $ do
main =
cabalTest $ withShorterPathForNewBuildStore $ do
skipUnless "no v2-build compatible boot-Cabal" =<< hasNewBuildCompatBootCabal
withRepo "repo" $ do
fails $ cabalG ["--store-dir=" ++ storeDir] "v2-build" ["pkg:my-exe", "--dry-run"]
fails $ cabal "v2-build" ["pkg:my-exe", "--dry-run"]
-- Disabled recording because whether or not we get
-- detailed information for the build of my-exe depends
-- on whether or not the Cabal library version is recent
-- enough
r1 <- recordMode DoNotRecord $ cabalG' ["--store-dir=" ++ storeDir] "v2-build" ["pkg:my-exe", "--independent-goals"]
r1 <- recordMode DoNotRecord $ cabal' "v2-build" ["pkg:my-exe", "--independent-goals"]
assertOutputContains "Setup.hs: setup-dep from project" r1
withPlan $ do
r2 <- runPlanExe' "pkg" "my-exe" []
Expand Down
9 changes: 4 additions & 5 deletions cabal-testsuite/PackageTests/CopyHie/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Test.Cabal.Prelude

main = withShorterPathForNewBuildStore $ \storeDir -> cabalTest $ withRepo "repo" $ do
main = cabalTest $ withShorterPathForNewBuildStore $ withRepo "repo" $ do
skipUnlessGhcVersion ">= 8.8"
cabalG ["--store-dir=" ++ storeDir] "v2-build" ["hie"]
liftIO $ do
installedDependencyLibDir <- findDependencyInStore storeDir "hie-dependency"
shouldExist $ installedDependencyLibDir </> "lib" </> "extra-compilation-artifacts" </> "hie" </> "HieDependency.hie"
cabal "v2-build" ["hie"]
installedDependencyLibDir <- findDependencyInStore "hie-dependency"
shouldExist $ installedDependencyLibDir </> "lib" </> "extra-compilation-artifacts" </> "hie" </> "HieDependency.hie"
4 changes: 2 additions & 2 deletions cabal-testsuite/PackageTests/HaddockArgs/hoogle.test.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Test.Cabal.Prelude

main = withShorterPathForNewBuildStore $ \storeDir -> cabalTest $ withRepo "repo" $ do
main = cabalTest $ withShorterPathForNewBuildStore $ withRepo "repo" $ do
-- Checks if hoogle txt files are generated.
-- Logs contain something like "Documentation created: dist/doc/html/indef/indef.txt", so we don't need
-- to do extra check
cabalG ["--store-dir=" ++ storeDir] "v2-build"
cabal "v2-build"
[ "example"
, "--enable-documentation"
, "--haddock-hoogle"
Expand Down
13 changes: 6 additions & 7 deletions cabal-testsuite/PackageTests/HaddockArgs/quickjump.test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import Test.Cabal.Prelude
import System.Directory
import System.FilePath

main = withShorterPathForNewBuildStore $ \storeDir -> cabalTest $ withRepo "repo" $ do
cabalG ["--store-dir=" ++ storeDir] "v2-build"
main = cabalTest $ withShorterPathForNewBuildStore $ withRepo "repo" $ do
cabal "v2-build"
[ "example"
, "--enable-documentation"
, "--haddock-quickjump"
]
liftIO $ do
libDir <- findDependencyInStore storeDir "indef"
assertFileDoesContain (libDir </> "cabal-hash.txt") "haddock-quickjump: True"
docIndexJsonExists <- doesFileExist (libDir </> "share" </> "doc" </> "html" </> "doc-index.json")
assertBool "doc-index.json doesn't exist, --quickjump is probably not passed to haddock" docIndexJsonExists
libDir <- findDependencyInStore "indef"
assertFileDoesContain (libDir </> "cabal-hash.txt") "haddock-quickjump: True"
docIndexJsonExists <- liftIO $ doesFileExist (libDir </> "share" </> "doc" </> "html" </> "doc-index.json")
assertBool "doc-index.json doesn't exist, --quickjump is probably not passed to haddock" docIndexJsonExists
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ main = cabalTest . withRepo "repo" $ do
cabal "build" ["--enable-documentation"]

env <- getTestEnv
let storeDir = testCabalDir env </> "store"

-- Check properties of executable component
libDir <- liftIO $ findDependencyInStore storeDir "exe"
libDir <- findDependencyInStore "exe"
-- Documentation is enabled..
assertFileDoesContain (libDir </> "cabal-hash.txt") "documentation: True"
-- But not built
shouldDirectoryNotExist ( libDir </> "share" </> "doc" )

-- Check properties of library
libDir <- liftIO $ findDependencyInStore storeDir "lib"
libDir <- findDependencyInStore "lib"
-- Documentation is enabled..
assertFileDoesContain (libDir </> "cabal-hash.txt") "documentation: True"
-- and has been built
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import Test.Cabal.Prelude

-- The one local package, pkg, has a setup dependency on setup-dep-2.0, which is
-- in the repository.
main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest $ do
main = cabalTest $ withShorterPathForNewBuildStore $ do
skipUnless "no v2-build compatible boot-Cabal" =<< hasNewBuildCompatBootCabal
withRepo "repo" $ do
r <- recordMode DoNotRecord $ cabalG' ["--store-dir=" ++ storeDir] "v2-build" ["pkg"]
r <- recordMode DoNotRecord $ cabal' "v2-build" ["pkg"]
-- pkg's setup script should print out a message that it imported from
-- setup-dep:
assertOutputContains "pkg Setup.hs: setup-dep-2.0" r
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import Test.Cabal.Prelude

-- The one local package, pkg, has a dependency on remote-pkg-2.0, which has a
-- setup dependency on remote-setup-dep-3.0.
main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest $ do
main =
cabalTest $ withShorterPathForNewBuildStore $ do

-- TODO: Debug this failure on Windows.
skipIfWindows

skipUnless "no v2-build compatible boot-Cabal" =<< hasNewBuildCompatBootCabal
withRepo "repo" $ do
r1 <- recordMode DoNotRecord $ cabalG' ["--store-dir=" ++ storeDir] "v2-build" ["pkg:my-exe"]
r1 <- recordMode DoNotRecord $ cabal' "v2-build" ["pkg:my-exe"]
-- remote-pkg's setup script should print out a message that it imported from
-- remote-setup-dep:
assertOutputContains "remote-pkg Setup.hs: remote-setup-dep-3.0" r1
Expand Down
6 changes: 3 additions & 3 deletions cabal-testsuite/PackageTests/NewBuild/T4375/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Test.Cabal.Prelude
main = withShorterPathForNewBuildStore $ \storeDir ->
main =
-- TODO: is this test ever run?
cabalTest $ do
cabalTest $ withShorterPathForNewBuildStore $ do
-- Don't run this test unless the GHC is sufficiently recent
-- to not ship boot old-time/old-locale
skipUnlessGhcVersion ">= 7.11"
Expand All @@ -10,4 +10,4 @@ main = withShorterPathForNewBuildStore $ \storeDir ->
-- we had the full Hackage index, we'd try it.)
skipUnlessGhcVersion "< 8.1"
withRepo "repo" $ do
cabalG ["--store-dir=" ++ storeDir] "v2-build" ["a"]
cabal "v2-build" ["a"]
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import Test.Cabal.Prelude

-- Test that 'cabal v2-configure' generates the config file appropriately
main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest $ do
main =
cabalTest . withShorterPathForNewBuildStore $ do
cwd <- fmap testCurrentDir getTestEnv
let configFile = cwd </> "cabal.project.local"

shouldNotExist configFile

-- should not create config file with --dry-run or --only-download
cabalG ["--store-dir=" ++ storeDir] "v2-configure" ["--dry-run"]
cabalG ["--store-dir=" ++ storeDir] "v2-configure" ["--only-download"]
cabal "v2-configure" ["--dry-run"]
cabal "v2-configure" ["--only-download"]
shouldNotExist configFile

-- should create the config file
cabalG ["--store-dir=" ++ storeDir] "v2-configure" []
cabal "v2-configure" []
shouldExist configFile
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import System.Directory

-- Test for 'cabal v2-freeze' with only a single library dependency.
-- my-local-package depends on my-library-dep, which has versions 1.0 and 2.0.
main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest $
main =
cabalTest $ withShorterPathForNewBuildStore $
withRepo "repo" $ do
cwd <- fmap testCurrentDir getTestEnv
let freezeFile = cwd </> "cabal.project.freeze"

shouldNotExist freezeFile

-- v2-build should choose the latest version for the dependency.
cabalG' ["--store-dir=" ++ storeDir] "v2-build" ["--dry-run"] >>= assertUsesLatestDependency
cabal' "v2-build" ["--dry-run"] >>= assertUsesLatestDependency

-- should not create freeze file with --dry-run or --only-download flags
cabalG' ["--store-dir=" ++ storeDir] "v2-freeze" ["--dry-run"]
cabalG' ["--store-dir=" ++ storeDir] "v2-freeze" ["--only-download"]
cabal' "v2-freeze" ["--dry-run"]
cabal' "v2-freeze" ["--only-download"]
shouldNotExist freezeFile

-- Freeze a dependency on the older version.
cabalG ["--store-dir=" ++ storeDir] "v2-freeze" ["--constraint=my-library-dep==1.0"]
cabal "v2-freeze" ["--constraint=my-library-dep==1.0"]

-- The file should constrain the dependency, but not the local package.
shouldExist freezeFile
Expand All @@ -31,21 +31,21 @@ main = withShorterPathForNewBuildStore $ \storeDir ->

-- cabal should be able to build the package using the constraint from the
-- freeze file.
cabalG' ["--store-dir=" ++ storeDir] "v2-build" [] >>= assertDoesNotUseLatestDependency
cabal' "v2-build" [] >>= assertDoesNotUseLatestDependency

-- Re-running v2-freeze should not change the constraints, because cabal
-- should use the existing freeze file when choosing the new install plan.
cabalG ["--store-dir=" ++ storeDir] "v2-freeze" []
cabal "v2-freeze" []
assertFileDoesContain freezeFile "any.my-library-dep ==1.0"

-- cabal should choose the latest version again after the freeze file is
-- removed.
liftIO $ removeFile freezeFile
cabalG' ["--store-dir=" ++ storeDir] "v2-build" ["--dry-run"] >>= assertUsesLatestDependency
cabal' "v2-build" ["--dry-run"] >>= assertUsesLatestDependency

-- Re-running v2-freeze with no constraints or freeze file should constrain
-- the dependency to the latest version.
cabalG ["--store-dir=" ++ storeDir] "v2-freeze" []
cabal "v2-freeze" []
assertFileDoesContain freezeFile "any.my-library-dep ==2.0"
assertFileDoesNotContain freezeFile "my-local-package"
where
Expand Down
10 changes: 5 additions & 5 deletions cabal-testsuite/PackageTests/OfflineFlag/offlineFlag.test.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Test.Cabal.Prelude

main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest $ do
main =
cabalTest $ withShorterPathForNewBuildStore $ do
skipUnlessGhcVersion ">= 8.1"
skipIfWindows
withProjectFile "cabal.repo.project" $ do
withRepo "repo" $ do
fails $ cabalG ["--store-dir=" ++ storeDir] "v2-build" ["current", "--offline"]
cabalG ["--store-dir=" ++ storeDir] "v2-build" ["current"]
cabalG ["--store-dir=" ++ storeDir] "v2-build" ["current", "--offline"]
fails $ cabal "v2-build" ["current", "--offline"]
cabal "v2-build" ["current"]
cabal "v2-build" ["current", "--offline"]
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import Test.Cabal.Prelude
-- Issue #5409 caused v2-build to use the same instance of build-tool-pkg for
-- the build-depends and build-tool-depends dependencies, even though it
-- violated the version constraints.
main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest $ do
main = cabalTest $ withShorterPathForNewBuildStore $ do
skipUnless "not v2-build compatible boot Cabal" =<< hasNewBuildCompatBootCabal
withRepo "repo" $ do
r1 <- recordMode DoNotRecord $
cabalG' ["--store-dir=" ++ storeDir] "v2-build" ["pkg:my-exe"]
cabal' "v2-build" ["pkg:my-exe"]

let msg = concat
[ "In order, the following will be built:"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,27 @@
-- as failed compilation or wrong exe output, which I do check.

import Test.Cabal.Prelude
main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest $ withDelay $ do
main =
cabalTest $ withShorterPathForNewBuildStore .
withDelay $ do
storeDir <- testStoreDir <$> getTestEnv
writeSourceFile "issue5782/src/Module.hs" "module Module where\nf = \"AAA\""
recordMode DoNotRecord $
cabalG ["--store-dir=" ++ storeDir, "--installdir=" ++ storeDir, "--overwrite-policy=always"] "v2-install" ["issue5782"]
cabalG ["--installdir=" ++ storeDir, "--overwrite-policy=always"] "v2-install" ["issue5782"]
withPlan $
runPlanExe' "issue5782" "E" []
>>= assertOutputContains "AAA"
delay
writeSourceFile "issue5782/src/Module.hs" "module Module where\nf = \"BBB\""
recordMode DoNotRecord $
cabalG ["--store-dir=" ++ storeDir, "--installdir=" ++ storeDir, "--overwrite-policy=always"] "v2-install" ["issue5782"]
cabalG ["--installdir=" ++ storeDir, "--overwrite-policy=always"] "v2-install" ["issue5782"]
withPlan $
runPlanExe' "issue5782" "E" []
>>= assertOutputContains "BBB"
writeSourceFile "issue5782/src/Module.hs" "module Module where\nf = \"CCC\""
delay -- different spot to try another scenario
recordMode DoNotRecord $
cabalG ["--store-dir=" ++ storeDir, "--installdir=" ++ storeDir, "--overwrite-policy=always"] "v2-install" ["issue5782"]
cabalG ["--installdir=" ++ storeDir, "--overwrite-policy=always"] "v2-install" ["issue5782"]
withPlan $
runPlanExe' "issue5782" "E" []
>>= assertOutputContains "CCC"
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Test.Cabal.Prelude

main = withShorterPathForNewBuildStore $ \storeDir -> cabalTest $ do
let options = ["--store-dir=" ++ storeDir, "--installdir=" ++ storeDir]
main = cabalTest $ withShorterPathForNewBuildStore $ do
storeDir <- testStoreDir <$> getTestEnv
let options = ["--installdir=" ++ storeDir]
-- Use install method copy that should surely work on Windows too but our
-- path normalization for testing is not good enough yet as can be seen in
-- this CI failure snippet diff:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Test.Cabal.Prelude

main = withShorterPathForNewBuildStore $ \storeDir -> cabalTest $ do
main = cabalTest $ withShorterPathForNewBuildStore $ do
storeDir <- testStoreDir <$> getTestEnv
-- The default install method is symlink that may not work on Windows.
skipIfWindows
let options = ["--store-dir=" ++ storeDir, "--installdir=" ++ storeDir]
let options = ["--installdir=" ++ storeDir]
cabalG options "v2-install" []
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import Test.Cabal.Prelude

import System.FilePath

main = withShorterPathForNewBuildStore $ \storeDir -> cabalTest $ do
main = cabalTest $ withShorterPathForNewBuildStore $ do

storeDir <- testStoreDir <$> getTestEnv
-- Windows does not natively include a touch command.
-- SEE: https://stackoverflow.com/questions/30011267/create-an-empty-file-on-the-commandline-in-windows-like-the-linux-touch-command
skipIfWindows
let options = ["--store-dir=" ++ storeDir, "--installdir=" ++ storeDir]
let options = ["--installdir=" ++ storeDir]
-- Touch the target to see if the warning is made early before the build.
_ <- runM "touch" [storeDir </> "warn-early-overwrite"] Nothing
fails $ cabalG options "v2-install" []
Expand Down

0 comments on commit 774ff04

Please sign in to comment.