Skip to content

Commit

Permalink
Add more diagnostics to isolateEnvironment.setEnvs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rufflewind committed Jan 5, 2025
1 parent 005fa06 commit bfb075d
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions tests/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ withNewDirectory keep dir action = do
where cleanup dir' | keep = return ()
| otherwise = removePathForcibly dir'

diffAscAssoc
:: (Ord a, Eq b) => (b -> c) -> [(a, b)] -> [(a, b)] -> [(a, Either c c)]
diffAscAssoc _ [] [] = []
diffAscAssoc f ((k, v) : kvs) [] = (k, Left (f v)) : diffAscAssoc f kvs []
diffAscAssoc f [] ((k, v) : kvs) = (k, Right (f v)) : diffAscAssoc f [] kvs
diffAscAssoc f ((k, v) : kvs) ((k', v') : kvs')
| k < k' = (k, Left (f v)) : diffAscAssoc f kvs ((k', v') : kvs')
| k > k' = (k', Right (f v')) : diffAscAssoc f ((k, v) : kvs) kvs'
| v /= v' = (k, Left (f v)) : (k', Right (f v')) : diffAscAssoc f kvs kvs'
| otherwise = diffAscAssoc f kvs kvs'

isolateEnvironment :: IO a -> IO a
isolateEnvironment = bracket getEnvs setEnvs . const
where
Expand All @@ -153,8 +164,9 @@ isolateEnvironment = bracket getEnvs setEnvs . const
updateEnvs current target
new <- getEnvs
when (target /= new) $ do
-- Environment variables may be sensitive, so don't log them.
throwIO (userError "isolateEnvironment.setEnvs failed")
let diff = diffAscAssoc length target new
-- Environment variables may be sensitive, so don't log their values.
throwIO (userError ("isolateEnvironment.setEnvs failed" <> show diff))
updateEnvs kvs1@((k1, v1) : kvs1') kvs2@((k2, v2) : kvs2') =
case compare k1 k2 of
LT -> unsetEnv k1 *> updateEnvs kvs1' kvs2
Expand Down

0 comments on commit bfb075d

Please sign in to comment.