source: test/test_sync.py @ 1718:8bed138bc404

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