source: test/test_sync.py @ 1660:93ea351f922b

Last change on this file since 1660:93ea351f922b was 1660:93ea351f922b, checked in by Stefan Schwarzer <sschwarzer@…>, 4 years ago
Remove `unittest.main` calls Since we now use pytest as test runner and not a shell loop in the makefile, there's no longer a need for explicit `unittest.main` calls. ticket: 98
File size: 3.4 KB
Line 
1# Copyright (C) 2007-2013, Stefan Schwarzer <sschwarzer@sschwarzer.net>
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5from __future__ import absolute_import
6from __future__ import unicode_literals
7
8import io
9import ntpath
10import os
11import shutil
12import unittest
13
14import ftputil
15import ftputil.sync
16
17
18# Assume the test subdirectories are or will be in the current directory.
19TEST_ROOT = os.getcwd()
20
21
22class TestLocalToLocal(unittest.TestCase):
23
24    def setUp(self):
25        if not os.path.exists("test_empty"):
26            os.mkdir("test_empty")
27        if os.path.exists("test_target"):
28            shutil.rmtree("test_target")
29        os.mkdir("test_target")
30
31    def test_sync_empty_dir(self):
32        source = ftputil.sync.LocalHost()
33        target = ftputil.sync.LocalHost()
34        syncer = ftputil.sync.Syncer(source, target)
35        source_dir = os.path.join(TEST_ROOT, "test_empty")
36        target_dir = os.path.join(TEST_ROOT, "test_target")
37        syncer.sync(source_dir, target_dir)
38
39    def test_source_with_and_target_without_slash(self):
40        source = ftputil.sync.LocalHost()
41        target = ftputil.sync.LocalHost()
42        syncer = ftputil.sync.Syncer(source, target)
43        source_dir = os.path.join(TEST_ROOT, "test_source/")
44        target_dir = os.path.join(TEST_ROOT, "test_target")
45        syncer.sync(source_dir, target_dir)
46
47
48# Helper classes for `TestUploadFromWindows`
49
50class LocalWindowsHost(ftputil.sync.LocalHost):
51
52    def __init__(self):
53        self.path = ntpath
54        self.sep = "\\"
55
56    def open(self, path, mode):
57        # Just return a dummy file object.
58        return io.StringIO("")
59
60    def walk(self, root):
61        """
62        Return a list of tuples as `os.walk`, but use tuples as if the
63        directory structure was
64
65        <root>
66            dir1
67                dir11
68                file1
69                file2
70
71        where <root> is the string passed in as `root`.
72        """
73        join = ntpath.join
74        return [(root,
75                 [join(root, "dir1")],
76                 []),
77                (join(root, "dir1"),
78                 ["dir11"],
79                 ["file1", "file2"])
80                ]
81
82
83class DummyFTPSession(object):
84
85    def pwd(self):
86        return "/"
87
88
89class DummyFTPPath(object):
90
91    def abspath(self, path):
92        # Don't care here if the path is absolute or not.
93        return path
94
95    def isdir(self, path):
96        return ("dir" in path)
97
98    def isfile(self, path):
99        return ("file" in path)
100
101
102class ArgumentCheckingFTPHost(ftputil.FTPHost):
103
104    def __init__(self, *args, **kwargs):
105        super(ArgumentCheckingFTPHost, self).__init__(*args, **kwargs)
106        self.path = DummyFTPPath()
107
108    def _make_session(self, *args, **kwargs):
109        return DummyFTPSession()
110
111    def mkdir(self, path):
112        assert "\\" not in path
113
114    def open(self, path, mode):
115        assert "\\" not in path
116        return io.StringIO("")
117
118
119class TestUploadFromWindows(unittest.TestCase):
120
121    def test_no_mixed_separators(self):
122        source = LocalWindowsHost()
123        target = ArgumentCheckingFTPHost()
124        local_root = ntpath.join("some", "directory")
125        syncer = ftputil.sync.Syncer(source, target)
126        # If the following call raises any `AssertionError`s, the
127        # test framework will catch them and show them.
128        syncer.sync(local_root, "not_used_by_ArgumentCheckingFTPHost")
Note: See TracBrowser for help on using the repository browser.