Skip to content

Commit d17f7ce

Browse files
committed
Make test suite actually work (in a sandbox)
1 parent b6c2bc1 commit d17f7ce

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

.hgignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
^(?:dist|\.DS_Store)$
33

44
syntax: glob
5-
cabal-dev
5+
cabal.sandbox.config
6+
.cabal-sandbox
67
*~
78
.*.swp
89
.\#*

configurator.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ test-suite unit-tests
8787
Build-depends: configurator,
8888
base,
8989
directory,
90+
filepath,
9091
HUnit,
9192
text,
9293
attoparsec,

tests/Test.hs

+21-23
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import Data.Text (Text)
1818
import Data.Word
1919
import System.Directory
2020
import System.Environment
21+
import System.FilePath
2122
import System.IO
2223
import Test.HUnit
23-
import Paths_configurator
2424

2525
main :: IO ()
2626
main = runTestTT tests >> return ()
@@ -35,17 +35,17 @@ tests = TestList
3535
, "reload" ~: reloadTest
3636
]
3737

38-
withLoad :: [Worth FilePath] -> (Config -> IO ()) -> IO ()
39-
withLoad files t = do
40-
mb <- try $ load files
38+
withLoad :: FilePath -> (Config -> IO ()) -> IO ()
39+
withLoad name t = do
40+
mb <- try $ load (testFile name)
4141
case mb of
4242
Left (err :: SomeException) -> assertFailure (show err)
4343
Right cfg -> t cfg
4444

45-
withReload :: [Worth FilePath] -> ([Maybe FilePath] -> Config -> IO ()) -> IO ()
46-
withReload files t = do
45+
withReload :: FilePath -> ([Maybe FilePath] -> Config -> IO ()) -> IO ()
46+
withReload name t = do
4747
tmp <- getTemporaryDirectory
48-
temps <- forM files $ \f -> do
48+
temps <- forM (testFile name) $ \f -> do
4949
exists <- doesFileExist (worth f)
5050
if exists
5151
then do
@@ -61,6 +61,9 @@ withReload files t = do
6161
Left (err :: SomeException) -> assertFailure (show err)
6262
Right (cfg, tid) -> t (map snd temps) cfg >> killThread tid
6363

64+
testFile :: FilePath -> [Worth FilePath]
65+
testFile name = [Required $ "tests" </> "resources" </> name]
66+
6467
takeMVarTimeout :: Int -> MVar a -> IO (Maybe a)
6568
takeMVarTimeout millis v = do
6669
w <- newEmptyMVar
@@ -74,9 +77,8 @@ takeMVarTimeout millis v = do
7477
takeMVar w
7578

7679
loadTest :: Assertion
77-
loadTest = do
78-
fp <- getDataFileName "tests/resources/pathological.cfg"
79-
withLoad [Required fp] $ \cfg -> do
80+
loadTest =
81+
withLoad "pathological.cfg" $ \cfg -> do
8082
aa <- lookup cfg "aa"
8183
assertEqual "int property" aa $ (Just 1 :: Maybe Int)
8284

@@ -102,9 +104,8 @@ loadTest = do
102104
assertEqual "deep bool" deep (Just False :: Maybe Bool)
103105

104106
typesTest :: Assertion
105-
typesTest = do
106-
fp <- getDataFileName "tests/resources/pathological.cfg"
107-
withLoad [Required fp] $ \ cfg -> do
107+
typesTest =
108+
withLoad "pathological.cfg" $ \cfg -> do
108109
asInt <- lookup cfg "aa" :: IO (Maybe Int)
109110
assertEqual "int" asInt (Just 1)
110111

@@ -154,15 +155,14 @@ typesTest = do
154155
assertEqual "char" asChar (Just 'x')
155156

156157
interpTest :: Assertion
157-
interpTest = do
158-
fp <- getDataFileName "tests/resources/pathological.cfg"
159-
withLoad [Required fp] $ \ cfg -> do
158+
interpTest =
159+
withLoad "pathological.cfg" $ \cfg -> do
160160
home <- getEnv "HOME"
161161
cfgHome <- lookup cfg "ba"
162162
assertEqual "home interp" (Just home) cfgHome
163163

164164
scopedInterpTest :: Assertion
165-
scopedInterpTest = withLoad [Required "resources/interp.cfg"] $ \ cfg -> do
165+
scopedInterpTest = withLoad "interp.cfg" $ \cfg -> do
166166
home <- getEnv "HOME"
167167

168168
lookup cfg "myprogram.exec"
@@ -175,18 +175,16 @@ scopedInterpTest = withLoad [Required "resources/interp.cfg"] $ \ cfg -> do
175175
>>= assertEqual "nested scope" (Just $ home++"/top/layer1/layer2")
176176

177177
importTest :: Assertion
178-
importTest = do
179-
fp <- getDataFileName "tests/resources/import.cfg"
180-
withLoad [Required fp] $ \ cfg -> do
178+
importTest =
179+
withLoad "import.cfg" $ \cfg -> do
181180
aa <- lookup cfg "x.aa" :: IO (Maybe Int)
182181
assertEqual "simple" aa (Just 1)
183182
acx <- lookup cfg "x.ac.x" :: IO (Maybe Int)
184183
assertEqual "nested" acx (Just 1)
185184

186185
reloadTest :: Assertion
187-
reloadTest = do
188-
fp <- getDataFileName "tests/resources/pathological.cfg"
189-
withReload [Required fp] $ \[Just f] cfg -> do
186+
reloadTest =
187+
withReload "pathological.cfg" $ \[Just f] cfg -> do
190188
aa <- lookup cfg "aa"
191189
assertEqual "simple property 1" aa $ Just (1 :: Int)
192190

0 commit comments

Comments
 (0)