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