Skip to content

Commit 8f9405c

Browse files
committed
Move download_file helpers to tests/support/pytest/helpers.py
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
1 parent 0e6fb14 commit 8f9405c

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

tests/support/helpers.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
import attr
3636
import pytest
37-
import requests
3837
import tornado.ioloop
3938
import tornado.web
4039
from pytestshellutils.exceptions import ProcessFailed
@@ -1897,15 +1896,3 @@ def __enter__(self):
18971896

18981897
def __exit__(self, *_):
18991898
shutil.rmtree(str(self.priv_path.parent), ignore_errors=True)
1900-
1901-
1902-
@pytest.helpers.register
1903-
def download_file(url, dest, auth=None):
1904-
# NOTE the stream=True parameter below
1905-
with requests.get(url, allow_redirects=True, stream=True, auth=auth) as r:
1906-
r.raise_for_status()
1907-
with salt.utils.files.fopen(dest, "wb") as f:
1908-
for chunk in r.iter_content(chunk_size=8192):
1909-
if chunk:
1910-
f.write(chunk)
1911-
return dest

tests/support/pytest/helpers.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import attr
2020
import pytest
21+
import requests
2122
from saltfactories.utils import random_string
2223
from saltfactories.utils.tempfiles import temp_file
2324

@@ -70,9 +71,7 @@ def test_blah():
7071
elif saltenv == "prod":
7172
directory = RUNTIME_VARS.TMP_PRODENV_STATE_TREE
7273
else:
73-
raise RuntimeError(
74-
'"saltenv" can only be "base" or "prod", not "{}"'.format(saltenv)
75-
)
74+
raise RuntimeError(f'"saltenv" can only be "base" or "prod", not "{saltenv}"')
7675
return temp_file(
7776
name, contents, directory=directory, strip_first_newline=strip_first_newline
7877
)
@@ -118,9 +117,7 @@ def test_blah():
118117
elif saltenv == "prod":
119118
directory = RUNTIME_VARS.TMP_PRODENV_PILLAR_TREE
120119
else:
121-
raise RuntimeError(
122-
'"saltenv" can only be "base" or "prod", not "{}"'.format(saltenv)
123-
)
120+
raise RuntimeError(f'"saltenv" can only be "base" or "prod", not "{saltenv}"')
124121
return temp_file(
125122
name, contents, directory=directory, strip_first_newline=strip_first_newline
126123
)
@@ -161,7 +158,7 @@ def salt_loader_module_functions(module):
161158
# Not a function? carry on
162159
continue
163160
funcname = func_alias.get(func.__name__) or func.__name__
164-
funcs["{}.{}".format(virtualname, funcname)] = func
161+
funcs[f"{virtualname}.{funcname}"] = func
165162
return funcs
166163

167164

@@ -178,7 +175,7 @@ def remove_stale_minion_key(master, minion_id):
178175
def remove_stale_proxy_minion_cache_file(proxy_minion, minion_id=None):
179176
cachefile = os.path.join(
180177
proxy_minion.config["cachedir"],
181-
"dummy-proxy-{}.cache".format(minion_id or proxy_minion.id),
178+
f"dummy-proxy-{minion_id or proxy_minion.id}.cache",
182179
)
183180
if os.path.exists(cachefile):
184181
os.unlink(cachefile)
@@ -273,7 +270,7 @@ def _default_hashed_password(self):
273270
@group_name.default
274271
def _default_group_name(self):
275272
if self.create_group:
276-
return "group-{}".format(self.username)
273+
return f"group-{self.username}"
277274
return None
278275

279276
@_group.default
@@ -788,6 +785,18 @@ def change_cwd(path):
788785
os.chdir(old_cwd)
789786

790787

788+
@pytest.helpers.register
789+
def download_file(url, dest, auth=None):
790+
# NOTE the stream=True parameter below
791+
with requests.get(url, allow_redirects=True, stream=True, auth=auth) as r:
792+
r.raise_for_status()
793+
with salt.utils.files.fopen(dest, "wb") as f:
794+
for chunk in r.iter_content(chunk_size=8192):
795+
if chunk:
796+
f.write(chunk)
797+
return dest
798+
799+
791800
# Only allow star importing the functions defined in this module
792801
__all__ = [
793802
name

0 commit comments

Comments
 (0)