1. Stefan Schwarzer
  2. ftputil

Commits

Stefan Schwarzer  committed de8409a

Test if all path separators are converted for the target system.

This is a test for ticket #62 that works even on Posix systems.
I do this by providing several mock classes which mimic a local
Windows filesystem.

  • Participants
  • Parent commits e634dcf
  • Branches default

Comments (0)

Files changed (1)

File test/test_ftp_sync.py

View file
-# Copyright (C) 2007-2011, Stefan Schwarzer <sschwarzer@sschwarzer.net>
+# Copyright (C) 2007-2012, Stefan Schwarzer <sschwarzer@sschwarzer.net>
 # See the file LICENSE for licensing terms.
 
+import ntpath
 import os
 import shutil
+import StringIO
 import sys
 import unittest
 
+import ftputil
 from ftputil import ftp_sync
 
 
         syncer.sync(source_dir, target_dir)
 
 
+# Helper classes for `TestUploadFromWindows`
+
+class LocalWindowsHostPath(object):
+
+    def __getattr__(self, name):
+        return getattr(ntpath, name)
+
+
+class LocalWindowsHost(ftp_sync.LocalHost):
+
+    def __init__(self):
+        self.path = LocalWindowsHostPath()
+        self.sep = u"\\"
+
+    def open(self, path, mode):
+        # Just return a dummy file object.
+        return StringIO.StringIO(u"")
+
+    def walk(self, root):
+        """
+        Return a list of tuples as `os.walk`, but use tuples as if the
+        directory structure was
+
+        <root>
+            dir1
+                dir11
+                file1
+                file2
+
+        where <root> is the string passed in as `root`.
+        """
+        join = ntpath.join
+        return [(root,
+                 [join(root, u"dir1")],
+                 []),
+                (join(root, u"dir1"),
+                 [u"dir11"],
+                 [u"file1", u"file2"])
+                ]
+
+
+class DummyFTPSession(object):
+
+    def pwd(self):
+        return u"/"
+
+
+class DummyFTPPath(object):
+
+    def abspath(self, path):
+        # Don't care here if the path is absolute or not.
+        return path
+
+    def isdir(self, path):
+        return path[:-1].endswith(u"dir")
+
+    def isfile(self, path):
+        return path[:-1].endswith(u"file")
+
+
+class ArgumentCheckingFTPHost(ftputil.FTPHost):
+
+    def __init__(self, *args, **kwargs):
+        super(ArgumentCheckingFTPHost, self).__init__(*args, **kwargs)
+        self.path = DummyFTPPath()
+
+    def _make_session(self, *args, **kwargs):
+        return DummyFTPSession()
+
+    def mkdir(self, path):
+        assert u"\\" not in path
+
+    def open(self, path, mode):
+        assert u"\\" not in path
+        return StringIO.StringIO(u"")
+
+
+class TestUploadFromWindows(unittest.TestCase):
+
+    def test_no_mixed_separators(self):
+        source = LocalWindowsHost()
+        target = ArgumentCheckingFTPHost()
+        local_root = ntpath.join(u"some", u"directory")
+        syncer = ftp_sync.Syncer(source, target)
+        # If the following call raises any `AssertionError`s, the
+        # `unittest` framework will catch them and show them.
+        syncer.sync(local_root, u"not_used_by_ArgumentCheckingFTPHost")
+
+
 if __name__ == '__main__':
     unittest.main()
-