|
2 | 2 | import types
|
3 | 3 | import unittest
|
4 | 4 | import warnings
|
| 5 | +import mock |
| 6 | +from backports import tempfile |
5 | 7 | from pythonforandroid.build import Context
|
6 | 8 | from pythonforandroid.recipe import Recipe, import_recipe
|
7 | 9 |
|
8 | 10 |
|
| 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 | + |
9 | 27 | class TestRecipe(unittest.TestCase):
|
10 | 28 |
|
11 | 29 | def test_recipe_dirs(self):
|
@@ -58,3 +76,49 @@ def test_import_recipe(self):
|
58 | 76 | module = import_recipe(name, pathname)
|
59 | 77 | assert module is not None
|
60 | 78 | 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 |
0 commit comments