Commit e79107e0 authored by Edward Lesmes's avatar Edward Lesmes Committed by Commit Bot

gclient_scm: Fix tests on windows

Change-Id: I649bee199e52ecbd66467cfaf850a7a57e2eedf6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1874506Reviewed-by: 's avatarAnthony Polito <apolito@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent 5bfa3ae8
......@@ -71,7 +71,6 @@ def CommonChecks(input_api, output_api, tests_to_black_list, run_on_python3):
tests_to_black_list = [
r'.*auth_test\.py$',
r'.*download_from_google_storage_unittest\.py$',
r'.*gclient_scm_test\.py$',
r'.*gclient_smoketest\.py$',
r'.*git_cl_test\.py$',
r'.*git_common_test\.py$',
......
......@@ -1269,9 +1269,7 @@ class GitWrapper(SCMWrapper):
# we don't accidentally go corrupting parent git checks too. See
# https://crbug.com/1000825 for an example.
if set_git_dir:
git_dir = os.path.abspath(os.path.join(self.checkout_path, '.git'))
# Depending on how the .gclient file was defined, self.checkout_path
# might be set to a unicode string, not a regular string; on Windows
# Python2, we can't set env vars to be unicode strings, so we
......
......@@ -14,6 +14,7 @@ import errno
import logging
import os
import pprint
import random
import re
import socket
import sys
......@@ -28,6 +29,13 @@ import scm
import subprocess2
# Attempt |MAX_TRY| times to find a free port. Each time select one port at
# random from the range [|PORT_START|, |PORT_END|].
MAX_TRY = 10
PORT_START = 20000
PORT_END = 65535
def write(path, content):
f = open(path, 'wb')
f.write(content.encode())
......@@ -74,20 +82,20 @@ def commit_git(repo):
return rev
def test_port(host, port):
def port_is_free(host, port):
s = socket.socket()
try:
return s.connect_ex((host, port)) == 0
return s.connect_ex((host, port)) != 0
finally:
s.close()
def find_free_port(host, base_port):
def find_free_port(host):
"""Finds a listening port free to listen to."""
while base_port < (2<<16):
if not test_port(host, base_port):
for _ in range(MAX_TRY):
base_port = random.randint(PORT_START, PORT_END)
if port_is_free(host, base_port):
return base_port
base_port += 1
assert False, 'Having issues finding an available port'
......@@ -239,7 +247,7 @@ class FakeReposBase(object):
self.set_up()
if self.gitdaemon:
return True
assert self.git_pid_file_name == None
assert self.git_pid_file_name == None, self.git_pid_file_name
try:
subprocess2.check_output(['git', '--version'])
except (OSError, subprocess2.CalledProcessError):
......@@ -247,12 +255,11 @@ class FakeReposBase(object):
for repo in ['repo_%d' % r for r in range(1, self.NB_GIT_REPOS + 1)]:
subprocess2.check_call(['git', 'init', '-q', join(self.git_root, repo)])
self.git_hashes[repo] = [(None, None)]
self.git_port = find_free_port(self.host, 20000)
self.git_base = 'git://%s:%d/git/' % (self.host, self.git_port)
# Start the daemon.
git_pid_file = tempfile.NamedTemporaryFile(delete=False)
self.git_pid_file_name = git_pid_file.name
git_pid_file.close()
self.git_port = find_free_port(self.host)
self.git_base = 'git://%s:%d/git/' % (self.host, self.git_port)
cmd = ['git', 'daemon',
'--export-all',
'--reuseaddr',
......@@ -261,7 +268,10 @@ class FakeReposBase(object):
'--port=%d' % self.git_port]
if self.host == '127.0.0.1':
cmd.append('--listen=' + self.host)
self.check_port_is_free(self.git_port)
# Verify that the port is free.
if not port_is_free(self.host, self.git_port):
return False
# Start the daemon.
self.gitdaemon = subprocess2.Popen(
cmd,
cwd=self.root_dir,
......@@ -304,17 +314,6 @@ class FakeReposBase(object):
subprocess2.check_call(
['git', 'fast-import', '--quiet'], cwd=repo_root, stdin=data.encode())
def check_port_is_free(self, port):
sock = socket.socket()
try:
sock.connect((self.host, port))
# It worked, throw.
assert False, '%d shouldn\'t be bound' % port
except (socket.error, EnvironmentError):
pass
finally:
sock.close()
def populateGit(self):
raise NotImplementedError()
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment