Skip to content

Commit

Permalink
Add integration tests for field name code action fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor authored and VeryMilkyJoe committed Jul 11, 2024
1 parent b0e9815 commit 26d3622
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 51 deletions.
1 change: 1 addition & 0 deletions haskell-language-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ test-suite hls-cabal-plugin-tests
, base
, bytestring
, Cabal-syntax >= 3.7
, extra
, filepath
, ghcide
, haskell-language-server:hls-cabal-plugin
Expand Down
3 changes: 2 additions & 1 deletion plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ import Ide.Plugin.Cabal.Completion.Types (ParseCabalCommonSe
ParseCabalFile (..))
import qualified Ide.Plugin.Cabal.Completion.Types as Types
import qualified Ide.Plugin.Cabal.Diagnostics as Diagnostics
import qualified Ide.Plugin.Cabal.FieldSuggest as FieldSuggest
import qualified Ide.Plugin.Cabal.LicenseSuggest as LicenseSuggest
import Ide.Plugin.Cabal.Orphans ()
import qualified Ide.Plugin.Cabal.FieldSuggest as FieldSuggest
import qualified Ide.Plugin.Cabal.Parse as Parse
import Ide.Types
import qualified Language.LSP.Protocol.Lens as JL
Expand Down Expand Up @@ -385,6 +385,7 @@ computeCompletionsAt recorder ide prefInfo fp fields = do
-- The `withStale` option is very important here, since we often call this rule with invalid cabal files.
mGPD <- runAction "cabal-plugin.modulesCompleter.gpd" ide $ useWithStale ParseCabalFile $ toNormalizedFilePath fp
pure $ fmap fst mGPD
, getCabalCommonSections = runAction "cabal-plugin.commonSections" ide $ use ParseCabalCommonSections $ toNormalizedFilePath fp
, cabalPrefixInfo = prefInfo
, stanzaName =
case fst ctx of
Expand Down
130 changes: 80 additions & 50 deletions plugins/hls-cabal-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ module Main (
import Completer (completerTests)
import Context (contextTests)
import Control.Lens ((^.))
import Control.Lens.Fold ((^?))
import Control.Monad (guard)
import qualified Data.ByteString as BS
import Data.Either (isRight)
import Data.List.Extra (nubOrdOn)
import qualified Data.Maybe as Maybe
import qualified Data.Text as T
import qualified Data.Text as Text
import Ide.Plugin.Cabal.LicenseSuggest (licenseErrorSuggestion)
Expand All @@ -30,6 +33,7 @@ main = do
, pluginTests
, completerTests
, contextTests
, codeActionTests
]

-- ------------------------------------------------------------------------
Expand Down Expand Up @@ -137,57 +141,83 @@ pluginTests =
unknownLicenseDiag ^. L.range @?= Range (Position 3 24) (Position 4 0)
unknownLicenseDiag ^. L.severity @?= Just DiagnosticSeverity_Error
]
, testGroup
"Code Actions"
[ runCabalTestCaseSession "BSD-3" "" $ do
doc <- openDoc "licenseCodeAction.cabal" "cabal"
diags <- waitForDiagnosticsFromSource doc "cabal"
reduceDiag <- liftIO $ inspectDiagnostic diags ["Unknown SPDX license identifier: 'BSD3'"]
liftIO $ do
length diags @?= 1
reduceDiag ^. L.range @?= Range (Position 3 24) (Position 4 0)
reduceDiag ^. L.severity @?= Just DiagnosticSeverity_Error
[codeAction] <- getLicenseAction "BSD-3-Clause" <$> getCodeActions doc (Range (Position 3 24) (Position 4 0))
executeCodeAction codeAction
contents <- documentContents doc
liftIO $
contents
@?= Text.unlines
[ "cabal-version: 3.0"
, "name: licenseCodeAction"
, "version: 0.1.0.0"
, "license: BSD-3-Clause"
, ""
, "library"
, " build-depends: base"
, " default-language: Haskell2010"
]
, runCabalTestCaseSession "Apache-2.0" "" $ do
doc <- openDoc "licenseCodeAction2.cabal" "cabal"
diags <- waitForDiagnosticsFromSource doc "cabal"
-- test if it supports typos in license name, here 'apahe'
reduceDiag <- liftIO $ inspectDiagnostic diags ["Unknown SPDX license identifier: 'APAHE'"]
liftIO $ do
length diags @?= 1
reduceDiag ^. L.range @?= Range (Position 3 25) (Position 4 0)
reduceDiag ^. L.severity @?= Just DiagnosticSeverity_Error
[codeAction] <- getLicenseAction "Apache-2.0" <$> getCodeActions doc (Range (Position 3 24) (Position 4 0))
executeCodeAction codeAction
contents <- documentContents doc
liftIO $
contents
@?= Text.unlines
[ "cabal-version: 3.0"
, "name: licenseCodeAction2"
, "version: 0.1.0.0"
, "license: Apache-2.0"
, ""
, "library"
, " build-depends: base"
, " default-language: Haskell2010"
]
]
]
-- ----------------------------------------------------------------------------
-- Code Action Tests
-- ----------------------------------------------------------------------------

codeActionTests :: TestTree
codeActionTests = testGroup "Code Actions"
[ runCabalTestCaseSession "BSD-3" "" $ do
doc <- openDoc "licenseCodeAction.cabal" "cabal"
diags <- waitForDiagnosticsFromSource doc "cabal"
reduceDiag <- liftIO $ inspectDiagnostic diags ["Unknown SPDX license identifier: 'BSD3'"]
liftIO $ do
length diags @?= 1
reduceDiag ^. L.range @?= Range (Position 3 24) (Position 4 0)
reduceDiag ^. L.severity @?= Just DiagnosticSeverity_Error
[codeAction] <- getLicenseAction "BSD-3-Clause" <$> getCodeActions doc (Range (Position 3 24) (Position 4 0))
executeCodeAction codeAction
contents <- documentContents doc
liftIO $
contents
@?= Text.unlines
[ "cabal-version: 3.0"
, "name: licenseCodeAction"
, "version: 0.1.0.0"
, "license: BSD-3-Clause"
, ""
, "library"
, " build-depends: base"
, " default-language: Haskell2010"
]
, runCabalTestCaseSession "Apache-2.0" "" $ do
doc <- openDoc "licenseCodeAction2.cabal" "cabal"
diags <- waitForDiagnosticsFromSource doc "cabal"
-- test if it supports typos in license name, here 'apahe'
reduceDiag <- liftIO $ inspectDiagnostic diags ["Unknown SPDX license identifier: 'APAHE'"]
liftIO $ do
length diags @?= 1
reduceDiag ^. L.range @?= Range (Position 3 25) (Position 4 0)
reduceDiag ^. L.severity @?= Just DiagnosticSeverity_Error
[codeAction] <- getLicenseAction "Apache-2.0" <$> getCodeActions doc (Range (Position 3 24) (Position 4 0))
executeCodeAction codeAction
contents <- documentContents doc
liftIO $
contents
@?= Text.unlines
[ "cabal-version: 3.0"
, "name: licenseCodeAction2"
, "version: 0.1.0.0"
, "license: Apache-2.0"
, ""
, "library"
, " build-depends: base"
, " default-language: Haskell2010"
]
, runCabalGoldenSession "Code Actions - Can fix field names" "code-actions" "FieldSuggestions" $ \doc -> do
_ <- waitForDiagnosticsFrom doc
cas <- Maybe.mapMaybe (^? _R) <$> getAllCodeActions doc
-- Filter out the code actions we want to invoke.
-- We only want to invoke Code Actions with certain titles, and
-- we want to invoke them only once, not once for each cursor request.
-- 'getAllCodeActions' iterates over each cursor position and requests code actions.
let selectedCas = nubOrdOn (^. L.title) $ filter
(\ca -> (ca ^. L.title) `elem`
[ "Replace with license"
, "Replace with build-type"
, "Replace with extra-doc-files"
, "Replace with ghc-options"
, "Replace with location"
, "Replace with default-language"
, "Replace with import"
, "Replace with build-depends"
, "Replace with main-is"
, "Replace with hs-source-dirs"
]) cas
mapM_ executeCodeAction selectedCas
pure ()
]
where
getLicenseAction :: T.Text -> [Command |? CodeAction] -> [CodeAction]
getLicenseAction license codeActions = do
Expand Down
3 changes: 3 additions & 0 deletions plugins/hls-cabal-plugin/test/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ runCabalSession :: FilePath -> Session a -> IO a
runCabalSession subdir =
failIfSessionTimeout . runSessionWithServer def cabalPlugin (testDataDir </> subdir)

runCabalGoldenSession :: TestName -> FilePath -> FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree
runCabalGoldenSession title subdir fp act = goldenWithCabalDoc def cabalPlugin title testDataDir (subdir </> fp) "golden" "cabal" act

testDataDir :: FilePath
testDataDir = "plugins" </> "hls-cabal-plugin" </> "test" </> "testdata"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
cabal-version: 3.0
name: FieldSuggestions
version: 0.1.0
licens: BSD-3-Clause

buil-type: Simple

extra-doc-fils:
ChangeLog

-- Default warnings in HLS
common warnings
-- Common sections are currently not supported. So, ignore!
ghc-option: -Wall
-Wredundant-constraints
-Wunused-packages
-Wno-name-shadowing
-Wno-unticked-promoted-constructors

source-repository head
type: git
loc: fake

library
default-lang: Haskell2010
-- Import isn't supported right now.
impor: warnings
build-dep: base

executable my-exe
mains: Main.hs

test-suite Test
type: exitcode-stdio-1.0
main-is: Test.hs
hs-source-drs:

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
cabal-version: 3.0
name: FieldSuggestions
version: 0.1.0
license: BSD-3-Clause

build-type: Simple

extra-doc-files:
ChangeLog

-- Default warnings in HLS
common warnings
-- Common sections are currently not supported. So, ignore!
ghc-options: -Wall
-Wredundant-constraints
-Wunused-packages
-Wno-name-shadowing
-Wno-unticked-promoted-constructors

source-repository head
type: git
location: fake

library
default-language: Haskell2010
-- Import isn't supported right now.
import: warnings
build-depends: base

executable my-exe
main-is: Main.hs

test-suite Test
type: exitcode-stdio-1.0
main-is: Test.hs
hs-source-dirs:

0 comments on commit 26d3622

Please sign in to comment.