From 66350d3ea6b22bfbc85f3c163ff4c67cd926b5b2 Mon Sep 17 00:00:00 2001 From: Patrick Augusto Date: Fri, 26 Nov 2021 18:50:13 -0300 Subject: [PATCH 1/2] Prepend library to executable deps in cabal init --- .../Client/Init/Interactive/Command.hs | 13 ------------- .../Client/Init/NonInteractive/Command.hs | 9 ++++++--- .../src/Distribution/Client/Init/Utils.hs | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/cabal-install/src/Distribution/Client/Init/Interactive/Command.hs b/cabal-install/src/Distribution/Client/Init/Interactive/Command.hs index 73c23db91d2..10c758cc213 100644 --- a/cabal-install/src/Distribution/Client/Init/Interactive/Command.hs +++ b/cabal-install/src/Distribution/Client/Init/Interactive/Command.hs @@ -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 diff --git a/cabal-install/src/Distribution/Client/Init/NonInteractive/Command.hs b/cabal-install/src/Distribution/Client/Init/NonInteractive/Command.hs index 6247ad9c3d7..7be2cd9306b 100644 --- a/cabal-install/src/Distribution/Client/Init/NonInteractive/Command.hs +++ b/cabal-install/src/Distribution/Client/Init/NonInteractive/Command.hs @@ -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 @@ -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) diff --git a/cabal-install/src/Distribution/Client/Init/Utils.hs b/cabal-install/src/Distribution/Client/Init/Utils.hs index 853eb36011a..f340fdf14b6 100644 --- a/cabal-install/src/Distribution/Client/Init/Utils.hs +++ b/cabal-install/src/Distribution/Client/Init/Utils.hs @@ -18,6 +18,8 @@ module Distribution.Client.Init.Utils , fixupDocFiles , mkStringyDep , getBaseDep +, addLibDepToExe +, addLibDepToTest ) where @@ -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] + } From 3d61cfa5bdb8b8d89c96525b957b82cd74a116f4 Mon Sep 17 00:00:00 2001 From: Patrick Augusto Date: Fri, 26 Nov 2021 19:36:00 -0300 Subject: [PATCH 2/2] Adapting tests for library dependency --- .../Client/Init/NonInteractive.hs | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/cabal-install/tests/UnitTests/Distribution/Client/Init/NonInteractive.hs b/cabal-install/tests/UnitTests/Distribution/Client/Init/NonInteractive.hs index 135e871c08b..9cddbfbb035 100644 --- a/cabal-install/tests/UnitTests/Distribution/Client/Init/NonInteractive.hs +++ b/cabal-install/tests/UnitTests/Distribution/Client/Init/NonInteractive.hs @@ -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 @@ -105,7 +105,7 @@ 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 @@ -113,9 +113,14 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject" _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 @@ -185,7 +190,7 @@ 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 @@ -193,9 +198,14 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject" _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 @@ -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 @@ -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 @@ -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