• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
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
6import os
7import re
8import subprocess
9import sys
10import tempfile
11import unittest
12
13TOOLS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
14COMPARE_SCRIPT = os.path.join(TOOLS_DIR, 'compare_torque_output.py')
15TEST_DATA = os.path.join(TOOLS_DIR, 'unittests', 'testdata', 'compare_torque')
16
17
18class 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          sys.executable, '-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.assertRegex(output.decode('utf-8'), 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.assertRegex(output.decode('utf-8'), 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.assertRegex(output.decode('utf-8'), r'Some files exist only in.*f1\nfile4')
53
54  def tearDown(self):
55    os.unlink(self.tmp_file)
56