From 1770e95acec8965921b30dee2a5ba3032ab8776a Mon Sep 17 00:00:00 2001 From: Emily Pillmore Date: Wed, 27 Jan 2021 21:58:57 -0500 Subject: [PATCH] Add ability for init script to ignore globally defined appdir when building libraries if executable, ignore library dir entry move FileCreators.hs suite to Init.hs and let it serve as unit test env for init --- .../src/Distribution/Client/Init/Command.hs | 27 +++++++------------ cabal-install/tests/UnitTests.hs | 6 ++--- .../Client/{Init/FileCreators.hs => Init.hs} | 14 ++++++++++ cabal-install/tests/test-cabal-install | 0 cabal-install/tests/test-cabal-install-user | 0 5 files changed, 27 insertions(+), 20 deletions(-) rename cabal-install/tests/UnitTests/Distribution/Client/{Init/FileCreators.hs => Init.hs} (90%) mode change 100644 => 100755 cabal-install/tests/test-cabal-install mode change 100644 => 100755 cabal-install/tests/test-cabal-install-user diff --git a/cabal-install/src/Distribution/Client/Init/Command.hs b/cabal-install/src/Distribution/Client/Init/Command.hs index 088a32b614a..da4ed5e3809 100644 --- a/cabal-install/src/Distribution/Client/Init/Command.hs +++ b/cabal-install/src/Distribution/Client/Init/Command.hs @@ -475,21 +475,17 @@ getGenComments flags = do -- | Ask for the application root directory. getAppDir :: InitFlags -> IO InitFlags getAppDir flags = do - appDirs <- - return (applicationDirs flags) - ?>> noAppDirIfLibraryOnly + appDirs <- noAppDirIfLibraryOnly ?>> guessAppDir flags ?>> promptUserForApplicationDir ?>> setDefault return $ flags { applicationDirs = appDirs } - where - -- If the packageType==Library, then there is no application dir. + -- If the packageType==Library, ignore defined appdir. noAppDirIfLibraryOnly :: IO (Maybe [String]) - noAppDirIfLibraryOnly = - if (packageType flags) == Flag Library - then return (Just []) - else return Nothing + noAppDirIfLibraryOnly + | packageType flags == Flag Library = return $ Just [] + | otherwise = return $ applicationDirs flags -- Set the default application directory. setDefault :: IO (Maybe [String]) @@ -530,9 +526,7 @@ guessAppDir flags = do -- | Ask for the source (library) root directory. getSrcDir :: InitFlags -> IO InitFlags getSrcDir flags = do - srcDirs <- - return (sourceDirs flags) - ?>> noSourceDirIfExecutableOnly + srcDirs <- noSourceDirIfExecutableOnly ?>> guessSourceDir flags ?>> promptUserForSourceDir ?>> setDefault @@ -540,12 +534,11 @@ getSrcDir flags = do return $ flags { sourceDirs = srcDirs } where - -- If the packageType==Executable, then there is no source dir. + -- If the packageType==Executable, then ignore source dir noSourceDirIfExecutableOnly :: IO (Maybe [String]) - noSourceDirIfExecutableOnly = - if (packageType flags) == Flag Executable - then return (Just []) - else return Nothing + noSourceDirIfExecutableOnly + | packageType flags == Flag Executable = return $ Just [] + | otherwise = return $ sourceDirs flags -- Set the default source directory. setDefault :: IO (Maybe [String]) diff --git a/cabal-install/tests/UnitTests.hs b/cabal-install/tests/UnitTests.hs index 07f3f7d8e9d..d133190e33b 100644 --- a/cabal-install/tests/UnitTests.hs +++ b/cabal-install/tests/UnitTests.hs @@ -19,7 +19,7 @@ import qualified UnitTests.Distribution.Client.Described import qualified UnitTests.Distribution.Client.FileMonitor import qualified UnitTests.Distribution.Client.Glob import qualified UnitTests.Distribution.Client.GZipUtils -import qualified UnitTests.Distribution.Client.Init.FileCreators +import qualified UnitTests.Distribution.Client.Init import qualified UnitTests.Distribution.Client.Store import qualified UnitTests.Distribution.Client.Tar import qualified UnitTests.Distribution.Client.Targets @@ -57,8 +57,8 @@ tests mtimeChangeCalibrated = UnitTests.Distribution.Client.Glob.tests , testGroup "Distribution.Client.GZipUtils" UnitTests.Distribution.Client.GZipUtils.tests - , testGroup "Distribution.Client.Init.FileCreators" - UnitTests.Distribution.Client.Init.FileCreators.tests + , testGroup "Distribution.Client.Init" + UnitTests.Distribution.Client.Init.tests , testGroup "Distribution.Client.Store" UnitTests.Distribution.Client.Store.tests , testGroup "Distribution.Client.Tar" diff --git a/cabal-install/tests/UnitTests/Distribution/Client/Init/FileCreators.hs b/cabal-install/tests/UnitTests/Distribution/Client/Init.hs similarity index 90% rename from cabal-install/tests/UnitTests/Distribution/Client/Init/FileCreators.hs rename to cabal-install/tests/UnitTests/Distribution/Client/Init.hs index 1ef746f8b3d..5904b1335d6 100644 --- a/cabal-install/tests/UnitTests/Distribution/Client/Init/FileCreators.hs +++ b/cabal-install/tests/UnitTests/Distribution/Client/Init.hs @@ -40,6 +40,11 @@ tests = [ testGroup "cabal init goldens" , checkCabalFileGolden libExeAndTestFlags "lib-exe-and-test-golden.cabal" , checkCabalFileGolden libExeAndTestWithCommentsFlags "lib-exe-and-test-with-comments-golden.cabal" ] + , testGroup "Check init flag outputs against init script builds" + [ checkInitFlags "Check library-only build flags" libFlags Library + , checkInitFlags "Check lib+exe build flags" libAndExeFlags LibraryAndExecutable + , checkInitFlags "Check exe-only build flags" exeFlags Executable + ] ] checkCabalFileGolden :: InitFlags -> FilePath -> TestTree @@ -52,6 +57,15 @@ checkCabalFileGolden flags goldenFileName = generatedCabalFile :: IO BS.ByteString generatedCabalFile = pure . BS8.pack $ generateCabalFile goldenFileName flags +checkInitFlagsFor :: String -> InitFlags -> PackageType -> IO () +checkInitFlagsFor label flags pkgtype = do + flags' <- getLibOrExec pkgType + >>= getAppDir + >>= getSrcDir + + assertBool label $ flags == flags' + + -- ================================================== -- Base flags to set common InitFlags values. diff --git a/cabal-install/tests/test-cabal-install b/cabal-install/tests/test-cabal-install old mode 100644 new mode 100755 diff --git a/cabal-install/tests/test-cabal-install-user b/cabal-install/tests/test-cabal-install-user old mode 100644 new mode 100755