source: test/test_sync.py

Last change on this file was 1935:38acf28f3905, checked in by Stefan Schwarzer <sschwarzer@…>, 6 weeks ago
Format docstrings and comments Reformat docstrings and comments to 80 characters. Traditionally, the margin for the code was at 79 columns, so I chose a bit less for the comments. With the switch to Black for formatting the right margin is now at nearly 90 columns, so it makes sense to also allow a greater right margin for docstrings and comments. Lines still don't get too long because the formatted text often starts only in column 5 or 9.
File size: 3.2 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    def setup_method(self, method):
20        if not os.path.exists("test_empty"):
21            os.mkdir("test_empty")
22        if os.path.exists("test_target"):
23            shutil.rmtree("test_target")
24        os.mkdir("test_target")
25
26    def test_sync_empty_dir(self):
27        source = ftputil.sync.LocalHost()
28        target = ftputil.sync.LocalHost()
29        syncer = ftputil.sync.Syncer(source, target)
30        source_dir = os.path.join(TEST_ROOT, "test_empty")
31        target_dir = os.path.join(TEST_ROOT, "test_target")
32        syncer.sync(source_dir, target_dir)
33
34    def test_source_with_and_target_without_slash(self):
35        source = ftputil.sync.LocalHost()
36        target = ftputil.sync.LocalHost()
37        syncer = ftputil.sync.Syncer(source, target)
38        source_dir = os.path.join(TEST_ROOT, "test_source/")
39        target_dir = os.path.join(TEST_ROOT, "test_target")
40        syncer.sync(source_dir, target_dir)
41
42
43# Helper classes for `TestUploadFromWindows`
44
45
46class LocalWindowsHost(ftputil.sync.LocalHost):
47    def __init__(self):
48        self.path = ntpath
49        self.sep = "\\"
50
51    def open(self, path, mode):
52        # Just return a dummy file object.
53        return io.StringIO("")
54
55    def walk(self, root):
56        """
57        Return a list of tuples as `os.walk`, but use tuples as if the
58        directory structure was
59
60        <root>
61            dir1
62                dir11
63                file1
64                file2
65
66        where <root> is the string passed in as `root`.
67        """
68        join = ntpath.join
69        return [
70            (root, [join(root, "dir1")], []),
71            (join(root, "dir1"), ["dir11"], ["file1", "file2"]),
72        ]
73
74
75class DummyFTPSession:
76    def pwd(self):
77        return "/"
78
79
80class DummyFTPPath:
81    def abspath(self, path):
82        # Don't care here if the path is absolute or not.
83        return path
84
85    def isdir(self, path):
86        return "dir" in path
87
88    def isfile(self, path):
89        return "file" in path
90
91
92class ArgumentCheckingFTPHost(ftputil.FTPHost):
93    def __init__(self, *args, **kwargs):
94        super().__init__(*args, **kwargs)
95        self.path = DummyFTPPath()
96
97    def _make_session(self, *args, **kwargs):
98        return DummyFTPSession()
99
100    def mkdir(self, path):
101        assert "\\" not in path
102
103    def open(self, path, mode):
104        assert "\\" not in path
105        return io.StringIO("")
106
107
108class TestUploadFromWindows:
109    def test_no_mixed_separators(self):
110        source = LocalWindowsHost()
111        target = ArgumentCheckingFTPHost()
112        local_root = ntpath.join("some", "directory")
113        syncer = ftputil.sync.Syncer(source, target)
114        # If the following call raises any `AssertionError`s, the test
115        # framework will catch them and show them.
116        syncer.sync(local_root, "not_used_by_ArgumentCheckingFTPHost")
Note: See TracBrowser for help on using the repository browser.