source: test/test_base.py

Last change on this file was 1843:80e8e9872c20, checked in by Stefan Schwarzer <sschwarzer@…>, 5 months ago
Make `session_factory` mandatory With the switch to the "scripted session" approach, we always need the factory for mocking. Without the previous default value for `session_factory`, we no longer need to import `mock_ftplib`.
File size: 3.5 KB
Line 
1# Copyright (C) 2003-2019, Stefan Schwarzer <sschwarzer@sschwarzer.net>
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5import io
6
7import ftputil
8
9
10# Since `io.BytesIO` and `io.StringIO` are built-in, they can't be
11# patched with `unittest.mock.patch`. However, derived classes can be
12# mocked. Mocking is useful to test the arguments of `write` calls,
13# i. e. whether the expected data was written.
14class MockableBytesIO(io.BytesIO):
15    pass
16
17class MockableStringIO(io.StringIO):
18    pass
19
20
21# Factory to produce `FTPHost`-like classes from a given `FTPHost`
22# class and (usually) a given `MockSession` class.
23def ftp_host_factory(session_factory,
24                     ftp_host_class=ftputil.FTPHost):
25    return ftp_host_class("dummy_host", "dummy_user", "dummy_password",
26                          session_factory=session_factory)
27
28
29def dir_line(mode_string="-r--r--r--",
30             nlink=1,
31             user="dummy_user",
32             group="dummy_group",
33             size=512,
34             date_=None,
35             datetime_=None,
36             name="dummy_name",
37             link_target=None):
38    """
39    Return a line as it would be returned by an FTP `DIR` invocation.
40
41    Some values are handled specially:
42
43    - One of `date_` or `datetime_` must be given, the other must be `None`.
44
45      If `date_` is given, it must be a `datetime.date` object. The timestamp
46      in the `DIR` line is formatted like "Apr 22 2019", with the concrete
47      value taken from the `date_` object.
48
49      If `datetime_` is given, it must be a `datetime.datetime` object. The
50      timestamp in the `DIR` line is formatted like "Apr 22 16:50", with the
51      concrete value taken from the `datetime_` object. Timezone information in
52      the `datetime_` object is ignored.
53
54    - If `link_target` is left at the default `None`, the name part is the
55      value from the `name` argument. If `link_target` isn't `None`, the name
56      part of the `DIR` line is formatted as "name -> link_target".
57
58    Note that the spacing between the parts of the line isn't necessarily
59    exactly what you'd get from an FTP server because the parser in ftputil
60    doesn't take the exact amount of spaces into account, so the `DIR` lines
61    don't have to be that accurate.
62
63    Examples:
64
65      # Result:
66      # "drwxr-xr-x  2  dummy_user dummy_group  182  Apr 22 16:50  file_name"
67      line = dir_line(mode_string="-rw-rw-r--",
68                      nlink=2,
69                      size=182,
70                      datetime=datetime.datetime.now(),
71                      name="file_name")
72 
73      # Result:
74      # "drwxr-xr-x  1  dummy_user dummy_group  512  Apr 22 2019  dir_name -> dir_target"
75      line = dir_line(mode_string="drwxr-xr-x",
76                      date=datetime.date.today(),
77                      name="dir_name",
78                      link_target="dir_target")
79    """
80    # Date or datetime. We must have exactly one of `date_` and `datetime_`
81    # set. The other value must be `None`.
82    assert [date_, datetime_].count(None) == 1, "specify exactly one of `date_` and `datetime_`"
83    if date_:
84        datetime_string = date_.strftime("%b %d %Y")
85    else:
86        datetime_string = datetime_.strftime("%b %d %H:%M")
87    # Name, possibly with link target
88    if not link_target:
89        name_string = name
90    else:
91        name_string = "{} -> {}".format(name, link_target)
92    #
93    return "{}  {}  {} {}  {}  {}  {}".format(mode_string, nlink, user, group, size,
94                                              datetime_string, name_string)
Note: See TracBrowser for help on using the repository browser.