forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigSpec.hs
More file actions
108 lines (94 loc) · 3.74 KB
/
ConfigSpec.hs
File metadata and controls
108 lines (94 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
module Stack.ConfigSpec where
import Control.Applicative
import Control.Monad.Logger
import Control.Exception
import Data.Maybe
import Data.Monoid
import Network.HTTP.Conduit (Manager)
import Path
--import System.FilePath
import System.Directory
import System.IO.Temp
import System.Environment
import Test.Hspec
import Stack.Config
import Stack.Types.Config
import Stack.Types.StackT
sampleConfig :: String
sampleConfig =
"resolver: lts-2.10\n" ++
"packages: ['.']\n"
stackDotYaml :: Path Rel File
stackDotYaml = $(mkRelFile "stack.yaml")
data T = T
{ manager :: Manager
}
setup :: IO T
setup = do
manager <- newTLSManager
unsetEnv "STACK_YAML"
return T{..}
teardown :: T -> IO ()
teardown _ = return ()
spec :: Spec
spec = beforeAll setup $ afterAll teardown $ do
let logLevel = LevelDebug
-- TODO(danburton): not use inTempDir
let inTempDir action = do
currentDirectory <- getCurrentDirectory
withSystemTempDirectory "Stack_ConfigSpec" $ \tempDir -> do
let enterDir = setCurrentDirectory tempDir
let exitDir = setCurrentDirectory currentDirectory
bracket_ enterDir exitDir action
-- TODO(danburton): a safer version of this?
let withEnvVar name newValue action = do
originalValue <- fromMaybe "" <$> lookupEnv name
let setVar = setEnv name newValue
let resetVar = setEnv name originalValue
bracket_ setVar resetVar action
describe "loadConfig" $ do
let loadConfig' m = runStackLoggingT m logLevel False False (loadConfig mempty Nothing)
let loadBuildConfigRest m = runStackLoggingT m logLevel False False
-- TODO(danburton): make sure parent dirs also don't have config file
it "works even if no config file exists" $ \T{..} -> example $ do
_config <- loadConfig' manager
return ()
it "works with a blank config file" $ \T{..} -> inTempDir $ do
writeFile (toFilePath stackDotYaml) ""
-- TODO(danburton): more specific test for exception
loadConfig' manager `shouldThrow` anyException
it "finds the config file in a parent directory" $ \T{..} -> inTempDir $ do
writeFile (toFilePath stackDotYaml) sampleConfig
parentDir <- getCurrentDirectory >>= parseAbsDir
let childDir = "child"
createDirectory childDir
setCurrentDirectory childDir
LoadConfig{..} <- loadConfig' manager
BuildConfig{..} <- loadBuildConfigRest manager
(lcLoadBuildConfig Nothing)
bcRoot `shouldBe` parentDir
it "respects the STACK_YAML env variable" $ \T{..} -> inTempDir $ do
withSystemTempDirectory "config-is-here" $ \dirFilePath -> do
dir <- parseAbsDir dirFilePath
let stackYamlFp = toFilePath (dir </> stackDotYaml)
writeFile stackYamlFp sampleConfig
withEnvVar "STACK_YAML" stackYamlFp $ do
LoadConfig{..} <- loadConfig' manager
BuildConfig{..} <- loadBuildConfigRest manager
(lcLoadBuildConfig Nothing)
bcRoot `shouldBe` dir
it "STACK_YAML can be relative" $ \T{..} -> inTempDir $ do
parentDir <- getCurrentDirectory >>= parseAbsDir
let childRel = $(mkRelDir "child")
yamlRel = childRel </> $(mkRelFile "some-other-name.config")
yamlAbs = parentDir </> yamlRel
createDirectoryIfMissing True $ toFilePath $ parent yamlAbs
writeFile (toFilePath yamlAbs) "resolver: ghc-7.8"
withEnvVar "STACK_YAML" (toFilePath yamlRel) $ do
LoadConfig{..} <- loadConfig' manager
BuildConfig{..} <- loadBuildConfigRest manager
(lcLoadBuildConfig Nothing)
bcStackYaml `shouldBe` yamlAbs