Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(656)

Side by Side Diff: tests/git_cl_test.py

Issue 8715007: Improve git-cl, add unit test! (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« git_cl.py ('K') | « git_cl.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2011 The Chromium 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 """Unit tests for git_cl.py."""
7
8 import os
9 import sys
10 import unittest
11
12 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13
14 from testing_support.auto_stub import TestCase
15
16 import git_cl
17 import subprocess2
18
19
20 class TestGitCl(TestCase):
21 def setUp(self):
22 super(TestGitCl, self).setUp()
23 self.calls = []
24 self._calls_done = 0
25 def mock_call(args, **kwargs):
26 expected_args, result = self.calls.pop(0)
27 self.assertEquals(
28 expected_args,
29 args,
30 '@%d Expected: %r Actual: %r' % (
31 self._calls_done, expected_args, args))
32 self._calls_done += 1
33 return result
34 self.mock(subprocess2, 'call', mock_call)
35 self.mock(subprocess2, 'check_call', mock_call)
36 self.mock(subprocess2, 'check_output', mock_call)
37 self.mock(subprocess2, 'communicate', mock_call)
38 self.mock(subprocess2, 'Popen', mock_call)
39
40 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
41
42 class Presubmit(object):
43 def __init__(self, *args, **kwargs):
44 pass
45 @staticmethod
46 def should_continue():
47 return True
48 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', Presubmit)
49
50 class Rietveld(object):
51 def __init__(self, *args, **kwargs):
52 pass
53 self.mock(git_cl.rietveld, 'Rietveld', Rietveld)
54
55 self.mock(git_cl.upload, 'RealMain', self.fail)
56
57 class Watchlists(object):
58 def __init__(self, _):
59 pass
60 @staticmethod
61 def GetWatchersForPaths(_):
62 return ['joe@example.com']
63 self.mock(git_cl.watchlists, 'Watchlists', Watchlists)
64 # It's important to reset settings to not have inter-tests interference.
65 git_cl.settings = None
66
67 def tearDown(self):
68 if not self.has_failed():
69 self.assertEquals([], self.calls)
70 super(TestGitCl, self).tearDown()
71
72 @staticmethod
73 def _upload_calls():
74 return [
75 (['git', 'update-index', '--refresh', '-q'], ''),
76 (['git', 'diff-index', 'HEAD'], ''),
77 (['git', 'config', 'rietveld.server'], 'codereview.example.com'),
78 (['git', 'symbolic-ref', 'HEAD'], 'master'),
79 (['git', 'config', 'branch.master.merge'], 'master'),
80 (['git', 'config', 'branch.master.remote'], 'origin'),
81 (['git', 'rev-parse', '--show-cdup'], ''),
82 (['git', 'rev-parse', 'HEAD'], '12345'),
83 (['git', 'diff', '--name-status', '-r', 'master...', '.'],
84 'M\t.gitignore\n'),
85 (['git', 'rev-parse', '--git-dir'], '.git'),
86 (['git', 'config', 'branch.master.rietveldissue'], ''),
87 (['git', 'config', 'branch.master.rietveldpatchset'], ''),
88 (['git', 'log', '--pretty=format:%s%n%n%b', 'master...'], 'foo'),
89 (['git', 'config', 'user.email'], 'me@example.com'),
90 (['git', 'diff', '--no-ext-diff', '--stat', '-M', 'master...'], '+dat'),
91 (['git', 'log', '--pretty=format:%s\n\n%b', 'master..'], 'desc\n'),
92 (['git', 'config', 'rietveld.cc'], ''),
93 (['git', 'config', '--get-regexp', '^svn-remote\\.'], (('', None), 0)),
94 (['git', 'rev-parse', '--show-cdup'], ''),
95 (['git', 'svn', 'info'], ''),
96 (['git', 'config', 'branch.master.rietveldissue', '1'], ''),
97 (['git', 'config', 'branch.master.rietveldserver',
98 'http://codereview.example.com'], ''),
99 (['git', 'config', 'branch.master.rietveldpatchset', '2'], ''),
100 ]
101
102 def test_reviewers(self):
103 self.calls = self._upload_calls()
104 def RunEditor(desc, _):
105 self.assertEquals(
106 '# Enter a description of the change.\n'
107 '# This will displayed on the codereview site.\n'
108 '# The first line will also be used as the subject of the review.\n'
109 'desc\n\nR=foo@example.com\nBUG=\nTEST=', desc)
110 return desc
111 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
112 def check_upload(args):
113 self.assertEquals(
114 ['upload', '--assume_yes', '--server',
115 'http://codereview.example.com',
116 '--message', 'desc',
117 '--description', 'desc\n\nR=foo@example.com\nBUG=\nTEST=\n',
118 # Reviewer is used from the command line.
119 '--reviewers', 'foo@example.com',
120 '--cc', 'joe@example.com',
121 'master...'],
122 args)
123 return 1, 2
124 self.mock(git_cl.upload, 'RealMain', check_upload)
125 git_cl.main(['upload', '-r' 'foo@example.com'])
126
127 def test_reviewer_tbr_overriden(self):
128 self.calls = self._upload_calls()
129 def RunEditor(desc, _):
130 self.assertEquals(
131 '# Enter a description of the change.\n'
132 '# This will displayed on the codereview site.\n'
133 '# The first line will also be used as the subject of the review.\n'
134 'desc\n\nR=foo@example.com\nBUG=\nTEST=', desc)
135 return '\n\nFoo Bar\nTBR=reviewer@example.com\n\n'
136 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
137 def check_upload(args):
138 self.assertEquals(
139 ['upload', '--assume_yes', '--server',
140 'http://codereview.example.com',
141 '--message', 'Foo Bar',
142 '--description', 'Foo Bar\nTBR=reviewer@example.com\n',
143 # Reviewer is updated from the CL description.
144 '--reviewers', 'reviewer@example.com',
145 '--cc', 'joe@example.com',
146 'master...'],
147 args)
148 return 1, 2
149 self.mock(git_cl.upload, 'RealMain', check_upload)
150 git_cl.main(['upload', '-r' 'foo@example.com'])
151
152
153 if __name__ == '__main__':
154 unittest.main()
OLDNEW
« git_cl.py ('K') | « git_cl.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698