Skip to content

Commit 0ebea4d

Browse files
committed
Add tests for haskell and cabal file interaction
1 parent 6c8d4c2 commit 0ebea4d

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

plugins/hls-cabal-plugin/test/Main.hs

+28-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{-# LANGUAGE OverloadedStrings #-}
22
{-# OPTIONS_GHC -Wno-orphans #-}
33
{-# LANGUAGE NamedFieldPuns #-}
4-
{-# LANGUAGE ViewPatterns #-}
54
module Main
65
( main
76
) where
87

98
import Control.Lens ((^.))
9+
import Data.Either (isRight)
1010
import Data.Function
1111
import qualified Data.Text as Text
1212
import Development.IDE.Types.Logger
@@ -15,14 +15,13 @@ import qualified Ide.Plugin.Cabal.Parse as Lib
1515
import qualified Language.LSP.Types.Lens as J
1616
import System.FilePath
1717
import Test.Hls
18-
import Data.Either (isRight)
1918

2019
cabalPlugin :: Recorder (WithPriority Log) -> PluginDescriptor IdeState
2120
cabalPlugin recorder = descriptor recorder "cabal"
2221

2322
main :: IO ()
2423
main = do
25-
recorder <- initialiseRecorder True
24+
recorder <- initialiseRecorder False
2625
defaultTestRunner $
2726
testGroup "Cabal Plugin Tests"
2827
[ unitTests
@@ -68,22 +67,42 @@ pluginTests recorder = testGroup "Plugin Tests"
6867
[ runCabalTestCaseSession "Publishes Diagnostics on Error" recorder "" $ do
6968
doc <- openDoc "invalid.cabal" "cabal"
7069
diags <- waitForDiagnosticsFromSource doc "parsing"
71-
reduceDiag <- liftIO $ inspectDiagnostic diags ["Unknown SPDX license identifier: 'BSD3'"]
70+
unknownLicenseDiag <- liftIO $ inspectDiagnostic diags ["Unknown SPDX license identifier: 'BSD3'"]
7271
liftIO $ do
7372
length diags @?= 1
74-
reduceDiag ^. J.range @?= Range (Position 3 24) (Position 4 0)
75-
reduceDiag ^. J.severity @?= Just DsError
73+
unknownLicenseDiag ^. J.range @?= Range (Position 3 24) (Position 4 0)
74+
unknownLicenseDiag ^. J.severity @?= Just DsError
7675
, runCabalTestCaseSession "Clears diagnostics" recorder "" $ do
7776
doc <- openDoc "invalid.cabal" "cabal"
7877
diags <- waitForDiagnosticsFrom doc
79-
reduceDiag <- liftIO $ inspectDiagnostic diags ["Unknown SPDX license identifier: 'BSD3'"]
78+
unknownLicenseDiag <- liftIO $ inspectDiagnostic diags ["Unknown SPDX license identifier: 'BSD3'"]
8079
liftIO $ do
8180
length diags @?= 1
82-
reduceDiag ^. J.range @?= Range (Position 3 24) (Position 4 0)
83-
reduceDiag ^. J.severity @?= Just DsError
81+
unknownLicenseDiag ^. J.range @?= Range (Position 3 24) (Position 4 0)
82+
unknownLicenseDiag ^. J.severity @?= Just DsError
8483
_ <- applyEdit doc $ TextEdit (Range (Position 3 20) (Position 4 0)) "BSD-3-Clause\n"
8584
newDiags <- waitForDiagnosticsFrom doc
8685
liftIO $ newDiags @?= []
86+
, runCabalTestCaseSession "No Diagnostics in .hs files from valid .cabal file" recorder "simple-cabal" $ do
87+
hsDoc <- openDoc "A.hs" "haskell"
88+
expectNoMoreDiagnostics 1 hsDoc "typechecking"
89+
cabalDoc <- openDoc "simple-cabal.cabal" "cabal"
90+
expectNoMoreDiagnostics 1 cabalDoc "parsing"
91+
, runCabalTestCaseSession "Diagnostics in .hs files from invalid .cabal file" recorder "simple-cabal" $ do
92+
hsDoc <- openDoc "A.hs" "haskell"
93+
expectNoMoreDiagnostics 1 hsDoc "typechecking"
94+
cabalDoc <- openDoc "simple-cabal.cabal" "cabal"
95+
expectNoMoreDiagnostics 1 cabalDoc "parsing"
96+
let theRange = Range (Position 3 20) (Position 3 23)
97+
-- Invalid license
98+
changeDoc cabalDoc [TextDocumentContentChangeEvent (Just theRange) Nothing "MIT3"]
99+
cabalDiags <- waitForDiagnosticsFrom cabalDoc
100+
unknownLicenseDiag <- liftIO $ inspectDiagnostic cabalDiags ["Unknown SPDX license identifier: 'MIT3'"]
101+
expectNoMoreDiagnostics 1 hsDoc "typechecking"
102+
liftIO $ do
103+
length cabalDiags @?= 1
104+
unknownLicenseDiag ^. J.range @?= Range (Position 3 24) (Position 4 0)
105+
unknownLicenseDiag ^. J.severity @?= Just DsError
87106
]
88107
, testGroup "Code Actions"
89108
[ runCabalTestCaseSession "BSD-3" recorder "" $ do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module A where
2+
3+
-- definitions don't matter here.
4+
foo = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cradle:
2+
cabal:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cabal-version: 3.0
2+
name: simple-cabal
3+
version: 0.1.0.0
4+
license: MIT
5+
6+
library
7+
build-depends: base
8+
hs-source-dirs: .
9+
exposed-modules: A
10+
default-language: Haskell2010

0 commit comments

Comments
 (0)