@@ -9,17 +9,26 @@ import Control.Lens.Fold ((^?))
99import qualified Data.Maybe as Maybe
1010import qualified Data.Text as T
1111import qualified Data.Text.Internal.Search as T
12+ import qualified Data.Text.IO as T
1213import Distribution.Utils.Generic (safeHead )
1314import Ide.Plugin.Cabal.CabalAdd (hiddenPackageSuggestion )
1415import qualified Language.LSP.Protocol.Lens as L
15- import Language.LSP.Protocol.Types (Diagnostic (.. ), mkRange )
16+ import Language.LSP.Protocol.Types (Diagnostic (.. ),
17+ DidChangeWatchedFilesParams (.. ),
18+ FileChangeType (FileChangeType_Changed ),
19+ FileEvent (.. ), filePathToUri ,
20+ mkRange )
1621import System.FilePath
17- import Test.Hls (Session , TestTree , _R , anyMessage ,
18- assertEqual , documentContents ,
22+ import Test.Hls (SMethod (SMethod_WorkspaceDidChangeWatchedFiles ),
23+ Session , TestTree , _R , anyMessage ,
24+ assertEqual , assertFailure ,
25+ documentContents ,
1926 executeCodeAction ,
2027 getAllCodeActions ,
2128 getDocumentEdit , liftIO , openDoc ,
22- skipManyTill , testCase , testGroup ,
29+ sendNotification , skipManyTill ,
30+ testCase , testGroup ,
31+ type (|? ) (InR ),
2332 waitForDiagnosticsFrom , (@?=) )
2433import Utils
2534
@@ -141,6 +150,9 @@ cabalAddTests =
141150 , (" AAI" , " 0.1" )
142151 , (" AWin32Console" , " 1.19.1" )
143152 ]
153+
154+ , runShiftedRootInTmpDirTestCaseSession " Reload HLS after cabal file changes - without cabal plugin" (" cabal-add-testdata" </> " cabal-add-lib" )
155+ (generateReloadHlsAfterCabalChangeTestSession " cabal-add-lib.cabal" (" src" </> " MyLib.hs" ))
144156 ]
145157 where
146158 generateAddDependencyTestSession :: FilePath -> FilePath -> T. Text -> [Int ] -> Session ()
@@ -183,3 +195,17 @@ cabalAddTests =
183195 cas <- Maybe. mapMaybe (^? _R) <$> getAllCodeActions hsdoc
184196 let selectedCas = filter (\ ca -> " Add dependency" `T.isPrefixOf` (ca ^. L. title)) cas
185197 liftIO $ assertEqual " PackageYAML" [] selectedCas
198+
199+ generateReloadHlsAfterCabalChangeTestSession :: FilePath -> FilePath -> FilePath -> Session ()
200+ generateReloadHlsAfterCabalChangeTestSession cabalFile haskellFile root = do
201+ hsdoc <- openDoc haskellFile " haskell"
202+ _ <- waitForDiagnosticsFrom hsdoc
203+ cabalContent <- liftIO $ T. readFile cabalFile
204+ let fix = T. replace " build-depends: base" " build-depends: base, split"
205+ liftIO $ T. writeFile cabalFile (fix cabalContent)
206+ sendNotification SMethod_WorkspaceDidChangeWatchedFiles $ DidChangeWatchedFilesParams
207+ [ FileEvent (filePathToUri $ root </> cabalFile) FileChangeType_Changed ]
208+ diagnostics <- waitForDiagnosticsFrom hsdoc
209+ case diagnostics of
210+ [diagnostic] -> liftIO $ assertEqual " Wrong diagnostic after HLS restarts" (Just $ InR " GHC-66111" ) (diagnostic ^. L. code)
211+ _ -> liftIO $ assertFailure $ " Expect one diagnostic but got " <> show (length diagnostics)
0 commit comments