Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse .cabal files; show error and warning diagnostics #2954

Merged
merged 26 commits into from
Nov 21, 2022
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
be0150d
Extract cabal plugin in its own package
fendor Jul 10, 2022
c7b1a97
hls-cabal-plugin: Add plugin
runeksvendsen Jun 11, 2022
34dbd33
Add CI workflow
fendor Jun 23, 2022
62a6fad
Add test-suite for hls-cabal-plugin
fendor Jun 26, 2022
309260a
Fix various cabal issues
fendor Jul 10, 2022
28052ee
Update Codeowners file for hls-cabal-plugin
fendor Jul 10, 2022
f927930
Document Bytestring is UTF-8 encoded
fendor Jul 26, 2022
f7e5d64
Remove code duplication
fendor Aug 14, 2022
3638280
Add cabal files of interest and kick function
fendor Oct 10, 2022
fb941e3
Add more documentation
fendor Oct 30, 2022
46f6657
Mark flaky test-case as flaky with issue ref
fendor Nov 13, 2022
033caa9
Make fendor the only CODEOWNER for hls-cabal-plugin
fendor Nov 13, 2022
7a0f066
Add missing extra-source-files for hls-cabal-plugin
fendor Nov 13, 2022
fcd223d
Add proper CHANGELOG entry for the first version of hls-cabal-plugin
fendor Nov 13, 2022
1932674
Add support for Cabal 3.8
fendor Nov 19, 2022
525e2da
Set diagnostics source to cabal
fendor Nov 19, 2022
76137d4
Remove unused function
fendor Nov 19, 2022
66c6089
Add unit tests for code action utilities
fendor Nov 19, 2022
65780af
Remove overly specific logging of diagnostics from hls-cabal-plugin
fendor Nov 19, 2022
f33531c
Improve logging for Cabal FOIs
fendor Nov 19, 2022
c27b063
Add Range manipulation functions
fendor Nov 19, 2022
edd227f
Use Range manipulation functions from hls-plugin-api
fendor Nov 19, 2022
3938b8f
Add more documentation for crucial shake restart function
fendor Nov 19, 2022
99c7081
Add hls-cabal-plugin features to features.md
fendor Nov 19, 2022
701915c
Re-use existing GetFileContents rule
fendor Nov 19, 2022
ac3f998
Merge branch 'master' into rune/hls-cabal-plugin
mergify[bot] Nov 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add unit tests for code action utilities
fendor committed Nov 19, 2022
commit 66c6089cca3e3278728dd60c9146a4ff78af005d
1 change: 1 addition & 0 deletions plugins/hls-cabal-plugin/hls-cabal-plugin.cabal
Original file line number Diff line number Diff line change
@@ -70,6 +70,7 @@ test-suite tests
main-is: Main.hs
build-depends:
, base
, bytestring
, filepath
, ghcide
, hls-cabal-plugin
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE OverloadedStrings #-}
module Ide.Plugin.Cabal.Diagnostics
( errorDiagnostic
, warningDiagnostic
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ licenseErrorAction
-- ^ Output of 'Ide.Plugin.Cabal.Diag.errorDiagnostic'
-> Maybe CodeAction
licenseErrorAction uri diag =
mkCodeAction <$> licenseErrorSuggestion diag
mkCodeAction <$> licenseErrorSuggestion (_message diag)
where
mkCodeAction (original, suggestion) =
let
@@ -52,17 +52,17 @@ licenseErrorAction uri diag =
edit = WorkspaceEdit (Just $ Map.singleton uri $ List tedit) Nothing Nothing
in CodeAction title (Just CodeActionQuickFix) (Just $ List []) Nothing Nothing (Just edit) Nothing Nothing

-- | Given a diagnostic returned by 'Ide.Plugin.Cabal.Diag.errorDiagnostic',
-- | Given an error message returned by 'Ide.Plugin.Cabal.Diag.errorDiagnostic',
-- if it represents an "Unknown SPDX license identifier"-error along
-- with a suggestion then return the suggestion (after the "Do you mean"-text)
-- along with the incorrect identifier.
licenseErrorSuggestion
fendor marked this conversation as resolved.
Show resolved Hide resolved
:: Diagnostic
:: T.Text
-- ^ Output of 'Ide.Plugin.Cabal.Diag.errorDiagnostic'
-> Maybe (T.Text, T.Text)
-- ^ (Original (incorrect) license identifier, suggested replacement)
licenseErrorSuggestion diag =
mSuggestion (_message diag) >>= \case
licenseErrorSuggestion message =
mSuggestion message >>= \case
[original, suggestion] -> Just (original, suggestion)
_ -> Nothing
where
34 changes: 28 additions & 6 deletions plugins/hls-cabal-plugin/test/Main.hs
Original file line number Diff line number Diff line change
@@ -5,17 +5,19 @@ module Main
( main
) where

import Control.Lens ((^.))
import Data.Either (isRight)
import Control.Lens ((^.))
import qualified Data.ByteString as BS
import Data.Either (isRight)
import Data.Function
import qualified Data.Text as Text
import qualified Data.Text as Text
import Development.IDE.Types.Logger
import Ide.Plugin.Cabal
import qualified Ide.Plugin.Cabal.Parse as Lib
import qualified Language.LSP.Types.Lens as J
import Ide.Plugin.Cabal.LicenseSuggest (licenseErrorSuggestion)
import qualified Ide.Plugin.Cabal.Parse as Lib
import qualified Language.LSP.Types.Lens as J
import System.FilePath
import Test.Hls
import qualified Data.ByteString as BS


cabalPlugin :: Recorder (WithPriority Log) -> PluginDescriptor IdeState
cabalPlugin recorder = descriptor recorder "cabal"
@@ -51,13 +53,33 @@ initialiseRecorder False = do
unitTests :: TestTree
unitTests =
testGroup "Unit Tests"
[ cabalParserUnitTests,
codeActionUnitTests
]

cabalParserUnitTests :: TestTree
cabalParserUnitTests = testGroup "Parsing Cabal"
[ testCase "Simple Parsing works" $ do
(warnings, pm) <- Lib.parseCabalFileContents =<< BS.readFile (testDataDir </> "simple.cabal")
liftIO $ do
null warnings @? "Found unexpected warnings"
isRight pm @? "Failed to parse GenericPackageDescription"
]

codeActionUnitTests :: TestTree
codeActionUnitTests = testGroup "Code Action Tests"
[ testCase "Unknown format" $ do
-- the message has the wrong format
licenseErrorSuggestion "Unknown license identifier: 'BSD3' Do you mean BSD-3-Clause?" @?= Nothing,

testCase "BSD-3-Clause" $ do
licenseErrorSuggestion "Unknown SPDX license identifier: 'BSD3' Do you mean BSD-3-Clause?" @?= Just ("BSD3", "BSD-3-Clause"),

testCase "MIT" $ do
-- contains no suggestion
licenseErrorSuggestion "Unknown SPDX license identifier: 'MIT3'" @?= Nothing
]

-- ------------------------------------------------------------------------
-- Integration Tests
-- ------------------------------------------------------------------------