vanilla-test.T 11.2 KB
Newer Older
1
2
3

-----------------------------------------------------------------------
--- Stuff to do with simple single-source-file tests.  We assume    ---
4
--- that the name of the test is to be used as the basename         ---
5
6
7
--- for everything.                                                 ---
-----------------------------------------------------------------------

8
9
10
11
12
13
---------------------------------------------------------------
--- UTILITY FNs                                             ---
---------------------------------------------------------------

include ($confdir ++ "/" ++ $conffilename)

14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- Gotta do some pretty basic stuff :)
def not ( $_bool )
{
        if $_bool == "True" then return False
   else if $_bool == "False" then return True
   else framefail ("not(): invalid input: " ++ $_bool )
   fi fi
}

-- (eg) "run.stdout"  -->  "testdir/testname.run.stdout"
def qualify ( $_filename_frag )
{
   if   $_filename_frag == ""
   then
28
      return $testdir ++ "/" ++ $testname
29
   else
30
      return $testdir ++ "/" ++ $testname ++ "." ++ $_filename_frag
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
   fi
}

-- Delete a file and abort if that doesn't work.
def rm_or_fail ( $_files )
{
   $cmd = "rm -f " ++ $_files
   $res = run $cmd
   if $res /= "0" then framefail ("rm_or_fail: can't rm: " ++ $_files) fi
}

-- Delete a file but keep going antidisirregardless of the outcome.
def rm_nofail ( $_files )
{
   $cmd = "rm -f " ++ $_files
   $res = run $cmd
}

49
-- Pipe an error message through normalise_errmsg.
50
51
52
53
54
55
56
def normalise_errmsg ( $errmsg )
{
   $unpathify = $confdir ++ "/../../utils/normalise_errmsg/normalise_errmsg"
   $normd = $errmsg | $unpathify
   return $normd
}

57
58
59
60
-- Give hints as to why a test is failing.
def say_fail_because_noteq ( $filename1, $filename2 )
{
   print "--- FAIL because the following files differ:"
61
62
   print "---      " ++ $filename1
   print "---      " ++ $filename2
63
}
64

65
66
67
def say_fail_because_nonempty ( $filename1 )
{
   print "--- FAIL because the following file is non-empty:"
68
   print "---      " ++ $filename1
69
70
71
72
73
74
}

def say_fail_because_compiler_barfd ( $res )
{
   print "--- FAIL because the compiler returned non-zero exit code = " ++ $res
}
75
76
77
78
79

---------------------------------------------------------------
--- COMPILATION                                             ---
---------------------------------------------------------------

80
81
82
83
-- Clean up prior to the test, so that we can't spuriously conclude
-- that it passed on the basis of old run outputs.
def pretest_cleanup()
{
84
   rm_nofail(qualify("comp.stderr"))
85
86
87
88
89
90
91
92
93
94
95
96
97
   rm_nofail(qualify("run.stderr"))
   rm_nofail(qualify("run.stdout"))
   -- simple_build_Main zaps the following:
   -- rm_nofail(qualify("o"))
   -- rm_nofail(qualify(""))
   -- not interested in the return code
}

-- Guess flags suitable for the compiler.
def guess_compiler_flags()
{
   if   $tool contains "ghc"
   then 
98
        return "-no-recomp -dcore-lint"
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
   else 
   if   $tool contains "nhc"
   then 
        return "-an-nhc-specific-flag"
   else
   if   $tool contains "hbc"
   then
        return ""
   else
        framefail ("Can't guess what kind of Haskell compiler " ++ 
                   "you're testing: $tool = " ++ $tool)
   fi
   fi
   fi
}

115
116
-- Build Main, and return the compiler result code.  Compilation
-- output goes into testname.comp.stderr.
117

118
def simple_build_Main_WRK ( $_extra_args, $compile_only ) 
119
120
121
122
123
124
125
126
{
   $flags = guess_compiler_flags()
   $errname = qualify("comp.stderr")
   $exename = qualify("")  -- ie, the exe name == the test name
   $srcname = qualify("hs")
   rm_or_fail($errname)
   rm_or_fail($exename)
   $cmd = $tool ++ " " ++ $flags ++ " " ++ $_extra_args
127
128
129
130
          ++ if   $compile_only 
             then " -c " 
             else " -o " ++ $exename ++ " " 
             fi
131
132
          ++ $srcname ++ " &> " ++ $errname
   $res = run $cmd
133
134
135
136
137
138
139
140
141
   return $res
}

-- Compile testname.hs into testname; comp errors -> testname.comp.stderr.
-- Used for run tests, so framefail if compilation fails.

def simple_build_Main ( $_extra_args ) 
{
   $res = simple_build_Main_WRK ( $_extra_args, False )
142
143
144
145
   if $res /= "0" then framefail "simple_build_Main: failed" fi
}


146
147
148
149
---------------------------------------------------------------
--- RUNNING, AND ASSESSING RUN RESULTS                      ---
---------------------------------------------------------------

150
151
152
153
-- Run testname.  If testname.stdin exists, route input from that, else
-- from /dev/null.  Route output to testname.run.stdout and 
-- testname.run.stderr.  Returns the exit code of the run.

154
def simple_run_main( $_extra_args )
155
156
157
158
159
160
161
162
163
164
165
{
   $devnull = "/dev/null"
   $spec_stdin = qualify("stdin")
   $run_stdout = qualify("run.stdout")
   $run_stderr = qualify("run.stderr")
   $use_stdin
      = if exists($spec_stdin) then $spec_stdin else $devnull fi

   rm_or_fail($run_stdout)
   rm_or_fail($run_stderr)
   $cmd = qualify("") 
166
          ++ " " ++ $_extra_args
167
168
169
170
171
172
173
174
175
176
177
          ++ " < " ++ $use_stdin
          ++ " > " ++ $run_stdout
          ++ " 2> " ++ $run_stderr
   $res = run $cmd
   return $res
}


-- returns True if both files exist and are identical.
def exist_and_same ( $_file1, $_file2 )
{
178
   if   not(exists($_file1) && exists($_file2))
179
180
   then return False
   else print "vanilla-test: comparing " ++ $_file1 ++ " and " ++ $_file2
181
182
183
        $cts1 = contents($_file1)
        $cts2 = contents($_file2)
        $same = $cts1 == $cts2
184
        if not($same) then say_fail_because_noteq($_file1, $_file2) fi
185
        return $same
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
   fi
}


-- Check that the run.stdout file matches at least one spec'd stdout.
def check_stdout_ok()
{
   $r_stdout  = qualify("run.stdout")
   $s_stdout  = qualify("stdout")
   $s1_stdout = qualify("stdout1")
   $s2_stdout = qualify("stdout2")
   $s3_stdout = qualify("stdout3")
   $s4_stdout = qualify("stdout4")
   $sm_stdout = qualify("stdout-mingw")

   -- check for minimal level of sanity
   if   not ( exists($s_stdout) && exists($r_stdout) )
203
   then framefail "vanilla-test: .stdout and/or .run.stdout are missing"
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
   fi

   return
      exist_and_same($s_stdout, $r_stdout)
      || exist_and_same($s1_stdout, $r_stdout)
      || exist_and_same($s2_stdout, $r_stdout)
      || exist_and_same($s3_stdout, $r_stdout)
      || exist_and_same($s4_stdout, $r_stdout)
      || exist_and_same($sm_stdout, $r_stdout)
}


-- If there's any spec'd stderr files, check that the run.stderr matches it.
-- Check that the run.stdout file matches at least one spec'd stdout.
def check_stderr_ok()
{
   $r_stderr  = qualify("run.stderr")
   $s_stderr  = qualify("stderr")
   $s1_stderr = qualify("stderr1")
   $s2_stderr = qualify("stderr2")
   $s3_stderr = qualify("stderr3")
   $s4_stderr = qualify("stderr4")
   $sm_stderr = qualify("stderr-mingw")

   -- if it doesn't look like there's any stderr to be seen, it's OK.
   if not ( exists($s_stderr) ) then return True fi

   return
      exist_and_same($s_stderr, $r_stderr)
      || exist_and_same($s1_stderr, $r_stderr)
      || exist_and_same($s2_stderr, $r_stderr)
      || exist_and_same($s3_stderr, $r_stderr)
      || exist_and_same($s4_stderr, $r_stderr)
      || exist_and_same($sm_stderr, $r_stderr)
}


241
242
243
244
245
246
---------------------------------------------------------------
--- CONDUCTING A COMPLETE TEST                              ---
---------------------------------------------------------------

-- Compile and run (should_run) style test

247
248
249
def vanilla-test-actions ( $extra_compile_args, 
                           $extra_run_args, 
                           $allowable_nonzero_exit_code )
250
251
{
   pretest_cleanup()
252
253
254
   simple_build_Main( $extra_compile_args )
   $res = simple_run_main( $extra_run_args )
   if   $res /= "0" && $res /= $allowable_nonzero_exit_code
255
256
257
   then print "vanilla-test: unexpected exit code (" ++ $res 
              ++ ") from testee"
        return False
258
   fi
259
   $test_passed = check_stdout_ok() && check_stderr_ok()
260
   return $test_passed
261
262
}

263
264

-- Compile only (should_compile) style test.  Deemed to have
265
266
-- succeeded if the compiler returned zero AND (testname.comp.stderr
-- matches testname.stderr, if it exists, or is empty).
267
268
269
270
271

def vanilla-comp-test-actions ( $extra_compile_args )
{
   pretest_cleanup()
   $res = simple_build_Main_WRK ( $extra_compile_args, True )
272
273

   -- If the compiler barf'd, fail.
274
275
276
277
   if   $res /= "0" 
   then say_fail_because_compiler_barfd ( $res )
        return False 
   fi
278

279
280
281
   -- If there's an expected .stderr, presumably containing
   -- warnings, ensure the compiler produced the same.
   $actual_stderr   = qualify("comp.stderr")
282
   $expected_stderr = qualify("stderr")
283
284
285
286
287
288
289
   if   exists($expected_stderr)
   then $stderr_a = normalise_errmsg(contents($actual_stderr))
        $stderr_e = normalise_errmsg(contents($expected_stderr))
        if   $stderr_e /= $stderr_a
        then print ( "-- UNMATCHED WARNING MSGS: EXPECTED\n" ++ $stderr_e ++
                     "-- ACTUAL\n" ++ $stderr_a ++ "-- END\n" )
             return False
290
        else return True
291
        fi
292
293
294
295
   fi

   -- There's no expected stderr, so just insist that the compiler
   -- produced nothing on stderr.
296
297
298
299
   if   contents($actual_stderr) /= "" 
   then say_fail_because_nonempty($actual_stderr)
        return False
   fi
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

   -- Must have succeeded.
   return True
}


-- Compile with expected fail (should_fail) style test.  Deemed to have
-- succeeded if the compiler returned nonzero AND testname.comp.stderr
-- equals testname.stderr.

def vanilla-compfail-test-actions ( $extra_compile_args )
{
   pretest_cleanup()
   $expected_stderr = qualify("stderr")

   -- Sanity check
   if   not(exists($expected_stderr)) 
   then framefail "should_fail: expected .stderr is missing" 
   fi

   $res = simple_build_Main_WRK ( $extra_compile_args, True )

   $stderr_a = normalise_errmsg(contents(qualify("comp.stderr")))
   $stderr_e = normalise_errmsg(contents($expected_stderr))

   if $stderr_e /= $stderr_a
   then print ( "-- UNMATCHED ERROR MSGS: EXPECTED\n" ++ $stderr_e ++
                "-- ACTUAL\n" ++ $stderr_a ++ "-- END\n" )
   fi

   if $res /= "0" && $stderr_e == $stderr_a
331
332
   then
     return True
333
334
   else
     return False
335
336
337
338
339
340
341
342
343
   fi
}


---------------------------------------------------------------
--- TOP-LEVEL FNS                                           ---
---------------------------------------------------------------

--------------------------------------------------------------
344
-- top-level
345
346
-- Compile and run (should_run) style test

347
348
349
350
351
352
353
354
355
356
357
358
def vt ( $extra_compile_args, 
         $extra_run_args, 
         $allowable_nonzero_exit_code )
{
   $test_passed 
      = vanilla-test-actions ( $extra_compile_args, 
                               $extra_run_args, 
                               $allowable_nonzero_exit_code )
   expect pass
   pass when $test_passed
   fail when otherwise
}
359

360
361
362
363
364
365
366
367
368
369
370
371

-- Compile only (should_compile) style test

def vtc ( $extra_compile_args )
{
   $test_passed 
      = vanilla-comp-test-actions ( $extra_compile_args )
   expect pass
   pass when $test_passed
   fail when otherwise
}

372
373
374
375
376
377
378
379
380
381
382
383
384
385

-- Compile only, and expect failure (should_fail) style test

def vtf ( $extra_compile_args )
{
   $test_passed 
      = vanilla-compfail-test-actions ( $extra_compile_args )
   expect pass
   pass when $test_passed
   fail when otherwise
}



386
387
388
-----------------------------------------------------------------------
--- end                                              vanilla-test.T ---
-----------------------------------------------------------------------