Skip to content

Commit

Permalink
Fix dir creation on init, add unit tests + rename test file
Browse files Browse the repository at this point in the history
  • Loading branch information
emilypi committed Feb 1, 2021
1 parent a05a72e commit a940fc4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cabal-install/cabal-install.cabal.dev
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ Test-Suite unit-tests
UnitTests.Distribution.Client.Get
UnitTests.Distribution.Client.Glob
UnitTests.Distribution.Client.GZipUtils
UnitTests.Distribution.Client.Init.FileCreators
UnitTests.Distribution.Client.Init
UnitTests.Distribution.Client.Store
UnitTests.Distribution.Client.Tar
UnitTests.Distribution.Client.TreeDiffInstances
Expand Down
4 changes: 2 additions & 2 deletions cabal-install/cabal-install.cabal.zinza
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Cabal-Version: 2.0
{% else %}
Cabal-Version: >= 1.10
{% endif %}
-- NOTE: This file is autogenerated from 'cabal-install.cabal.pp'.
-- NOTE: This file is autogenerated from 'cabal-install.cabal.'.
-- DO NOT EDIT MANUALLY.
-- To update this file, edit 'cabal-install.cabal.pp' and run
-- 'make cabal-install-prod' in the project's root folder.
Expand Down Expand Up @@ -420,7 +420,7 @@ Test-Suite unit-tests
UnitTests.Distribution.Client.Get
UnitTests.Distribution.Client.Glob
UnitTests.Distribution.Client.GZipUtils
UnitTests.Distribution.Client.Init.FileCreators
UnitTests.Distribution.Client.Init
UnitTests.Distribution.Client.Store
UnitTests.Distribution.Client.Tar
UnitTests.Distribution.Client.TreeDiffInstances
Expand Down
51 changes: 31 additions & 20 deletions cabal-install/src/Distribution/Client/Init/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,30 @@
--
-----------------------------------------------------------------------------

module Distribution.Client.Init.Command (

-- * Commands
module Distribution.Client.Init.Command
( -- * Commands
initCabal
, incVersion

-- * Helpers
, getSimpleProject
, getLibOrExec
, getCabalVersion
, getPackageName
, getVersion
, getLicense
, getAuthorInfo
, getHomepage
, getSynopsis
, getCategory
, getExtraSourceFiles
, getAppDir
, getSrcDir
, getGenTests
, getTestDir
, getLanguage
, getGenComments
, getModulesBuildToolsAndDeps
) where

import Prelude ()
Expand Down Expand Up @@ -475,21 +493,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 +544,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
2 changes: 1 addition & 1 deletion cabal-install/src/Distribution/Client/Init/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ data InitFlags =
, initVerbosity :: Flag Verbosity
, overwrite :: Flag Bool
}
deriving (Show, Generic)
deriving (Eq, Show, Generic)

-- the Monoid instance for Flag has later values override earlier
-- ones, which is why we want Maybe [foo] for collecting foo values,
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
@@ -1,18 +1,21 @@
module UnitTests.Distribution.Client.Init.FileCreators (
tests
module UnitTests.Distribution.Client.Init
( tests
) where

import Distribution.Client.Init.FileCreators
( generateCabalFile )

import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.Golden (goldenVsString)

import System.FilePath
( (</>) )
import qualified Data.ByteString.Lazy as BS
import qualified Data.ByteString.Lazy.Char8 as BS8

import Distribution.Client.Init.Command
( getLibOrExec, getAppDir, getSrcDir )
import Distribution.Client.Init.Types
( InitFlags(..), PackageType(..), defaultInitFlags )
import Distribution.Simple.Setup
Expand Down Expand Up @@ -40,6 +43,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 +60,22 @@ checkCabalFileGolden flags goldenFileName =
generatedCabalFile :: IO BS.ByteString
generatedCabalFile = pure . BS8.pack $ generateCabalFile goldenFileName flags

checkInitFlags :: String -> InitFlags -> PackageType -> TestTree
checkInitFlags label flags pkgType = testCase label $ do
flags' <- getLibOrExec rawFlags
>>= getAppDir
>>= getSrcDir

flags @=? flags'
where
rawFlags
| pkgType == Executable = baseFlags
{ packageType = Flag pkgType
, exposedModules = Nothing
}
| otherwise = baseFlags { packageType = Flag pkgType }


-- ==================================================
-- Base flags to set common InitFlags values.

Expand Down Expand Up @@ -90,12 +114,23 @@ baseFlags = defaultInitFlags {
, mainIs = Flag "Main.hs"
, applicationDirs = Just ["app"]
, sourceDirs = Nothing
, exposedModules = Nothing
, exposedModules = Just [ModuleName.fromString "MyLib"]
, initializeTestSuite = Flag False
, testDirs = Nothing
}


-- ==================================================
-- Simple library flags

libFlags :: InitFlags
libFlags = baseFlags
{ packageType = Flag Library
, mainIs = NoFlag
, sourceDirs = Just ["src"]
, applicationDirs = Just []
}

-- ==================================================
-- Simple exe.

Expand All @@ -104,7 +139,9 @@ exeFlags = baseFlags {
-- Create an executable only, with main living in app/Main.hs.
packageType = Flag Executable
, mainIs = Flag "Main.hs"
, sourceDirs = Just []
, applicationDirs = Just ["app"]
, exposedModules = Nothing
}


Expand All @@ -127,7 +164,6 @@ libAndExeFlags = baseFlags {

-- Library sources live in src/ and expose the module MyLib.
, sourceDirs = Just ["src"]
, exposedModules = Just (map ModuleName.fromString ["MyLib"])
}


Expand Down

0 comments on commit a940fc4

Please sign in to comment.