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

Commit fd66fa7

Browse files
committed
Update tests to select libdir based on cradle
1 parent f77fa4c commit fd66fa7

File tree

13 files changed

+126
-88
lines changed

13 files changed

+126
-88
lines changed

haskell-ide-engine.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ test-suite plugin-dispatcher-test
245245
main-is: Main.hs
246246
build-depends: base
247247
, data-default
248+
, directory
249+
, filepath
248250
, haskell-ide-engine
249251
, haskell-lsp-types
250252
, hie-plugin-api

hie-plugin-api/Haskell/Ide/Engine/Cradle.hs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,26 @@ getProjectGhcPath :: Cradle -> IO (Maybe FilePath)
8181
getProjectGhcPath crdl = do
8282
isStackInstalled <- isJust <$> findExecutable "stack"
8383
isCabalInstalled <- isJust <$> findExecutable "cabal"
84-
if isStackCradle crdl && isStackInstalled
84+
ghcpath <- if isStackCradle crdl && isStackInstalled
8585
then
86-
catch (Just <$> tryCommand "stack path --compiler-exe") $ \(_ :: IOException) ->
86+
catch (Just <$> tryCommand "stack path --compiler-exe") $ \(_ :: IOException) -> do
87+
errorm "Command `stack path --compiler-exe` failed."
8788
return Nothing
8889
else if isCabalCradle crdl && isCabalInstalled then do
89-
Just ghcCabalVersion <- catch (Just <$> tryCommand "cabal v2-exec ghc -- --numeric-version") $ \(_ ::IOException) ->
90+
ghcCabalVersion <- catch (Just <$> tryCommand "cabal v2-exec -v0 ghc -- --numeric-version") $ \(_ ::IOException) -> do
91+
errorm "Command `cabal v2-exec -v0 ghc -- --numeric-version` failed."
9092
return Nothing
91-
findExecutable ("ghc-" ++ ghcCabalVersion)
92-
else
93+
case ghcCabalVersion of
94+
Just ghcNumericVersion -> do
95+
let ghcVersion = "ghc-" ++ ghcNumericVersion
96+
logm $ "Ghc Version to find: " ++ ghcVersion
97+
findExecutable ghcVersion
98+
Nothing -> return Nothing
99+
else do
100+
logm "Neither cabal nor stack project, look for ghc project."
93101
findExecutable "ghc"
102+
logm $ "Found ghc path: " ++ show ghcpath
103+
return ghcpath
94104

95105
tryCommand :: String -> IO String
96106
tryCommand cmd =

src/Haskell/Ide/Engine/Server.hs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,11 @@ run scheduler _origDir plugins captureFp = flip E.catches handlers $ do
157157

158158
-- Check for mismatching GHC versions
159159
-- Ignore hie.yaml parse errors. They get reported in ModuleCache.hs
160-
let parseErrorHandler (_ :: Yaml.ParseException) = return Nothing
161-
dummyCradleFile = (fromMaybe currentDir lspRootDir) </> "File.hs"
160+
let parseErrorHandler (_ :: Yaml.ParseException) = do
161+
logm "Caught a yaml parse exception"
162+
return Nothing
163+
dummyCradleFile = fromMaybe currentDir lspRootDir </> "File.hs"
164+
logm $ "Dummy Cradle File: " ++ dummyCradleFile
162165
mcradle <- liftIO $ E.catch (Just <$> findLocalCradle dummyCradleFile) parseErrorHandler
163166

164167
-- haskell lsp sets the current directory to the project root in the InitializeRequest
@@ -410,13 +413,12 @@ reactor inp diagIn = do
410413
currentDir <- liftIO getCurrentDirectory
411414

412415
-- Check for mismatching GHC versions
413-
-- Ignore hie.yaml parse errors. They get reported in ModuleCache.hs
414-
let parseErrorHandler (_ :: Yaml.ParseException) = return Nothing
415-
dummyCradleFile = (fromMaybe currentDir lspRootDir) </> "File.hs"
416-
cradleRes <- liftIO $ E.catch (Just <$> findLocalCradle dummyCradleFile) parseErrorHandler
416+
let dummyCradleFile = (fromMaybe currentDir lspRootDir) </> "File.hs"
417+
logm $ "Dummy Cradle file result: " ++ dummyCradleFile
418+
cradleRes <- liftIO $ E.try (findLocalCradle dummyCradleFile)
417419

418420
case cradleRes of
419-
Just cradle -> do
421+
Right cradle -> do
420422
projGhcVersion <- liftIO $ getProjectGhcVersion cradle
421423
when (projGhcVersion /= hieGhcVersion) $ do
422424
let msg = T.pack $ "Mismatching GHC versions: " ++ cradleDisplay cradle ++
@@ -433,7 +435,9 @@ reactor inp diagIn = do
433435
reactorSend $ NotShowMessage $ fmServerShowMessageNotification J.MtWarning cabalMsg
434436
reactorSend $ NotLogMessage $ fmServerLogMessageNotification J.MtWarning cabalMsg
435437

436-
Nothing -> return ()
438+
Left (_ :: Yaml.ParseException) -> do
439+
logm "Failed to parse it"
440+
reactorSend $ NotShowMessage $ fmServerShowMessageNotification J.MtError "Couldn't parse hie.yaml"
437441

438442
renv <- ask
439443
let hreq = GReq tn "init-hoogle" Nothing Nothing Nothing callback Nothing $ IdeResultOk <$> Hoogle.initializeHoogleDb

test/dispatcher/Main.hs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Data.Default
1414
import GHC ( TypecheckedModule )
1515
import GHC.Generics
1616
import Haskell.Ide.Engine.Ghc
17+
import qualified Haskell.Ide.Engine.Cradle as Bios
1718
import Haskell.Ide.Engine.MonadTypes
1819
import Haskell.Ide.Engine.PluginUtils
1920
import Haskell.Ide.Engine.Scheduler
@@ -26,7 +27,7 @@ import System.FilePath
2627

2728
import Test.Hspec
2829
import Test.Hspec.Runner
29-
import System.IO
30+
import System.IO
3031

3132
-- ---------------------------------------------------------------------
3233
-- plugins
@@ -70,14 +71,18 @@ startServer :: IO (Scheduler IO, TChan LogVal, ThreadId)
7071
startServer = do
7172
scheduler <- newScheduler plugins testOptions
7273
logChan <- newTChanIO
74+
-- This is correct because we set the working directory to
75+
-- "test/testdata" in the function set-up.
76+
cwd <- getCurrentDirectory
77+
crdl <- Bios.findLocalCradle (cwd </> "File.hs")
7378
dispatcher <- forkIO $ do
7479
flushStackEnvironment
7580
runScheduler
7681
scheduler
7782
(\lid errCode e -> logToChan logChan ("received an error", Left (lid, errCode, e)))
7883
(\g x -> g x)
7984
def
80-
Nothing
85+
(Just crdl)
8186

8287
return (scheduler, logChan, dispatcher)
8388

@@ -127,6 +132,7 @@ instance ToJSON Cached where
127132

128133
funcSpec :: Spec
129134
funcSpec = describe "functional dispatch" $ do
135+
-- required to not kill the 'findLocalCradle' logic in 'startServer'.
130136
runIO $ setCurrentDirectory "test/testdata"
131137
(scheduler, logChan, dispatcher) <- runIO startServer
132138

test/functional/HieBiosSpec.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ spec = beforeAll_ (writeFile (hieBiosErrorPath </> "hie.yaml") "") $ do
2020
_ <- openDoc "Main.hs" "haskell"
2121
_ <- count 2 waitForDiagnostics
2222
return ()
23-
23+
2424
it "reports errors in hie.yaml" $ runSession hieCommand fullCaps hieBiosErrorPath $ do
2525
_ <- openDoc "Foo.hs" "haskell"
2626
_ <- skipManyTill loggingNotification (satisfy isMessage)
2727
return ()
28-
28+
2929
where hieBiosErrorPath = "test/testdata/hieBiosError"
30-
30+
3131
isMessage (NotShowMessage (NotificationMessage _ _ (ShowMessageParams MtError s))) =
3232
"Couldn't parse hie.yaml" `T.isInfixOf` s
3333
isMessage _ = False
34-

test/plugin-dispatcher/Main.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import Control.Concurrent.STM.TChan
77
import Control.Monad.STM
88
import qualified Data.Text as T
99
import Data.Default
10+
import qualified Haskell.Ide.Engine.Cradle as Bios
1011
import Haskell.Ide.Engine.MonadTypes
1112
import Haskell.Ide.Engine.Scheduler
1213
import Haskell.Ide.Engine.Types
1314
import Language.Haskell.LSP.Types
1415
import TestUtils
1516
import Test.Hspec
1617
import Test.Hspec.Runner
18+
import System.Directory (getCurrentDirectory)
19+
import System.FilePath
1720

1821
-- ---------------------------------------------------------------------
1922

@@ -42,11 +45,14 @@ newPluginSpec = do
4245

4346
let makeReq = sendRequest scheduler
4447

48+
cwd <- getCurrentDirectory
49+
crdl <- Bios.findLocalCradle (cwd </> "test" </> "testdata" </> "File.hs")
50+
4551
pid <- forkIO $ runScheduler scheduler
4652
(\_ _ _ -> return ())
4753
(\f x -> f x)
4854
def
49-
Nothing
55+
(Just crdl)
5056

5157
updateDocument scheduler (filePathToUri "test") 3
5258
sendRequest scheduler req0

test/unit/ApplyRefactPluginSpec.hs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ testPlugins = pluginDescToIdePlugins [applyRefactDescriptor "applyrefact"]
3535
applyRefactSpec :: Spec
3636
applyRefactSpec = do
3737
describe "apply-refact plugin commands" $ do
38-
applyRefactPath <- runIO $ filePathToUri <$> makeAbsolute "./test/testdata/ApplyRefact.hs"
39-
38+
applyRefactFp <- runIO $ makeAbsolute "./test/testdata/ApplyRefact.hs"
39+
let applyRefactPath = filePathToUri applyRefactFp
4040
-- ---------------------------------
4141

4242
it "applies one hint only" $ do
@@ -48,7 +48,7 @@ applyRefactSpec = do
4848
res = IdeResultOk $ WorkspaceEdit
4949
(Just $ H.singleton applyRefactPath textEdits)
5050
Nothing
51-
testCommand testPlugins act "applyrefact" "applyOne" arg res
51+
testCommand testPlugins applyRefactFp act "applyrefact" "applyOne" arg res
5252

5353
-- ---------------------------------
5454

@@ -61,7 +61,7 @@ applyRefactSpec = do
6161
res = IdeResultOk $ WorkspaceEdit
6262
(Just $ H.singleton applyRefactPath textEdits)
6363
Nothing
64-
testCommand testPlugins act "applyrefact" "applyAll" arg res
64+
testCommand testPlugins applyRefactFp act "applyrefact" "applyAll" arg res
6565

6666
-- ---------------------------------
6767

@@ -85,7 +85,7 @@ applyRefactSpec = do
8585
"Redundant bracket\nFound:\n (x + 1)\nWhy not:\n x + 1\n"
8686
Nothing
8787
]}
88-
runIGM testPlugins act `shouldReturn` res
88+
runIGM testPlugins applyRefactFp act `shouldReturn` res
8989

9090
-- ---------------------------------
9191

@@ -105,15 +105,15 @@ applyRefactSpec = do
105105
, _source = Just "hlint"
106106
, _message = T.pack filePathNoUri <> ":13:24: error:\n Operator applied to too few arguments: +\n data instance Sing (z :: (a :~: b)) where\n> SRefl :: Sing Refl +\n\n"
107107
, _relatedInformation = Nothing }]}
108-
runIGM testPlugins act `shouldReturn` res
108+
runIGM testPlugins applyRefactFp act `shouldReturn` res
109109

110110
-- ---------------------------------
111111

112112
it "respects hlint pragmas in the source file" $ do
113-
filePath <- filePathToUri <$> makeAbsolute "./test/testdata/HlintPragma.hs"
114-
113+
fp <- makeAbsolute "./test/testdata/HlintPragma.hs"
114+
let filePath = filePathToUri fp
115115
let req = lint filePath
116-
r <- runIGM testPlugins req
116+
r <- runIGM testPlugins fp req
117117
r `shouldBe`
118118
(IdeResultOk
119119
(PublishDiagnosticsParams
@@ -132,10 +132,11 @@ applyRefactSpec = do
132132
-- ---------------------------------
133133

134134
it "respects hlint config files in project root dir" $ do
135-
filePath <- filePathToUri <$> makeAbsolute "./test/testdata/HlintPragma.hs"
135+
fp <- makeAbsolute "./test/testdata/HlintPragma.hs"
136+
let filePath = filePathToUri fp
136137

137138
let req = lint filePath
138-
r <- withCurrentDirectory "./test/testdata" $ runIGM testPlugins req
139+
r <- withCurrentDirectory "./test/testdata" $ runIGM testPlugins fp req
139140
r `shouldBe`
140141
(IdeResultOk
141142
(PublishDiagnosticsParams
@@ -148,11 +149,11 @@ applyRefactSpec = do
148149
-- ---------------------------------
149150

150151
it "reports error without crash" $ do
151-
filePath <- filePathToUri <$> makeAbsolute "./test/testdata/ApplyRefactError.hs"
152-
152+
fp <- makeAbsolute "./test/testdata/ApplyRefactError.hs"
153+
let filePath = filePathToUri fp
153154
let req = applyAllCmd filePath
154155
isExpectedError (IdeResultFail (IdeError PluginError err _)) =
155156
"Illegal symbol '.' in type" `T.isInfixOf` err
156157
isExpectedError _ = False
157-
r <- withCurrentDirectory "./test/testdata" $ runIGM testPlugins req
158+
r <- withCurrentDirectory "./test/testdata" $ runIGM testPlugins fp req
158159
r `shouldSatisfy` isExpectedError

test/unit/ContextSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec = describe "Context of different cursor positions" $ do
2323
fp <- makeAbsolute "./ExampleContext.hs"
2424
let arg = filePathToUri fp
2525
let res = IdeResultOk (Nothing :: Maybe Context)
26-
actual <- runSingle (IdePlugins mempty) $ do
26+
actual <- runSingle (IdePlugins mempty) fp $ do
2727
_ <- setTypecheckedModule arg
2828
return $ IdeResultOk Nothing
2929

@@ -243,7 +243,7 @@ spec = describe "Context of different cursor positions" $ do
243243
getContextAt :: FilePath -> Position -> IO (IdeResult (Maybe Context))
244244
getContextAt fp pos = do
245245
let arg = filePathToUri fp
246-
runSingle (IdePlugins mempty) $ do
246+
runSingle (IdePlugins mempty) fp $ do
247247
_ <- setTypecheckedModule arg
248248
pluginGetFile "getContext: " arg $ \fp_ ->
249249
ifCachedModuleAndData fp_ (IdeResultOk Nothing) $ \tm _ () ->

test/unit/ExtensibleStateSpec.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Data.Typeable
66
import Haskell.Ide.Engine.MonadTypes
77
import Haskell.Ide.Engine.MonadFunctions
88
import TestUtils
9+
import System.Directory
10+
import System.FilePath
911

1012
import Test.Hspec
1113

@@ -20,7 +22,9 @@ extensibleStateSpec :: Spec
2022
extensibleStateSpec =
2123
describe "stores and retrieves in the state" $
2224
it "stores the first one" $ do
23-
r <- runIGM testPlugins $ do
25+
cwd <- getCurrentDirectory
26+
let fp = cwd </> "test" </> "testdata" </> "File.hs"
27+
r <- runIGM testPlugins fp $ do
2428
r1 <- makeRequest "test" "cmd1" ()
2529
r2 <- makeRequest "test" "cmd2" ()
2630
return (r1,r2)

0 commit comments

Comments
 (0)