Skip to content

Commit 3a6d592

Browse files
committed
* 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 * Add TESTING.md, describing how to create unit test env for cabal-install. * Add entry to CONTRIBUTING.md regarding `cabal-install` testing.
1 parent 0164e3c commit 3a6d592

File tree

7 files changed

+101
-28
lines changed

7 files changed

+101
-28
lines changed

CONTRIBUTING.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ There are also other test suites:
147147
For these test executables, `-p` which applies a regex filter to the test
148148
names.
149149

150+
**Testing `cabal-install` Locally**
151+
152+
If you are testing `cabal-install` locally, you may refer to its [TESTING.md](cabal-install/TESTING.md) for
153+
instructions on how to use the `Makefile` to produce the appropriate `.cabal` file
154+
with test targets. From there, you may add tests in the usual way.
155+
156+
150157
Conventions
151158
-----------
152159

cabal-install/TESTING.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Testing `cabal-install`
2+
3+
Local testing
4+
=======
5+
6+
In order to effectively test the `cabal-install` library, the `cabal-install.cabal` file must be modified
7+
to build the targets in the `/test` directory. The current recommended way to set this up is to
8+
use the [makefile](../Makefile) supplied in `Cabal` project parent directory, issuing the following command:
9+
10+
11+
```
12+
> make cabal-install-dev
13+
```
14+
15+
This command will copy the dev `.cabal` generated by a project build into the `cabal-install.cabal`, and set your git index to ignore
16+
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.
17+
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
18+
`tasty`-style pattern expressions like the following:
19+
20+
```
21+
> cabal run cabal-install:unit-tests -- -p /cabal init/
22+
```
23+
24+
Please remember to test your changes! Happy hacking.

cabal-install/cabal-install.cabal.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ Test-Suite unit-tests
351351
UnitTests.Distribution.Client.Get
352352
UnitTests.Distribution.Client.Glob
353353
UnitTests.Distribution.Client.GZipUtils
354-
UnitTests.Distribution.Client.Init.FileCreators
354+
UnitTests.Distribution.Client.Init
355355
UnitTests.Distribution.Client.Store
356356
UnitTests.Distribution.Client.Tar
357357
UnitTests.Distribution.Client.TreeDiffInstances

cabal-install/src/Distribution/Client/Init/Command.hs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,30 @@
1313
--
1414
-----------------------------------------------------------------------------
1515

16-
module Distribution.Client.Init.Command (
17-
18-
-- * Commands
16+
module Distribution.Client.Init.Command
17+
( -- * Commands
1918
initCabal
2019
, incVersion
2120

21+
-- * Helpers
22+
, getSimpleProject
23+
, getLibOrExec
24+
, getCabalVersion
25+
, getPackageName
26+
, getVersion
27+
, getLicense
28+
, getAuthorInfo
29+
, getHomepage
30+
, getSynopsis
31+
, getCategory
32+
, getExtraSourceFiles
33+
, getAppDir
34+
, getSrcDir
35+
, getGenTests
36+
, getTestDir
37+
, getLanguage
38+
, getGenComments
39+
, getModulesBuildToolsAndDeps
2240
) where
2341

2442
import Prelude ()
@@ -475,21 +493,17 @@ getGenComments flags = do
475493
-- | Ask for the application root directory.
476494
getAppDir :: InitFlags -> IO InitFlags
477495
getAppDir flags = do
478-
appDirs <-
479-
return (applicationDirs flags)
480-
?>> noAppDirIfLibraryOnly
496+
appDirs <- noAppDirIfLibraryOnly
481497
?>> guessAppDir flags
482498
?>> promptUserForApplicationDir
483499
?>> setDefault
484500
return $ flags { applicationDirs = appDirs }
485-
486501
where
487-
-- If the packageType==Library, then there is no application dir.
502+
-- If the packageType==Library, ignore defined appdir.
488503
noAppDirIfLibraryOnly :: IO (Maybe [String])
489-
noAppDirIfLibraryOnly =
490-
if (packageType flags) == Flag Library
491-
then return (Just [])
492-
else return Nothing
504+
noAppDirIfLibraryOnly
505+
| packageType flags == Flag Library = return $ Just []
506+
| otherwise = return $ applicationDirs flags
493507

494508
-- Set the default application directory.
495509
setDefault :: IO (Maybe [String])
@@ -530,22 +544,19 @@ guessAppDir flags = do
530544
-- | Ask for the source (library) root directory.
531545
getSrcDir :: InitFlags -> IO InitFlags
532546
getSrcDir flags = do
533-
srcDirs <-
534-
return (sourceDirs flags)
535-
?>> noSourceDirIfExecutableOnly
547+
srcDirs <- noSourceDirIfExecutableOnly
536548
?>> guessSourceDir flags
537549
?>> promptUserForSourceDir
538550
?>> setDefault
539551

540552
return $ flags { sourceDirs = srcDirs }
541553

542554
where
543-
-- If the packageType==Executable, then there is no source dir.
555+
-- If the packageType==Executable, then ignore source dir
544556
noSourceDirIfExecutableOnly :: IO (Maybe [String])
545-
noSourceDirIfExecutableOnly =
546-
if (packageType flags) == Flag Executable
547-
then return (Just [])
548-
else return Nothing
557+
noSourceDirIfExecutableOnly
558+
| packageType flags == Flag Executable = return $ Just []
559+
| otherwise = return $ sourceDirs flags
549560

550561
-- Set the default source directory.
551562
setDefault :: IO (Maybe [String])

cabal-install/src/Distribution/Client/Init/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ data InitFlags =
7979
, initVerbosity :: Flag Verbosity
8080
, overwrite :: Flag Bool
8181
}
82-
deriving (Show, Generic)
82+
deriving (Eq, Show, Generic)
8383

8484
-- the Monoid instance for Flag has later values override earlier
8585
-- ones, which is why we want Maybe [foo] for collecting foo values,

cabal-install/tests/UnitTests.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import qualified UnitTests.Distribution.Client.Described
1919
import qualified UnitTests.Distribution.Client.FileMonitor
2020
import qualified UnitTests.Distribution.Client.Glob
2121
import qualified UnitTests.Distribution.Client.GZipUtils
22-
import qualified UnitTests.Distribution.Client.Init.FileCreators
22+
import qualified UnitTests.Distribution.Client.Init
2323
import qualified UnitTests.Distribution.Client.Store
2424
import qualified UnitTests.Distribution.Client.Tar
2525
import qualified UnitTests.Distribution.Client.Targets
@@ -57,8 +57,8 @@ tests mtimeChangeCalibrated =
5757
UnitTests.Distribution.Client.Glob.tests
5858
, testGroup "Distribution.Client.GZipUtils"
5959
UnitTests.Distribution.Client.GZipUtils.tests
60-
, testGroup "Distribution.Client.Init.FileCreators"
61-
UnitTests.Distribution.Client.Init.FileCreators.tests
60+
, testGroup "Distribution.Client.Init"
61+
UnitTests.Distribution.Client.Init.tests
6262
, testGroup "Distribution.Client.Store"
6363
UnitTests.Distribution.Client.Store.tests
6464
, testGroup "Distribution.Client.Tar"

cabal-install/tests/UnitTests/Distribution/Client/Init/FileCreators.hs renamed to cabal-install/tests/UnitTests/Distribution/Client/Init.hs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
module UnitTests.Distribution.Client.Init.FileCreators (
2-
tests
1+
module UnitTests.Distribution.Client.Init
2+
( tests
33
) where
44

55
import Distribution.Client.Init.FileCreators
66
( generateCabalFile )
77

88
import Test.Tasty
9+
import Test.Tasty.HUnit
910
import Test.Tasty.Golden (goldenVsString)
1011

1112
import System.FilePath
1213
( (</>) )
1314
import qualified Data.ByteString.Lazy as BS
1415
import qualified Data.ByteString.Lazy.Char8 as BS8
1516

17+
import Distribution.Client.Init.Command
18+
( getLibOrExec, getAppDir, getSrcDir )
1619
import Distribution.Client.Init.Types
1720
( InitFlags(..), PackageType(..), defaultInitFlags )
1821
import Distribution.Simple.Setup
@@ -40,6 +43,11 @@ tests = [ testGroup "cabal init goldens"
4043
, checkCabalFileGolden libExeAndTestFlags "lib-exe-and-test-golden.cabal"
4144
, checkCabalFileGolden libExeAndTestWithCommentsFlags "lib-exe-and-test-with-comments-golden.cabal"
4245
]
46+
, testGroup "Check init flag outputs against init script builds"
47+
[ checkInitFlags "Check library-only build flags" libFlags Library
48+
, checkInitFlags "Check lib+exe build flags" libAndExeFlags LibraryAndExecutable
49+
, checkInitFlags "Check exe-only build flags" exeFlags Executable
50+
]
4351
]
4452

4553
checkCabalFileGolden :: InitFlags -> FilePath -> TestTree
@@ -52,6 +60,17 @@ checkCabalFileGolden flags goldenFileName =
5260
generatedCabalFile :: IO BS.ByteString
5361
generatedCabalFile = pure . BS8.pack $ generateCabalFile goldenFileName flags
5462

63+
checkInitFlags :: String -> InitFlags -> PackageType -> TestTree
64+
checkInitFlags label flags pkgType = testCase label $ do
65+
flags' <- getLibOrExec rawFlags
66+
>>= getAppDir
67+
>>= getSrcDir
68+
69+
flags @=? flags'
70+
where
71+
rawFlags = baseFlags { packageType = Flag pkgType }
72+
73+
5574
-- ==================================================
5675
-- Base flags to set common InitFlags values.
5776

@@ -96,6 +115,17 @@ baseFlags = defaultInitFlags {
96115
}
97116

98117

118+
-- ==================================================
119+
-- Simple library flags
120+
121+
libFlags :: InitFlags
122+
libFlags = baseFlags
123+
{ packageType = Flag Library
124+
, mainIs = NoFlag
125+
, sourceDirs = Just ["src"]
126+
, applicationDirs = Just []
127+
}
128+
99129
-- ==================================================
100130
-- Simple exe.
101131

@@ -104,6 +134,7 @@ exeFlags = baseFlags {
104134
-- Create an executable only, with main living in app/Main.hs.
105135
packageType = Flag Executable
106136
, mainIs = Flag "Main.hs"
137+
, sourceDirs = Just []
107138
, applicationDirs = Just ["app"]
108139
}
109140

@@ -127,7 +158,7 @@ libAndExeFlags = baseFlags {
127158

128159
-- Library sources live in src/ and expose the module MyLib.
129160
, sourceDirs = Just ["src"]
130-
, exposedModules = Just (map ModuleName.fromString ["MyLib"])
161+
, exposedModules = Just [ModuleName.fromString "MyLib"]
131162
}
132163

133164

0 commit comments

Comments
 (0)