From 47c2103e33efeaa72509785ac3c9971d137eb1c8 Mon Sep 17 00:00:00 2001 From: Rodney Lorrimar Date: Tue, 3 Dec 2019 21:16:24 +1000 Subject: [PATCH] Test.Utils.Paths: Fix the test data directory for Windows and Nix When running tests under either Windows or Nix, we want the test data directory to be relative to the current directory, rather than an absolute path. --- lib/test-utils/src/Test/Utils/Paths.hs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/test-utils/src/Test/Utils/Paths.hs b/lib/test-utils/src/Test/Utils/Paths.hs index dc6413e3930..ef725e81297 100644 --- a/lib/test-utils/src/Test/Utils/Paths.hs +++ b/lib/test-utils/src/Test/Utils/Paths.hs @@ -10,15 +10,31 @@ module Test.Utils.Paths import Prelude +import Control.Monad.IO.Class + ( liftIO ) import Data.FileEmbed ( makeRelativeToProject ) import Language.Haskell.TH.Syntax ( Exp, Q, liftData ) +import System.Environment + ( lookupEnv ) import System.FilePath ( () ) --- | A TH function to get the test data directory. It combines the current --- source file location and cabal file to locate the package directory in such a --- way that works in both the package build and ghci. +-- | A TH function to get the test data directory. +-- +-- It combines the current source file location and cabal file to locate the +-- package directory in such a way that works in both the stack/cabal package +-- build and ghci. +-- +-- For the Nix build, rather than baking in a path that starts with @/build@, it +-- makes the test data path relative to the current directory. getTestData :: Q Exp -getTestData = makeRelativeToProject ("test" "data") >>= liftData +getTestData = do + let relPath = "test" "data" + absPath <- makeRelativeToProject relPath + + -- This environment variable indicates we are building under nix. + nixBuildDir <- liftIO $ lookupEnv "NIX_BUILD_TOP" + + liftData $ maybe absPath (const relPath) nixBuildDir