Skip to content

Commit 181e479

Browse files
committed
Add test-case for error message
1 parent 3e83a49 commit 181e479

File tree

9 files changed

+72
-44
lines changed

9 files changed

+72
-44
lines changed

ghcide/session-loader/Development/IDE/Session/Diagnostics.hs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1+
{-# LANGUAGE DeriveAnyClass #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
14
module Development.IDE.Session.Diagnostics where
5+
import Control.Applicative
26
import Control.Monad
37
import qualified Data.Aeson as Aeson
4-
import Data.Char (isLower)
58
import Data.List
6-
import Data.List.Extra (dropPrefix, split)
9+
import Data.List.Extra (split)
710
import Data.Maybe
811
import qualified Data.Text as T
9-
import qualified Data.Vector as Vector
1012
import Development.IDE.Types.Diagnostics
1113
import Development.IDE.Types.Location
14+
import GHC.Generics
1215
import qualified HIE.Bios.Cradle as HieBios
1316
import HIE.Bios.Types hiding (Log)
1417
import System.FilePath
15-
import Control.Applicative
18+
19+
data CradleErrorDetails =
20+
CradleErrorDetails
21+
{ cabalProjectFiles :: [FilePath]
22+
-- ^ files related to the cradle error
23+
-- i.e. .cabal, cabal.project, etc.
24+
} deriving (Show, Eq, Ord, Read, Generic, Aeson.ToJSON, Aeson.FromJSON)
1625

1726
{- | Takes a cradle error, the corresponding cradle and the file path where
1827
the cradle error occurred (of the file we attempted to load).
@@ -22,7 +31,7 @@ renderCradleError :: CradleError -> Cradle a -> NormalizedFilePath -> FileDiagno
2231
renderCradleError (CradleError deps _ec ms) cradle nfp
2332
| HieBios.isCabalCradle cradle =
2433
let (fp, showDiag, diag) = ideErrorWithSource (Just "cradle") (Just DiagnosticSeverity_Error) nfp $ T.unlines $ map T.pack userFriendlyMessage in
25-
(fp, showDiag, diag{_data_ = Just (Aeson.Array $ Vector.fromList $ map (Aeson.String . T.pack) absDeps)})
34+
(fp, showDiag, diag{_data_ = Just $ Aeson.toJSON CradleErrorDetails{cabalProjectFiles=absDeps}})
2635
| otherwise = ideErrorWithSource (Just "cradle") (Just DiagnosticSeverity_Error) nfp $ T.unlines $ map T.pack userFriendlyMessage
2736
where
2837
absDeps = fmap (cradleRootDir cradle </>) deps
@@ -33,7 +42,7 @@ renderCradleError (CradleError deps _ec ms) cradle nfp
3342

3443
mkUnknownModuleMessage :: Maybe [String]
3544
mkUnknownModuleMessage
36-
| any (isInfixOf "Error: cabal: Failed extracting script block:") ms = Just $ unknownModuleMessage (fromNormalizedFilePath nfp) Nothing
45+
| any (isInfixOf "Error: cabal: Failed extracting script block:") ms = Just $ unknownModuleMessage (fromNormalizedFilePath nfp)
3746
| otherwise = Nothing
3847

3948
fileMissingMessage :: Maybe [String]
@@ -79,20 +88,18 @@ parseMultiCradleErr ms = do
7988

8089
multiCradleErrMessage :: MultiCradleErr -> [String]
8190
multiCradleErrMessage e =
82-
unknownModuleMessage moduleFileName (Just moduleName)
91+
unknownModuleMessage (mcFilePath e)
8392
<> [""]
8493
<> map prefix (mcPrefixes e)
8594
where
86-
localFilePath f = dropWhile (==pathSeparator) $ dropPrefix (mcPwd e) f
87-
moduleFileName = localFilePath $ mcFilePath e
88-
moduleName = intercalate "." $ map dropExtension $ dropWhile isSourceFolder $ splitDirectories moduleFileName
89-
isSourceFolder p = all isLower $ take 1 p
9095
prefix (f, r) = f <> " - " <> r
9196

92-
unknownModuleMessage :: String -> Maybe String -> [String]
93-
unknownModuleMessage moduleFileName moduleNameM =
97+
unknownModuleMessage :: String -> [String]
98+
unknownModuleMessage moduleFileName =
9499
[ "Loading the module '" <> moduleFileName <> "' failed."
100+
, ""
95101
, "It may not be listed in your .cabal file!"
96-
, "Perhaps you need to add `"<> fromMaybe (takeFileName moduleFileName) moduleNameM <> "` to other-modules or exposed-modules."
102+
, "Perhaps you need to add `"<> dropExtension (takeFileName moduleFileName) <> "` to other-modules or exposed-modules."
103+
, ""
97104
, "For more information, visit: https://cabal.readthedocs.io/en/3.4/developing-packages.html#modules-included-in-the-package"
98105
]

test/functional/FunctionalBadProject.hs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,27 @@
22

33
module FunctionalBadProject (tests) where
44

5-
-- import Control.Lens hiding (List)
6-
-- import Control.Monad.IO.Class
7-
-- import qualified Data.Text as T
8-
-- import Language.LSP.Test hiding (message)
9-
-- import Language.LSP.Types as LSP
10-
-- import Language.LSP.Types.Lens as LSP hiding (contents, error )
5+
import Control.Lens
6+
import qualified Data.Text as T
7+
import qualified Language.LSP.Protocol.Lens as L
118
import Test.Hls
9+
import Test.Hls.Command
10+
1211

13-
-- ---------------------------------------------------------------------
14-
-- TODO: Currently this can not succeed, since such an error is thrown in "runActionWithContext" which
15-
-- can produce diagnostics at the moment. Needs more investigation
16-
-- TODO: @fendor: Add issue link here
17-
--
1812
tests :: TestTree
19-
tests = testGroup "behaviour on malformed projects" [
20-
testCase "no test executed" $ True @?= True
13+
tests = testGroup "behaviour on malformed projects"
14+
[ testCase "Missing module diagnostic" $ do
15+
runSession hlsCommand fullCaps "test/testdata/missingModuleTest/missingModule/" $ do
16+
doc <- openDoc "src/MyLib.hs" "haskell"
17+
[diag] <- waitForDiagnosticsFrom doc
18+
liftIO $ assertBool "missing module name" $ "MyLib" `T.isInfixOf` (diag ^. L.message)
19+
liftIO $ assertBool "module missing context" $ "may not be listed" `T.isInfixOf` (diag ^. L.message)
20+
, testCase "Missing module diagnostic - no matching prefix" $ do
21+
runSession hlsCommand fullCaps "test/testdata/missingModuleTest/noPrefixMatch/" $ do
22+
doc <- openDoc "app/Other.hs" "haskell"
23+
[diag] <- waitForDiagnosticsFrom doc
24+
liftIO $ assertBool "missing module name" $
25+
"Other" `T.isInfixOf` (diag ^. L.message)
26+
liftIO $ assertBool "hie-bios message" $
27+
"Cabal {component = Just \"exe:testExe\"}" `T.isInfixOf` (diag ^. L.message)
2128
]
22-
23-
-- testCase "deals with cabal file with unsatisfiable dependency" $
24-
-- runSession hlsCommandExamplePlugin codeActionSupportCaps "test/testdata/badProjects/cabal" $ do
25-
-- _doc <- openDoc "Foo.hs" "haskell"
26-
27-
-- diags@(d:_) <- waitForDiagnosticsSource "bios"
28-
-- -- liftIO $ show diags @?= ""
29-
-- -- liftIO $ putStrLn $ show diags
30-
-- -- liftIO $ putStrLn "a"
31-
-- liftIO $ do
32-
-- length diags @?= 1
33-
-- d ^. range @?= Range (Position 0 0) (Position 1 0)
34-
-- d ^. severity @?= (Just DsError)
35-
-- d ^. code @?= Nothing
36-
-- d ^. source @?= Just "bios"
37-
-- d ^. message @?=
38-
-- (T.pack "readCreateProcess: stack \"build\" \"--only-configure\" \".\" (exit 1): failed\n")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: ./
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cabal-version: 3.4
2+
name: missingModule
3+
version: 0.1.0.0
4+
build-type: Simple
5+
6+
library
7+
hs-source-dirs: ./src/
8+
exposed-modules:
9+
build-depends: base
10+
default-language: Haskell2010
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module MyLib where
2+
3+
someFunc :: IO ()
4+
someFunc = do
5+
putStrLn "someFunc"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
main :: IO ()
3+
main = do
4+
putStrLn "someFunc"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module Other where
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: ./
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cabal-version: 3.4
2+
name: noPrefixMatch
3+
version: 0.1.0.0
4+
build-type: Simple
5+
6+
executable testExe
7+
main-is: Main.hs
8+
hs-source-dirs: app
9+
build-depends: base

0 commit comments

Comments
 (0)