Skip to content

Commit 706736a

Browse files
committed
Specifying test components only builds/runs those tests #398
1 parent 5b67946 commit 706736a

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Give a reason for unregistering packages [#389](https://github.com/commercialhaskell/stack/issues/389)
1111
* `stack exec` accepts the `--no-ghc-package-path` parameter
1212
* Don't require build plan to upload [#400](https://github.com/commercialhaskell/stack/issues/400)
13+
* Specifying test components only builds/runs those tests [#398](https://github.com/commercialhaskell/stack/issues/398)
1314

1415
Bug fixes:
1516

src/Stack/Build/Execute.hs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module Stack.Build.Execute
99
( printPlan
1010
, preFetch
1111
, executePlan
12+
-- TESTING
13+
, compareTestsComponents
1214
) where
1315

1416
import Control.Applicative ((<$>), (<*>))
@@ -680,11 +682,19 @@ singleTest ac ee task =
680682
_ -> assert False True)
681683
|| True -- FIXME above logic is incorrect, see: https://github.com/commercialhaskell/stack/issues/319
682684
needHpc = boptsCoverage (eeBuildOpts ee)
685+
686+
componentsRaw =
687+
case taskType task of
688+
TTLocal lp -> Set.toList $ lpComponents lp
689+
TTUpstream _ _ -> assert False []
690+
testsToRun = compareTestsComponents componentsRaw $ Set.toList $ packageTests package
691+
components = map (T.unpack . T.append "test:") testsToRun
692+
683693
when needBuild $ do
684694
announce "build (test)"
685695
fileModTimes <- getPackageFileModTimes package cabalfp
686696
writeBuildCache pkgDir fileModTimes
687-
cabal (console && configHideTHLoading config) ["build"]
697+
cabal (console && configHideTHLoading config) $ "build" : components
688698

689699
bconfig <- asks getBuildConfig
690700
buildDir <- distDirFromDir pkgDir
@@ -696,7 +706,7 @@ singleTest ac ee task =
696706
Platform _ Windows -> ".exe"
697707
_ -> ""
698708

699-
errs <- liftM Map.unions $ forM (Set.toList $ packageTests package) $ \testName -> do
709+
errs <- liftM Map.unions $ forM testsToRun $ \testName -> do
700710
nameDir <- parseRelDir $ T.unpack testName
701711
nameExe <- parseRelFile $ T.unpack testName ++ exeExtension
702712
nameTix <- liftM (pkgDir </>) $ parseRelFile $ T.unpack testName ++ ".tix"
@@ -778,6 +788,22 @@ singleTest ac ee task =
778788
(fmap fst mlogFile)
779789
bs
780790

791+
-- | Determine the tests to be run based on the list of components.
792+
compareTestsComponents :: [Text] -- ^ components
793+
-> [Text] -- ^ all test names
794+
-> [Text] -- ^ tests to be run
795+
compareTestsComponents [] tests = tests -- no components -- all tests
796+
compareTestsComponents comps tests2 =
797+
Set.toList $ Set.intersection tests1 $ Set.fromList tests2
798+
where
799+
tests1 = Set.unions $ map toSet comps
800+
801+
toSet x =
802+
case T.break (== ':') x of
803+
(y, "") -> assert (x == y) (Set.singleton x)
804+
("test", y) -> Set.singleton $ T.drop 1 y
805+
_ -> Set.empty
806+
781807
-- | Generate the HTML report and
782808
generateHpcReport
783809
:: M env m
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Stack.Build.ExecuteSpec (main, spec) where
2+
3+
import Stack.Build.Execute
4+
import Test.Hspec
5+
import qualified Data.Text as T
6+
7+
main :: IO ()
8+
main = hspec spec
9+
10+
spec :: Spec
11+
spec = describe "compareTestComponents" $ do
12+
let test comps names expected = it (show (comps, names)) $
13+
compareTestsComponents
14+
(T.words $ T.pack comps)
15+
(T.words $ T.pack names)
16+
`shouldBe`
17+
(T.words $ T.pack expected)
18+
19+
test "" "" ""
20+
test "" "foo" "foo"
21+
test "foo" "bar" ""
22+
test "foo" "foo bar" "foo"
23+
test "test:foo" "foo bar" "foo"
24+
test "test:foo exe:bar" "foo bar" "foo"

stack.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ test-suite stack-test
196196
main-is: Test.hs
197197
other-modules: Spec
198198
, Stack.BuildPlanSpec
199+
, Stack.Build.ExecuteSpec
199200
, Stack.ConfigSpec
200201
, Stack.PackageDumpSpec
201202
, Stack.ArgsSpec
@@ -217,6 +218,7 @@ test-suite stack-test
217218
, conduit-extra
218219
, resourcet
219220
, Cabal
221+
, text
220222
default-language: Haskell2010
221223

222224
test-suite stack-integration-test

0 commit comments

Comments
 (0)