Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit efe1660

Browse files
committed
Revert "Merge pull request #1237 from fendor/add-package-tests"
This reverts commit 9fca31c, reversing changes made to a96ca84.
1 parent 9fca31c commit efe1660

File tree

22 files changed

+47
-639
lines changed

22 files changed

+47
-639
lines changed

haskell-ide-engine.cabal

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,10 @@ test-suite unit-test
173173
DiffSpec
174174
ExtensibleStateSpec
175175
GhcModPluginSpec
176+
LiquidSpec
176177
HaRePluginSpec
177178
HooglePluginSpec
178179
JsonSpec
179-
LiquidSpec
180-
PackagePluginSpec
181180
Spec
182181
-- Technically cabal-helper should be a 'run-tool-depends', but that doesn't exist yet
183182
build-tool-depends: cabal-helper:cabal-helper-main, hspec-discover:hspec-discover

src/Haskell/Ide/Engine/Plugin/Package.hs

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ data AddParams = AddParams
6767
{ rootDirParam :: FilePath -- ^ The root directory.
6868
, fileParam :: ModulePath -- ^ A path to a module inside the
6969
-- library/executable/test-suite you want to
70-
-- add the package to. May be a relative or
70+
-- add the package to. May be a realtive oir
7171
-- absolute path, thus, must be normalised.
7272
, packageParam :: Package -- ^ The name of the package to add.
7373
}
@@ -76,7 +76,7 @@ data AddParams = AddParams
7676
-- | FilePath to a cabal package description file.
7777
type CabalFilePath = FilePath
7878
-- | FilePath to a package.yaml package description file.
79-
type HpackFilePath = FilePath
79+
type PackageYamlFilePath = FilePath
8080
-- | FilePath to a module within the project.
8181
-- May be used to establish what component the dependency shall be added to.
8282
type ModulePath = FilePath
@@ -88,14 +88,8 @@ type Package = T.Text
8888
-- Supported are `*.cabal` and `package.yaml` specifications.
8989
-- Moreover, may fail with an IOException in case of a filesystem problem.
9090
addCmd :: CommandFunc AddParams J.WorkspaceEdit
91-
addCmd = CmdSync addCmd'
91+
addCmd = CmdSync $ \(AddParams rootDir modulePath pkg) -> do
9292

93-
-- | Add a package to the project's dependencies.
94-
-- May fail if no project dependency specification can be found.
95-
-- Supported are `*.cabal` and `package.yaml` specifications.
96-
-- Moreover, may fail with an IOException in case of a filesystem problem.
97-
addCmd' :: AddParams -> IdeGhcM (IdeResult J.WorkspaceEdit)
98-
addCmd' (AddParams rootDir modulePath pkg) = do
9993
packageType <- liftIO $ findPackageType rootDir
10094
fileMap <- GM.mkRevRedirMapFunc
10195

@@ -111,10 +105,9 @@ addCmd' (AddParams rootDir modulePath pkg) = do
111105
liftToGhc $ editHpackPackage absFp relModulePath pkg
112106
NoPackage -> return $ IdeResultFail (IdeError PluginError "No package.yaml or .cabal found" Null)
113107

114-
data PackageType = CabalPackage FilePath -- ^ Location of Cabal File. May be relative.
115-
| HpackPackage FilePath -- ^ Location of `package.yaml`. May be relative.
108+
data PackageType = CabalPackage FilePath -- ^ Location of Cabal File.
109+
| HpackPackage FilePath -- ^ Location of `package.yaml`
116110
| NoPackage -- ^ No package format has been found.
117-
deriving (Show, Eq)
118111

119112
-- | Find the package type the project with the given root uses.
120113
-- Might have weird results if there is more than one cabal package specification
@@ -136,13 +129,12 @@ findPackageType rootDir = do
136129
return $ fromMaybe NoPackage $ asum [HpackPackage <$> mHpack, CabalPackage <$> mCabal]
137130

138131
-- | Edit a hpack package to add the given package to the package.yaml.
139-
-- If package.yaml is not in an expected format, will fail fatally.
140132
--
141133
-- Currently does not preserve format.
142134
-- Keep an eye out on this other GSOC project!
143135
-- https://github.com/wisn/format-preserving-yaml
144-
editHpackPackage :: HpackFilePath -- ^ Path to the package.yaml file
145-
-- containing the package description.
136+
editHpackPackage :: PackageYamlFilePath -- ^ Path to the package.yaml file
137+
-- containing the package description.
146138
-> ModulePath -- ^ Path to the module where the command has
147139
-- been issued in.
148140
-- Used to find out what component the
@@ -156,29 +148,19 @@ editHpackPackage fp modulePath pkgName = do
156148

157149
case Y.decodeThrow contents :: Maybe Object of
158150
Just obj -> do
159-
-- Map over all major components, such as "executable", "executables",
160-
-- "tests" and "benchmarks". Note, that "library" is a major component,
161-
-- but its structure is different and can not be mapped over in the same way.
162-
--
163-
-- Only adds the package if the declared "source-dirs" field is part of the
164-
-- module path, or if no "source-dirs" is declared.
165151
let compsMapped = mapComponentTypes (ensureObject $ mapComponents (ensureObject $ mapCompDependencies addDep)) obj
166152

167-
-- Is there a global "dependencies" yaml object?
168-
let addDepToMainDep = fromMaybe False $ do
169-
Array _ <- HM.lookup "dependencies" compsMapped
170-
return True
153+
let addDepToMainLib = fromMaybe True $ do
154+
Object lib <- HM.lookup "library" compsMapped
155+
sourceDirs <- HM.lookup "source-dirs" lib
156+
return $ isInSourceDir sourceDirs
171157

172-
-- Either add the package to only the top-level "dependencies",
173-
-- or to all main components of which the given module is part of.
174-
let newPkg
175-
| addDepToMainDep = mapMainDependencies addDep obj
176-
-- Map over the library component at last, since it has different structure.
177-
| otherwise = mapLibraryDependency addDep compsMapped
158+
let newPkg = if addDepToMainLib
159+
then mapMainDependencies addDep compsMapped
160+
else compsMapped
178161

179-
let newPkgText = T.decodeUtf8 $ Y.encode newPkg
162+
newPkgText = T.decodeUtf8 $ Y.encode newPkg
180163

181-
-- Construct the WorkSpaceEdit
182164
let numOldLines = length $ T.lines $ T.decodeUtf8 contents
183165
range = J.Range (J.Position 0 0) (J.Position numOldLines 0)
184166
textEdit = J.TextEdit range newPkgText
@@ -197,18 +179,10 @@ editHpackPackage fp modulePath pkgName = do
197179

198180
mapMainDependencies :: (Value -> Value) -> Object -> Object
199181
mapMainDependencies f o =
200-
let g :: T.Text -> Value -> Value
201-
g "dependencies" = f
182+
let g "dependencies" = f
202183
g _ = id
203184
in HM.mapWithKey g o
204185

205-
mapLibraryDependency :: (Value -> Value) -> Object -> Object
206-
mapLibraryDependency f o =
207-
let g :: T.Text -> Value -> Value
208-
g "library" (Y.Object o') = Y.Object (mapCompDependencies f o')
209-
g _ x = x
210-
in HM.mapWithKey g o
211-
212186
mapComponentTypes :: (Value -> Value) -> Object -> Object
213187
mapComponentTypes f o =
214188
let g "executables" = f

test/functional/FunctionalCodeActionsSpec.hs

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE OverloadedStrings #-}
2-
{-# LANGUAGE CPP #-}
32

43
module FunctionalCodeActionsSpec where
54

@@ -178,9 +177,7 @@ spec = describe "code actions" $ do
178177
]
179178
]
180179
describe "add package suggestions" $ do
181-
-- Only execute this test with ghc 8.4.4, below seems to be broken in the package.
182-
#if (defined(MIN_VERSION_GLASGOW_HASKELL) && (MIN_VERSION_GLASGOW_HASKELL(8,4,0,0)))
183-
it "adds to .cabal files" $ runSession hieCommand fullCaps "test/testdata/addPackageTest/cabal-exe" $ do
180+
it "adds to .cabal files" $ runSession hieCommand fullCaps "test/testdata/addPackageTest/cabal" $ do
184181
doc <- openDoc "AddPackage.hs" "haskell"
185182

186183
-- ignore the first empty hlint diagnostic publish
@@ -204,9 +201,9 @@ spec = describe "code actions" $ do
204201

205202
contents <- getDocumentEdit . TextDocumentIdentifier =<< getDocUri "add-package-test.cabal"
206203
liftIO $ T.lines contents `shouldSatisfy` \x -> any (\l -> "text -any" `T.isSuffixOf` (x !! l)) [15, 16]
207-
#endif
204+
208205
it "adds to hpack package.yaml files" $
209-
runSession hieCommand fullCaps "test/testdata/addPackageTest/hpack-exe" $ do
206+
runSession hieCommand fullCaps "test/testdata/addPackageTest/hpack" $ do
210207
doc <- openDoc "app/Asdf.hs" "haskell"
211208

212209
-- ignore the first empty hlint diagnostic publish
@@ -230,35 +227,9 @@ spec = describe "code actions" $ do
230227

231228
contents <- getDocumentEdit . TextDocumentIdentifier =<< getDocUri "package.yaml"
232229
liftIO $ do
233-
T.lines contents !! 3 `shouldSatisfy` T.isSuffixOf "zlib"
234-
T.lines contents !! 21 `shouldNotSatisfy` T.isSuffixOf "zlib"
235-
236-
it "adds to hpack package.yaml files if both are present" $
237-
runSession hieCommand fullCaps "test/testdata/addPackageTest/hybrid-exe" $ do
238-
doc <- openDoc "app/Asdf.hs" "haskell"
239-
240-
-- ignore the first empty hlint diagnostic publish
241-
[_,diag:_] <- count 2 waitForDiagnostics
242-
243-
let preds = [ T.isPrefixOf "Could not load module ‘Codec.Compression.GZip’"
244-
, T.isPrefixOf "Could not find module ‘Codec.Compression.GZip’"
245-
]
246-
in liftIO $ diag ^. L.message `shouldSatisfy` \x -> any (\f -> f x) preds
247-
248-
mActions <- getAllCodeActions doc
249-
let allActions = map fromAction mActions
250-
action = head allActions
251-
252-
liftIO $ do
253-
action ^. L.title `shouldBe` "Add zlib as a dependency"
254-
forM_ allActions $ \a -> a ^. L.kind `shouldBe` Just CodeActionQuickFix
255-
forM_ allActions $ \a -> a ^. L.command . _Just . L.command `shouldSatisfy` T.isSuffixOf "package:add"
256-
257-
executeCodeAction action
258-
259-
contents <- getDocumentEdit . TextDocumentIdentifier =<< getDocUri "package.yaml"
260-
liftIO $
261-
T.lines contents !! 21 `shouldSatisfy` T.isSuffixOf "zlib"
230+
T.lines contents !! 33 `shouldSatisfy` T.isSuffixOf "zlib"
231+
T.lines contents !! 12 `shouldNotSatisfy` T.isSuffixOf "zlib"
232+
T.lines contents !! 13 `shouldNotSatisfy` T.isSuffixOf "zlib"
262233

263234
-- -----------------------------------
264235

@@ -345,11 +316,10 @@ spec = describe "code actions" $ do
345316

346317
contents <- documentContents doc
347318

348-
liftIO $ contents `shouldBe` T.concat
349-
[ "module TypedHoles where\n"
350-
, "foo :: [Int] -> Int\n"
351-
, "foo x = " <> suggestion
352-
]
319+
liftIO $ contents `shouldBe`
320+
"module TypedHoles where\n\
321+
\foo :: [Int] -> Int\n\
322+
\foo x = " <> suggestion
353323

354324
it "shows more suggestions" $
355325
runSession hieCommand fullCaps "test/testdata" $ do

test/testdata/addPackageTest/cabal-exe/add-package-test.cabal

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/testdata/addPackageTest/cabal-lib/AddPackage.hs

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/testdata/addPackageTest/hpack-lib/app/Asdf.hs

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/testdata/addPackageTest/hpack-lib/package.yaml

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/testdata/addPackageTest/hpack-exe/package.yaml renamed to test/testdata/addPackageTest/hpack/package.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ description: Please see the README on GitHub at <https://github.com/gith
2222
dependencies:
2323
- base >= 4.7 && < 5
2424

25+
library:
26+
source-dirs: src
27+
2528
executables:
2629
asdf-exe:
2730
main: Main.hs
@@ -31,4 +34,15 @@ executables:
3134
- -rtsopts
3235
- -with-rtsopts=-N
3336
dependencies:
34-
- asdf
37+
- asdf
38+
39+
tests:
40+
asdf-test:
41+
main: Spec.hs
42+
source-dirs: test
43+
ghc-options:
44+
- -threaded
45+
- -rtsopts
46+
- -with-rtsopts=-N
47+
dependencies:
48+
- asdf

test/testdata/addPackageTest/hybrid-exe/AddPackage.hs

Lines changed: 0 additions & 2 deletions
This file was deleted.

test/testdata/addPackageTest/hybrid-exe/app/Asdf.hs

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/testdata/addPackageTest/hybrid-exe/asdf.cabal

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)