Skip to content

Commit 3dabded

Browse files
authored
Merge pull request #1946 from AndreMiras/feature/unit_test_recipe_download
Unit tests Recipe download feature
2 parents e21cd8b + 721e7c5 commit 3dabded

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

pythonforandroid/recipe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ def report_hook(index, blksize, size):
156156
while True:
157157
try:
158158
urlretrieve(url, target, report_hook)
159-
except OSError as e:
159+
except OSError:
160160
attempts += 1
161161
if attempts >= 5:
162-
raise e
162+
raise
163163
stdout.write('Download failed retrying in a second...')
164164
time.sleep(1)
165165
continue

tests/test_recipe.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,28 @@
22
import types
33
import unittest
44
import warnings
5+
import mock
6+
from backports import tempfile
57
from pythonforandroid.build import Context
68
from pythonforandroid.recipe import Recipe, import_recipe
79

810

11+
def patch_logger(level):
12+
return mock.patch('pythonforandroid.recipe.{}'.format(level))
13+
14+
15+
def patch_logger_info():
16+
return patch_logger('info')
17+
18+
19+
def patch_logger_debug():
20+
return patch_logger('debug')
21+
22+
23+
class DummyRecipe(Recipe):
24+
pass
25+
26+
927
class TestRecipe(unittest.TestCase):
1028

1129
def test_recipe_dirs(self):
@@ -58,3 +76,49 @@ def test_import_recipe(self):
5876
module = import_recipe(name, pathname)
5977
assert module is not None
6078
assert recorded_warnings == []
79+
80+
def test_download_if_necessary(self):
81+
"""
82+
Download should happen via `Recipe.download()` only if the recipe
83+
specific environment variable is not set.
84+
"""
85+
# download should happen as the environment variable is not set
86+
recipe = DummyRecipe()
87+
with mock.patch.object(Recipe, 'download') as m_download:
88+
recipe.download_if_necessary()
89+
assert m_download.call_args_list == [mock.call()]
90+
# after setting it the download should be skipped
91+
env_var = 'P4A_test_recipe_DIR'
92+
env_dict = {env_var: '1'}
93+
with mock.patch.object(Recipe, 'download') as m_download, mock.patch.dict(os.environ, env_dict):
94+
recipe.download_if_necessary()
95+
assert m_download.call_args_list == []
96+
97+
def test_download(self):
98+
"""
99+
Verifies the actual download gets triggered when the URL is set.
100+
"""
101+
# test with no URL set
102+
recipe = DummyRecipe()
103+
with patch_logger_info() as m_info:
104+
recipe.download()
105+
assert m_info.call_args_list == [
106+
mock.call('Skipping test_recipe download as no URL is set')]
107+
# when the URL is set `Recipe.download_file()` should be called
108+
filename = 'Python-3.7.4.tgz'
109+
url = 'https://www.python.org/ftp/python/3.7.4/{}'.format(filename)
110+
recipe._url = url
111+
recipe.ctx = Context()
112+
with (
113+
patch_logger_debug()) as m_debug, (
114+
mock.patch.object(Recipe, 'download_file')) as m_download_file, (
115+
mock.patch('pythonforandroid.recipe.sh.touch')) as m_touch, (
116+
tempfile.TemporaryDirectory()) as temp_dir:
117+
recipe.ctx.setup_dirs(temp_dir)
118+
recipe.download()
119+
assert m_download_file.call_args_list == [mock.call(url, filename)]
120+
assert m_debug.call_args_list == [
121+
mock.call(
122+
'Downloading test_recipe from '
123+
'https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz')]
124+
assert m_touch.call_count == 1

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ deps =
88
pytest
99
virtualenv
1010
py3: coveralls
11+
backports.tempfile
1112
# makes it possible to override pytest args, e.g.
1213
# tox -- tests/test_graph.py
1314
commands = pytest {posargs:tests/}

0 commit comments

Comments
 (0)