Clean up after integration tests
[fur] / integration_tests.py
1 import os
2 import os.path
3 import subprocess
4 import unittest
5
6 # Go to the directory of the current file so we know where we are in the filesystem
7 os.chdir(os.path.dirname(os.path.abspath(__file__)))
8
9 class OutputTests(unittest.TestCase):
10     pass
11
12 def add_example_output_test(filename):
13     def test(self):
14         compile_fur_to_c_result = subprocess.call([
15             'python',
16             'main.py',
17             os.path.join('examples', filename),
18         ])
19
20         if compile_fur_to_c_result != 0:
21             raise Exception('Example "{}" did not compile'.format(filename))
22
23         compile_c_to_executable_result = subprocess.call([
24             'gcc',
25             os.path.join('examples', filename + '.c'),
26         ])
27
28         if compile_c_to_executable_result != 0:
29             raise Exception('Example output "{}" did not compile'.format(filename + '.c'))
30
31         try:
32             actual_output = subprocess.check_output(['./a.out'])
33
34             with open(os.path.join('examples', filename + '.output.txt'), 'rb') as f:
35                 expected_output = f.read()
36
37             self.assertEqual(expected_output, actual_output)
38
39             # We don't clean up the C file in the finally clause because it can be useful to have in case of errors
40             os.remove(os.path.join('examples', filename + '.c'))
41
42         finally:
43             try:
44                 os.remove('a.out')
45             except OSError:
46                 pass
47
48     setattr(OutputTests, 'test_' + filename, test)
49
50 filenames = (
51     entry.name
52     for entry in os.scandir('examples')
53     if entry.is_file()
54     if entry.name.endswith('.fur')
55 )
56
57 for filename in filenames:
58     add_example_output_test(filename)
59
60 unittest.main()