Skip to content

Commit

Permalink
Merge pull request #7839 from ptkato/cabal-init-libexe-dep
Browse files Browse the repository at this point in the history
Prepend library to executable deps in cabal init
  • Loading branch information
mergify[bot] authored Nov 27, 2021
2 parents cff9b1a + 3d61cfa commit 68c2c75
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
13 changes: 0 additions & 13 deletions cabal-install/src/Distribution/Client/Init/Interactive/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,6 @@ createProject v pkgIx srcDb initFlags = do
return $ ProjectSettings
(mkOpts comments cabalSpec) pkgDesc
Nothing Nothing testTarget
where
-- Add package name as dependency of test suite
--
addLibDepToTest _ Nothing = Nothing
addLibDepToTest n (Just t) = Just $ t
{ _testDependencies = _testDependencies t ++ [mkPackageNameDep n]
}

-- Add package name as dependency of executable
--
addLibDepToExe n exe = exe
{ _exeDependencies = _exeDependencies exe ++ [mkPackageNameDep n]
}

-- -------------------------------------------------------------------- --
-- Target and pkg description generation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ createProject comp v pkgIx srcDb initFlags = do
case pkgType of
Library -> do
libTarget <- genLibTarget initFlags comp pkgIx cabalSpec
testTarget <- genTestTarget initFlags comp pkgIx cabalSpec
testTarget <- addLibDepToTest pkgName <$>
genTestTarget initFlags comp pkgIx cabalSpec

return $ ProjectSettings
(mkOpts comments cabalSpec) pkgDesc
Expand All @@ -130,8 +131,10 @@ createProject comp v pkgIx srcDb initFlags = do

LibraryAndExecutable -> do
libTarget <- genLibTarget initFlags comp pkgIx cabalSpec
exeTarget <- genExeTarget initFlags comp pkgIx cabalSpec
testTarget <- genTestTarget initFlags comp pkgIx cabalSpec
exeTarget <- addLibDepToExe pkgName <$>
genExeTarget initFlags comp pkgIx cabalSpec
testTarget <- addLibDepToTest pkgName <$>
genTestTarget initFlags comp pkgIx cabalSpec

return $ ProjectSettings
(mkOpts comments cabalSpec) pkgDesc (Just libTarget)
Expand Down
17 changes: 17 additions & 0 deletions cabal-install/src/Distribution/Client/Init/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Distribution.Client.Init.Utils
, fixupDocFiles
, mkStringyDep
, getBaseDep
, addLibDepToExe
, addLibDepToTest
) where


Expand Down Expand Up @@ -310,3 +312,18 @@ mkStringyDep = mkPackageNameDep . mkPackageName
getBaseDep :: Interactive m => InstalledPackageIndex -> InitFlags -> m [Dependency]
getBaseDep pkgIx flags = retrieveDependencies silent flags
[(fromString "Prelude", fromString "Prelude")] pkgIx

-- Add package name as dependency of test suite
--
addLibDepToTest :: PackageName -> Maybe TestTarget -> Maybe TestTarget
addLibDepToTest _ Nothing = Nothing
addLibDepToTest n (Just t) = Just $ t
{ _testDependencies = _testDependencies t ++ [mkPackageNameDep n]
}

-- Add package name as dependency of executable
--
addLibDepToExe :: PackageName -> ExeTarget -> ExeTarget
addLibDepToExe n exe = exe
{ _exeDependencies = _exeDependencies exe ++ [mkPackageNameDep n]
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Distribution.ModuleName (fromString)
import Distribution.Simple.Flag
import Data.List (foldl')
import qualified Data.Set as Set
import Distribution.Client.Init.Utils (mkStringyDep)
import Distribution.Client.Init.Utils (mkPackageNameDep, mkStringyDep)

tests
:: Verbosity
Expand Down Expand Up @@ -105,17 +105,22 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject"
_exeLanguage exe @?= Haskell98
_exeOtherModules exe @?= []
_exeOtherExts exe @?= []
_exeDependencies exe @?= []
_exeDependencies exe @?! []
_exeBuildTools exe @?= []

_testMainIs test @?= HsFilePath "quxTest/Main.hs" Standard
_testDirs test @?= ["quxTest"]
_testLanguage test @?= Haskell98
_testOtherModules test @?= []
_testOtherExts test @?= []
_testDependencies test @?= []
_testDependencies test @?! []
_testBuildTools test @?= []

assertBool "The library should be a dependency of the executable" $
mkPackageNameDep (_optPkgName opts) `elem` _exeDependencies exe
assertBool "The library should be a dependency of the test executable" $
mkPackageNameDep (_optPkgName opts) `elem` _testDependencies test

Right (ProjectSettings _ _ lib exe test, _) -> do
lib @?! Nothing
exe @?! Nothing
Expand Down Expand Up @@ -185,17 +190,22 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject"
_exeLanguage exe @?= Haskell98
_exeOtherModules exe @?= []
_exeOtherExts exe @?= []
_exeDependencies exe @?= []
_exeDependencies exe @?! []
_exeBuildTools exe @?= []

_testMainIs test @?= HsFilePath "quxTest/Main.hs" Standard
_testDirs test @?= ["quxTest"]
_testLanguage test @?= Haskell98
_testOtherModules test @?= []
_testOtherExts test @?= []
_testDependencies test @?= []
_testDependencies test @?! []
_testBuildTools test @?= []

assertBool "The library should be a dependency of the executable" $
mkPackageNameDep (_optPkgName opts) `elem` _exeDependencies exe
assertBool "The library should be a dependency of the test executable" $
mkPackageNameDep (_optPkgName opts) `elem` _testDependencies test

Right (ProjectSettings _ _ lib exe test, _) -> do
lib @?! Nothing
exe @?! Nothing
Expand Down Expand Up @@ -369,6 +379,11 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject"
_testDependencies test @?! []
_testBuildTools test @?= [mkStringyDep "happy:happy"]

assertBool "The library should be a dependency of the executable" $
mkPackageNameDep (_optPkgName opts) `elem` _exeDependencies exe
assertBool "The library should be a dependency of the test executable" $
mkPackageNameDep (_optPkgName opts) `elem` _testDependencies test

Right (ProjectSettings _ _ lib exe test, _) -> do
lib @?! Nothing
exe @?! Nothing
Expand Down Expand Up @@ -499,6 +514,9 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject"
_testOtherExts test @?= map EnableExtension [OverloadedStrings, LambdaCase, RankNTypes, RecordWildCards]
_testDependencies test @?! []
_testBuildTools test @?= [mkStringyDep "happy:happy"]

assertBool "The library should be a dependency of the test executable" $
mkPackageNameDep (_optPkgName opts) `elem` _testDependencies test

Right (ProjectSettings _ _ lib exe test, _) -> do
lib @?! Nothing
Expand Down Expand Up @@ -640,6 +658,9 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject"
_exeDependencies exe @?! []
_exeBuildTools exe @?= [mkStringyDep "happy:happy"]

assertBool "The library should be a dependency of the executable" $
mkPackageNameDep (_optPkgName opts) `elem` _exeDependencies exe

Right (ProjectSettings _ _ lib exe test, _) -> do
lib @?! Nothing
exe @?! Nothing
Expand Down

0 comments on commit 68c2c75

Please sign in to comment.