|
8 | 8 |
|
9 | 9 | import fsspec |
10 | 10 | from fsspec import open_files |
11 | | -from fsspec.implementations.ftp import FTPFileSystem, ImplicitFTPTLS |
| 11 | +from fsspec.implementations.ftp import FTPFileSystem, ImplicitFTPTLS, _mlsd2 |
12 | 12 |
|
13 | 13 | ftplib = pytest.importorskip("ftplib") |
14 | 14 | here = os.path.dirname(os.path.abspath(__file__)) |
@@ -246,3 +246,85 @@ def test_rm_get_recursive(ftp_writable, tmpdir): |
246 | 246 |
|
247 | 247 | fs.rm("/tmp/topdir", recursive=True) |
248 | 248 | assert not fs.exists("/tmp/topdir") |
| 249 | + |
| 250 | + |
| 251 | +class _FakeDirFTP: |
| 252 | + """Minimal stand-in for ftplib.FTP whose .dir(path, callback) replays |
| 253 | + a canned `ls -l` style listing — enough to exercise the _mlsd2 parser |
| 254 | + without needing an FTP server that disables MLSD.""" |
| 255 | + |
| 256 | + def __init__(self, lines): |
| 257 | + self._lines = lines |
| 258 | + |
| 259 | + def dir(self, path, callback): |
| 260 | + for line in self._lines: |
| 261 | + callback(line) |
| 262 | + |
| 263 | + |
| 264 | +def test_mlsd2_parses_filename_without_spaces(): |
| 265 | + ftp = _FakeDirFTP(["-rw-r--r-- 1 owner group 38283 Jan 12 07:08 plain.pdf"]) |
| 266 | + out = _mlsd2(ftp, "/data") |
| 267 | + assert out == [ |
| 268 | + ( |
| 269 | + "plain.pdf", |
| 270 | + { |
| 271 | + "modify": "Jan 12 07:08", |
| 272 | + "unix.owner": "owner", |
| 273 | + "unix.group": "group", |
| 274 | + "unix.mode": "-rw-r--r--", |
| 275 | + "size": "38283", |
| 276 | + "type": "file", |
| 277 | + }, |
| 278 | + ) |
| 279 | + ] |
| 280 | + |
| 281 | + |
| 282 | +def test_mlsd2_parses_filename_with_spaces(): |
| 283 | + # Regression test: when the FTP server doesn't support MLSD, _mlsd2 falls |
| 284 | + # back to parsing `dir` output. Previously it took split_line[-1] as the |
| 285 | + # filename, which truncated names containing whitespace. |
| 286 | + ftp = _FakeDirFTP( |
| 287 | + [ |
| 288 | + "-rw-r--r-- 1 owner group 38283 Jan 12 07:08 my file.pdf", |
| 289 | + "drwxr-xr-x 2 owner group 4096 Mar 15 10:30 a dir with spaces", |
| 290 | + "-rw-r--r-- 1 owner group 100 Feb 3 09:15 trailing spaces.txt", |
| 291 | + ] |
| 292 | + ) |
| 293 | + out = _mlsd2(ftp, "/data") |
| 294 | + names = [name for name, _ in out] |
| 295 | + assert names == [ |
| 296 | + "my file.pdf", |
| 297 | + "a dir with spaces", |
| 298 | + "trailing spaces.txt", |
| 299 | + ] |
| 300 | + assert out[0][1]["type"] == "file" |
| 301 | + # _mlsd2 itself reports "dir"; ls() promotes it to "directory" later. |
| 302 | + assert out[1][1]["type"] == "dir" |
| 303 | + assert out[2][1]["size"] == "100" |
| 304 | + |
| 305 | + |
| 306 | +def test_mlsd2_parses_symlink_name_without_target(): |
| 307 | + # `ls -l` formats symlinks as "<name> -> <target>". Only the link's own |
| 308 | + # name should be returned; the previous parser took split_line[-1] and so |
| 309 | + # returned the target instead of the name. |
| 310 | + ftp = _FakeDirFTP( |
| 311 | + ["lrwxrwxrwx 1 owner group 10 Jan 12 07:08 link name -> some/target"] |
| 312 | + ) |
| 313 | + out = _mlsd2(ftp, "/data") |
| 314 | + assert len(out) == 1 |
| 315 | + name, info = out[0] |
| 316 | + assert name == "link name" |
| 317 | + assert info["unix.mode"].startswith("l") |
| 318 | + |
| 319 | + |
| 320 | +def test_mlsd2_skips_short_lines(): |
| 321 | + # The `total N` summary line emitted by some servers has fewer than 9 |
| 322 | + # fields and should be ignored. |
| 323 | + ftp = _FakeDirFTP( |
| 324 | + [ |
| 325 | + "total 4", |
| 326 | + "-rw-r--r-- 1 owner group 1 Jan 12 07:08 keep.txt", |
| 327 | + ] |
| 328 | + ) |
| 329 | + out = _mlsd2(ftp, "/data") |
| 330 | + assert [name for name, _ in out] == ["keep.txt"] |
0 commit comments