Skip to content

Commit ba7099e

Browse files
committed
Add test for #10717
The test checks that if you change directory when running a test executable that you can still access datafiles.
1 parent a7fda2f commit ba7099e

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Test.Cabal.Prelude
2+
3+
main = setupAndCabalTest $ do
4+
setup_build ["--enable-tests"]
5+
setup "test" ["--show-details=streaming"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cabal-version: 2.4
2+
name: datadir-test
3+
version: 0.1.0.0
4+
synopsis: Test for datadir environment variable
5+
license: BSD-3-Clause
6+
author: Cabal Test Suite
7+
maintainer: [email protected]
8+
build-type: Simple
9+
10+
data-files:
11+
testdata/sample.txt
12+
13+
library
14+
exposed-modules: MyLib
15+
build-depends: base >=4.7 && <5
16+
other-modules: Paths_datadir_test
17+
hs-source-dirs: src
18+
default-language: Haskell2010
19+
20+
test-suite datadir-test
21+
type: exitcode-stdio-1.0
22+
main-is: DataDirTest.hs
23+
hs-source-dirs: test
24+
build-depends: base >=4.7 && <5,
25+
datadir-test,
26+
directory
27+
default-language: Haskell2010
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module MyLib (getDataFileName) where
2+
3+
import qualified Paths_datadir_test as Paths
4+
5+
getDataFileName :: FilePath -> IO FilePath
6+
getDataFileName = Paths.getDataFileName
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
2+
module Main where
3+
4+
import Control.Monad (when)
5+
import System.Directory (createDirectory, doesFileExist, getCurrentDirectory, setCurrentDirectory)
6+
import System.Environment (getEnv)
7+
import System.Exit (exitFailure, exitSuccess)
8+
import System.IO (hPutStrLn, stderr)
9+
import MyLib (getDataFileName)
10+
import Control.Exception
11+
12+
main :: IO ()
13+
main = do
14+
-- Print the datadir environment variable
15+
dataDirEnv <- getEnv "datadir_test_datadir"
16+
putStrLn $ "datadir_test_datadir: " ++ dataDirEnv
17+
18+
-- Get path to our test data file
19+
dataFilePath <- getDataFileName "testdata/sample.txt"
20+
putStrLn $ "Data file path: " ++ dataFilePath
21+
22+
-- Check that we can access the file
23+
fileExists <- doesFileExist dataFilePath
24+
putStrLn $ "File exists: " ++ show fileExists
25+
26+
-- Create a subdirectory and change into it
27+
currentDir <- getCurrentDirectory
28+
putStrLn $ "Current directory: " ++ currentDir
29+
createDirectory "subdir" `catch` \(_ :: SomeException) -> pure ()
30+
setCurrentDirectory "subdir"
31+
newDir <- getCurrentDirectory
32+
putStrLn $ "New directory: " ++ newDir
33+
34+
-- Try to access the data file again after changing directory
35+
dataFilePathAfterCd <- getDataFileName "testdata/sample.txt"
36+
putStrLn $ "Data file path after cd: " ++ dataFilePathAfterCd
37+
38+
fileExistsAfterCd <- doesFileExist dataFilePathAfterCd
39+
putStrLn $ "File exists after cd: " ++ show fileExistsAfterCd
40+
41+
-- Exit with error if we can't find the file
42+
when (not fileExistsAfterCd) $ do
43+
hPutStrLn stderr "ERROR: Could not find data file after changing directory!"
44+
hPutStrLn stderr $ "datadir_test_datadir was set to: " ++ dataDirEnv
45+
exitFailure
46+
47+
putStrLn "SUCCESS: Data file found correctly even after changing directory!"
48+
exitSuccess
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is test data for the datadir test.

0 commit comments

Comments
 (0)