Skip to content

Commit f3ec04c

Browse files
committed
Revert "Remove --actual"
This reverts commit 531c67f.
1 parent 590da47 commit f3ec04c

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/Print.hs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@ where
44
import Import
55
import Types
66

7-
-- | Print a shell test. See CLI documentation for details.
7+
-- TODO Print output depending on --print=FORMAT (currently only v3)
8+
-- | Print a shell test considering the @--actual=mode@ option. See CLI
9+
-- documentation for details on.
810
printShellTest
911
:: String -- ^ Shelltest format. Value of option @--print[=FORMAT]@.
12+
-> Maybe String -- ^ Value of option @--actual[=MODE]@. @Nothing@ if option is not given.
1013
-> ShellTest -- ^ Test to print
14+
-> Either String String -- ^ Non-matching or matching exit status
15+
-> Either String String -- ^ Non-matching or matching exit status
16+
-> Either Int Int -- ^ Non-matching or matching exit status
1117
-> IO ()
12-
printShellTest format ShellTest{command=c,stdin=i,comments=comments,trailingComments=trailingComments,
18+
printShellTest format actualMode ShellTest{command=c,stdin=i,comments=comments,trailingComments=trailingComments,
1319
stdoutExpected=o_expected,stderrExpected=e_expected,exitCodeExpected=x_expected}
14-
= do
20+
o_actual e_actual x_actual = do
21+
(o,e,x) <- computeResults actualMode
1522
case format of
1623
"v1" -> do
1724
printComments comments
1825
printCommand "" c
1926
printStdin "<<<" i
20-
printStdouterr ">>>" o_expected
21-
printStdouterr ">>>2" e_expected
22-
printExitStatus True ">>>=" x_expected
27+
printStdouterr ">>>" $ justMatcherOutErr o
28+
printStdouterr ">>>2" $ justMatcherOutErr e
29+
printExitStatus True ">>>=" x
2330
printComments trailingComments
2431
"v2" -> do
2532
printComments comments
@@ -38,6 +45,20 @@ printShellTest format ShellTest{command=c,stdin=i,comments=comments,trailingComm
3845
printExitStatus False ">=" x_expected
3946
printComments trailingComments
4047
_ -> fail $ "Unsupported --print format: " ++ format
48+
where
49+
computeResults :: Maybe String -> IO (Maybe Matcher, Maybe Matcher, Matcher)
50+
computeResults Nothing = do
51+
return (o_expected, e_expected, x_expected)
52+
computeResults (Just mode)
53+
| mode `isPrefixOf` "all" = return
54+
(Just $ Lines 0 $ fromEither o_actual -- TODO what about 0? how is it in parser?
55+
,Just $ Lines 0 $ fromEither e_actual
56+
,Numeric $ show $ fromEither x_actual)
57+
| mode `isPrefixOf` "update" = return
58+
(either (Just . Lines 0) (const o_expected) o_actual
59+
,either (Just . Lines 0) (const e_expected) e_actual
60+
,either (Numeric . show) (const x_expected) x_actual)
61+
| otherwise = fail "Unsupported argument for --actual option. Allowed: all, update, or a prefix thereof."
4162

4263
printComments :: [String] -> IO ()
4364
printComments = mapM_ putStrLn
@@ -56,7 +77,7 @@ printStdouterr _ Nothing = return ()
5677
printStdouterr _ (Just (Lines _ "")) = return ()
5778
printStdouterr _ (Just (Numeric _)) = fail "FATAL: Cannot handle Matcher (Numeric) for stdout/stderr."
5879
printStdouterr _ (Just (NegativeNumeric _)) = fail "FATAL: Cannot handle Matcher (NegativeNumeric) for stdout/stderr."
59-
printStdouterr prefix (Just (Lines _ s)) = printf "%s\n%s" prefix s
80+
printStdouterr prefix (Just (Lines _ s)) = printf "%s\n%s\n" prefix s -- TODO trailing \n ?
6081
printStdouterr prefix (Just regex) = printf "%s %s\n" prefix (show regex)
6182

6283
-- | Print exit status. First arg says 'alwaysPrintEvenIfZero'.
@@ -65,3 +86,14 @@ printExitStatus _ _ (Lines _ _) = fail "FATAL: Cannot handle Matcher (Lines) for
6586
printExitStatus False _ (Numeric "0") = return ()
6687
printExitStatus True prefix (Numeric "0") = printf "%s 0\n" prefix
6788
printExitStatus _ prefix s = printf "%s %s\n" prefix (show s)
89+
90+
mkEither :: Bool -> a -> Either a a
91+
mkEither True = Right
92+
mkEither False = Left
93+
94+
fromEither :: Either a a -> a
95+
fromEither = either id id
96+
97+
-- | Make a Matcher out of Nothing.
98+
justMatcherOutErr :: Maybe Matcher -> Maybe Matcher
99+
justMatcherOutErr = Just . fromMaybe (Lines 0 "")

src/shelltest.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ data Args = Args {
6868
,debug_parse :: Bool
6969
,testpaths :: [FilePath]
7070
,print_ :: Maybe String
71+
,actual :: Maybe String
7172
} deriving (Show, Data, Typeable)
7273

7374
argdefs = Args {
@@ -91,6 +92,7 @@ argdefs = Args {
9192
,debug_parse = def &= help "Show test file parsing results and stop"
9293
,testpaths = def &= args &= typ "TESTFILES|TESTDIRS"
9394
,print_ = def &= typ "FORMAT" &= opt "v3" &= groupname "Print test file" &= help "Print test files in specified format (default: v3)."
95+
,actual = def &= typ "MODE" &= opt "all" &= help "Combined with --print, print test files with actual results (stdout, stderr, exit status). This can be used to generate or update tests. Mode 'all' prints all actual results (default). Mode 'update' prints actual results only for non-matching results, i.e. regular expressions in tests are retained."
9496
}
9597
&= helpArg [explicit, name "help", name "h"]
9698
&= program progname
@@ -153,6 +155,8 @@ checkArgs :: Args -> IO Args
153155
checkArgs args = do
154156
when (null $ testpaths args) $
155157
warn $ printf "Please specify at least one test file or directory, eg: %s tests" progname
158+
when (isJust (actual args) && not (isJust (print_ args))) $
159+
warn "Option --actual can only be used with --print."
156160
return args
157161

158162
-- running tests
@@ -184,7 +188,7 @@ prepareShellTest args st@ShellTest{testname=n,command=c,stdin=i,stdoutExpected=o
184188
let errorMatch = maybe True (e_actual `matches`) e_expected
185189
let exitCodeMatch = show x_actual `matches` x_expected
186190
case print_ args of
187-
Just format -> printShellTest format st
191+
Just format -> printShellTest format (actual args) st (mkEither outputMatch o_actual) (mkEither errorMatch e_actual) (mkEither exitCodeMatch x_actual)
188192
Nothing -> if (x_actual == 127) -- catch bad executable - should work on posix systems at least
189193
then ioError $ userError $ unwords $ filter (not . null) [e_actual, printf "Command: '%s' Exit code: %i" cmd x_actual] -- XXX still a test failure; should be an error
190194
else assertString $ concat $ filter (not . null) [

0 commit comments

Comments
 (0)