[email protected] | 45d8db0 | 2011-03-31 20:43:56 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | 99ac1c5 | 2012-01-16 14:52:12 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
[email protected] | fae707b | 2011-09-15 18:57:58 | [diff] [blame] | 6 | import os |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 7 | import StringIO |
[email protected] | 428342a | 2011-11-10 15:46:33 | [diff] [blame] | 8 | import sys |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 9 | |
[email protected] | 428342a | 2011-11-10 15:46:33 | [diff] [blame] | 10 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 11 | |
[email protected] | 0927b7e | 2011-11-11 16:06:22 | [diff] [blame] | 12 | from testing_support.super_mox import SuperMoxTestBase |
| 13 | from testing_support import trial_dir |
[email protected] | fae707b | 2011-09-15 18:57:58 | [diff] [blame] | 14 | |
[email protected] | ba55177 | 2010-02-03 18:21:42 | [diff] [blame] | 15 | import gclient_utils |
[email protected] | fae707b | 2011-09-15 18:57:58 | [diff] [blame] | 16 | import subprocess2 |
[email protected] | ba55177 | 2010-02-03 18:21:42 | [diff] [blame] | 17 | |
[email protected] | 9a2f37e | 2009-12-19 16:03:28 | [diff] [blame] | 18 | |
[email protected] | 399318e | 2010-08-17 15:30:01 | [diff] [blame] | 19 | class GclientUtilBase(SuperMoxTestBase): |
| 20 | def setUp(self): |
| 21 | super(GclientUtilBase, self).setUp() |
| 22 | gclient_utils.sys.stdout.flush = lambda: None |
[email protected] | fae707b | 2011-09-15 18:57:58 | [diff] [blame] | 23 | self.mox.StubOutWithMock(subprocess2, 'Popen') |
| 24 | self.mox.StubOutWithMock(subprocess2, 'communicate') |
[email protected] | 399318e | 2010-08-17 15:30:01 | [diff] [blame] | 25 | |
| 26 | |
[email protected] | 17d0179 | 2010-09-01 18:07:10 | [diff] [blame] | 27 | class CheckCallAndFilterTestCase(GclientUtilBase): |
[email protected] | dd218e5 | 2010-08-23 13:02:41 | [diff] [blame] | 28 | class ProcessIdMock(object): |
| 29 | def __init__(self, test_string): |
| 30 | self.stdout = StringIO.StringIO(test_string) |
[email protected] | 2fd6c3f | 2013-05-03 21:57:55 | [diff] [blame] | 31 | self.pid = 9284 |
[email protected] | f2d7d6b | 2013-10-17 20:41:43 | [diff] [blame] | 32 | # pylint: disable=R0201 |
[email protected] | dd218e5 | 2010-08-23 13:02:41 | [diff] [blame] | 33 | def wait(self): |
[email protected] | f2d7d6b | 2013-10-17 20:41:43 | [diff] [blame] | 34 | return 0 |
[email protected] | dd218e5 | 2010-08-23 13:02:41 | [diff] [blame] | 35 | |
[email protected] | 2b9aa8e | 2010-08-25 20:01:42 | [diff] [blame] | 36 | def _inner(self, args, test_string): |
| 37 | cwd = 'bleh' |
[email protected] | 559c3f8 | 2010-08-23 19:26:08 | [diff] [blame] | 38 | gclient_utils.sys.stdout.write( |
| 39 | '\n________ running \'boo foo bar\' in \'bleh\'\n') |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 40 | for i in test_string: |
| 41 | gclient_utils.sys.stdout.write(i) |
[email protected] | 428342a | 2011-11-10 15:46:33 | [diff] [blame] | 42 | # pylint: disable=E1101 |
[email protected] | fae707b | 2011-09-15 18:57:58 | [diff] [blame] | 43 | subprocess2.Popen( |
[email protected] | 2b9aa8e | 2010-08-25 20:01:42 | [diff] [blame] | 44 | args, |
| 45 | cwd=cwd, |
[email protected] | fae707b | 2011-09-15 18:57:58 | [diff] [blame] | 46 | stdout=subprocess2.PIPE, |
| 47 | stderr=subprocess2.STDOUT, |
[email protected] | dd218e5 | 2010-08-23 13:02:41 | [diff] [blame] | 48 | bufsize=0).AndReturn(self.ProcessIdMock(test_string)) |
| 49 | |
[email protected] | f2d7d6b | 2013-10-17 20:41:43 | [diff] [blame] | 50 | os.getcwd() |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 51 | self.mox.ReplayAll() |
[email protected] | dd218e5 | 2010-08-23 13:02:41 | [diff] [blame] | 52 | compiled_pattern = gclient_utils.re.compile(r'a(.*)b') |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 53 | line_list = [] |
| 54 | capture_list = [] |
| 55 | def FilterLines(line): |
| 56 | line_list.append(line) |
[email protected] | 17d0179 | 2010-09-01 18:07:10 | [diff] [blame] | 57 | assert isinstance(line, str), type(line) |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 58 | match = compiled_pattern.search(line) |
| 59 | if match: |
| 60 | capture_list.append(match.group(1)) |
[email protected] | 17d0179 | 2010-09-01 18:07:10 | [diff] [blame] | 61 | gclient_utils.CheckCallAndFilterAndHeader( |
| 62 | args, cwd=cwd, always=True, filter_fn=FilterLines) |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 63 | self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb']) |
| 64 | self.assertEquals(capture_list, ['cc', 'dd']) |
| 65 | |
[email protected] | 17d0179 | 2010-09-01 18:07:10 | [diff] [blame] | 66 | def testCheckCallAndFilter(self): |
[email protected] | 2b9aa8e | 2010-08-25 20:01:42 | [diff] [blame] | 67 | args = ['boo', 'foo', 'bar'] |
[email protected] | dd218e5 | 2010-08-23 13:02:41 | [diff] [blame] | 68 | test_string = 'ahah\naccb\nallo\naddb\n' |
[email protected] | 2b9aa8e | 2010-08-25 20:01:42 | [diff] [blame] | 69 | self._inner(args, test_string) |
[email protected] | dd057f3 | 2010-09-20 19:33:31 | [diff] [blame] | 70 | self.checkstdout('\n________ running \'boo foo bar\' in \'bleh\'\n' |
| 71 | 'ahah\naccb\nallo\naddb\n\n' |
[email protected] | bf38a7e | 2010-12-14 18:15:54 | [diff] [blame] | 72 | '________ running \'boo foo bar\' in \'bleh\'\nahah\naccb\nallo\naddb' |
| 73 | '\n') |
[email protected] | dd218e5 | 2010-08-23 13:02:41 | [diff] [blame] | 74 | |
| 75 | def testNoLF(self): |
[email protected] | 17d0179 | 2010-09-01 18:07:10 | [diff] [blame] | 76 | # Exactly as testCheckCallAndFilterAndHeader without trailing \n |
[email protected] | 2b9aa8e | 2010-08-25 20:01:42 | [diff] [blame] | 77 | args = ['boo', 'foo', 'bar'] |
[email protected] | dd218e5 | 2010-08-23 13:02:41 | [diff] [blame] | 78 | test_string = 'ahah\naccb\nallo\naddb' |
[email protected] | 2b9aa8e | 2010-08-25 20:01:42 | [diff] [blame] | 79 | self._inner(args, test_string) |
[email protected] | dd057f3 | 2010-09-20 19:33:31 | [diff] [blame] | 80 | self.checkstdout('\n________ running \'boo foo bar\' in \'bleh\'\n' |
| 81 | 'ahah\naccb\nallo\naddb\n' |
| 82 | '________ running \'boo foo bar\' in \'bleh\'\nahah\naccb\nallo\naddb') |
[email protected] | dd218e5 | 2010-08-23 13:02:41 | [diff] [blame] | 83 | |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 84 | |
[email protected] | 399318e | 2010-08-17 15:30:01 | [diff] [blame] | 85 | class SplitUrlRevisionTestCase(GclientUtilBase): |
[email protected] | ac915bb | 2009-11-13 17:03:01 | [diff] [blame] | 86 | def testSSHUrl(self): |
| 87 | url = "ssh://[email protected]/test.git" |
| 88 | rev = "ac345e52dc" |
| 89 | out_url, out_rev = gclient_utils.SplitUrlRevision(url) |
| 90 | self.assertEquals(out_rev, None) |
| 91 | self.assertEquals(out_url, url) |
| 92 | out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
| 93 | self.assertEquals(out_rev, rev) |
| 94 | self.assertEquals(out_url, url) |
| 95 | url = "ssh://example.com/test.git" |
| 96 | out_url, out_rev = gclient_utils.SplitUrlRevision(url) |
| 97 | self.assertEquals(out_rev, None) |
| 98 | self.assertEquals(out_url, url) |
| 99 | out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
| 100 | self.assertEquals(out_rev, rev) |
| 101 | self.assertEquals(out_url, url) |
[email protected] | a4d60f9 | 2009-11-18 19:34:08 | [diff] [blame] | 102 | url = "ssh://example.com/git/test.git" |
| 103 | out_url, out_rev = gclient_utils.SplitUrlRevision(url) |
| 104 | self.assertEquals(out_rev, None) |
| 105 | self.assertEquals(out_url, url) |
| 106 | out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
| 107 | self.assertEquals(out_rev, rev) |
| 108 | self.assertEquals(out_url, url) |
[email protected] | b9f2f62 | 2009-11-19 23:45:35 | [diff] [blame] | 109 | rev = "test-stable" |
| 110 | out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
| 111 | self.assertEquals(out_rev, rev) |
| 112 | self.assertEquals(out_url, url) |
[email protected] | 78b8cd1 | 2010-10-26 12:47:07 | [diff] [blame] | 113 | url = "ssh://[email protected]/~/test.git" |
| 114 | out_url, out_rev = gclient_utils.SplitUrlRevision(url) |
| 115 | self.assertEquals(out_rev, None) |
| 116 | self.assertEquals(out_url, url) |
| 117 | out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
| 118 | self.assertEquals(out_rev, rev) |
| 119 | self.assertEquals(out_url, url) |
| 120 | url = "ssh://[email protected]/~username/test.git" |
| 121 | out_url, out_rev = gclient_utils.SplitUrlRevision(url) |
| 122 | self.assertEquals(out_rev, None) |
| 123 | self.assertEquals(out_url, url) |
| 124 | out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
| 125 | self.assertEquals(out_rev, rev) |
| 126 | self.assertEquals(out_url, url) |
[email protected] | f1eccaf | 2014-04-11 15:51:33 | [diff] [blame] | 127 | url = "[email protected]:dart-lang/spark.git" |
| 128 | out_url, out_rev = gclient_utils.SplitUrlRevision(url) |
| 129 | self.assertEquals(out_rev, None) |
| 130 | self.assertEquals(out_url, url) |
| 131 | out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
| 132 | self.assertEquals(out_rev, rev) |
| 133 | self.assertEquals(out_url, url) |
[email protected] | ac915bb | 2009-11-13 17:03:01 | [diff] [blame] | 134 | |
| 135 | def testSVNUrl(self): |
| 136 | url = "svn://example.com/test" |
| 137 | rev = "ac345e52dc" |
| 138 | out_url, out_rev = gclient_utils.SplitUrlRevision(url) |
| 139 | self.assertEquals(out_rev, None) |
| 140 | self.assertEquals(out_url, url) |
| 141 | out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
| 142 | self.assertEquals(out_rev, rev) |
| 143 | self.assertEquals(out_url, url) |
| 144 | |
| 145 | |
[email protected] | f904072 | 2011-03-09 14:47:51 | [diff] [blame] | 146 | class GClientUtilsTest(trial_dir.TestCase): |
| 147 | def testHardToDelete(self): |
| 148 | # Use the fact that tearDown will delete the directory to make it hard to do |
| 149 | # so. |
| 150 | l1 = os.path.join(self.root_dir, 'l1') |
| 151 | l2 = os.path.join(l1, 'l2') |
| 152 | l3 = os.path.join(l2, 'l3') |
| 153 | f3 = os.path.join(l3, 'f3') |
| 154 | os.mkdir(l1) |
| 155 | os.mkdir(l2) |
| 156 | os.mkdir(l3) |
| 157 | gclient_utils.FileWrite(f3, 'foo') |
| 158 | os.chmod(f3, 0) |
| 159 | os.chmod(l3, 0) |
| 160 | os.chmod(l2, 0) |
| 161 | os.chmod(l1, 0) |
| 162 | |
[email protected] | eb5edbc | 2012-01-16 17:03:28 | [diff] [blame] | 163 | def testUpgradeToHttps(self): |
| 164 | values = [ |
| 165 | ['', ''], |
| 166 | [None, None], |
| 167 | ['foo', 'https://foo'], |
| 168 | ['http://foo', 'https://foo'], |
| 169 | ['foo/', 'https://foo/'], |
| 170 | ['ssh-svn://foo', 'ssh-svn://foo'], |
| 171 | ['ssh-svn://foo/bar/', 'ssh-svn://foo/bar/'], |
[email protected] | 07d4439 | 2012-09-12 16:59:22 | [diff] [blame] | 172 | ['codereview.chromium.org', 'https://codereview.chromium.org'], |
| 173 | ['codereview.chromium.org/', 'https://codereview.chromium.org/'], |
[email protected] | 626e77d | 2014-03-13 01:05:58 | [diff] [blame] | 174 | ['http://foo:10000', 'http://foo:10000'], |
| 175 | ['http://foo:10000/bar', 'http://foo:10000/bar'], |
| 176 | ['foo:10000', 'http://foo:10000'], |
[email protected] | eb5edbc | 2012-01-16 17:03:28 | [diff] [blame] | 177 | ['foo:', 'https://foo:'], |
| 178 | ] |
| 179 | for content, expected in values: |
| 180 | self.assertEquals( |
| 181 | expected, gclient_utils.UpgradeToHttps(content)) |
| 182 | |
[email protected] | 99ac1c5 | 2012-01-16 14:52:12 | [diff] [blame] | 183 | def testParseCodereviewSettingsContent(self): |
[email protected] | eb5edbc | 2012-01-16 17:03:28 | [diff] [blame] | 184 | values = [ |
| 185 | ['# bleh\n', {}], |
| 186 | ['\t# foo : bar\n', {}], |
| 187 | ['Foo:bar', {'Foo': 'bar'}], |
| 188 | ['Foo:bar:baz\n', {'Foo': 'bar:baz'}], |
| 189 | [' Foo : bar ', {'Foo': 'bar'}], |
| 190 | [' Foo : bar \n', {'Foo': 'bar'}], |
| 191 | ['a:b\n\rc:d\re:f', {'a': 'b', 'c': 'd', 'e': 'f'}], |
| 192 | ['an_url:http://value/', {'an_url': 'http://value/'}], |
| 193 | [ |
| 194 | 'CODE_REVIEW_SERVER : http://r/s', |
| 195 | {'CODE_REVIEW_SERVER': 'https://r/s'} |
| 196 | ], |
| 197 | ['VIEW_VC:http://r/s', {'VIEW_VC': 'https://r/s'}], |
| 198 | ] |
| 199 | for content, expected in values: |
| 200 | self.assertEquals( |
| 201 | expected, gclient_utils.ParseCodereviewSettingsContent(content)) |
[email protected] | 99ac1c5 | 2012-01-16 14:52:12 | [diff] [blame] | 202 | |
[email protected] | f904072 | 2011-03-09 14:47:51 | [diff] [blame] | 203 | |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 204 | if __name__ == '__main__': |
| 205 | import unittest |
| 206 | unittest.main() |
| 207 | |
| 208 | # vim: ts=2:sw=2:tw=80:et: |