Skip to content

Commit fea0135

Browse files
VenInffendor
andauthored
More tests and better docs for cabal-add (#4455)
* new tests * change codeAction title * more tests and docs --------- Co-authored-by: fendor <fendor@users.noreply.github.com>
1 parent c23909d commit fea0135

File tree

9 files changed

+84
-8
lines changed

9 files changed

+84
-8
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/CabalAdd.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,8 @@ addDependencySuggestCodeAction plId verTxtDocId suggestions haskellFilePath caba
156156
-- | Gives the build targets that are used in the `CabalAdd`.
157157
-- Note the unorthodox usage of `readBuildTargets`:
158158
-- If the relative path to the haskell file is provided,
159-
-- the `readBuildTargets` will return a main build target.
160-
-- This behaviour is acceptable for now, but changing to a way of getting
161-
-- all build targets in a file is advised.
159+
-- the `readBuildTargets` will return build targets, where this
160+
-- module is mentioned (in exposed-modules or other-modules).
162161
getBuildTargets :: GenericPackageDescription -> FilePath -> FilePath -> IO [BuildTarget]
163162
getBuildTargets gpd cabalFilePath haskellFilePath = do
164163
let haskellFileRelativePath = makeRelative (dropFileName cabalFilePath) haskellFilePath
@@ -167,10 +166,10 @@ addDependencySuggestCodeAction plId verTxtDocId suggestions haskellFilePath caba
167166
mkCodeAction :: FilePath -> Maybe String -> (T.Text, T.Text) -> CodeAction
168167
mkCodeAction cabalFilePath target (suggestedDep, suggestedVersion) =
169168
let
170-
versionTitle = if T.null suggestedVersion then T.empty else " version " <> suggestedVersion
169+
versionTitle = if T.null suggestedVersion then T.empty else "-" <> suggestedVersion
171170
targetTitle = case target of
172171
Nothing -> T.empty
173-
Just t -> " target " <> T.pack t
172+
Just t -> " at " <> T.pack t
174173
title = "Add dependency " <> suggestedDep <> versionTitle <> targetTitle
175174
version = if T.null suggestedVersion then Nothing else Just suggestedVersion
176175

plugins/hls-cabal-plugin/test/CabalAdd.hs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,30 @@ cabalAddTests :: TestTree
2727
cabalAddTests =
2828
testGroup
2929
"CabalAdd Tests"
30-
[ runHaskellTestCaseSession "Code Actions - Can add hidden package" ("cabal-add-testdata" </> "cabal-add-exe")
30+
[ runHaskellTestCaseSession "Code Actions - Can add hidden package to an executable" ("cabal-add-testdata" </> "cabal-add-exe")
3131
(generateAddDependencyTestSession "cabal-add-exe.cabal" ("src" </> "Main.hs") "split" [253])
32-
, runHaskellTestCaseSession "Code Actions - Guard against HPack" ("cabal-add-testdata" </> "cabal-add-packageYaml")
33-
(generatePackageYAMLTestSession ("src" </> "Main.hs"))
3432
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a library" ("cabal-add-testdata" </> "cabal-add-lib")
3533
(generateAddDependencyTestSession "cabal-add-lib.cabal" ("src" </> "MyLib.hs") "split" [348])
3634
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a test" ("cabal-add-testdata" </> "cabal-add-tests")
3735
(generateAddDependencyTestSession "cabal-add-tests.cabal" ("test" </> "Main.hs") "split" [478])
3836
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a benchmark" ("cabal-add-testdata" </> "cabal-add-bench")
3937
(generateAddDependencyTestSession "cabal-add-bench.cabal" ("bench" </> "Main.hs") "split" [403])
38+
39+
, runHaskellTestCaseSession "Code Actions - Can add hidden package to an executable, multiple targets" ("cabal-add-testdata" </> "cabal-add-multitarget")
40+
(generateAddDependencyTestSession "cabal-add-multitarget.cabal" ("src" </> "Main.hs") "split" [269])
41+
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a library, multiple targets" ("cabal-add-testdata" </> "cabal-add-multitarget")
42+
(generateAddDependencyTestSession "cabal-add-multitarget.cabal" ("lib" </> "MyLib.hs") "split" [413])
43+
, runHaskellTestCaseSession "Code Actions - Can add hidden package to an internal library, multiple targets" ("cabal-add-testdata" </> "cabal-add-multitarget")
44+
(generateAddDependencyTestSession "cabal-add-multitarget.cabal" ("lib" </> "InternalLib.hs") "split" [413])
45+
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a test, multiple targets" ("cabal-add-testdata" </> "cabal-add-multitarget")
46+
(generateAddDependencyTestSession "cabal-add-multitarget.cabal" ("test" </> "Main.hs") "split" [655])
47+
, runHaskellTestCaseSession "Code Actions - Can add hidden package to a benchmark, multiple targets" ("cabal-add-testdata" </> "cabal-add-multitarget")
48+
(generateAddDependencyTestSession "cabal-add-multitarget.cabal" ("bench" </> "Main.hs") "split" [776])
49+
50+
51+
, runHaskellTestCaseSession "Code Actions - Guard against HPack" ("cabal-add-testdata" </> "cabal-add-packageYaml")
52+
(generatePackageYAMLTestSession ("src" </> "Main.hs"))
53+
4054
, testHiddenPackageSuggestions "Check CabalAdd's parser, no version"
4155
[ "It is a member of the hidden package 'base'"
4256
, "It is a member of the hidden package 'Blammo-wai'"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Main (main) where
2+
3+
import Data.List.Split
4+
5+
main :: IO ()
6+
main = putStrLn "Test suite not yet implemented."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cabal-version: 2.4
2+
name: cabal-add-multitarget
3+
version: 0.1.0.0
4+
build-type: Simple
5+
6+
executable cabal-add-exe
7+
main-is: Main.hs
8+
hs-source-dirs: src
9+
ghc-options: -Wall
10+
build-depends: base
11+
default-language: Haskell2010
12+
13+
library
14+
exposed-modules: MyLib
15+
other-modules: InternalLib
16+
build-depends: base >= 4 && < 5
17+
hs-source-dirs: lib
18+
ghc-options: -Wall
19+
20+
test-suite cabal-add-tests-test
21+
main-is: Main.hs
22+
hs-source-dirs: test
23+
type: exitcode-stdio-1.0
24+
build-depends: base
25+
default-language: Haskell2010
26+
27+
benchmark benchmark
28+
main-is: Main.hs
29+
build-depends: base
30+
hs-source-dirs: bench
31+
type: exitcode-stdio-1.0
32+
ghc-options: -threaded
33+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module InternalLib (internalFunc) where
2+
3+
import Data.List.Split
4+
5+
internalFunc :: IO ()
6+
internalFunc = putStrLn "internalFunc"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module MyLib (someFunc) where
2+
3+
import Data.List.Split
4+
5+
someFunc :: IO ()
6+
someFunc = putStrLn "someFunc"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Main where
2+
3+
import Data.List.Split
4+
5+
main = putStrLn "Hello, Haskell!"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Main (main) where
2+
3+
import Data.List.Split
4+
5+
main :: IO ()
6+
main = putStrLn "Test suite not yet implemented."

plugins/hls-cabal-plugin/test/testdata/cabal-add-testdata/cabal.project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ packages: cabal-add-exe
22
cabal-add-lib
33
cabal-add-tests
44
cabal-add-bench
5+
cabal-add-multitarget
56
cabal-add-packageYaml

0 commit comments

Comments
 (0)