Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 1 | import unittest |
| 2 | from test import support |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 3 | import os |
| 4 | import sys |
Steve Dower | 28f6cb3 | 2019-01-22 10:49:52 -0800 | [diff] [blame] | 5 | import sysconfig |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 6 | import subprocess |
| 7 | |
| 8 | |
| 9 | SYMBOL_FILE = support.findfile('symbol.py') |
Serhiy Storchaka | 8ac6581 | 2018-12-22 11:18:40 +0200 | [diff] [blame] | 10 | GEN_SYMBOL_FILE = os.path.join(os.path.dirname(__file__), |
| 11 | '..', '..', 'Tools', 'scripts', |
| 12 | 'generate_symbol_py.py') |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 13 | GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), |
| 14 | '..', '..', 'Include', 'graminit.h') |
| 15 | TEST_PY_FILE = 'symbol_test.py' |
| 16 | |
| 17 | |
| 18 | class TestSymbolGeneration(unittest.TestCase): |
| 19 | |
| 20 | def _copy_file_without_generated_symbols(self, source_file, dest_file): |
Victor Stinner | f08fea9 | 2015-09-02 14:23:40 +0200 | [diff] [blame] | 21 | with open(source_file) as fp: |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 22 | lines = fp.readlines() |
Victor Stinner | f08fea9 | 2015-09-02 14:23:40 +0200 | [diff] [blame] | 23 | with open(dest_file, 'w') as fp: |
| 24 | fp.writelines(lines[:lines.index("#--start constants--\n") + 1]) |
| 25 | fp.writelines(lines[lines.index("#--end constants--\n"):]) |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 26 | |
| 27 | def _generate_symbols(self, grammar_file, target_symbol_py_file): |
| 28 | proc = subprocess.Popen([sys.executable, |
Serhiy Storchaka | 8ac6581 | 2018-12-22 11:18:40 +0200 | [diff] [blame] | 29 | GEN_SYMBOL_FILE, |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 30 | grammar_file, |
| 31 | target_symbol_py_file], stderr=subprocess.PIPE) |
| 32 | stderr = proc.communicate()[1] |
| 33 | return proc.returncode, stderr |
| 34 | |
Victor Stinner | f08fea9 | 2015-09-02 14:23:40 +0200 | [diff] [blame] | 35 | def compare_files(self, file1, file2): |
| 36 | with open(file1) as fp: |
| 37 | lines1 = fp.readlines() |
| 38 | with open(file2) as fp: |
| 39 | lines2 = fp.readlines() |
| 40 | self.assertEqual(lines1, lines2) |
| 41 | |
Steve Dower | 28f6cb3 | 2019-01-22 10:49:52 -0800 | [diff] [blame] | 42 | @unittest.skipUnless(sysconfig.is_python_build(), |
| 43 | 'test only works from source build directory') |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 44 | def test_real_grammar_and_symbol_file(self): |
Victor Stinner | f08fea9 | 2015-09-02 14:23:40 +0200 | [diff] [blame] | 45 | output = support.TESTFN |
| 46 | self.addCleanup(support.unlink, output) |
| 47 | |
| 48 | self._copy_file_without_generated_symbols(SYMBOL_FILE, output) |
| 49 | |
| 50 | exitcode, stderr = self._generate_symbols(GRAMMAR_FILE, output) |
| 51 | self.assertEqual(b'', stderr) |
| 52 | self.assertEqual(0, exitcode) |
| 53 | |
| 54 | self.compare_files(SYMBOL_FILE, output) |
Yury Selivanov | 26f7057 | 2015-05-28 14:06:12 -0400 | [diff] [blame] | 55 | |
| 56 | |
| 57 | if __name__ == "__main__": |
| 58 | unittest.main() |