From f3ec04c8a168e1c8a483019a6bc1f82f7516765b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Sch=C3=B6ttl?= Date: Mon, 24 Aug 2020 23:07:37 +0200 Subject: [PATCH 1/3] Revert "Remove --actual" This reverts commit 531c67fe88b10bfa551e52d035afe79b45d2b670. --- src/Print.hs | 46 +++++++++++++++++++++++++++++++++++++++------- src/shelltest.hs | 6 +++++- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/Print.hs b/src/Print.hs index f8f8c0e..e13bf31 100644 --- a/src/Print.hs +++ b/src/Print.hs @@ -4,22 +4,29 @@ where import Import import Types --- | Print a shell test. See CLI documentation for details. +-- TODO Print output depending on --print=FORMAT (currently only v3) +-- | Print a shell test considering the @--actual=mode@ option. See CLI +-- documentation for details on. printShellTest :: String -- ^ Shelltest format. Value of option @--print[=FORMAT]@. + -> Maybe String -- ^ Value of option @--actual[=MODE]@. @Nothing@ if option is not given. -> ShellTest -- ^ Test to print + -> Either String String -- ^ Non-matching or matching exit status + -> Either String String -- ^ Non-matching or matching exit status + -> Either Int Int -- ^ Non-matching or matching exit status -> IO () -printShellTest format ShellTest{command=c,stdin=i,comments=comments,trailingComments=trailingComments, +printShellTest format actualMode ShellTest{command=c,stdin=i,comments=comments,trailingComments=trailingComments, stdoutExpected=o_expected,stderrExpected=e_expected,exitCodeExpected=x_expected} - = do + o_actual e_actual x_actual = do + (o,e,x) <- computeResults actualMode case format of "v1" -> do printComments comments printCommand "" c printStdin "<<<" i - printStdouterr ">>>" o_expected - printStdouterr ">>>2" e_expected - printExitStatus True ">>>=" x_expected + printStdouterr ">>>" $ justMatcherOutErr o + printStdouterr ">>>2" $ justMatcherOutErr e + printExitStatus True ">>>=" x printComments trailingComments "v2" -> do printComments comments @@ -38,6 +45,20 @@ printShellTest format ShellTest{command=c,stdin=i,comments=comments,trailingComm printExitStatus False ">=" x_expected printComments trailingComments _ -> fail $ "Unsupported --print format: " ++ format + where + computeResults :: Maybe String -> IO (Maybe Matcher, Maybe Matcher, Matcher) + computeResults Nothing = do + return (o_expected, e_expected, x_expected) + computeResults (Just mode) + | mode `isPrefixOf` "all" = return + (Just $ Lines 0 $ fromEither o_actual -- TODO what about 0? how is it in parser? + ,Just $ Lines 0 $ fromEither e_actual + ,Numeric $ show $ fromEither x_actual) + | mode `isPrefixOf` "update" = return + (either (Just . Lines 0) (const o_expected) o_actual + ,either (Just . Lines 0) (const e_expected) e_actual + ,either (Numeric . show) (const x_expected) x_actual) + | otherwise = fail "Unsupported argument for --actual option. Allowed: all, update, or a prefix thereof." printComments :: [String] -> IO () printComments = mapM_ putStrLn @@ -56,7 +77,7 @@ printStdouterr _ Nothing = return () printStdouterr _ (Just (Lines _ "")) = return () printStdouterr _ (Just (Numeric _)) = fail "FATAL: Cannot handle Matcher (Numeric) for stdout/stderr." printStdouterr _ (Just (NegativeNumeric _)) = fail "FATAL: Cannot handle Matcher (NegativeNumeric) for stdout/stderr." -printStdouterr prefix (Just (Lines _ s)) = printf "%s\n%s" prefix s +printStdouterr prefix (Just (Lines _ s)) = printf "%s\n%s\n" prefix s -- TODO trailing \n ? printStdouterr prefix (Just regex) = printf "%s %s\n" prefix (show regex) -- | Print exit status. First arg says 'alwaysPrintEvenIfZero'. @@ -65,3 +86,14 @@ printExitStatus _ _ (Lines _ _) = fail "FATAL: Cannot handle Matcher (Lines) for printExitStatus False _ (Numeric "0") = return () printExitStatus True prefix (Numeric "0") = printf "%s 0\n" prefix printExitStatus _ prefix s = printf "%s %s\n" prefix (show s) + +mkEither :: Bool -> a -> Either a a +mkEither True = Right +mkEither False = Left + +fromEither :: Either a a -> a +fromEither = either id id + +-- | Make a Matcher out of Nothing. +justMatcherOutErr :: Maybe Matcher -> Maybe Matcher +justMatcherOutErr = Just . fromMaybe (Lines 0 "") diff --git a/src/shelltest.hs b/src/shelltest.hs index 0a9e40f..05ba4c8 100644 --- a/src/shelltest.hs +++ b/src/shelltest.hs @@ -68,6 +68,7 @@ data Args = Args { ,debug_parse :: Bool ,testpaths :: [FilePath] ,print_ :: Maybe String + ,actual :: Maybe String } deriving (Show, Data, Typeable) argdefs = Args { @@ -91,6 +92,7 @@ argdefs = Args { ,debug_parse = def &= help "Show test file parsing results and stop" ,testpaths = def &= args &= typ "TESTFILES|TESTDIRS" ,print_ = def &= typ "FORMAT" &= opt "v3" &= groupname "Print test file" &= help "Print test files in specified format (default: v3)." + ,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." } &= helpArg [explicit, name "help", name "h"] &= program progname @@ -153,6 +155,8 @@ checkArgs :: Args -> IO Args checkArgs args = do when (null $ testpaths args) $ warn $ printf "Please specify at least one test file or directory, eg: %s tests" progname + when (isJust (actual args) && not (isJust (print_ args))) $ + warn "Option --actual can only be used with --print." return args -- running tests @@ -184,7 +188,7 @@ prepareShellTest args st@ShellTest{testname=n,command=c,stdin=i,stdoutExpected=o let errorMatch = maybe True (e_actual `matches`) e_expected let exitCodeMatch = show x_actual `matches` x_expected case print_ args of - Just format -> printShellTest format st + Just format -> printShellTest format (actual args) st (mkEither outputMatch o_actual) (mkEither errorMatch e_actual) (mkEither exitCodeMatch x_actual) Nothing -> if (x_actual == 127) -- catch bad executable - should work on posix systems at least 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 else assertString $ concat $ filter (not . null) [ From 15c4d45ede1adbc7af5e1d2ca401dcb1dc69fc9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Sch=C3=B6ttl?= Date: Mon, 24 Aug 2020 23:15:11 +0200 Subject: [PATCH 2/3] Fix comments --- src/Print.hs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Print.hs b/src/Print.hs index e13bf31..1737a52 100644 --- a/src/Print.hs +++ b/src/Print.hs @@ -4,7 +4,6 @@ where import Import import Types --- TODO Print output depending on --print=FORMAT (currently only v3) -- | Print a shell test considering the @--actual=mode@ option. See CLI -- documentation for details on. printShellTest @@ -51,7 +50,7 @@ printShellTest format actualMode ShellTest{command=c,stdin=i,comments=comments,t return (o_expected, e_expected, x_expected) computeResults (Just mode) | mode `isPrefixOf` "all" = return - (Just $ Lines 0 $ fromEither o_actual -- TODO what about 0? how is it in parser? + (Just $ Lines 0 $ fromEither o_actual ,Just $ Lines 0 $ fromEither e_actual ,Numeric $ show $ fromEither x_actual) | mode `isPrefixOf` "update" = return @@ -77,7 +76,7 @@ printStdouterr _ Nothing = return () printStdouterr _ (Just (Lines _ "")) = return () printStdouterr _ (Just (Numeric _)) = fail "FATAL: Cannot handle Matcher (Numeric) for stdout/stderr." printStdouterr _ (Just (NegativeNumeric _)) = fail "FATAL: Cannot handle Matcher (NegativeNumeric) for stdout/stderr." -printStdouterr prefix (Just (Lines _ s)) = printf "%s\n%s\n" prefix s -- TODO trailing \n ? +printStdouterr prefix (Just (Lines _ s)) = printf "%s\n%s" prefix s printStdouterr prefix (Just regex) = printf "%s %s\n" prefix (show regex) -- | Print exit status. First arg says 'alwaysPrintEvenIfZero'. From 312ad3c4259b6e750416f377851338cc8939a702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Sch=C3=B6ttl?= Date: Mon, 24 Aug 2020 23:24:53 +0200 Subject: [PATCH 3/3] Bugfix and fix comments --- src/Print.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Print.hs b/src/Print.hs index 1737a52..416cc05 100644 --- a/src/Print.hs +++ b/src/Print.hs @@ -10,8 +10,8 @@ printShellTest :: String -- ^ Shelltest format. Value of option @--print[=FORMAT]@. -> Maybe String -- ^ Value of option @--actual[=MODE]@. @Nothing@ if option is not given. -> ShellTest -- ^ Test to print - -> Either String String -- ^ Non-matching or matching exit status - -> Either String String -- ^ Non-matching or matching exit status + -> Either String String -- ^ Non-matching or matching stdout + -> Either String String -- ^ Non-matching or matching stderr -> Either Int Int -- ^ Non-matching or matching exit status -> IO () printShellTest format actualMode ShellTest{command=c,stdin=i,comments=comments,trailingComments=trailingComments,