-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunner.hs
More file actions
186 lines (137 loc) · 5.28 KB
/
runner.hs
File metadata and controls
186 lines (137 loc) · 5.28 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{-# LANGUAGE RecordWildCards #-}
import System.IO
import System.Exit
import System.Environment
import Control.Monad
import Development.Shake hiding ((*>))
import Development.Shake.Command
import Development.Shake.FilePath
import Development.Shake.Util
import Text.Parsec hiding (parseTest)
import Text.Parsec.Char
import Text.Regex.Base
import Text.Regex.TDFA
import Data.Text (Text, pack, unpack, strip)
import Data.String.AnsiEscapeCodes.Strip.Text
type TestPath = FilePath
build = "_build"
data Expect = ExpectOK | ExpectFail String deriving Show
data Test = Test
{ name :: String
, files :: [(String, String)] -- filename , contents
, javac :: Expect
, statix :: Expect
} deriving (Show)
-- entrypoint
main = do
[scriptDir, testFile] <- getArgs
rules scriptDir testFile
-- Parser for test files
parseName = many1 (satisfy $ \ c -> c /= ']')
discard :: Parsec Text u a -> Parsec Text u ()
discard p = do
p
return ()
header = do
between (char '[') (char ']') parseName
parseFile = do
filename <- spaces *> header
let endOfFile = try $ lookAhead (discard header <|> eof)
lines <- manyTill (manyTill anyChar newline) endOfFile
return (filename, unlines lines)
parseExpectation :: Parsec Text u Expect
parseExpectation =
(try $ spaces >> string "ok" <* newline >> return ExpectOK)
<|> (spaces >> ExpectFail <$> (string "fail" *> manyTill anyChar newline))
parseTest :: String -> Parsec Text u Test
parseTest name = do
stxExp <- string "STATIX" >> parseExpectation
javExp <- string "JAVAC" >> parseExpectation
files <- many1 parseFile
eof
return $ Test name files javExp stxExp
where
stripStr = unpack . strip . pack
-- auxiliary defs
checkJavaExpectation :: Expect -> ExitCode -> String -> Bool
checkJavaExpectation ExpectOK code sout = code == ExitSuccess
checkJavaExpectation (ExpectFail er) code sout =
case code of
ExitSuccess -> False
ExitFailure erno -> sout =~ (".*" <> er <> ".*")
checkStxExpectation :: Expect -> ExitCode -> String -> Bool
checkStxExpectation ExpectOK code sout = code == ExitSuccess
checkStxExpectation (ExpectFail er) code sout =
case code of
ExitSuccess -> False
ExitFailure erno -> sout =~ (".*" <> er <> ".*")
resultString :: Bool -> String
resultString ok = if ok then "SUCCESS" else "FAILURE"
-- rules
rules :: FilePath -> TestPath -> IO ()
rules scriptDir testPath = do
-- some directories
let specDir = scriptDir </> ".." </> "src"
let sunshine = scriptDir </> ".." </> "bin/org.metaborg.sunshine2-2.5.11.jar"
let langDir = scriptDir </> ".." </> "lib/java.spfx/lang.java"
let buildDir = build </> testPath
-- parse the test file
txt <- pack <$> readFile testPath
Test{..} <- either (\err -> die $ "Couldn't parse test file: " ++ show err) return
$ parse (parseTest testPath) testPath txt
-- write the java files if they've changed
forM_ files $ \(name, txt) -> do
writeFileChanged (buildDir </> name) txt
shake shakeOptions{ shakeFiles = buildDir, shakeChange = ChangeDigest } $ do
let javaFiles = [ buildDir </> j | (j, _) <- files ]
let aterms = [ buildDir </> (j -<.> "aterm") | (j, _) <- files ]
let result = buildDir </> "result"
want [ result ]
-- auxiliary file targets
-------------------------
"//*.jav" %> \out -> do
let input = out -<.> "java"
need [ input ]
cmd_ "cp" input out
"//*.aterm" %> \out -> do
let jav = out -<.> "jav"
need [ jav ]
cmd_ "java -jar" sunshine
"transform -n" [ "Explicate injections"] "-l"
langDir "-p . -i" jav
-- toplevel targets
-------------------
result %> \out -> do
alwaysRerun
stxRes <- readFile' $ buildDir </> "stx.result"
javaRes <- readFile' $ buildDir </> "java.result"
let res = resultString $ stxRes == "SUCCESS" && javaRes == "SUCCESS"
writeFile' out res
liftIO $ putStrLn $ "[" <> res <> "] " <> testPath
[ buildDir </> "java.out" , buildDir </> "java.result" ] &%> \[out, res] -> do
-- Make sure to depend on the main input
-- because Shake doesn't support dynamic dependencies
need [ testPath ]
need javaFiles
-- run java on the inputs
(Exit code, Stdouterr sout) <- withVerbosity Verbose $
cmd "javac -d" buildDir javaFiles
writeFileChanged out $ sout
let result = resultString (checkJavaExpectation javac code sout)
writeFile' res result
liftIO $ putStrLn $ "[JAVA:" <> result <> "] " <> testPath
[ buildDir </> "stx.out" , buildDir </> "stx.result" ] &%> \[out, res] -> do
-- Make sure to depend on the main input
need [ testPath ]
need aterms
-- depend on the entire spec
getDirectoryFiles specDir ["//*.mstx"] >>= \mf -> do
need [ specDir </> f | f <- mf ]
-- run statix on all the aterms
(Exit code, Stdouterr sout) <- withVerbosity Diagnostic $
cmd "statix check -I" [specDir] "java" aterms
let sout' = unpack $ stripAnsiEscapeCodes (pack sout)
writeFileChanged out $ sout'
let result = resultString (checkStxExpectation statix code sout')
writeFile' res result
liftIO $ putStrLn $ "[STATIX:" <> result <> "] " <> testPath