add basic tests for client side.
authorJelmer Vernooij <jelmer@samba.org>
Wed, 11 Mar 2009 20:27:34 +0000 (20:27 +0000)
committerJelmer Vernooij <jelmer@samba.org>
Wed, 11 Mar 2009 20:27:34 +0000 (20:27 +0000)
dulwich/client.py
dulwich/tests/test_client.py [new file with mode: 0644]

index 0477d0049bb76383945892cda04684fb27b1f6b0..59b3ece99f76cab04c6fbd4be70e0c302a6b99b9 100644 (file)
@@ -30,6 +30,11 @@ from dulwich.pack import (
     write_pack_data,
     )
 
+
+def _fileno_can_read(fileno):
+    return len(select.select([fileno], [], [], 0)[0]) > 0
+
+
 class SimpleFetchGraphWalker(object):
 
     def __init__(self, local_heads, get_parents):
@@ -62,17 +67,12 @@ class GitClient(object):
 
     """
 
-    def __init__(self, fileno, read, write, thin_packs=True, include_tag=True, 
-                 shallow=True):
+    def __init__(self, can_read, read, write, thin_packs=True):
         self.proto = Protocol(read, write)
-        self.fileno = fileno
+        self._can_read = can_read
         self._capabilities = list(CAPABILITIES)
         if thin_packs:
             self._capabilities.append("thin-pack")
-        if include_tag:
-            self._capabilities.append("include-tag")
-        if shallow:
-            self._capabilities.append("shallow")
 
     def capabilities(self):
         return " ".join(self._capabilities)
@@ -127,7 +127,7 @@ class GitClient(object):
         have = graph_walker.next()
         while have:
             self.proto.write_pkt_line("have %s\n" % have)
-            if len(select.select([self.fileno], [], [], 0)[0]) > 0:
+            if self.can_read():
                 pkt = self.proto.read_pkt_line()
                 parts = pkt.rstrip("\n").split(" ")
                 if parts[0] == "ACK":
@@ -162,7 +162,7 @@ class TCPGitClient(GitClient):
         self.rfile = self._socket.makefile('rb', -1)
         self.wfile = self._socket.makefile('wb', 0)
         self.host = host
-        super(TCPGitClient, self).__init__(self._socket.fileno(), self.rfile.read, self.wfile.write, *args, **kwargs)
+        super(TCPGitClient, self).__init__(lambda: _fileno_can_read(self._socket.fileno()), self.rfile.read, self.wfile.write, *args, **kwargs)
 
     def send_pack(self, path):
         self.proto.send_cmd("git-receive-pack", path, "host=%s" % self.host)
@@ -190,7 +190,7 @@ class SubprocessGitClient(GitClient):
         def write_fn(data):
             self.proc.stdin.write(data)
             self.proc.stdin.flush()
-        return GitClient(self.proc.stdout.fileno(), read_fn, write_fn, *args, **kwargs)
+        return GitClient(lambda: _fileno_can_read(self.proc.stdout.fileno()), read_fn, write_fn, *args, **kwargs)
 
     def send_pack(self, path):
         client = self._connect("git-receive-pack", path)
@@ -246,11 +246,11 @@ class SSHGitClient(GitClient):
 
     def send_pack(self, path):
         remote = get_ssh_vendor().connect_ssh(self.host, ["git-receive-pack %s" % path], port=self.port)
-        client = GitClient(remote.proc.stdout.fileno(), remote.recv, remote.send)
+        client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send)
         client.send_pack(path)
 
     def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
         remote = get_ssh_vendor().connect_ssh(self.host, ["git-upload-pack %s" % path], port=self.port)
-        client = GitClient(remote.proc.stdout.fileno(), remote.recv, remote.send)
+        client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send)
         client.fetch_pack(path, determine_wants, graph_walker, pack_data, progress)
 
diff --git a/dulwich/tests/test_client.py b/dulwich/tests/test_client.py
new file mode 100644 (file)
index 0000000..9a48f95
--- /dev/null
@@ -0,0 +1,43 @@
+# test_client.py -- Tests for the git protocol, client side
+# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
+# 
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2
+# or (at your option) any later version of the License.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.
+
+from cStringIO import StringIO
+from unittest import TestCase
+
+from dulwich.client import (
+    GitClient,
+    )
+
+class GitClientTests(TestCase):
+
+    def setUp(self):
+        self.rout = StringIO()
+        self.rin = StringIO()
+        self.client = GitClient(lambda x: True, self.rin.read, 
+            self.rout.write)
+
+    def test_caps(self):
+        self.assertEquals(['multi_ack', 'side-band-64k', 'ofs-delta', 'thin-pack'], self.client._capabilities)
+
+    def test_fetch_pack_none(self):
+        self.rin.write(
+            "008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD.multi_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress include-tag\n"
+            "0000")
+        self.rin.seek(0)
+        self.client.fetch_pack("bla", lambda heads: [], None, None, None)
+        self.assertEquals(self.rout.getvalue(), "0000")