|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | + |
| 3 | +module Config (tests) where |
| 4 | + |
| 5 | +import Control.Lens hiding (List) |
| 6 | +import Control.Monad.IO.Class |
| 7 | +import Data.Aeson |
| 8 | +import Data.Default |
| 9 | +import qualified Data.Map as Map |
| 10 | +import qualified Data.Text as T |
| 11 | +import Ide.Plugin.Config |
| 12 | +import Language.Haskell.LSP.Test as Test |
| 13 | +import Language.Haskell.LSP.Types |
| 14 | +import qualified Language.Haskell.LSP.Types.Lens as L |
| 15 | +import System.FilePath ((</>)) |
| 16 | +import Test.Hls.Util |
| 17 | +import Test.Tasty |
| 18 | +import Test.Tasty.HUnit |
| 19 | + |
| 20 | +{-# ANN module ("HLint: ignore Reduce duplication"::String) #-} |
| 21 | + |
| 22 | +tests :: TestTree |
| 23 | +tests = testGroup "plugin config" [ |
| 24 | + -- Note: because the flag is treated generically in the plugin handler, we |
| 25 | + -- do not have to test each individual plugin |
| 26 | + hlintTests |
| 27 | + ] |
| 28 | + |
| 29 | +hlintTests :: TestTree |
| 30 | +hlintTests = testGroup "hlint plugin enables" [ |
| 31 | + |
| 32 | + testCase "changing hlintOn configuration enables or disables hlint diagnostics" $ runHlintSession "" $ do |
| 33 | + let config = def { hlintOn = True } |
| 34 | + sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config)) |
| 35 | + |
| 36 | + doc <- openDoc "ApplyRefact2.hs" "haskell" |
| 37 | + testHlintDiagnostics doc |
| 38 | + |
| 39 | + let config' = def { hlintOn = False } |
| 40 | + sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config')) |
| 41 | + |
| 42 | + diags' <- waitForDiagnosticsFrom doc |
| 43 | + |
| 44 | + liftIO $ noHlintDiagnostics diags' |
| 45 | + |
| 46 | + , testCase "changing hlint plugin configuration enables or disables hlint diagnostics" $ runHlintSession "" $ do |
| 47 | + let config = def { hlintOn = True } |
| 48 | + sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config)) |
| 49 | + |
| 50 | + doc <- openDoc "ApplyRefact2.hs" "haskell" |
| 51 | + testHlintDiagnostics doc |
| 52 | + |
| 53 | + let config' = pluginGlobalOn config "hlint" False |
| 54 | + sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (toJSON config')) |
| 55 | + |
| 56 | + diags' <- waitForDiagnosticsFrom doc |
| 57 | + |
| 58 | + liftIO $ noHlintDiagnostics diags' |
| 59 | + |
| 60 | + ] |
| 61 | + where |
| 62 | + runHlintSession :: FilePath -> Session a -> IO a |
| 63 | + runHlintSession subdir = |
| 64 | + failIfSessionTimeout . runSession hlsCommand fullCaps ("test/testdata/hlint" </> subdir) |
| 65 | + |
| 66 | + noHlintDiagnostics :: [Diagnostic] -> Assertion |
| 67 | + noHlintDiagnostics diags = |
| 68 | + Just "hlint" `notElem` map (^. L.source) diags @? "There are no hlint diagnostics" |
| 69 | + |
| 70 | + testHlintDiagnostics doc = do |
| 71 | + diags <- waitForDiagnosticsFromSource doc "hlint" |
| 72 | + liftIO $ length diags > 0 @? "There are hlint diagnostics" |
| 73 | + |
| 74 | +pluginGlobalOn :: Config -> T.Text -> Bool -> Config |
| 75 | +pluginGlobalOn config pid state = config' |
| 76 | + where |
| 77 | + pluginConfig = def { plcGlobalOn = state } |
| 78 | + config' = def { plugins = Map.insert pid pluginConfig (plugins config) } |
0 commit comments