@@ -3,6 +3,7 @@ module CabalHelperSpec where
3
3
4
4
import Data.Maybe (isJust )
5
5
import Haskell.Ide.Engine.Cradle
6
+ import HIE.Bios.Types (runCradle , cradleOptsProg , Cradle , CradleLoadResult (.. ))
6
7
import Test.Hspec
7
8
import System.FilePath
8
9
import System.Directory (findExecutable , getCurrentDirectory , removeFile )
@@ -26,6 +27,9 @@ simpleCabalPath cwd = rootPath cwd </> "simple-cabal"
26
27
simpleStackPath :: FilePath -> FilePath
27
28
simpleStackPath cwd = rootPath cwd </> " simple-stack"
28
29
30
+ multiSourceDirsPath :: FilePath -> FilePath
31
+ multiSourceDirsPath cwd = rootPath cwd </> " multi-source-dirs"
32
+
29
33
spec :: Spec
30
34
spec = beforeAll_ setupStackFiles $ do
31
35
describe " stack and cabal executables should be accesible" $ do
@@ -36,88 +40,124 @@ spec = beforeAll_ setupStackFiles $ do
36
40
cabal <- findExecutable " stack"
37
41
cabal `shouldSatisfy` isJust
38
42
describe " cabal-helper spec" $ do
39
- describe " find cabal entry point spec " findCabalHelperEntryPointSpec
40
- describe " cradle discovery" cabalHelperCradleSpec
43
+ describe " find entry point" findCabalHelperEntryPointSpec
44
+ describe " cradle discovery and loading " cabalHelperCradleSpec
41
45
42
46
cabalHelperCradleSpec :: Spec
43
47
cabalHelperCradleSpec = do
44
48
cwd <- runIO getCurrentDirectory
45
49
describe " dummy filepath, finds none-cradle" $ do
46
- it " implicit exe, dummy filepath " $ do
50
+ it " implicit exe" $ do
47
51
crdl <- cabalHelperCradle (implicitExePath cwd </> " File.hs" )
48
52
crdl `shouldSatisfy` isCabalCradle
49
- it " mono repo, dummy filepath " $ do
53
+ it " mono repo" $ do
50
54
crdl <- cabalHelperCradle (monoRepoPath cwd </> " File.hs" )
51
55
crdl `shouldSatisfy` isCabalCradle
52
- it " stack repo, dummy filepath " $ do
56
+ it " stack repo" $ do
53
57
crdl <- cabalHelperCradle (simpleStackPath cwd </> " File.hs" )
54
58
crdl `shouldSatisfy` isStackCradle
55
- it " cabal repo, dummy filepath " $
59
+ it " cabal repo" $
56
60
pendingWith " Can not work because of global `cabal.project`"
57
61
-- crdl <- cabalHelperCradle (simpleCabalPath cwd </> "File.hs")
58
62
-- crdl `shouldSatisfy` isCabalCradle
59
- it " sub package, dummy filepath " $ do
63
+ it " sub package" $ do
60
64
crdl <- cabalHelperCradle (subPackagePath cwd </> " File.hs" )
61
65
crdl `shouldSatisfy` isStackCradle
66
+ it " multi-source-dirs" $ do
67
+ crdl <- cabalHelperCradle (multiSourceDirsPath cwd </> " File.hs" )
68
+ crdl `shouldSatisfy` isStackCradle
62
69
63
- describe " Existing projects" $ do
70
+ describe " existing projects" $ do
64
71
it " implicit exe" $ do
65
- crdl <- cabalHelperCradle ( implicitExePath cwd </> " src" </> " Exe.hs" )
66
- crdl `shouldSatisfy` isCabalCradle
72
+ let fp = implicitExePath cwd </> " src" </> " Exe.hs"
73
+ componentTest fp isCabalCradle
67
74
it " mono repo" $ do
68
- crdl <- cabalHelperCradle ( monoRepoPath cwd </> " A" </> " Main.hs" )
69
- crdl `shouldSatisfy` isCabalCradle
75
+ let fp = monoRepoPath cwd </> " A" </> " Main.hs"
76
+ componentTest fp isCabalCradle
70
77
it " stack repo" $ do
71
- crdl <- cabalHelperCradle ( simpleStackPath cwd </> " MyLib.hs" )
72
- crdl `shouldSatisfy` isStackCradle
78
+ let fp = simpleStackPath cwd </> " MyLib.hs"
79
+ componentTest fp isStackCradle
73
80
it " cabal repo" $
74
81
pendingWith " Can not work because of global `cabal.project`"
75
- -- crdl <- cabalHelperCradle (simpleCabalPath cwd </> "MyLib.hs")
76
- -- crdl `shouldSatisfy` isCabalCradle
82
+ -- let fp = (simpleCabalPath cwd </> "MyLib.hs")
83
+ -- componentTest fp isStackCradle
77
84
it " sub package" $ do
78
- crdl <- cabalHelperCradle (subPackagePath cwd </> " plugins-api" </> " PluginLib.hs" )
79
- crdl `shouldSatisfy` isStackCradle
85
+ let fp = subPackagePath cwd </> " plugins-api" </> " PluginLib.hs"
86
+ componentTest fp isStackCradle
87
+ it " multi-source-dirs, nested dir" $ do
88
+ let fp = multiSourceDirsPath cwd </> " src" </> " input" </> " Lib.hs"
89
+ componentTest fp isStackCradle
90
+ it " multi-source-dirs" $ do
91
+ let fp = multiSourceDirsPath cwd </> " src" </> " BetterLib.hs"
92
+ componentTest fp isStackCradle
93
+
94
+ componentTest :: FilePath -> (Cradle -> Bool ) -> Expectation
95
+ componentTest fp testCradleType = do
96
+ crdl <- cabalHelperCradle fp
97
+ crdl `shouldSatisfy` testCradleType
98
+ loadComponent crdl fp
99
+
100
+ loadComponent :: Cradle -> FilePath -> Expectation
101
+ loadComponent crdl fp = do
102
+ result <- runCradle (cradleOptsProg crdl) (\ _ -> return () ) fp
103
+ case result of
104
+ CradleFail err -> expectationFailure $ " Loading should not have failed: " ++ show err
105
+ _ -> return ()
106
+ return ()
80
107
81
108
findCabalHelperEntryPointSpec :: Spec
82
109
findCabalHelperEntryPointSpec = do
83
110
cwd <- runIO getCurrentDirectory
84
111
describe " implicit exe" $ do
85
- it " Find project root with dummy filepath" $ do
112
+ it " dummy filepath" $ do
86
113
let dummyFile = implicitExePath cwd </> " File.hs"
87
114
cabalTest dummyFile
88
- it " Find project root from source component" $ do
115
+ it " source component" $ do
89
116
let libFile = implicitExePath cwd </> " src" </> " Lib.hs"
90
117
cabalTest libFile
91
- it " Find project root from executable component" $ do
118
+ it " executable component" $ do
92
119
let mainFile = implicitExePath cwd </> " src" </> " Exe.hs"
93
120
cabalTest mainFile
94
121
95
122
describe " mono repo" $ do
96
- it " Find project root with dummy filepath" $ do
123
+ it " dummy filepath" $ do
97
124
let dummyFile = monoRepoPath cwd </> " File.hs"
98
125
cabalTest dummyFile
99
- it " Find project root with existing executable" $ do
126
+ it " existing executable" $ do
100
127
let mainFile = monoRepoPath cwd </> " A" </> " Main.hs"
101
128
cabalTest mainFile
102
129
103
130
describe " sub package repo" $ do
104
- it " Find project root with dummy filepath" $ do
131
+ it " dummy filepath" $ do
105
132
let dummyFile = subPackagePath cwd </> " File.hs"
106
133
stackTest dummyFile
107
- it " Find project root with existing executable" $ do
134
+ it " existing executable" $ do
108
135
let mainFile = subPackagePath cwd </> " plugins-api" </> " PluginLib.hs"
109
136
stackTest mainFile
110
137
111
138
describe " stack repo" $ do
112
- it " Find project root with dummy filepath" $ do
139
+ it " dummy filepath" $ do
113
140
let dummyFile = simpleStackPath cwd </> " File.hs"
114
141
stackTest dummyFile
115
- it " Find project root with real filepath" $ do
142
+ it " real filepath" $ do
116
143
let dummyFile = simpleStackPath cwd </> " MyLib.hs"
117
144
stackTest dummyFile
118
145
146
+ describe " multi-source-dirs" $ do
147
+ it " dummy filepath" $ do
148
+ let dummyFile = multiSourceDirsPath cwd </> " File.hs"
149
+ stackTest dummyFile
150
+
151
+ it " real filepath" $ do
152
+ let dummyFile = multiSourceDirsPath cwd </> " src" </> " BetterLib.hs"
153
+ stackTest dummyFile
154
+
155
+ it " nested filpath" $ do
156
+ let dummyFile = multiSourceDirsPath cwd </> " src" </> " input" </> " Lib.hs"
157
+ stackTest dummyFile
158
+
119
159
describe " simple cabal repo" $
120
- it " Find porject root with dummy filepath" $
160
+ it " Find project root with dummy filepath" $
121
161
pendingWith " Change test-setup, we will always find `cabal.project` in root dir"
122
162
123
163
-- -------------------------------------------------------------
@@ -141,20 +181,23 @@ stackTest fp = do
141
181
setupStackFiles :: IO ()
142
182
setupStackFiles = do
143
183
resolver <- readResolver
144
- cwd <- getCurrentDirectory
184
+ cwd <- getCurrentDirectory
145
185
writeFile (implicitExePath cwd </> " stack.yaml" ) (standardStackYaml resolver)
146
- writeFile (monoRepoPath cwd </> " stack.yaml" ) (monoRepoStackYaml resolver)
147
- writeFile (subPackagePath cwd </> " stack.yaml" ) (subPackageStackYaml resolver)
186
+ writeFile (monoRepoPath cwd </> " stack.yaml" ) (monoRepoStackYaml resolver)
187
+ writeFile (subPackagePath cwd </> " stack.yaml" ) (subPackageStackYaml resolver)
148
188
writeFile (simpleStackPath cwd </> " stack.yaml" ) (standardStackYaml resolver)
189
+ writeFile (multiSourceDirsPath cwd </> " stack.yaml" )
190
+ (standardStackYaml resolver)
149
191
150
192
151
193
cleanupStackFiles :: IO ()
152
194
cleanupStackFiles = do
153
- cwd <- getCurrentDirectory
195
+ cwd <- getCurrentDirectory
154
196
removeFile (implicitExePath cwd </> " stack.yaml" )
155
- removeFile (monoRepoPath cwd </> " stack.yaml" )
156
- removeFile (subPackagePath cwd </> " stack.yaml" )
197
+ removeFile (monoRepoPath cwd </> " stack.yaml" )
198
+ removeFile (subPackagePath cwd </> " stack.yaml" )
157
199
removeFile (simpleStackPath cwd </> " stack.yaml" )
200
+ removeFile (multiSourceDirsPath cwd </> " stack.yaml" )
158
201
159
202
-- -------------------------------------------------------------
160
203
0 commit comments