test_update_node.py 3.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/env python
# Copyright 2017 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import shutil
import subprocess
import sys
import tempfile
import unittest

import update_node

# Base paths.
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TEST_DATA = os.path.join(BASE_DIR, 'testdata')

# Expectations.
EXPECTED_GITIGNORE = """
/testing/gtest/*
!/testing/gtest/include
/testing/gtest/include/*
!/testing/gtest/include/gtest
/testing/gtest/include/gtest/*
!/testing/gtest/include/gtest/gtest_prod.h
!/third_party/jinja2
!/third_party/markupsafe
/unrelated
"""

32
EXPECTED_GIT_DIFF = """
33
 create mode 100644 deps/v8/base/trace_event/common/common
34 35 36 37 38 39 40
 rename deps/v8/baz/{delete_me => v8_new} (100%)
 rename deps/v8/{delete_me => new/v8_new} (100%)
 create mode 100644 deps/v8/third_party/jinja2/jinja2
 create mode 100644 deps/v8/third_party/markupsafe/markupsafe
 create mode 100644 deps/v8/v8_new
"""

41 42 43 44 45 46 47
ADDED_FILES = [
  'v8_new',
  'new/v8_new',
  'baz/v8_new',
  'testing/gtest/gtest_new',
  'testing/gtest/new/gtest_new',
  'testing/gtest/baz/gtest_new',
48 49
  'third_party/jinja2/jinja2',
  'third_party/markupsafe/markupsafe'
50 51 52 53 54 55 56 57 58 59 60 61 62
]

REMOVED_FILES = [
  'delete_me',
  'baz/delete_me',
  'testing/gtest/delete_me',
  'testing/gtest/baz/delete_me',
]

def gitify(path):
  files = os.listdir(path)
  subprocess.check_call(['git', 'init'], cwd=path)
  subprocess.check_call(['git', 'add'] + files, cwd=path)
63
  subprocess.check_call(['git', 'commit', '-m', 'Initial'], cwd=path)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82


class TestUpdateNode(unittest.TestCase):
  def setUp(self):
    self.workdir = tempfile.mkdtemp(prefix='tmp_test_node_')

  def tearDown(self):
    shutil.rmtree(self.workdir)

  def testUpdate(self):
    v8_cwd = os.path.join(self.workdir, 'v8')
    node_cwd = os.path.join(self.workdir, 'node')

    # Set up V8 test fixture.
    shutil.copytree(src=os.path.join(TEST_DATA, 'v8'), dst=v8_cwd)
    gitify(v8_cwd)
    for repository in update_node.SUB_REPOSITORIES:
      gitify(os.path.join(v8_cwd, *repository))

83
    # Set up node test fixture.
84
    shutil.copytree(src=os.path.join(TEST_DATA, 'node'), dst=node_cwd)
85
    gitify(os.path.join(node_cwd))
86

87 88 89 90 91
    # Add a patch.
    with open(os.path.join(v8_cwd, 'v8_foo'), 'w') as f:
      f.write('zonk')
    subprocess.check_call(['git', 'add', 'v8_foo'], cwd=v8_cwd)

92
    # Run update script.
93
    update_node.Main([v8_cwd, node_cwd, "--commit", "--with-patch"])
94 95 96 97 98 99 100 101 102 103 104

    # Check expectations.
    with open(os.path.join(node_cwd, 'deps', 'v8', '.gitignore')) as f:
      actual_gitignore = f.read()
    self.assertEquals(EXPECTED_GITIGNORE.strip(), actual_gitignore.strip())
    for f in ADDED_FILES:
      added_file = os.path.join(node_cwd, 'deps', 'v8', *f.split('/'))
      self.assertTrue(os.path.exists(added_file))
    for f in REMOVED_FILES:
      removed_file = os.path.join(node_cwd, 'deps', 'v8', *f.split('/'))
      self.assertFalse(os.path.exists(removed_file))
105 106 107 108
    gitlog = subprocess.check_output(
        ['git', 'diff', 'master', '--summary'],
        cwd=node_cwd,
    )
109
    self.assertEquals(EXPECTED_GIT_DIFF.strip(), gitlog.strip())
110

111 112 113 114 115 116 117
    # Check patch.
    gitlog = subprocess.check_output(
        ['git', 'diff', 'master', '--cached', '--', 'deps/v8/v8_foo'],
        cwd=node_cwd,
    )
    self.assertIn('+zonk', gitlog.strip())

118 119
if __name__ == "__main__":
  unittest.main()