|
| 1 | +module Print |
| 2 | +where |
| 3 | + |
| 4 | +import Import |
| 5 | +import Types |
| 6 | + |
| 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. |
| 10 | +printShellTest |
| 11 | + :: String -- ^ Shelltest format. Value of option @--print[=FORMAT]@. |
| 12 | + -> Maybe String -- ^ Value of option @--actual[=MODE]@. @Nothing@ if option is not given. |
| 13 | + -> 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 |
| 17 | + -> IO () |
| 18 | +printShellTest format actualMode ShellTest{command=c,stdin=i,comments=comments, |
| 19 | + stdoutExpected=o_expected,stderrExpected=e_expected,exitCodeExpected=x_expected} |
| 20 | + o_actual e_actual x_actual = do |
| 21 | + (o,e,x) <- computeResults actualMode |
| 22 | + case format of |
| 23 | + "v1" -> do |
| 24 | + printComments comments |
| 25 | + printCommand "" c |
| 26 | + printStdin "<<<" i |
| 27 | + printStdouterr ">>>" o |
| 28 | + printStdouterr ">>>2" e |
| 29 | + printExitStatus ">>>=" x |
| 30 | + "v2" -> do |
| 31 | + printComments comments |
| 32 | + printCommand "$ " c |
| 33 | + printStdin "<<<" i |
| 34 | + printStdouterr ">>>" o |
| 35 | + printStdouterr ">>>2" e |
| 36 | + printExitStatus ">>>=" x |
| 37 | + "v3" -> do |
| 38 | + printComments comments |
| 39 | + printCommand "$ " c |
| 40 | + printStdin "<" i |
| 41 | + printStdouterr ">" o |
| 42 | + printStdouterr ">2" e |
| 43 | + printExitStatus ">=" x |
| 44 | + _ -> fail $ "Unsupported --print format: " ++ format |
| 45 | + where |
| 46 | + computeResults :: Maybe String -> IO (Maybe Matcher, Maybe Matcher, Matcher) |
| 47 | + computeResults Nothing = do |
| 48 | + return (o_expected, e_expected, x_expected) |
| 49 | + computeResults (Just mode) |
| 50 | + | mode `isPrefixOf` "all" = return |
| 51 | + (Just $ Lines 0 $ fromEither o_actual -- TODO what about 0? how is it in parser? |
| 52 | + ,Just $ Lines 0 $ fromEither e_actual |
| 53 | + ,Numeric $ show $ fromEither x_actual) |
| 54 | + | mode `isPrefixOf` "update" = return |
| 55 | + (either (Just . Lines 0) (const o_expected) o_actual |
| 56 | + ,either (Just . Lines 0) (const e_expected) e_actual |
| 57 | + ,either (Numeric . show) (const x_expected) x_actual) |
| 58 | + | otherwise = fail "Unsupported argument for --actual option. Allowed: all, update, or a prefix thereof." |
| 59 | + |
| 60 | +printComments :: [String] -> IO () |
| 61 | +printComments = mapM_ (putStrLn . ("#"++)) -- TODO commentline discards leading space and comment character :( |
| 62 | + |
| 63 | +printStdin :: String -> Maybe String -> IO () |
| 64 | +printStdin _ (Just "") = return () |
| 65 | +printStdin _ Nothing = return () |
| 66 | +printStdin prefix (Just s) = printf "%s\n%s" prefix s |
| 67 | + |
| 68 | +printCommand :: String -> TestCommand -> IO () |
| 69 | +printCommand prefix (ReplaceableCommand s) = printf "%s%s\n" prefix s |
| 70 | +printCommand prefix (FixedCommand s) = printf "%s %s\n" prefix s |
| 71 | + |
| 72 | +printStdouterr :: String -> Maybe Matcher -> IO () |
| 73 | +printStdouterr _ Nothing = return () |
| 74 | +printStdouterr _ (Just (Lines _ "")) = return () |
| 75 | +printStdouterr _ (Just (Numeric _)) = fail "FATAL: Cannot handle Matcher (Numeric) for stdout/stderr." |
| 76 | +printStdouterr _ (Just (NegativeNumeric _)) = fail "FATAL: Cannot handle Matcher (NegativeNumeric) for stdout/stderr." |
| 77 | +printStdouterr prefix (Just (Lines _ s)) = printf "%s\n%s\n" prefix s -- TODO trailing \n ? |
| 78 | +printStdouterr prefix (Just regex) = printf "%s %s\n" prefix (show regex) |
| 79 | + |
| 80 | +printExitStatus :: String -> Matcher -> IO () |
| 81 | +printExitStatus _ (Numeric "0") = return () |
| 82 | +printExitStatus _ (Lines _ _) = fail "FATAL: Cannot handle Matcher (Lines) for exit status." |
| 83 | +printExitStatus prefix s = printf "%s %s\n" prefix (show s) |
| 84 | + |
| 85 | +mkEither :: Bool -> a -> Either a a |
| 86 | +mkEither True = Right |
| 87 | +mkEither False = Left |
| 88 | + |
| 89 | +fromEither :: Either a a -> a |
| 90 | +fromEither = either id id |
| 91 | + |
0 commit comments