Skip to content

Commit

Permalink
Test.Utils.Paths: Fix the test data directory for Windows and Nix
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rvl committed Dec 3, 2019
1 parent 320719b commit 47c2103
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/test-utils/src/Test/Utils/Paths.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 47c2103

Please sign in to comment.