From a53c2082af4ac79b2117d84eb6b21329641a6470 Mon Sep 17 00:00:00 2001 From: brandon s allbery kf8nh Date: Tue, 16 Jul 2024 19:04:56 -0400 Subject: [PATCH] make cabal-install integration tests hermetic They were using the user's config, which is a problem if they don't have one or if it contains settings that interfere with tests (such as `documentation: True`). I'm not entirely certain of this, but it seems to work here. It's a bit of a hack, though. (cherry picked from commit cd45432a8c96eada48d412b4a2cc497100d082b2) --- .gitignore | 1 + cabal-install/cabal-install.cabal | 1 + cabal-install/tests/IntegrationTests2.hs | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 97632785e28..2aff19d820c 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ bootstrap/*.plan.json /cabal-install/dist/ /cabal-install/Setup /cabal-install/source-file-list +/cabal-install/tests/IntegrationTests2/config/cabal-config .stylish-haskell.yaml .stylish-haskell.yml diff --git a/cabal-install/cabal-install.cabal b/cabal-install/cabal-install.cabal index f5fe61496c6..7369dfdb85d 100644 --- a/cabal-install/cabal-install.cabal +++ b/cabal-install/cabal-install.cabal @@ -398,6 +398,7 @@ test-suite integration-tests2 containers, directory, filepath, + process, tasty >= 1.2.3 && <1.6, tasty-hunit >= 0.10, tagged diff --git a/cabal-install/tests/IntegrationTests2.hs b/cabal-install/tests/IntegrationTests2.hs index 487c99d8819..393df73250e 100644 --- a/cabal-install/tests/IntegrationTests2.hs +++ b/cabal-install/tests/IntegrationTests2.hs @@ -74,7 +74,9 @@ import Control.Concurrent (threadDelay) import Control.Exception hiding (assert) import System.FilePath import System.Directory +import System.Environment (setEnv) import System.IO (hPutStrLn, stderr) +import System.Process (callProcess) import Test.Tasty import Test.Tasty.HUnit @@ -93,7 +95,16 @@ removePathForcibly = removeDirectoryRecursive #endif main :: IO () -main = +main = do + -- this is needed to ensure tests aren't affected by the user's cabal config + cwd <- getCurrentDirectory + let configDir = cwd basedir "config" "cabal-config" + setEnv "CABAL_DIR" configDir + removeDirectoryRecursive configDir <|> return () + createDirectoryIfMissing True configDir + -- sigh + callProcess "cabal" ["user-config", "init", "-f"] + callProcess "cabal" ["update"] defaultMainWithIngredients (defaultIngredients ++ [includingOptions projectConfigOptionDescriptions]) (withProjectConfig $ \config -> @@ -1971,8 +1982,8 @@ testNixFlags = do -- Tests whether config options are commented or not testConfigOptionComments :: Assertion testConfigOptionComments = do - _ <- createDefaultConfigFile verbosity [] (basedir "config/default-config") - defaultConfigFile <- readFile (basedir "config/default-config") + _ <- createDefaultConfigFile verbosity [] (basedir "config" "default-config") + defaultConfigFile <- readFile (basedir "config" "default-config") " url" @=? findLineWith False "url" defaultConfigFile " -- secure" @=? findLineWith True "secure" defaultConfigFile