Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit d1c75d0

Browse files
committed
Add failing test-cases for #1576
1 parent 9e9432a commit d1c75d0

File tree

6 files changed

+103
-35
lines changed

6 files changed

+103
-35
lines changed

haskell-ide-engine.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ test-suite unit-test
204204
build-tool-depends: cabal-helper:cabal-helper-main, hspec-discover:hspec-discover
205205
build-depends: QuickCheck
206206
, aeson
207-
, cabal-helper
207+
, cabal-helper
208208
, ghc
209209
, base
210210
, bytestring
@@ -215,6 +215,7 @@ test-suite unit-test
215215
, ghc
216216
, haskell-ide-engine
217217
, haskell-lsp-types == 0.19.*
218+
, hie-bios
218219
, hie-test-utils
219220
, hie-plugin-api
220221
, hoogle > 5.0.11
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cabal-version: >=1.10
2+
name: multi-source-dirs
3+
version: 0.1.0.0
4+
license-file: LICENSE
5+
build-type: Simple
6+
7+
library
8+
exposed-modules: Lib, BetterLib
9+
hs-source-dirs: src, src/input
10+
build-depends: base >=4.12 && <5
11+
default-language: Haskell2010
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module BetterLib where
2+
3+
4+
foo = 3
5+
bar = "String"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Lib where
2+
3+
foobar = 15
4+
5+
fizbuzz :: Int -> String
6+
fizbuzz n = "Fizz"

test/unit/CabalHelperSpec.hs

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
module CabalHelperSpec where
33

44
import Haskell.Ide.Engine.Cradle
5+
import HIE.Bios.Types (runCradle, cradleOptsProg, Cradle, CradleLoadResult(..))
56
import Test.Hspec
67
import System.FilePath
78
import System.Directory (getCurrentDirectory, removeFile)
@@ -25,91 +26,130 @@ simpleCabalPath cwd = rootPath cwd </> "simple-cabal"
2526
simpleStackPath :: FilePath -> FilePath
2627
simpleStackPath cwd = rootPath cwd </> "simple-stack"
2728

29+
multiSourceDirsPath :: FilePath -> FilePath
30+
multiSourceDirsPath cwd = rootPath cwd </> "multi-source-dirs"
31+
2832
spec :: Spec
2933
spec = beforeAll_ setupStackFiles $ do
3034
describe "cabal-helper spec" $ do
31-
describe "find cabal entry point spec" findCabalHelperEntryPointSpec
32-
describe "cradle discovery" cabalHelperCradleSpec
35+
describe "find entry point" findCabalHelperEntryPointSpec
36+
describe "cradle discovery and loading" cabalHelperCradleSpec
3337

3438
cabalHelperCradleSpec :: Spec
3539
cabalHelperCradleSpec = do
3640
cwd <- runIO getCurrentDirectory
3741
describe "dummy filepath, finds none-cradle" $ do
38-
it "implicit exe, dummy filepath" $ do
42+
it "implicit exe" $ do
3943
crdl <- cabalHelperCradle (implicitExePath cwd </> "File.hs")
4044
crdl `shouldSatisfy` isCabalCradle
41-
it "mono repo, dummy filepath" $ do
45+
it "mono repo" $ do
4246
crdl <- cabalHelperCradle (monoRepoPath cwd </> "File.hs")
4347
crdl `shouldSatisfy` isCabalCradle
44-
it "stack repo, dummy filepath" $ do
48+
it "stack repo" $ do
4549
crdl <- cabalHelperCradle (simpleStackPath cwd </> "File.hs")
4650
crdl `shouldSatisfy` isStackCradle
47-
it "cabal repo, dummy filepath" $
51+
it "cabal repo" $
4852
pendingWith "Can not work because of global `cabal.project`"
4953
-- crdl <- cabalHelperCradle (simpleCabalPath cwd </> "File.hs")
5054
-- crdl `shouldSatisfy` isCabalCradle
51-
it "sub package, dummy filepath" $ do
55+
it "sub package" $ do
5256
crdl <- cabalHelperCradle (subPackagePath cwd </> "File.hs")
5357
crdl `shouldSatisfy` isStackCradle
58+
it "multi-source-dirs" $ do
59+
crdl <- cabalHelperCradle (multiSourceDirsPath cwd </> "File.hs")
60+
crdl `shouldSatisfy` isStackCradle
5461

55-
describe "Existing projects" $ do
62+
describe "existing projects" $ do
5663
it "implicit exe" $ do
57-
crdl <- cabalHelperCradle (implicitExePath cwd </> "src" </> "Exe.hs")
58-
crdl `shouldSatisfy` isCabalCradle
64+
let fp = implicitExePath cwd </> "src" </> "Exe.hs"
65+
componentTest fp isCabalCradle
5966
it "mono repo" $ do
60-
crdl <- cabalHelperCradle (monoRepoPath cwd </> "A" </> "Main.hs")
61-
crdl `shouldSatisfy` isCabalCradle
67+
let fp = monoRepoPath cwd </> "A" </> "Main.hs"
68+
componentTest fp isCabalCradle
6269
it "stack repo" $ do
63-
crdl <- cabalHelperCradle (simpleStackPath cwd </> "MyLib.hs")
64-
crdl `shouldSatisfy` isStackCradle
70+
let fp = simpleStackPath cwd </> "MyLib.hs"
71+
componentTest fp isStackCradle
6572
it "cabal repo" $
6673
pendingWith "Can not work because of global `cabal.project`"
67-
-- crdl <- cabalHelperCradle (simpleCabalPath cwd </> "MyLib.hs")
68-
-- crdl `shouldSatisfy` isCabalCradle
74+
-- let fp = (simpleCabalPath cwd </> "MyLib.hs")
75+
-- componentTest fp isStackCradle
6976
it "sub package" $ do
70-
crdl <- cabalHelperCradle (subPackagePath cwd </> "plugins-api" </> "PluginLib.hs")
71-
crdl `shouldSatisfy` isStackCradle
77+
let fp = subPackagePath cwd </> "plugins-api" </> "PluginLib.hs"
78+
componentTest fp isStackCradle
79+
it "multi-source-dirs, nested dir" $ do
80+
let fp = multiSourceDirsPath cwd </> "src" </> "input" </> "Lib.hs"
81+
componentTest fp isStackCradle
82+
it "multi-source-dirs" $ do
83+
let fp = multiSourceDirsPath cwd </> "src" </> "BetterLib.hs"
84+
componentTest fp isStackCradle
85+
86+
componentTest :: FilePath -> (Cradle -> Bool) -> Expectation
87+
componentTest fp testCradleType = do
88+
crdl <- cabalHelperCradle fp
89+
crdl `shouldSatisfy` testCradleType
90+
loadComponent crdl fp
91+
92+
loadComponent :: Cradle -> FilePath -> Expectation
93+
loadComponent crdl fp = do
94+
result <- runCradle (cradleOptsProg crdl) (\_ -> return ()) fp
95+
case result of
96+
CradleFail err -> expectationFailure $ "Loading should not have failed: " ++ show err
97+
_ -> return ()
98+
return ()
7299

73100
findCabalHelperEntryPointSpec :: Spec
74101
findCabalHelperEntryPointSpec = do
75102
cwd <- runIO getCurrentDirectory
76103
describe "implicit exe" $ do
77-
it "Find project root with dummy filepath" $ do
104+
it "dummy filepath" $ do
78105
let dummyFile = implicitExePath cwd </> "File.hs"
79106
cabalTest dummyFile
80-
it "Find project root from source component" $ do
107+
it "source component" $ do
81108
let libFile = implicitExePath cwd </> "src" </> "Lib.hs"
82109
cabalTest libFile
83-
it "Find project root from executable component" $ do
110+
it "executable component" $ do
84111
let mainFile = implicitExePath cwd </> "src" </> "Exe.hs"
85112
cabalTest mainFile
86113

87114
describe "mono repo" $ do
88-
it "Find project root with dummy filepath" $ do
115+
it "dummy filepath" $ do
89116
let dummyFile = monoRepoPath cwd </> "File.hs"
90117
cabalTest dummyFile
91-
it "Find project root with existing executable" $ do
118+
it "existing executable" $ do
92119
let mainFile = monoRepoPath cwd </> "A" </> "Main.hs"
93120
cabalTest mainFile
94121

95122
describe "sub package repo" $ do
96-
it "Find project root with dummy filepath" $ do
123+
it "dummy filepath" $ do
97124
let dummyFile = subPackagePath cwd </> "File.hs"
98125
stackTest dummyFile
99-
it "Find project root with existing executable" $ do
126+
it "existing executable" $ do
100127
let mainFile = subPackagePath cwd </> "plugins-api" </> "PluginLib.hs"
101128
stackTest mainFile
102129

103130
describe "stack repo" $ do
104-
it "Find project root with dummy filepath" $ do
131+
it "dummy filepath" $ do
105132
let dummyFile = simpleStackPath cwd </> "File.hs"
106133
stackTest dummyFile
107-
it "Find project root with real filepath" $ do
134+
it "real filepath" $ do
108135
let dummyFile = simpleStackPath cwd </> "MyLib.hs"
109136
stackTest dummyFile
110137

138+
describe "multi-source-dirs" $ do
139+
it "dummy filepath" $ do
140+
let dummyFile = multiSourceDirsPath cwd </> "File.hs"
141+
stackTest dummyFile
142+
143+
it "real filepath" $ do
144+
let dummyFile = multiSourceDirsPath cwd </> "src" </> "BetterLib.hs"
145+
stackTest dummyFile
146+
147+
it "nested filpath" $ do
148+
let dummyFile = multiSourceDirsPath cwd </> "src" </> "input" </> "Lib.hs"
149+
stackTest dummyFile
150+
111151
describe "simple cabal repo" $
112-
it "Find porject root with dummy filepath" $
152+
it "Find project root with dummy filepath" $
113153
pendingWith "Change test-setup, we will always find `cabal.project` in root dir"
114154

115155
-- -------------------------------------------------------------
@@ -133,20 +173,23 @@ stackTest fp = do
133173
setupStackFiles :: IO ()
134174
setupStackFiles = do
135175
resolver <- readResolver
136-
cwd <- getCurrentDirectory
176+
cwd <- getCurrentDirectory
137177
writeFile (implicitExePath cwd </> "stack.yaml") (standardStackYaml resolver)
138-
writeFile (monoRepoPath cwd </> "stack.yaml") (monoRepoStackYaml resolver)
139-
writeFile (subPackagePath cwd </> "stack.yaml") (subPackageStackYaml resolver)
178+
writeFile (monoRepoPath cwd </> "stack.yaml") (monoRepoStackYaml resolver)
179+
writeFile (subPackagePath cwd </> "stack.yaml") (subPackageStackYaml resolver)
140180
writeFile (simpleStackPath cwd </> "stack.yaml") (standardStackYaml resolver)
181+
writeFile (multiSourceDirsPath cwd </> "stack.yaml")
182+
(standardStackYaml resolver)
141183

142184

143185
cleanupStackFiles :: IO ()
144186
cleanupStackFiles = do
145-
cwd <- getCurrentDirectory
187+
cwd <- getCurrentDirectory
146188
removeFile (implicitExePath cwd </> "stack.yaml")
147-
removeFile (monoRepoPath cwd </> "stack.yaml")
148-
removeFile (subPackagePath cwd </> "stack.yaml")
189+
removeFile (monoRepoPath cwd </> "stack.yaml")
190+
removeFile (subPackagePath cwd </> "stack.yaml")
149191
removeFile (simpleStackPath cwd </> "stack.yaml")
192+
removeFile (multiSourceDirsPath cwd </> "stack.yaml")
150193

151194
-- -------------------------------------------------------------
152195

0 commit comments

Comments
 (0)