Skip to content

Commit 35d865f

Browse files
committed
WIP
1 parent 1a031e3 commit 35d865f

File tree

9 files changed

+80
-45
lines changed

9 files changed

+80
-45
lines changed

cabal-testsuite/PackageTests/TestSuiteTests/LibV09/tests/Deadlock.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import Distribution.TestSuite
55
import Lib
66

77
tests :: IO [Test]
8-
tests = return [nullt x | x <- [1 .. 1000]]
8+
tests = return [nullt x | x <- [1 .. 3]]

cabal-testsuite/src/Test/Cabal/DecodeShowBuildInfo.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ recordBuildInfo pkgName cname = do
3333
recordMode RecordAll $ do
3434
recordHeader ["show-build-info", prettyShow pkgName, prettyShow cname]
3535
buildInfo <- liftIO $ readFile fp
36-
recordLog $ Result ExitSuccess "build --enable-build-info" buildInfo
36+
recordLog $ Result ExitSuccess "build --enable-build-info" buildInfo mempty
3737

3838
-- | Decode the given filepath into a 'BuildInfo'.
3939
--

cabal-testsuite/src/Test/Cabal/Monad.hs

+24-14
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module Test.Cabal.Monad (
3535
testSourceCopyDir,
3636
testCabalDir,
3737
testUserCabalConfigFile,
38-
testActualFile,
38+
testActualFileStdout,
39+
testActualFileStderr,
3940
-- * Skipping tests
4041
skip,
4142
skipIf,
@@ -351,22 +352,23 @@ runTestM mode m = withSystemTempDirectory "cabal-testsuite" $ \tmp_dir -> do
351352

352353
check_expect accept = do
353354
env <- getTestEnv
354-
actual_raw <- liftIO $ readFileOrEmpty (testActualFile env)
355-
expect <- liftIO $ readFileOrEmpty (testExpectFile env)
355+
-- TODO: Compare stderr
356+
actual_raw <- liftIO $ readFileOrEmpty (testActualFileStdout env)
357+
expect <- liftIO $ readFileOrEmpty (testExpectFileStdout env)
356358
norm_env <- mkNormalizerEnv
357359
let actual = normalizeOutput norm_env actual_raw
358360
when (words actual /= words expect) $ do
359361
-- First try whitespace insensitive diff
360-
let actual_fp = testNormalizedActualFile env
361-
expect_fp = testNormalizedExpectFile env
362+
let actual_fp = testNormalizedActualFileStdout env
363+
expect_fp = testNormalizedExpectFileStdout env
362364
liftIO $ writeFile actual_fp actual
363365
liftIO $ writeFile expect_fp expect
364366
liftIO $ putStrLn "Actual output differs from expected:"
365367
b <- diff ["-uw"] expect_fp actual_fp
366368
unless b . void $ diff ["-u"] expect_fp actual_fp
367369
if accept
368370
then do liftIO $ putStrLn "Accepting new output."
369-
liftIO $ writeFileNoCR (testExpectFile env) actual
371+
liftIO $ writeFileNoCR (testExpectFileStdout env) actual
370372
else liftIO $ exitWith (ExitFailure 1)
371373

372374
readFileOrEmpty :: FilePath -> IO String
@@ -597,17 +599,25 @@ testUserCabalConfigFile :: TestEnv -> FilePath
597599
testUserCabalConfigFile env = testCabalDir env </> "config"
598600

599601
-- | The file where the expected output of the test lives
600-
testExpectFile :: TestEnv -> FilePath
601-
testExpectFile env = testSourceDir env </> testName env <.> "out"
602+
testExpectFileStdout :: TestEnv -> FilePath
603+
testExpectFileStdout env = testSourceDir env </> testName env <.> "stdout"
602604

603605
-- | Where we store the actual output
604-
testActualFile :: TestEnv -> FilePath
605-
testActualFile env = testWorkDir env </> testName env <.> "comp.out"
606+
testActualFileStdout :: TestEnv -> FilePath
607+
testActualFileStdout env = testWorkDir env </> testName env <.> "comp.stdout"
608+
609+
-- | The file where the expected output of the test lives
610+
testExpectFileStderr :: TestEnv -> FilePath
611+
testExpectFileStderr env = testSourceDir env </> testName env <.> "stderr"
612+
613+
-- | Where we store the actual errput
614+
testActualFileStderr :: TestEnv -> FilePath
615+
testActualFileStderr env = testWorkDir env </> testName env <.> "comp.stderr"
606616

607617
-- | Where we will write the normalized actual file (for diffing)
608-
testNormalizedActualFile :: TestEnv -> FilePath
609-
testNormalizedActualFile env = testActualFile env <.> "normalized"
618+
testNormalizedActualFileStdout :: TestEnv -> FilePath
619+
testNormalizedActualFileStdout env = testActualFileStdout env <.> "normalized"
610620

611621
-- | Where we will write the normalized expected file (for diffing)
612-
testNormalizedExpectFile :: TestEnv -> FilePath
613-
testNormalizedExpectFile env = testWorkDir env </> testName env <.> "out.normalized"
622+
testNormalizedExpectFileStdout :: TestEnv -> FilePath
623+
testNormalizedExpectFileStdout env = testWorkDir env </> testName env <.> "stdout.normalized"

cabal-testsuite/src/Test/Cabal/Prelude.hs

+26-16
Original file line numberDiff line numberDiff line change
@@ -563,14 +563,17 @@ withRepo repo_dir m = do
563563
requireSuccess :: Result -> TestM Result
564564
requireSuccess r@Result { resultCommand = cmd
565565
, resultExitCode = exitCode
566-
, resultOutput = output } = withFrozenCallStack $ do
566+
, resultStdout
567+
, resultStderr } = withFrozenCallStack $ do
567568
env <- getTestEnv
568569
when (exitCode /= ExitSuccess && not (testShouldFail env)) $
569570
assertFailure $ "Command " ++ cmd ++ " failed.\n" ++
570-
"Output:\n" ++ output ++ "\n"
571+
"Stdout:\n" ++ resultStdout ++ "\n" ++
572+
"Stderr:\n" ++ resultStderr ++ "\n"
571573
when (exitCode == ExitSuccess && testShouldFail env) $
572574
assertFailure $ "Command " ++ cmd ++ " succeeded.\n" ++
573-
"Output:\n" ++ output ++ "\n"
575+
"Stdout:\n" ++ resultStdout ++ "\n" ++
576+
"Stderr:\n" ++ resultStderr ++ "\n"
574577
return r
575578

576579
initWorkDir :: TestM ()
@@ -595,21 +598,28 @@ recordHeader args = do
595598
initWorkDir
596599
liftIO $ putStr str_header
597600
liftIO $ C.appendFile (testWorkDir env </> "test.log") header
598-
liftIO $ C.appendFile (testActualFile env) header
601+
liftIO $ C.appendFile (testActualFileStdout env) header
599602

600603
recordLog :: Result -> TestM ()
601-
recordLog res = do
604+
recordLog Result{resultStdout, resultStderr, resultCommand} = do
602605
env <- getTestEnv
603606
let mode = testRecordMode env
604607
initWorkDir
605608
liftIO $ C.appendFile (testWorkDir env </> "test.log")
606-
(C.pack $ "+ " ++ resultCommand res ++ "\n"
607-
++ resultOutput res ++ "\n\n")
608-
liftIO . C.appendFile (testActualFile env) . C.pack . testRecordNormalizer env $
609-
case mode of
610-
RecordAll -> unlines (lines (resultOutput res))
611-
RecordMarked -> getMarkedOutput (resultOutput res)
609+
(C.pack $ "+ " ++ resultCommand ++ "\n"
610+
++ "STDOUT\n" <> resultStdout ++ "\n"
611+
++ "STDERR\n" <> resultStderr ++ "\n"
612+
++ "\n")
613+
let report f txt
614+
= liftIO
615+
. C.appendFile f
616+
. C.pack
617+
. testRecordNormalizer env $ case mode of
618+
RecordAll -> unlines (lines txt)
619+
RecordMarked -> getMarkedOutput txt
612620
DoNotRecord -> ""
621+
report (testActualFileStdout env) resultStdout
622+
report (testActualFileStderr env) resultStderr
613623

614624
getMarkedOutput :: String -> String -- trailing newline
615625
getMarkedOutput out = unlines (go (lines out) False)
@@ -669,7 +679,7 @@ shouldNotExist path =
669679
assertRegex :: MonadIO m => String -> String -> Result -> m ()
670680
assertRegex msg regex r =
671681
withFrozenCallStack $
672-
let out = resultOutput r
682+
let out = resultStdout r
673683
in assertBool (msg ++ ",\nactual output:\n" ++ out)
674684
(out =~ regex)
675685

@@ -695,14 +705,14 @@ assertOutputContains needle result =
695705
withFrozenCallStack $
696706
unless (needle `isInfixOf` (concatOutput output)) $
697707
assertFailure $ " expected: " ++ needle
698-
where output = resultOutput result
708+
where output = resultStdout result
699709

700710
assertOutputDoesNotContain :: MonadIO m => WithCallStack (String -> Result -> m ())
701711
assertOutputDoesNotContain needle result =
702712
withFrozenCallStack $
703713
when (needle `isInfixOf` (concatOutput output)) $
704714
assertFailure $ "unexpected: " ++ needle
705-
where output = resultOutput result
715+
where output = resultStdout result
706716

707717
assertFindInFile :: MonadIO m => WithCallStack (String -> FilePath -> m ())
708718
assertFindInFile needle path =
@@ -897,7 +907,7 @@ withSourceCopy m = do
897907
let cwd = testCurrentDir env
898908
dest = testSourceCopyDir env
899909
r <- git' "ls-files" ["--cached", "--modified"]
900-
forM_ (lines (resultOutput r)) $ \f -> do
910+
forM_ (lines (resultStdout r)) $ \f -> do
901911
unless (isTestFile f) $ do
902912
liftIO $ createDirectoryIfMissing True (takeDirectory (dest </> f))
903913
liftIO $ copyFile (cwd </> f) (dest </> f)
@@ -923,7 +933,7 @@ getIPID :: String -> TestM String
923933
getIPID pn = do
924934
r <- ghcPkg' "field" ["--global", pn, "id"]
925935
-- Don't choke on warnings from ghc-pkg
926-
case mapMaybe (stripPrefix "id: ") (lines (resultOutput r)) of
936+
case mapMaybe (stripPrefix "id: ") (lines (resultStdout r)) of
927937
-- ~/.cabal/store may contain multiple versions of single package
928938
-- we pick first one. It should work
929939
(x:_) -> return (takeWhile (not . Char.isSpace) x)

cabal-testsuite/src/Test/Cabal/Run.hs

+28-13
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ import System.IO
1515
import System.Exit
1616
import System.Directory
1717
import System.FilePath
18+
import Data.Foldable (traverse_)
1819

1920
-- | The result of invoking the command line.
2021
data Result = Result
2122
{ resultExitCode :: ExitCode
2223
, resultCommand :: String
23-
, resultOutput :: String
24+
, result :: String
25+
, resultStdout :: String
26+
, resultStderr :: String
2427
} deriving Show
2528

2629
-- | Run a command, streaming its output to stdout, and return a 'Result'
@@ -46,22 +49,29 @@ run _verbosity mb_cwd env_overrides path0 args input = do
4649

4750
mb_env <- getEffectiveEnvironment env_overrides
4851
putStrLn $ "+ " ++ showCommandForUser path args
49-
(readh, writeh) <- Compat.createPipe
50-
hSetBuffering readh LineBuffering
51-
hSetBuffering writeh LineBuffering
52-
let drain = do
53-
r <- hGetContents readh
54-
putStr r -- forces the output
55-
hClose readh
52+
(readstdout, writestdout) <- Compat.createPipe
53+
(readstderr, writestderr) <- Compat.createPipe
54+
(readall, writeall) <- Compat.createPipe
55+
traverse_ (`hSetBuffering` LineBuffering) [ readstdout, writestdout, readstderr, writestderr, readall, writeall ]
56+
let mkDrain h = do
57+
r <- hGetContents h
58+
length r `seq` hClose h
59+
hPutStr writeall r
5660
return r
57-
withAsync drain $ \sync -> do
61+
let mkDrain' h = do
62+
r <- hGetContents h
63+
length r `seq` hClose h
64+
return r
65+
withAsync (mkDrain readstdout) $ \syncstdout -> do
66+
withAsync (mkDrain readstderr) $ \syncstderr -> do
67+
withAsync (mkDrain' readall) $ \syncall -> do
5868

5969
let prc = (proc path args)
6070
{ cwd = mb_cwd
6171
, env = mb_env
6272
, std_in = case input of { Just _ -> CreatePipe; Nothing -> Inherit }
63-
, std_out = UseHandle writeh
64-
, std_err = UseHandle writeh
73+
, std_out = UseHandle writestdout
74+
, std_err = UseHandle writestderr
6575
}
6676
(stdin_h, _, _, procHandle) <- createProcess prc
6777

@@ -74,10 +84,15 @@ run _verbosity mb_cwd env_overrides path0 args input = do
7484

7585
-- wait for the program to terminate
7686
exitcode <- waitForProcess procHandle
77-
out <- wait sync
87+
rStdout <- wait syncstdout
88+
rStderr <- wait syncstderr
89+
hClose writeall
90+
rAll <- wait syncall
7891

7992
return Result {
8093
resultExitCode = exitcode,
8194
resultCommand = showCommandForUser path args,
82-
resultOutput = out
95+
result = rAll,
96+
resultStdout = rStdout,
97+
resultStderr = rStderr
8398
}

0 commit comments

Comments
 (0)