Skip to content

Commit 9446424

Browse files
committed
SetupHooks: add tests
This adds tests for error messages related to SetupHooks: - error when returning an invalid component diff in a per-component pre-configure hook - error when declaring pre-build rules whose dependency graph contains cycles - error when we cannot find a dependency of a pre-build rule - warning when there are pre-build rules that are declared but never demanded It also adds some correctness tests for SetupHooks, e.g. that pre-build rules are run in dependency order (see the `SetupHooksRuleOrdering` test).
1 parent 4c0d245 commit 9446424

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+507
-28
lines changed

Cabal-hooks/src/Distribution/Simple/SetupHooks.hs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module Distribution.Simple.SetupHooks
9090
-- $rulesAPI
9191
, RulesM
9292
, registerRule, registerAction
93-
, declareRuleDependencies
93+
, addRuleMonitors
9494

9595
-- **** Local name generation for t'ActionId'
9696
, FreshT
@@ -354,10 +354,9 @@ Rules can declare various kinds of dependencies:
354354
Rules are considered __out-of-date__ precisely when any of the following
355355
conditions apply:
356356
357-
[O1] a file dependency of a rule has changed in some way,
358-
[O2] the environment passed to the computation of rules has changed,
359-
[O3] there has been a relevant change in the set of files and
357+
[O1] there has been a relevant change in the set of files and
360358
directories monitored by the rules.
359+
[O2] the environment passed to the computation of rules has changed,
361360
362361
If the rules are out-of-date, the build system is expected to re-run the
363362
computation that computes all rules.
@@ -394,7 +393,7 @@ Defining pre-build rules can be done in the following style:
394393
> return $ do
395394
> -- IO actions allowed here
396395
> myData <- liftIO someIOAction
397-
> declareRuleDependencies [ MonitorDir "someSearchDir" DirContents ]
396+
> addRuleMonitors [ MonitorDir "someSearchDir" DirContents ]
398397
> registerRule $ simpleRule action1 deps1 outs1
399398
> registerRule $ simpleRule action1 deps2 outs2
400399
> registerRule $ simpleRule action1 deps3 outs3
@@ -405,10 +404,10 @@ rather than directly using the v'Rules', v'Rule' and v'Action' constructors,
405404
which insulates us from internal changes to the t'Rules', t'Rule' and t'Action'
406405
datatypes, respectively.
407406
408-
We use 'declareRuleDependencies' to declare that the collection of rules as a
409-
whole depends on. In this case, we declare that they depend on the contents of
410-
the "searchDir" directory. This means that the rules will be computed anew
411-
whenever the contents of this directory change.
407+
We use 'addRuleMonitorss' to declare a monitored directory that the collection
408+
of rules as a whole depends on. In this case, we declare that they depend on the
409+
contents of the "searchDir" directory. This means that the rules will be
410+
computed anew whenever the contents of this directory change.
412411
413412
Additional convenience functions are also provided, such as the 'generateModules'
414413
function which can be used to generate a collection of modules ex nihilo without
@@ -566,19 +565,17 @@ register zeroId succId rule = FreshT $ do
566565
registerRule :: Monad m => Rule -> RulesT m ()
567566
registerRule r = Writer.tell ( [r], [] )
568567

569-
-- | Declare a dependency for the collection of all rules.
568+
-- | Declare additional monitored objects for the collection of all rules.
570569
--
571-
-- When this dependency changes, the rules are re-computed.
572-
declareRuleDependencies :: Monad m => [ MonitorFileOrDir ] -> RulesT m ()
573-
declareRuleDependencies mons = Writer.tell ( [], mons )
570+
-- When these monitored objects change, the rules are re-computed.
571+
addRuleMonitors :: Monad m => [ MonitorFileOrDir ] -> RulesT m ()
572+
addRuleMonitors mons = Writer.tell ( [], mons )
574573

575574
-- | Register an action. Returns a unique identifier for that action.
576575
registerAction :: Action -> FreshT Action ActionId Identity ActionId
577576
registerAction = register ( ActionId 1 ) ( \ ( ActionId i ) -> ActionId ( i + 1 ) )
578577

579578
-- | Find a file in the given search directories.
580-
--
581-
--
582579
findFileInDirs :: FilePath -> [FilePath] -> IO (Maybe Location)
583580
findFileInDirs file dirs =
584581
findFirstFile

Cabal/src/Distribution/Simple/SetupHooks/Internal.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ data PreConfPackageInputs = PreConfPackageInputs
201201
-- of this datatype.
202202
data PreConfPackageOutputs = PreConfPackageOutputs
203203
{ buildOptions :: BuildOptions
204-
, extraConfiguredPrograms :: ConfiguredProgs
204+
, extraConfiguredProgs :: ConfiguredProgs
205205
}
206206
deriving (Generic, Show)
207207

@@ -215,7 +215,7 @@ noPreConfPackageOutputs :: PreConfPackageInputs -> PreConfPackageOutputs
215215
noPreConfPackageOutputs (PreConfPackageInputs{localBuildConfig = lbc}) =
216216
PreConfPackageOutputs
217217
{ buildOptions = LBC.withBuildOptions lbc
218-
, extraConfiguredPrograms = Map.empty
218+
, extraConfiguredProgs = Map.empty
219219
}
220220

221221
-- | Package-wide post-configure step.
@@ -347,12 +347,12 @@ instance Semigroup PreConfPkgSemigroup where
347347
do
348348
PreConfPackageOutputs
349349
{ buildOptions = opts1
350-
, extraConfiguredPrograms = progs1
350+
, extraConfiguredProgs = progs1
351351
} <-
352352
f1 inputs
353353
PreConfPackageOutputs
354354
{ buildOptions = opts2
355-
, extraConfiguredPrograms = progs2
355+
, extraConfiguredProgs = progs2
356356
} <-
357357
f2 $
358358
PreConfPackageInputs
@@ -370,7 +370,7 @@ instance Semigroup PreConfPkgSemigroup where
370370
return $
371371
PreConfPackageOutputs
372372
{ buildOptions = opts2
373-
, extraConfiguredPrograms = progs1 <> progs2
373+
, extraConfiguredProgs = progs1 <> progs2
374374
}
375375

376376
-- | A newtype to hang off the @Semigroup PreConfComponentHook@ instance.

Cabal/src/Distribution/Simple/SetupHooks/Rule.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ data Rule
185185
-- it will pass these locations as the first argument to the action.
186186
, results :: !(NE.NonEmpty Location)
187187
-- ^ Results of this rule; see t'Result'.
188+
--
189+
-- When the build system executes the action associated to this rule,
190+
-- it will pass these locations as the second argument to the action.
188191
, actionId :: !ActionId
189192
-- ^ To run this rule, which t'Action' should we execute?
190193
--
@@ -200,7 +203,7 @@ data Rule
200203
-- environment passed to the rule.
201204
--
202205
-- A value of 'Nothing' means: always re-run the rule when the
203-
-- environment passes to it changes.
206+
-- environment passed to it changes.
204207
}
205208
deriving (Generic, Show)
206209

@@ -220,7 +223,8 @@ instance Binary Rule
220223
instance Structured Rule
221224

222225
-- | A (fully resolved) location of a dependency or result of a rule,
223-
-- consisting of an absolute path and of a file path relative to that base path.
226+
-- consisting of a base directory and of a file path relative to that base
227+
-- directory path.
224228
--
225229
-- In practice, this will be something like @( dir, toFilePath modName )@,
226230
-- where:
@@ -269,7 +273,7 @@ newtype Action
269273
-- \^ Locations of the __dependencies__ of this action,
270274
-- as declared by the rule that this action is executing.
271275
-> NE.NonEmpty Location
272-
-- \^ Locations in which the __results_ of this action
276+
-- \^ Locations in which the __results__ of this action
273277
-- should be put.
274278
-> IO ()
275279
}
@@ -286,8 +290,9 @@ simpleAction f = Action{action = f}
286290
-- constructor.
287291
--
288292
-- Actions are registered using 'registerAction', and rules are registered
289-
-- using 'registerRule'. Additional rule dependencies (whose changes should
290-
-- trigger rule recompilation) are declared using 'declareRuleDependencies'.
293+
-- using 'registerRule'. One can declare additional monitored files or
294+
-- directories using 'addRuleMonitors'; a change in these will trigger the
295+
-- recomputation of all rules.
291296
--
292297
-- The @env@ type parameter represents an extra argument, which usually
293298
-- consists of information known to Cabal such as 'LocalBuildInfo' and
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Main where
2+
3+
import Distribution.Simple ( defaultMainWithSetupHooks )
4+
import SetupHooks ( setupHooks )
5+
6+
main :: IO ()
7+
main = defaultMainWithSetupHooks setupHooks
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module SetupHooks where
2+
3+
import Distribution.Simple.SetupHooks
4+
5+
import Control.Monad ( void )
6+
7+
setupHooks :: SetupHooks
8+
setupHooks =
9+
noSetupHooks
10+
{ configureHooks =
11+
noConfigureHooks
12+
{ preConfComponentHook = Just pccHook }
13+
}
14+
15+
pccHook :: PreConfComponentHook
16+
pccHook _ = return $
17+
PreConfComponentOutputs $ ComponentDiff $ CExe emptyExecutable
18+
-- Bad: component is a library, but we returned an executable!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cabal-version: 2.2
2+
name: setup-hooks-bad-diff1-test
3+
version: 0.1.0.0
4+
synopsis: Test 1 for a bad component diff
5+
license: BSD-3-Clause
6+
author: NA
7+
maintainer: NA
8+
category: Testing
9+
build-type: Hooks
10+
11+
custom-setup
12+
setup-depends: Cabal-hooks, base
13+
14+
library
15+
build-depends: base
16+
default-language: Haskell2010
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Setup configure
2+
Configuring setup-hooks-bad-diff1-test-0.1.0.0...
3+
Error: [Cabal-9491]
4+
Hooks: mismatched component types in per-component configure hook.
5+
Trying to apply an executable diff to a library.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Test.Cabal.Prelude
2+
main = setupTest $ do
3+
fails $ setup "configure" []
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Main where
2+
3+
import Distribution.Simple ( defaultMainWithSetupHooks )
4+
import SetupHooks ( setupHooks )
5+
6+
main :: IO ()
7+
main = defaultMainWithSetupHooks setupHooks

0 commit comments

Comments
 (0)