Kaido Kert | f309f9a | 2021-04-30 12:09:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2020 the V8 project authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import os |
| 7 | import re |
| 8 | import subprocess |
| 9 | import sys |
| 10 | import tempfile |
| 11 | import unittest |
| 12 | |
| 13 | TOOLS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 14 | COMPARE_SCRIPT = os.path.join(TOOLS_DIR, 'compare_torque_output.py') |
| 15 | TEST_DATA = os.path.join(TOOLS_DIR, 'unittests', 'testdata', 'compare_torque') |
| 16 | |
| 17 | |
| 18 | class PredictableTest(unittest.TestCase): |
| 19 | def setUp(self): |
| 20 | fd, self.tmp_file = tempfile.mkstemp() |
| 21 | os.close(fd) |
| 22 | |
| 23 | def _compare_from(self, test_folder): |
| 24 | file1 = os.path.join(TEST_DATA, test_folder, 'f1') |
| 25 | file2 = os.path.join(TEST_DATA, test_folder, 'f2') |
| 26 | proc = subprocess.Popen([ |
| 27 | 'python', '-u', |
| 28 | COMPARE_SCRIPT, file1, file2, self.tmp_file |
| 29 | ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 30 | _, err = proc.communicate() |
| 31 | return proc.returncode, err |
| 32 | |
| 33 | def test_content_diff(self): |
| 34 | exitcode, output = self._compare_from('test1') |
| 35 | self.assertEqual(1, exitcode) |
| 36 | full_match = r'^Found.*-line 2\+line 2 with diff.*\+line 3\n\n$' |
| 37 | self.assertRegexpMatches(output, re.compile(full_match, re.M | re.S)) |
| 38 | |
| 39 | def test_no_diff(self): |
| 40 | exitcode, output = self._compare_from('test2') |
| 41 | self.assertEqual(0, exitcode) |
| 42 | self.assertFalse(output) |
| 43 | |
| 44 | def test_right_only(self): |
| 45 | exitcode, output = self._compare_from('test3') |
| 46 | self.assertEqual(1, exitcode) |
| 47 | self.assertRegexpMatches(output, r'Some files exist only in.*f2\nfile3') |
| 48 | |
| 49 | def test_left_only(self): |
| 50 | exitcode, output = self._compare_from('test4') |
| 51 | self.assertEqual(1, exitcode) |
| 52 | self.assertRegexpMatches(output, r'Some files exist only in.*f1\nfile4') |
| 53 | |
| 54 | def tearDown(self): |
| 55 | os.unlink(self.tmp_file) |