source: test/test_sync.py @ 1663:40280d912474

Last change on this file since 1663:40280d912474 was 1663:40280d912474, checked in by Stefan Schwarzer <sschwarzer@…>, 3 years ago
Remove `unittest` dependency - Remove `unittest` imports - Let test case classes inherit from `object` instead of `unittest.TestCase` - Use `setup_method` and `teardown_method` instead of `unittest`'s `setUp` and `tearDown` - Use `pytest.mark.skipif` instead of `unittest.skipIf` - Use `pytest.mark.slow_test` instead of decorator in `test/__init__.py`. Exclude slow tests with py.test -m "not slow_test" test - Replace some leftover `assert*` calls with `assert` statements - Replace `assert False, ...` with `pytest.fail` 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
12
13import ftputil
14import ftputil.sync
15
16
17# Assume the test subdirectories are or will be in the current directory.
18TEST_ROOT = os.getcwd()
19
20
21class TestLocalToLocal(object):
22
23    def setup_method(self, method):
24        if not os.path.exists("test_empty"):
25            os.mkdir("test_empty")
26        if os.path.exists("test_target"):
27            shutil.rmtree("test_target")
28        os.mkdir("test_target")
29
30    def test_sync_empty_dir(self):
31        source = ftputil.sync.LocalHost()
32        target = ftputil.sync.LocalHost()
33        syncer = ftputil.sync.Syncer(source, target)
34        source_dir = os.path.join(TEST_ROOT, "test_empty")
35        target_dir = os.path.join(TEST_ROOT, "test_target")
36        syncer.sync(source_dir, target_dir)
37
38    def test_source_with_and_target_without_slash(self):
39        source = ftputil.sync.LocalHost()
40        target = ftputil.sync.LocalHost()
41        syncer = ftputil.sync.Syncer(source, target)
42        source_dir = os.path.join(TEST_ROOT, "test_source/")
43        target_dir = os.path.join(TEST_ROOT, "test_target")
44        syncer.sync(source_dir, target_dir)
45
46
47# Helper classes for `TestUploadFromWindows`
48
49class LocalWindowsHost(ftputil.sync.LocalHost):
50
51    def __init__(self):
52        self.path = ntpath
53        self.sep = "\\"
54
55    def open(self, path, mode):
56        # Just return a dummy file object.
57        return io.StringIO("")
58
59    def walk(self, root):
60        """
61        Return a list of tuples as `os.walk`, but use tuples as if the
62        directory structure was
63
64        <root>
65            dir1
66                dir11
67                file1
68                file2
69
70        where <root> is the string passed in as `root`.
71        """
72        join = ntpath.join
73        return [(root,
74                 [join(root, "dir1")],
75                 []),
76                (join(root, "dir1"),
77                 ["dir11"],
78                 ["file1", "file2"])
79                ]
80
81
82class DummyFTPSession(object):
83
84    def pwd(self):
85        return "/"
86
87
88class DummyFTPPath(object):
89
90    def abspath(self, path):
91        # Don't care here if the path is absolute or not.
92        return path
93
94    def isdir(self, path):
95        return ("dir" in path)
96
97    def isfile(self, path):
98        return ("file" in path)
99
100
101class ArgumentCheckingFTPHost(ftputil.FTPHost):
102
103    def __init__(self, *args, **kwargs):
104        super(ArgumentCheckingFTPHost, self).__init__(*args, **kwargs)
105        self.path = DummyFTPPath()
106
107    def _make_session(self, *args, **kwargs):
108        return DummyFTPSession()
109
110    def mkdir(self, path):
111        assert "\\" not in path
112
113    def open(self, path, mode):
114        assert "\\" not in path
115        return io.StringIO("")
116
117
118class TestUploadFromWindows(object):
119
120    def test_no_mixed_separators(self):
121        source = LocalWindowsHost()
122        target = ArgumentCheckingFTPHost()
123        local_root = ntpath.join("some", "directory")
124        syncer = ftputil.sync.Syncer(source, target)
125        # If the following call raises any `AssertionError`s, the
126        # test framework will catch them and show them.
127        syncer.sync(local_root, "not_used_by_ArgumentCheckingFTPHost")
Note: See TracBrowser for help on using the repository browser.