Python3 compability for tests

This commit is contained in:
Sebastian Lohff 2019-02-17 18:07:26 +01:00
parent d52669b8b0
commit 638668242d
1 changed files with 10 additions and 6 deletions

View File

@ -43,7 +43,9 @@ def datadir(tmp_path):
new_path.mkdir() new_path.mkdir()
_datadir(v, new_path) _datadir(v, new_path)
else: else:
(path / k).write_text(v.decode()) if hasattr(v, 'decode'):
v = v.decode() # python2 compability
(path / k).write_text(v)
return path return path
return _datadir return _datadir
@ -66,6 +68,7 @@ def download_ok(r, expected_data, expected_code=200):
def make_request(path='/', host='localhost', port=8080, method='get', protocol='http', **kwargs): def make_request(path='/', host='localhost', port=8080, method='get', protocol='http', **kwargs):
url = '{}://{}:{}{}'.format(protocol, host, port, path) url = '{}://{}:{}{}'.format(protocol, host, port, path)
print('Calling {} on {} with {}'.format(method, url, kwargs))
r = getattr(requests, method)(url, **kwargs) r = getattr(requests, method)(url, **kwargs)
return r return r
@ -83,9 +86,10 @@ def check_download(expected_data=None, path='/', status_code=200, **kwargs):
def test_version(run_servefile): def test_version(run_servefile):
s = run_servefile('--version', stderr=subprocess.PIPE) # we expect the version on stdout (python3.4+) or stderr(python2.6-3.3)
s = run_servefile('--version', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
s.wait() s.wait()
version = s.stderr.readline().strip() version = s.stdout.readline().decode().strip()
assert version == 'servefile 0.4.5-unreleased' assert version == 'servefile 0.4.5-unreleased'
@ -192,7 +196,7 @@ def test_upload(run_servefile, tmp_path):
assert 'multipart/form-data' in r.text assert 'multipart/form-data' in r.text
# upload file # upload file
files = {'file': ('haiku.txt', data.decode())} files = {'file': ('haiku.txt', data)}
r = make_request(method='post', files=files) r = make_request(method='post', files=files)
assert 'Thanks' in r.text assert 'Thanks' in r.text
assert r.status_code == 200 assert r.status_code == 200
@ -200,7 +204,7 @@ def test_upload(run_servefile, tmp_path):
assert f.read() == data assert f.read() == data
# upload file AGAIN!! (and check it is available unter a different name) # upload file AGAIN!! (and check it is available unter a different name)
files = {'file': ('haiku.txt', data.decode())} files = {'file': ('haiku.txt', data)}
r = make_request(method='post', files=files) r = make_request(method='post', files=files)
assert r.status_code == 200 assert r.status_code == 200
with open(str(uploaddir / 'haiku.txt(1)')) as f: with open(str(uploaddir / 'haiku.txt(1)')) as f:
@ -246,7 +250,7 @@ def test_tar_mode(run_servefile, datadir):
for filename, content in d['foo'].items(): for filename, content in d['foo'].items():
info = tar.getmember('foo/{}'.format(filename)) info = tar.getmember('foo/{}'.format(filename))
assert info.isfile assert info.isfile
assert tar.extractfile(info.path).read() == content assert tar.extractfile(info.path).read().decode() == content
def test_tar_compression(run_servefile, datadir): def test_tar_compression(run_servefile, datadir):