Skip to content

Commit c7e29f4

Browse files
committed
Draft to print actual output
1 parent 27dd471 commit c7e29f4

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

src/Parse.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Preprocessor
1414
import System.IO hiding (stdin)
1515

1616

17-
parseFromFileWithPreprocessor :: (Parser [ShellTest]) -> PreProcessor -> FilePath -> IO (Either ParseError [ShellTest])
17+
parseFromFileWithPreprocessor :: Parser [ShellTest] -> PreProcessor -> FilePath -> IO (Either ParseError [ShellTest])
1818
parseFromFileWithPreprocessor p preproc fname =
1919
do
2020
h <- openFile fname ReadMode
@@ -82,7 +82,7 @@ shelltestfile = do
8282

8383
format1test = do
8484
ptrace_ " format1test 0"
85-
skipMany whitespaceorcommentline
85+
comments <- many whitespaceorcommentline
8686
ptrace_ " format1test 1"
8787
ln <- sourceLine <$> getPosition
8888
c <- command1 <?> "command line"
@@ -97,7 +97,7 @@ format1test = do
9797
ptrace " format1test x" x
9898
when (null (show c) && (isNothing i) && (null $ catMaybes [o,e]) && null (show x)) $ fail ""
9999
f <- sourceName . statePos <$> getParserState
100-
let t = ShellTest{testname=f,command=c,stdin=i,stdoutExpected=o,stderrExpected=e,exitCodeExpected=x,lineNumber=ln}
100+
let t = ShellTest{testname=f,command=c,stdin=i,stdoutExpected=o,stderrExpected=e,exitCodeExpected=x,lineNumber=ln,comments=comments}
101101
ptrace " format1test ." t
102102
return t
103103

@@ -151,7 +151,7 @@ format2testgroup inputRequiresDelimiter = do
151151
format2test :: Maybe String -> Parser ShellTest
152152
format2test i = do
153153
ptrace_ " format2test 0"
154-
skipMany whitespaceorcommentline
154+
comments <- many whitespaceorcommentline
155155
ptrace_ " format2test 1"
156156
ln <- sourceLine <$> getPosition
157157
c <- command2 <?> "command line"
@@ -166,7 +166,7 @@ format2test i = do
166166
ptrace " format2test x" x
167167
when (null (show c) && (isNothing i) && (null $ catMaybes [o,e]) && null (show x)) $ fail ""
168168
f <- sourceName . statePos <$> getParserState
169-
let t = ShellTest{testname=f,command=c,stdin=i,stdoutExpected=o,stderrExpected=e,exitCodeExpected=x,lineNumber=ln}
169+
let t = ShellTest{testname=f,command=c,stdin=i,stdoutExpected=o,stderrExpected=e,exitCodeExpected=x,lineNumber=ln,comments=comments}
170170
ptrace " format2test ." t
171171
return t
172172

@@ -249,7 +249,7 @@ format3testgroup inputRequiresDelimiter = do
249249
format3test :: Maybe String -> Parser ShellTest
250250
format3test i = do
251251
ptrace_ " format3test 0"
252-
skipMany whitespaceorcommentline
252+
comments <- many whitespaceorcommentline
253253
ptrace_ " format3test 1"
254254
ln <- sourceLine <$> getPosition
255255
c <- command3 <?> "command line"
@@ -264,7 +264,7 @@ format3test i = do
264264
ptrace " format3test x" x
265265
when (null (show c) && (isNothing i) && (null $ catMaybes [o,e]) && null (show x)) $ fail ""
266266
f <- sourceName . statePos <$> getParserState
267-
let t = ShellTest{testname=f,command=c,stdin=i,stdoutExpected=o,stderrExpected=e,exitCodeExpected=x,lineNumber=ln}
267+
let t = ShellTest{testname=f,command=c,stdin=i,stdoutExpected=o,stderrExpected=e,exitCodeExpected=x,lineNumber=ln,comments=comments}
268268
ptrace " format3test ." t
269269
return t
270270

src/Types.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ where
44
import Import
55
import Utils
66
import Text.Parsec
7-
7+
88
data ShellTest = ShellTest {
9-
command :: TestCommand
9+
comments :: [String] -- # COMMENTS OR BLANK LINES before test
10+
,command :: TestCommand
1011
,stdin :: Maybe String
1112
,stdoutExpected :: Maybe Matcher
1213
,stderrExpected :: Maybe Matcher

src/Utils.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,6 @@ replace old new = replace'
6666
else h : replace' ts
6767
len = length old
6868

69+
-- | Show a message, usage string, and terminate with exit status 1.
70+
warn :: String -> IO ()
71+
warn s = putStrLn s >> exitWith (ExitFailure 1)

src/shelltest.hs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import Import
2828
import Utils
2929
import Types
3030
import Parse
31+
import Print
3132
import Preprocessor
3233

3334

@@ -66,6 +67,8 @@ data Args = Args {
6667
,debug :: Bool
6768
,debug_parse :: Bool
6869
,testpaths :: [FilePath]
70+
,print_ :: Maybe String
71+
,actual :: Maybe String
6972
} deriving (Show, Data, Typeable)
7073

7174
argdefs = Args {
@@ -88,6 +91,8 @@ argdefs = Args {
8891
,debug = def &= help "Show debug info while running"
8992
,debug_parse = def &= help "Show test file parsing results and stop"
9093
,testpaths = def &= args &= typ "TESTFILES|TESTDIRS"
94+
,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). 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."
9196
}
9297
&= helpArg [explicit, name "help", name "h"]
9398
&= program progname
@@ -132,33 +137,41 @@ main = do
132137
when (debug args) $ printf "processing %d test files: %s\n" (length testfiles) (intercalate ", " testfiles)
133138
parseresults <- mapM (parseShellTestFile (debug args || debug_parse args) preprocessor) testfiles
134139

135-
-- run tests
136-
when (debug args) $ printf "running tests:\n"
140+
-- run tests / print
137141
unless (debug_parse args) $
138-
defaultMainWithArgs (concatMap (hUnitTestToTests . testFileParseToHUnitTest args) parseresults) tfopts
142+
if isJust $ print_ args
143+
then mapM_ (printShellTestsWithResults args) parseresults
144+
else do
145+
when (debug args) $ printf "running tests:\n"
146+
defaultMainWithArgs (concatMap (hUnitTestToTests . testFileParseToHUnitTest args) parseresults) tfopts
147+
148+
149+
printShellTestsWithResults :: Args -> Either ParseError [ShellTest] -> IO ()
150+
printShellTestsWithResults args (Right ts) = mapM_ (prepareShellTest args True) ts
151+
printShellTestsWithResults _ (Left e) = putStrLn $ "*** parse error in " ++ (sourceName $ errorPos e)
139152

140153
-- | Additional argument checking.
141154
checkArgs :: Args -> IO Args
142155
checkArgs args = do
143156
when (null $ testpaths args) $
144157
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."
160+
when (fromMaybe "v3" (print_ args) /= "v3") $
161+
warn "Currently, --print only supports test format v3."
145162
return args
146163

147-
-- | Show a message, usage string, and terminate with exit status 1.
148-
warn :: String -> IO ()
149-
warn s = putStrLn s >> exitWith (ExitFailure 1)
150-
151-
152164
-- running tests
153165

154166
testFileParseToHUnitTest :: Args -> Either ParseError [ShellTest] -> Test.HUnit.Test
155-
testFileParseToHUnitTest args (Right ts) = TestList $ map (shellTestToHUnitTest args) ts
167+
testFileParseToHUnitTest args (Right ts) = TestList $ map (\t -> testname t ~: prepareShellTest args False t) ts
156168
testFileParseToHUnitTest _ (Left e) = ("parse error in " ++ (sourceName $ errorPos e)) ~: (assertFailure :: (String -> IO ())) $ show e
157169

158-
shellTestToHUnitTest :: Args -> ShellTest -> Test.HUnit.Test
159-
shellTestToHUnitTest args ShellTest{testname=n,command=c,stdin=i,stdoutExpected=o_expected,
160-
stderrExpected=e_expected,exitCodeExpected=x_expected,lineNumber=ln} =
161-
n ~: do
170+
-- | Prepare test as IO action and optionally print it (as specified in args).
171+
prepareShellTest :: Args -> Bool -> ShellTest -> IO ()
172+
prepareShellTest args printTests st@ShellTest{testname=n,command=c,stdin=i,stdoutExpected=o_expected,
173+
stderrExpected=e_expected,exitCodeExpected=x_expected,lineNumber=ln} =
174+
do
162175
let e = with args
163176
cmd = case (e,c) of (_:_, ReplaceableCommand s) -> e ++ " " ++ dropWhile (/=' ') s
164177
(_, ReplaceableCommand s) -> s
@@ -191,6 +204,8 @@ shellTestToHUnitTest args ShellTest{testname=n,command=c,stdin=i,stdoutExpected=
191204
then ""
192205
else showExpectedActual args{diff=False} "exit code" x_expected (show x_actual)
193206
]
207+
when printTests $
208+
printShellTest (actual args) st (mkEither outputMatch o_actual) (mkEither errorMatch e_actual) (mkEither exitCodeMatch x_actual)
194209

195210
-- | Run a shell command line, passing it standard input if provided,
196211
-- and return the standard output, standard error output and exit code.

0 commit comments

Comments
 (0)