Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2015 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 unittest | |
|
Michael Achenbach
2015/04/28 21:23:51
nit sort by module name
Michael Hablich
2015/04/29 09:22:43
Done.
| |
| 7 import shutil | |
| 8 from subprocess import Popen, PIPE, check_call | |
| 9 from os import path | |
| 10 from collections import namedtuple | |
| 11 | |
| 12 import search_related_commits | |
| 13 | |
| 14 TEST_CONFIG = { | |
| 15 "GIT_REPO": "/tmp/test-v8-search-related-commits", | |
| 16 } | |
| 17 | |
| 18 class TestSearchRelatedCommits(unittest.TestCase): | |
| 19 | |
| 20 base_dir = TEST_CONFIG["GIT_REPO"] | |
| 21 | |
| 22 def _execute_git(self, commands): | |
|
Michael Achenbach
2015/04/28 21:23:51
Maybe use *args for commands (e.g. *commands), the
Michael Hablich
2015/04/29 09:22:43
Acknowledged.
| |
| 23 | |
| 24 fullCommand = ["git", "-C", self.base_dir] + commands | |
| 25 p = Popen(args=fullCommand, stdin=PIPE, | |
| 26 stdout=PIPE, stderr=PIPE) | |
| 27 output, err = p.communicate() | |
| 28 rc = p.returncode | |
| 29 if rc != 0: | |
| 30 raise Exception(err) | |
| 31 return output | |
| 32 | |
| 33 def _add_commit_new_file(self, file_name, message, file_content): | |
| 34 #Initial commit | |
| 35 f = open(self.base_dir + "/" + file_name, 'w') | |
|
Michael Achenbach
2015/04/28 21:23:51
Use "with" statement
Michael Hablich
2015/04/29 09:22:42
Acknowledged.
| |
| 36 f.write(file_content) | |
| 37 f.close() | |
| 38 | |
| 39 self._execute_git(["add", self.base_dir + "/" + file_name]) | |
|
Michael Achenbach
2015/04/28 21:23:51
Do you need files? There is a flag that allows emp
Michael Hablich
2015/04/29 09:22:43
Acknowledged.
| |
| 40 self._execute_git(["commit", "-a", "-m", message]) | |
| 41 | |
| 42 def setUp(self): | |
| 43 if path.exists(self.base_dir): | |
| 44 shutil.rmtree(self.base_dir) | |
| 45 | |
| 46 check_call(["git", "init", self.base_dir]) | |
|
Michael Achenbach
2015/04/28 21:23:52
I like this testing approach! Will look into the d
| |
| 47 | |
| 48 #Initial commit | |
| 49 message = """[turbofan] Sanitize language mode for javascript operators. | |
| 50 | |
| 51 R=mstarzinger@chromium.org | |
| 52 | |
| 53 Review URL: https://codereview.chromium.org/1084243005 | |
| 54 | |
| 55 Cr-Commit-Position: refs/heads/master@{#28059}""" | |
| 56 self._add_commit_new_file("first", message, "Content") | |
| 57 | |
| 58 message = """[crankshaft] Do some stuff | |
| 59 | |
| 60 R=hablich@chromium.org | |
| 61 | |
| 62 Review URL: https://codereview.chromium.org/1084243007 | |
| 63 | |
| 64 Cr-Commit-Position: refs/heads/master@{#28030}""" | |
| 65 self._add_commit_new_file("second", message, "Content2") | |
| 66 | |
| 67 def tearDown(self): | |
| 68 if path.exists(self.base_dir): | |
| 69 shutil.rmtree(self.base_dir) | |
| 70 | |
| 71 def _assert_correct_standard_result( | |
| 72 self, result, all_commits, hash_of_first_commit): | |
| 73 self.assertEqual(len(result), 1, "Master commit not found") | |
| 74 self.assertEqual( | |
| 75 result.keys()[0], | |
| 76 hash_of_first_commit, | |
| 77 "Master commit is wrong") | |
| 78 | |
| 79 self.assertEqual( | |
| 80 len(result[hash_of_first_commit]), | |
| 81 1, | |
| 82 "Child commit not found") | |
| 83 self.assertEqual( | |
| 84 all_commits[2], | |
| 85 result[hash_of_first_commit][0], | |
| 86 "Child commit wrong") | |
| 87 | |
| 88 def _get_commits(self): | |
| 89 commits = self._execute_git( | |
| 90 ["log", "--format=%H", "--reverse"]).splitlines() | |
| 91 return commits | |
| 92 | |
| 93 def testSearchByCommitPosition(self): | |
| 94 f = open(self.base_dir + "/first", 'w') | |
|
Michael Achenbach
2015/04/28 21:23:51
use "with" statement and/or extract some helper me
Michael Hablich
2015/04/29 09:22:42
Acknowledged.
| |
| 95 f.write("reverted stuff") | |
| 96 f.close() | |
| 97 | |
| 98 message = """Revert of some stuff. | |
| 99 > Cr-Commit-Position: refs/heads/master@{#28059} | |
| 100 R=mstarzinger@chromium.org | |
| 101 | |
| 102 Review URL: https://codereview.chromium.org/1084243005 | |
| 103 | |
| 104 Cr-Commit-Position: refs/heads/master@{#28088}""" | |
| 105 | |
| 106 self._execute_git(["commit", "-a", "-m", message]) | |
| 107 | |
| 108 commits = self._get_commits() | |
| 109 hash_of_first_commit = commits[0] | |
| 110 | |
| 111 result = search_related_commits.search_all_related_commits( | |
| 112 self.base_dir, hash_of_first_commit, "HEAD",None) | |
| 113 | |
| 114 self._assert_correct_standard_result(result, commits, hash_of_first_commit) | |
| 115 | |
| 116 def testSearchByTitle(self): | |
| 117 f = open(self.base_dir + "/first", 'w') | |
| 118 f.write("reverted stuff") | |
| 119 f.close() | |
| 120 | |
| 121 message = """Revert of some stuff. | |
| 122 > [turbofan] Sanitize language mode for javascript operators. | |
| 123 > Cr-Commit-Position: refs/heads/master@{#289} | |
| 124 R=mstarzinger@chromium.org | |
| 125 | |
| 126 Review URL: https://codereview.chromium.org/1084243005 | |
| 127 | |
| 128 Cr-Commit-Position: refs/heads/master@{#28088}""" | |
| 129 | |
| 130 self._execute_git(["commit", "-a", "-m", message]) | |
| 131 | |
| 132 commits = self._get_commits() | |
| 133 hash_of_first_commit = commits[0] | |
| 134 | |
| 135 result = search_related_commits.search_all_related_commits( | |
| 136 self.base_dir, hash_of_first_commit, "HEAD",None) | |
| 137 | |
| 138 self._assert_correct_standard_result(result, commits, hash_of_first_commit) | |
| 139 | |
| 140 def testSearchByHash(self): | |
| 141 f = open(self.base_dir + "/first", 'w') | |
| 142 f.write("reverted stuff") | |
| 143 f.close() | |
| 144 | |
| 145 commits = self._get_commits() | |
| 146 hash_of_first_commit = commits[0] | |
| 147 | |
| 148 message = """Revert of some stuff. | |
| 149 > [turbofan] Sanitize language mode for javascript operators. | |
| 150 > Reverting """ + hash_of_first_commit + """ | |
| 151 > R=mstarzinger@chromium.org | |
| 152 | |
| 153 Review URL: https://codereview.chromium.org/1084243005 | |
| 154 | |
| 155 Cr-Commit-Position: refs/heads/master@{#28088}""" | |
| 156 | |
| 157 self._execute_git(["commit", "-a", "-m", message]) | |
| 158 | |
| 159 #Fetch again for an update | |
| 160 commits = self._get_commits() | |
| 161 hash_of_first_commit = commits[0] | |
| 162 | |
| 163 result = search_related_commits.search_all_related_commits( | |
| 164 self.base_dir, | |
| 165 hash_of_first_commit, | |
| 166 "HEAD", | |
| 167 None) | |
| 168 | |
| 169 self._assert_correct_standard_result(result, commits, hash_of_first_commit) | |
| 170 | |
| 171 def testPrettyPrint(self): | |
| 172 f = open(self.base_dir + "/first", 'w') | |
| 173 f.write("reverted stuff") | |
| 174 f.close() | |
| 175 | |
| 176 message = """Revert of some stuff. | |
| 177 > [turbofan] Sanitize language mode for javascript operators. | |
| 178 > Cr-Commit-Position: refs/heads/master@{#289} | |
| 179 R=mstarzinger@chromium.org | |
| 180 | |
| 181 Review URL: https://codereview.chromium.org/1084243005 | |
| 182 | |
| 183 Cr-Commit-Position: refs/heads/master@{#28088}""" | |
| 184 | |
| 185 self._execute_git(["commit", "-a", "-m", message]) | |
| 186 | |
| 187 commits = self._get_commits() | |
| 188 hash_of_first_commit = commits[0] | |
| 189 OptionsStruct = namedtuple( | |
| 190 "OptionsStruct", | |
| 191 "git_dir of until all prettyprint deadline verbose") | |
| 192 options = OptionsStruct( | |
| 193 git_dir= self.base_dir, | |
| 194 of= [hash_of_first_commit], | |
| 195 until= [commits[2]], | |
| 196 all= True, | |
| 197 prettyprint= True, | |
| 198 deadline = None, | |
| 199 verbose=False) | |
| 200 output = [] | |
| 201 for current_line in search_related_commits.main(options): | |
| 202 output.append(current_line) | |
| 203 | |
| 204 self.assertIs(len(output), 2, "Not exactly two entries written") | |
| 205 self.assertTrue(output[0].startswith("+"), "Master entry not marked with +") | |
| 206 self.assertTrue(output[1].startswith("| "), "Child entry not marked with |") | |
| 207 | |
| 208 if __name__ == "__main__": | |
| 209 #import sys;sys.argv = ['', 'Test.testName'] | |
| 210 unittest.main() | |
| OLD | NEW |