forked from byteskeptical/sftpretty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_get.py
66 lines (54 loc) · 2.39 KB
/
test_get.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''test sftpretty.get'''
import pytest
from common import conn, tempfile_containing, VFS
from pathlib import Path
from sftpretty import Connection
from unittest.mock import Mock
def test_get(sftpserver):
'''download a file'''
with sftpserver.serve_content(VFS):
with Connection(**conn(sftpserver)) as psftp:
psftp.chdir('pub/foo1')
with tempfile_containing('') as fname:
psftp.get('foo1.txt', fname)
assert open(fname, 'rb').read() == b'content of foo1.txt'
def test_get_callback(sftpserver):
'''test .get callback'''
with sftpserver.serve_content(VFS):
with Connection(**conn(sftpserver)) as psftp:
psftp.chdir('pub/foo1')
cback = Mock(return_value=None)
with tempfile_containing('') as fname:
result = psftp.get('foo1.txt', fname, callback=cback)
assert open(fname, 'rb').read() == b'content of foo1.txt'
# verify callback was called
assert cback.call_count
# unlike .put() nothing is returned from the operation
assert result is None
def test_get_bad_remote(sftpserver):
'''download a file'''
with sftpserver.serve_content(VFS):
with Connection(**conn(sftpserver)) as psftp:
psftp.chdir('pub/foo1')
with tempfile_containing('') as fname:
with pytest.raises(IOError):
psftp.get('readme-not-there.txt', fname)
assert open(fname, 'rb').read()[0:7] != b'Welcome'
def test_get_preserve_mtime(sftpserver):
'''test that m_time is preserved from local to remote, when get'''
with sftpserver.serve_content(VFS):
with Connection(**conn(sftpserver)) as psftp:
psftp.chdir('pub/foo1')
remotefile = 'foo1.txt'
r_stat = psftp.stat(remotefile)
with tempfile_containing('') as localfile:
psftp.get(remotefile, localfile, preserve_mtime=True)
assert r_stat.st_mtime == Path(localfile).stat().st_mtime
def test_get_glob_fails(sftpserver):
'''try and use get a file with a pattern - Fails'''
with sftpserver.serve_content(VFS):
with Connection(**conn(sftpserver)) as psftp:
psftp.chdir('pub/foo1')
with tempfile_containing('') as fname:
with pytest.raises(IOError):
psftp.get('*', fname)