Change withEnv to not clear the whole env (#5281)

The way we use `withEnv` it is really intended to set a few
environment variables in an already existing environment instead of
clearing everything before. This PR changes it to do only that.

changelog_begin
changelog_end

closes #5280
This commit is contained in:
Moritz Kiefer 2020-03-30 18:21:25 +02:00 committed by GitHub
parent 3fc1175436
commit a84311ac39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,7 +33,9 @@ import qualified Data.Conduit.Tar as Tar
-- unix specific
import System.PosixCompat.Files (createSymbolicLink)
-- | Replace all environment variables for test action, then restore them.
-- | Set the given environment variables, then restore them.
-- Envirnoment variables not in the given list are unmodified.
--
-- Avoids System.Environment.setEnv because it treats empty strings as
-- "delete environment variable", unlike main-tester's withEnv which
-- consequently conflates (Just "") with Nothing.
@ -41,11 +43,7 @@ withEnv :: [(String, Maybe String)] -> IO t -> IO t
withEnv vs m = bracket pushEnv popEnv (const m)
where
pushEnv :: IO [(String, Maybe String)]
pushEnv = do
oldEnv <- getEnvironment
let ks = map fst vs
vs' = [(key, Nothing) | (key, _) <- oldEnv, key `notElem` ks] ++ vs
replaceEnv vs'
pushEnv = replaceEnv vs
popEnv :: [(String, Maybe String)] -> IO ()
popEnv vs' = void $ replaceEnv vs'