improve display of test failures

Display the transcript as part of the failure message for the assertion.

This avoids scrambling the tasty display.

This commit was sponsored by Ethan Aubin on Patreon.
This commit is contained in:
Joey Hess 2020-11-24 17:17:09 -04:00
parent 84377c9b75
commit 90a7552511
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
2 changed files with 394 additions and 402 deletions

720
Test.hs

File diff suppressed because it is too large Load diff

View file

@ -51,45 +51,40 @@ import qualified Command.Uninit
-- Run a process. The output and stderr is captured, and is only -- Run a process. The output and stderr is captured, and is only
-- displayed if the process does not return the expected value. -- displayed if the process does not return the expected value.
-- Returns true if the process returned the expected value. testProcess :: String -> [String] -> (Bool -> Bool) -> String -> Assertion
testProcess :: String -> [String] -> Bool -> IO Bool testProcess command params expectedret faildesc = do
testProcess command params expectedret = do
(transcript, ret) <- Utility.Process.Transcript.processTranscript command params Nothing (transcript, ret) <- Utility.Process.Transcript.processTranscript command params Nothing
if ret == expectedret (expectedret ret) @? (faildesc ++ " failed (transcript follows)\n" ++ transcript)
then return True
else do
hPutStrLn stderr transcript
return False
-- Run git. (Do not use to run git-annex as the one being tested -- Run git. (Do not use to run git-annex as the one being tested
-- may not be in path.) -- may not be in path.)
git :: String -> [String] -> IO Bool git :: String -> [String] -> String -> Assertion
git command params = testProcess "git" (command:params) True git command params = testProcess "git" (command:params) (== True)
-- For when git is expected to fail. -- For when git is expected to fail.
git_shouldfail :: String -> [String] -> IO Bool git_shouldfail :: String -> [String] -> String -> Assertion
git_shouldfail command params = testProcess "git" (command:params) False git_shouldfail command params = testProcess "git" (command:params) (== False)
-- Run git-annex. -- Run git-annex.
git_annex :: String -> [String] -> IO Bool git_annex :: String -> [String] -> String -> Assertion
git_annex command params = do git_annex = git_annex' (== True)
pp <- Annex.Path.programPath
testProcess pp (command:params) True
-- For when git-annex is expected to fail. -- For when git-annex is expected to fail.
git_annex_shouldfail :: String -> [String] -> IO Bool git_annex_shouldfail :: String -> [String] -> String -> Assertion
git_annex_shouldfail command params = do git_annex_shouldfail = git_annex' (== False)
pp <- Annex.Path.programPath
-- Run with -q to squelch error.
testProcess pp (command:"-q":params) False
{- Runs git-annex and returns its output. -} git_annex' :: (Bool -> Bool) -> String -> [String] -> String -> Assertion
git_annex' expectedret command params faildesc = do
pp <- Annex.Path.programPath
testProcess pp (command:params) expectedret faildesc
{- Runs git-annex and returns its standard output. -}
git_annex_output :: String -> [String] -> IO String git_annex_output :: String -> [String] -> IO String
git_annex_output command params = do git_annex_output command params = do
pp <- Annex.Path.programPath pp <- Annex.Path.programPath
Utility.Process.readProcess pp (command:params) Utility.Process.readProcess pp (command:params)
git_annex_expectoutput :: String -> [String] -> [String] -> IO () git_annex_expectoutput :: String -> [String] -> [String] -> Assertion
git_annex_expectoutput command params expected = do git_annex_expectoutput command params expected = do
got <- lines <$> git_annex_output command params got <- lines <$> git_annex_output command params
got == expected @? ("unexpected value running " ++ command ++ " " ++ show params ++ " -- got: " ++ show got ++ " expected: " ++ show expected) got == expected @? ("unexpected value running " ++ command ++ " " ++ show params ++ " -- got: " ++ show got ++ " expected: " ++ show expected)
@ -118,7 +113,7 @@ with_ssh_origin cloner a = cloner $ do
origindir <- absPath . Git.Types.fromConfigValue origindir <- absPath . Git.Types.fromConfigValue
=<< annexeval (Config.getConfig k v) =<< annexeval (Config.getConfig k v)
let originurl = "localhost:" ++ fromRawFilePath origindir let originurl = "localhost:" ++ fromRawFilePath origindir
git "config" [config, originurl] @? "git config failed" git "config" [config, originurl] "git config failed"
a a
where where
config = "remote.origin.url" config = "remote.origin.url"
@ -157,7 +152,7 @@ withtmpclonerepo' cfg a = do
throwM e throwM e
disconnectOrigin :: Assertion disconnectOrigin :: Assertion
disconnectOrigin = git "remote" ["rm", "origin"] @? "remote rm" disconnectOrigin = git "remote" ["rm", "origin"] "remote rm"
withgitrepo :: (FilePath -> Assertion) -> Assertion withgitrepo :: (FilePath -> Assertion) -> Assertion
withgitrepo a = do withgitrepo a = do
@ -185,7 +180,7 @@ setuprepo :: FilePath -> IO FilePath
setuprepo dir = do setuprepo dir = do
cleanup dir cleanup dir
ensuretmpdir ensuretmpdir
git "init" ["-q", dir] @? "git init failed" git "init" ["-q", dir] "git init"
configrepo dir configrepo dir
return dir return dir
@ -212,11 +207,16 @@ clonerepo old new cfg = do
, Just old , Just old
, Just new , Just new
] ]
git "clone" cloneparams @? "git clone failed" git "clone" cloneparams "git clone"
configrepo new configrepo new
indir new $ do indir new $ do
ver <- annexVersion <$> getTestMode ver <- annexVersion <$> getTestMode
git_annex "init" ["-q", new, "--version", show (Types.RepoVersion.fromRepoVersion ver)] @? "git annex init failed" git_annex "init"
[ "-q"
, new, "--version"
, show (Types.RepoVersion.fromRepoVersion ver)
]
"git annex init"
unless (bareClone cfg) $ unless (bareClone cfg) $
indir new $ indir new $
setupTestMode setupTestMode
@ -226,15 +226,15 @@ configrepo :: FilePath -> IO ()
configrepo dir = indir dir $ do configrepo dir = indir dir $ do
-- ensure git is set up to let commits happen -- ensure git is set up to let commits happen
git "config" ["user.name", "Test User"] git "config" ["user.name", "Test User"]
@? "git config failed" "git config"
git "config" ["user.email", "test@example.com"] git "config" ["user.email", "test@example.com"]
@? "git config failed" "git config"
-- avoid signed commits by test suite -- avoid signed commits by test suite
git "config" ["commit.gpgsign", "false"] git "config" ["commit.gpgsign", "false"]
@? "git config failed" "git config"
-- tell git-annex to not annex the ingitfile -- tell git-annex to not annex the ingitfile
git "config" ["annex.largefiles", "exclude=" ++ ingitfile] git "config" ["annex.largefiles", "exclude=" ++ ingitfile]
@? "git config annex.largefiles failed" "git config annex.largefiles"
ensuretmpdir :: IO () ensuretmpdir :: IO ()
ensuretmpdir = do ensuretmpdir = do
@ -415,10 +415,10 @@ annexed_notpresent_imported f = ifM (annexeval Config.crippledFileSystem)
unannexed :: FilePath -> Assertion unannexed :: FilePath -> Assertion
unannexed = runchecks [checkregularfile, checkcontent, checkwritable] unannexed = runchecks [checkregularfile, checkcontent, checkwritable]
add_annex :: FilePath -> IO Bool add_annex :: FilePath -> String -> Assertion
add_annex f = ifM (unlockedFiles <$> getTestMode) add_annex f faildesc = ifM (unlockedFiles <$> getTestMode)
( git "add" [f] ( git "add" [f] faildesc
, git_annex "add" [f] , git_annex "add" [f] faildesc
) )
data TestMode = TestMode data TestMode = TestMode
@ -492,8 +492,8 @@ setupTestMode :: IO ()
setupTestMode = do setupTestMode = do
testmode <- getTestMode testmode <- getTestMode
when (adjustedUnlockedBranch testmode) $ do when (adjustedUnlockedBranch testmode) $ do
git "commit" ["--allow-empty", "-m", "empty"] @? "git commit failed" git "commit" ["--allow-empty", "-m", "empty"] "git commit failed"
git_annex "adjust" ["--unlock"] @? "git annex adjust failed" git_annex "adjust" ["--unlock"] "git annex adjust failed"
changeToTmpDir :: FilePath -> IO () changeToTmpDir :: FilePath -> IO ()
changeToTmpDir t = do changeToTmpDir t = do