777edebb117acf0fc7ec85cca9a1c6671c78e957
[sandbox] / stutter_integration_tests.py
1 import os
2 import os.path
3 import subprocess
4 import unittest
5
6 CC = 'gcc-4.9'
7
8 FILE_PATH = os.path.dirname(os.path.realpath(__file__))
9 INTEGRATION_TEST_DIRECTORY_PATH = os.path.join(FILE_PATH, 'integration_tests')
10 os.chdir(FILE_PATH)
11
12 integration_test_file_names = [
13     fn
14     for fn in os.listdir(INTEGRATION_TEST_DIRECTORY_PATH) \
15     if fn.endswith('.stt')
16 ]
17
18 C_SOURCE_FILE_PATH = os.path.join(FILE_PATH, 'a.c')
19 BINARY_PATH = os.path.join(FILE_PATH, 'a.out')
20
21 def generate_test(input_file_name):
22     input_file_path = os.path.join(INTEGRATION_TEST_DIRECTORY_PATH, input_file_name)
23     expected_output_file_path = input_file_path[:-4] + '.txt'
24
25     def test(self):
26         c_source = subprocess.check_output(['python','stutter.py',input_file_path])
27
28         self.assertIsNotNone(c_source)
29         self.assertNotEqual(0, len(c_source))
30
31         with open(C_SOURCE_FILE_PATH,'wb') as c_source_file:
32             c_source_file.write(c_source)
33
34         cc_result = subprocess.call([CC, C_SOURCE_FILE_PATH])
35
36         self.assertEqual(0, cc_result)
37
38         with open(expected_output_file_path, 'rb') as expected_output_file:
39             expected_output = expected_output_file.read()
40
41         actual_output = subprocess.check_output(BINARY_PATH)
42
43         self.assertEqual(expected_output, actual_output)
44
45     return test
46
47 class IntegrationTests(unittest.TestCase):
48     def tearDown(self):
49         try:
50             os.remove(C_SOURCE_FILE_PATH)
51
52         except OSError as e:
53             if e.errno != errno.ENOENT:
54                 raise
55
56         try:
57             os.remove(BINARY_PATH)
58
59         except OSError as e:
60             if e.errno != errno.ENOENT:
61                 raise
62
63 for integration_test_file_name in integration_test_file_names:
64     test = generate_test(integration_test_file_name)
65     test_name = 'test_{}'.format(integration_test_file_name[:-4])
66     setattr(IntegrationTests, test_name, test)
67
68 unittest.main()