Skip to content

Commit

Permalink
* Add ability for init script to ignore globally defined appdir when …
Browse files Browse the repository at this point in the history
…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
* Add TESTING.md, describing how to create unit test env for cabal-install.
  • Loading branch information
emilypi committed Jan 28, 2021
1 parent 0164e3c commit 89a5c41
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 28 deletions.
24 changes: 24 additions & 0 deletions cabal-install/TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Testing `cabal-install`

Local testing
=======

In order to effectively test the `cabal-install` library, the `cabal-install.cabal` file must be modified
to build the targets in the `/test` directory. The current recommended way to set this up is to
use the [makefile](../Makefile) supplied in `Cabal` project parent directory, issuing the following command:


```
> make cabal-install-dev
```

This command will copy the dev `.cabal` generated by a project build into the `cabal-install.cabal`, and set your git index to ignore
any changes to that file. Any subsequent changes to the `.cabal` should unset and reset the git index to make sure you don't end up committing it.
From there, tests may be built with `cabal test` as usual. To choose a particular test so you don't end up running the whole thing, you can issue
`tasty`-style pattern expressions like the following:

```
> cabal run tests -- -p /my pattern/
```

Please remember to test your changes! Happy hacking.
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
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,17 @@ 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 = baseFlags { packageType = Flag pkgType }


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

Expand Down Expand Up @@ -96,6 +115,17 @@ baseFlags = defaultInitFlags {
}


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

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

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

Expand All @@ -104,6 +134,7 @@ 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"]
}

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

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


Expand Down

0 comments on commit 89a5c41

Please sign in to comment.