@@ -516,23 +516,45 @@ runWithLockInTempDir tree act = withLock lockForTempDirs $ do
516516    cleanupTempDir <-  lookupEnv " HLS_TEST_HARNESS_NO_TESTDIR_CLEANUP" 
517517    let  runTestInDir action =  case  cleanupTempDir of 
518518            Just  val |  val /=  " 0" ->  do 
519-                 (tempDir, _) <-  newTempDirWithin  testRoot
520-                 a <-  action tempDir
519+                 (tempDir, cacheHome,  _) <-  setupTemporaryTestDirectories  testRoot
520+                 a <-  withTempCacheHome cacheHome ( action tempDir) 
521521                logWith helperRecorder Debug  LogNoCleanup 
522522                pure  a
523523
524524            _ ->  do 
525-                 (tempDir, cleanup) <-  newTempDirWithin  testRoot
526-                 a <-  action tempDir `finally`  cleanup
525+                 (tempDir, cacheHome,  cleanup) <-  setupTemporaryTestDirectories  testRoot
526+                 a <-  withTempCacheHome cacheHome ( action tempDir)  `finally`    cleanup
527527                logWith helperRecorder Debug  LogCleanup 
528528                pure  a
529529    runTestInDir $  \ tmpDir' ->  do 
530530        --  we canonicalize the path, so that we do not need to do
531-         --  cannibalization  during the test when we compare two paths
531+         --  canonicalization  during the test when we compare two paths
532532        tmpDir <-  canonicalizePath tmpDir'
533533        logWith helperRecorder Info  $  LogTestDir  tmpDir
534534        fs <-  FS. materialiseVFT tmpDir tree
535535        act fs
536+     where 
537+         cache_home_var =  " XDG_CACHE_HOME" 
538+         --  Set the dir for "XDG_CACHE_HOME".
539+         --  When the operation finished, make sure the old value is restored.
540+         withTempCacheHome tempCacheHomeDir act = 
541+             bracket
542+               (do 
543+                 old_cache_home <-  lookupEnv cache_home_var
544+                 setEnv cache_home_var tempCacheHomeDir
545+                 pure  old_cache_home)
546+               (\ old_cache_home -> 
547+                 maybe  (pure  () ) (setEnv cache_home_var) old_cache_home
548+                 )
549+               (\ _ ->  act)
550+ 
551+         --  Set up a temporary directory for the test files and one for the 'XDG_CACHE_HOME'.
552+         --  The 'XDG_CACHE_HOME' is important for independent test runs, i.e. completely empty
553+         --  caches.
554+         setupTemporaryTestDirectories testRoot =  do 
555+             (tempTestCaseDir, cleanup1) <-  newTempDirWithin testRoot
556+             (tempCacheHomeDir, cleanup2) <-  newTempDirWithin testRoot
557+             pure  (tempTestCaseDir, tempCacheHomeDir, cleanup1 >>  cleanup2)
536558
537559runSessionWithServer  ::  Pretty  b  =>  Config  ->  PluginTestDescriptor  b  ->  FilePath ->  Session  a  ->  IO a 
538560runSessionWithServer config plugin fp act = 
0 commit comments