1+ {-# LANGUAGE RecordWildCards #-}
2+ {-# LANGUAGE TemplateHaskell #-}
13module Stack.ConfigSpec where
24
35import Control.Applicative
46import Control.Monad
57import Control.Monad.Logger
68import Control.Exception
79import Data.Maybe
10+ import Data.Monoid
11+ import Network.HTTP.Conduit (Manager )
812import Path
913-- import System.FilePath
1014import System.Directory
@@ -16,17 +20,37 @@ import Stack.Config
1620import Stack.Types.Config
1721import Stack.Types.StackT
1822
23+ sampleConfig :: String
24+ sampleConfig =
25+ " resolver: lts-2.10\n " ++
26+ " packages: ['.']\n "
27+
28+ stackDotYaml :: Path Rel File
29+ stackDotYaml = $ (mkRelFile " stack.yaml" )
30+
31+ data T = T
32+ { manager :: Manager
33+ }
34+
35+ setup :: IO T
36+ setup = do
37+ manager <- newTLSManager
38+ return T {.. }
39+
40+ teardown :: T -> IO ()
41+ teardown _ = return ()
42+
1943spec :: Spec
20- spec = do
21- manager <- runIO $ newTLSManager
22- stackDotYaml <- runIO $ parseRelFile " stack.yaml"
44+ spec = beforeAll setup $ afterAll teardown $ do
2345 let logLevel = LevelDebug
46+ -- TODO(danburton): not use inTempDir
2447 let inTempDir action = do
2548 currentDirectory <- getCurrentDirectory
2649 withSystemTempDirectory " Stack_ConfigSpec" $ \ tempDir -> do
2750 let enterDir = setCurrentDirectory tempDir
2851 let exitDir = setCurrentDirectory currentDirectory
2952 bracket_ enterDir exitDir action
53+ -- TODO(danburton): a safer version of this?
3054 let withEnvVar name newValue action = do
3155 originalValue <- fromMaybe " " <$> lookupEnv name
3256 let setVar = setEnv name newValue
@@ -35,32 +59,34 @@ spec = do
3559
3660
3761 describe " loadConfig" $ do
38- let loadConfig' = runStackLoggingT manager logLevel loadConfig
39-
40- -- TODO: make sure parent dirs also don't have config file
41- it " works even if no config file exists" $ inTempDir $ do
42- _config <- loadConfig'
62+ let loadConfig' m = runStackLoggingT m logLevel ( loadConfig mempty )
63+ let loadBuildConfigRest m = runStackLoggingT m logLevel
64+ -- TODO(danburton) : make sure parent dirs also don't have config file
65+ it " works even if no config file exists" $ \ T { .. } -> example $ do
66+ _config <- loadConfig' manager
4367 return ()
4468
45- -- TODO: should throw?
46- it " works with a blank config file" $ inTempDir $ do
69+ it " works with a blank config file" $ \ T {.. } -> inTempDir $ do
4770 writeFile (toFilePath stackDotYaml) " "
48- _config <- loadConfig'
49- return ()
71+ -- TODO(danburton): more specific test for exception
72+ loadConfig' manager `shouldThrow` anyException
5073
51- -- it "finds the config file in a parent directory" $ inTempDir $ do
52- -- writeFile (toFilePath stackDotYaml) "packages: ['child']"
53- -- parentDir <- getCurrentDirectory >>= parseAbsDir
54- -- let childDir = "child"
55- -- createDirectory childDir
56- -- setCurrentDirectory childDir
57- -- config <- loadConfig'
58- -- configDir config `shouldBe` parentDir
74+ it " finds the config file in a parent directory" $ \ T {.. } -> inTempDir $ do
75+ writeFile (toFilePath stackDotYaml) sampleConfig
76+ parentDir <- getCurrentDirectory >>= parseAbsDir
77+ let childDir = " child"
78+ createDirectory childDir
79+ setCurrentDirectory childDir
80+ LoadConfig {.. } <- loadConfig' manager
81+ BuildConfig {.. } <- loadBuildConfigRest manager lcLoadBuildConfig
82+ bcRoot `shouldBe` parentDir
5983
60- -- it "respects the STACK_YAML env variable" $ inTempDir $ do
61- -- withSystemTempDirectory "config-is-here" $ \dirFilePath -> do
62- -- dir <- parseAbsDir dirFilePath
63- -- writeFile (toFilePath (dir </> stackDotYaml)) "packages: ['child']"
64- -- withEnvVar "STACK_YAML" dirFilePath $ do
65- -- config <- loadConfig'
66- -- configDir config `shouldBe` dir
84+ it " respects the STACK_YAML env variable" $ \ T {.. } -> inTempDir $ do
85+ withSystemTempDirectory " config-is-here" $ \ dirFilePath -> do
86+ dir <- parseAbsDir dirFilePath
87+ let stackYamlFp = toFilePath (dir </> stackDotYaml)
88+ writeFile stackYamlFp sampleConfig
89+ withEnvVar " STACK_YAML" stackYamlFp $ do
90+ LoadConfig {.. } <- loadConfig' manager
91+ BuildConfig {.. } <- loadBuildConfigRest manager lcLoadBuildConfig
92+ bcRoot `shouldBe` dir
0 commit comments