Skip to content

Commit

Permalink
Add ability for init script to ignore globally defined appdir when bu…
Browse files Browse the repository at this point in the history
…ilding libraries

if executable, ignore library dir entry

move FileCreators.hs suite to Init.hs and let it serve as unit test env for init
  • Loading branch information
emilypi committed Jan 28, 2021
1 parent 0164e3c commit 1770e95
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
27 changes: 10 additions & 17 deletions cabal-install/src/Distribution/Client/Init/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -530,22 +526,19 @@ 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

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])
Expand Down
6 changes: 3 additions & 3 deletions cabal-install/tests/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand Down
Empty file modified cabal-install/tests/test-cabal-install
100644 → 100755
Empty file.
Empty file modified cabal-install/tests/test-cabal-install-user
100644 → 100755
Empty file.

0 comments on commit 1770e95

Please sign in to comment.