Skip to content

Commit

Permalink
More tests and better docs for cabal-add (haskell#4455)
Browse files Browse the repository at this point in the history
* new tests

* change codeAction title

* more tests and docs

---------

Co-authored-by: fendor <[email protected]>
  • Loading branch information
VenInf and fendor committed Dec 23, 2024
1 parent 1feb07d commit 3ce3cfb
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 8 deletions.
9 changes: 4 additions & 5 deletions plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/CabalAdd.hs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ addDependencySuggestCodeAction plId verTxtDocId suggestions haskellFilePath caba
-- | Gives the build targets that are used in the `CabalAdd`.
-- Note the unorthodox usage of `readBuildTargets`:
-- If the relative path to the haskell file is provided,
-- the `readBuildTargets` will return a main build target.
-- This behaviour is acceptable for now, but changing to a way of getting
-- all build targets in a file is advised.
-- the `readBuildTargets` will return build targets, where this
-- module is mentioned (in exposed-modules or other-modules).
getBuildTargets :: GenericPackageDescription -> FilePath -> FilePath -> IO [BuildTarget]
getBuildTargets gpd cabalFilePath haskellFilePath = do
let haskellFileRelativePath = makeRelative (dropFileName cabalFilePath) haskellFilePath
Expand All @@ -167,10 +166,10 @@ addDependencySuggestCodeAction plId verTxtDocId suggestions haskellFilePath caba
mkCodeAction :: FilePath -> Maybe String -> (T.Text, T.Text) -> CodeAction
mkCodeAction cabalFilePath target (suggestedDep, suggestedVersion) =
let
versionTitle = if T.null suggestedVersion then T.empty else " version " <> suggestedVersion
versionTitle = if T.null suggestedVersion then T.empty else "-" <> suggestedVersion
targetTitle = case target of
Nothing -> T.empty
Just t -> " target " <> T.pack t
Just t -> " at " <> T.pack t
title = "Add dependency " <> suggestedDep <> versionTitle <> targetTitle
version = if T.null suggestedVersion then Nothing else Just suggestedVersion

Expand Down
20 changes: 17 additions & 3 deletions plugins/hls-cabal-plugin/test/CabalAdd.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,30 @@ cabalAddTests :: TestTree
cabalAddTests =
testGroup
"CabalAdd Tests"
[ runHaskellTestCaseSession "Code Actions - Can add hidden package" ("cabal-add-testdata" </> "cabal-add-exe")
[ runHaskellTestCaseSession "Code Actions - Can add hidden package to an executable" ("cabal-add-testdata" </> "cabal-add-exe")
(generateAddDependencyTestSession "cabal-add-exe.cabal" ("src" </> "Main.hs") "split" [253])
, runHaskellTestCaseSession "Code Actions - Guard against HPack" ("cabal-add-testdata" </> "cabal-add-packageYaml")
(generatePackageYAMLTestSession ("src" </> "Main.hs"))
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a library" ("cabal-add-testdata" </> "cabal-add-lib")
(generateAddDependencyTestSession "cabal-add-lib.cabal" ("src" </> "MyLib.hs") "split" [348])
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a test" ("cabal-add-testdata" </> "cabal-add-tests")
(generateAddDependencyTestSession "cabal-add-tests.cabal" ("test" </> "Main.hs") "split" [478])
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a benchmark" ("cabal-add-testdata" </> "cabal-add-bench")
(generateAddDependencyTestSession "cabal-add-bench.cabal" ("bench" </> "Main.hs") "split" [403])

, runHaskellTestCaseSession "Code Actions - Can add hidden package to an executable, multiple targets" ("cabal-add-testdata" </> "cabal-add-multitarget")
(generateAddDependencyTestSession "cabal-add-multitarget.cabal" ("src" </> "Main.hs") "split" [269])
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a library, multiple targets" ("cabal-add-testdata" </> "cabal-add-multitarget")
(generateAddDependencyTestSession "cabal-add-multitarget.cabal" ("lib" </> "MyLib.hs") "split" [413])
, runHaskellTestCaseSession "Code Actions - Can add hidden package to an internal library, multiple targets" ("cabal-add-testdata" </> "cabal-add-multitarget")
(generateAddDependencyTestSession "cabal-add-multitarget.cabal" ("lib" </> "InternalLib.hs") "split" [413])
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a test, multiple targets" ("cabal-add-testdata" </> "cabal-add-multitarget")
(generateAddDependencyTestSession "cabal-add-multitarget.cabal" ("test" </> "Main.hs") "split" [655])
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a benchmark, multiple targets" ("cabal-add-testdata" </> "cabal-add-multitarget")
(generateAddDependencyTestSession "cabal-add-multitarget.cabal" ("bench" </> "Main.hs") "split" [776])


, runHaskellTestCaseSession "Code Actions - Guard against HPack" ("cabal-add-testdata" </> "cabal-add-packageYaml")
(generatePackageYAMLTestSession ("src" </> "Main.hs"))

, testHiddenPackageSuggestions "Check CabalAdd's parser, no version"
[ "It is a member of the hidden package 'base'"
, "It is a member of the hidden package 'Blammo-wai'"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main (main) where

import Data.List.Split

main :: IO ()
main = putStrLn "Test suite not yet implemented."
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cabal-version: 2.4
name: cabal-add-multitarget
version: 0.1.0.0
build-type: Simple

executable cabal-add-exe
main-is: Main.hs
hs-source-dirs: src
ghc-options: -Wall
build-depends: base
default-language: Haskell2010

library
exposed-modules: MyLib
other-modules: InternalLib
build-depends: base >= 4 && < 5
hs-source-dirs: lib
ghc-options: -Wall

test-suite cabal-add-tests-test
main-is: Main.hs
hs-source-dirs: test
type: exitcode-stdio-1.0
build-depends: base
default-language: Haskell2010

benchmark benchmark
main-is: Main.hs
build-depends: base
hs-source-dirs: bench
type: exitcode-stdio-1.0
ghc-options: -threaded

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module InternalLib (internalFunc) where

import Data.List.Split

internalFunc :: IO ()
internalFunc = putStrLn "internalFunc"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module MyLib (someFunc) where

import Data.List.Split

someFunc :: IO ()
someFunc = putStrLn "someFunc"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Main where

import Data.List.Split

main = putStrLn "Hello, Haskell!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main (main) where

import Data.List.Split

main :: IO ()
main = putStrLn "Test suite not yet implemented."
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ packages: cabal-add-exe
cabal-add-lib
cabal-add-tests
cabal-add-bench
cabal-add-multitarget
cabal-add-packageYaml

0 comments on commit 3ce3cfb

Please sign in to comment.