Home | History | Annotate | Download | only in node
      1 #!/usr/bin/env python
      2 # Copyright 2017 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 shutil
      8 import subprocess
      9 import sys
     10 import tempfile
     11 import unittest
     12 
     13 import backport_node
     14 
     15 # Base paths.
     16 BASE_DIR = os.path.dirname(os.path.abspath(__file__))
     17 TEST_DATA = os.path.join(BASE_DIR, 'testdata')
     18 
     19 def gitify(path):
     20   files = os.listdir(path)
     21   subprocess.check_call(['git', 'init'], cwd=path)
     22   subprocess.check_call(['git', 'add'] + files, cwd=path)
     23   subprocess.check_call(['git', 'commit', '-m', 'Initial'], cwd=path)
     24 
     25 class TestUpdateNode(unittest.TestCase):
     26   def setUp(self):
     27     self.workdir = tempfile.mkdtemp(prefix='tmp_test_node_')
     28 
     29   def tearDown(self):
     30     shutil.rmtree(self.workdir)
     31 
     32   def testUpdate(self):
     33     v8_cwd = os.path.join(self.workdir, 'v8')
     34     node_cwd = os.path.join(self.workdir, 'node')
     35 
     36     # Set up V8 test fixture.
     37     shutil.copytree(src=os.path.join(TEST_DATA, 'v8'), dst=v8_cwd)
     38     gitify(v8_cwd)
     39 
     40     # Set up node test fixture.
     41     shutil.copytree(src=os.path.join(TEST_DATA, 'node'), dst=node_cwd)
     42     gitify(os.path.join(node_cwd))
     43 
     44     # Add a patch.
     45     with open(os.path.join(v8_cwd, 'v8_foo'), 'w') as f:
     46       f.write('zonk')
     47     subprocess.check_call(['git', 'add', 'v8_foo'], cwd=v8_cwd)
     48     subprocess.check_call(['git', 'commit', '-m', "Title\n\nBody"], cwd=v8_cwd)
     49     commit = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=v8_cwd).strip()
     50 
     51     # Run update script.
     52     backport_node.Main([v8_cwd, node_cwd, commit, "--no-review"])
     53 
     54     # Check message.
     55     message = subprocess.check_output(['git', 'log', '-1', '--format=%B'], cwd=node_cwd)
     56     self.assertIn('Original commit message:\n\n  Title\n\n  Body', message)
     57 
     58     # Check patch.
     59     gitlog = subprocess.check_output(
     60         ['git', 'diff', 'master', '--cached', '--', 'deps/v8/v8_foo'],
     61         cwd=node_cwd,
     62     )
     63     self.assertIn('+zonk', gitlog.strip())
     64 
     65     # Check version.
     66     version_file = os.path.join(node_cwd, "deps", "v8", "include", "v8-version.h")
     67     self.assertIn('#define V8_PATCH_LEVEL 4322', backport_node.FileToText(version_file))
     68 
     69 if __name__ == "__main__":
     70   unittest.main()
     71