-
Notifications
You must be signed in to change notification settings - Fork 1
/
Test.hs
126 lines (111 loc) · 3.93 KB
/
Test.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-----------------------------------------------------------------------------
-- |
-- Module : Main
-- Copyright : Copyright (c) 2008 Igor Boehm - Bytelabs.org. All rights reserved.
-- License : BSD-style (see the file LICENSE)
-- Author : Igor Boehm <igor@bytelabs.org>
--
-- Run hburg on all test grammars that are available in order to spot regressions.
-- These tests are very basic and only check HBURGs exit status.
-----------------------------------------------------------------------------
module Main (main) where
{- unqualified imports -}
import System.Console.GetOpt
import System.Directory
import System.Environment
import System.Exit
import Data.List
import Control.Exception
{- qualified imports -}
import qualified Distribution.Simple as Cabal (defaultMainArgs)
import qualified System.Process as Cmd (system)
-----------------------------------------------------------------------------
type Suffix = String
type Grammar = String
seperator = take 80 $ repeat '#'
-- | main.
main :: IO ()
main = do
args <- getArgs
case args of
[] -> usage
xs | not $ null $ filter (\x -> "help" `isSuffixOf` x || "?" `isSuffixOf` x) xs
-> usage
_ -> do
build args
if (not $ null $ filter (isSuffixOf "test") args)
then do
-- run valid grammars
good <- inputFiles "test/" ".tpg"
resultGood <- runTests good ExitSuccess
-- run grammars that should trigger errors
bad <- inputFiles "test/errors/" ".tpg"
resultBad <- runTests bad (ExitFailure 1)
putStrLn $ seperator ++ "\n"
-- print out summary
let results = resultBad ++ resultGood
if (null results)
then
-- success case
putStrLn $ (show . length $ good ++ bad) ++ " Tests Successful!\n"
else
-- some tests failed
do
putStrLn $ (show . length $ results) ++ " of " ++
(show . length $ good ++ bad) ++ " Tests Failed!\n"
mapM_ (\r -> putStrLn $ "Failed Test: " ++ r) results
putStr "\n"
else
return ()
-- | Retrieve hburg input files
inputFiles :: FilePath -> Suffix -> IO [String]
inputFiles path sfx = do
contents <- getDirectoryContents path
`catch` (\e -> do {putStrLn . show $ (e::IOException); return []})
return $ map (path ++) $ filter (isSuffixOf sfx) contents
-- | Build HBURG by running specified Cabal targets
build :: [String] -> IO ()
build args =
let tasks = filter
(`elem` ["configure", "build", "clean"])
args
in
mapM_ (\t -> Cabal.defaultMainArgs [t]) tasks
-- | Print usage
usage :: IO ()
usage = do
prog <- getProgName
putStrLn $ "Usage: runghc "++ prog ++" (clean|configure|build|test|help|?)"
-- | Setup directory for tests
setupTest :: IO ()
setupTest = do
createDirectoryIfMissing True "test/target"
`catch` (\e -> do {putStrLn . show $ (e::IOException)})
-- | Cleanup after test
cleanUpTest :: IO ()
cleanUpTest = do
removeDirectoryRecursive "test/target"
`catch` (\e -> do {putStrLn . show $ (e::IOException)})
-- | Run our test case
runTest :: ExitCode -> Grammar -> IO (ExitCode, String)
runTest code gram =
let cmd = "dist/build/hburg/hburg -p test.target -l Java " ++ gram in
do
setupTest
putStrLn seperator
putStrLn $ "Test: " ++ cmd
putStrLn $ "Output Start>>"
exitCode <- Cmd.system cmd
`catch` (\e -> do {
putStrLn . show $ (e::IOException);
return (ExitFailure 2)})
cleanUpTest
putStrLn "<<Output End"
putStrLn $ "Successful? " ++ show (exitCode == code)
return (exitCode, cmd)
-- | Run a list of test cases
runTests :: [Grammar] -> ExitCode -> IO [String]
runTests grams code = do
retVal <- mapM (runTest code) grams
return (map (snd) $ filter (\r -> code /= fst r) retVal)
-----------------------------------------------------------------------------