source: test/test_base.py

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