| 1 |
# Copyright 2015-2026 Facundo Batista, Nicolás Demarchi |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify it |
| 4 |
# under the terms of the GNU General Public License version 3, as published |
| 5 |
# by the Free Software Foundation. |
| 6 |
# |
| 7 |
# This program is distributed in the hope that it will be useful, but |
| 8 |
# WITHOUT ANY WARRANTY; without even the implied warranties of |
| 9 |
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 10 |
# PURPOSE. See the GNU General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU General Public License along |
| 13 |
# with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 |
# |
| 15 |
# For further info, check https://github.com/PyAr/fades |
| 16 |
|
| 17 |
"""Tests for some code in main.""" |
| 18 |
|
| 19 |
import os |
| 20 |
import unittest |
| 21 |
from pathlib import Path |
| 22 |
from unittest.mock import patch |
| 23 |
|
| 24 |
from packaging.requirements import Requirement |
| 25 |
|
| 26 |
from fades import VERSION, FadesError, __version__, main, parsing, REPO_PYPI, REPO_VCS |
| 27 |
from tests import create_tempfile |
| 28 |
|
| 29 |
|
| 30 |
class VirtualenvCheckingTestCase(unittest.TestCase): |
| 31 |
"""Tests for the virtualenv checker.""" |
| 32 |
|
| 33 |
def test_have_realprefix(self): |
| 34 |
resp = main.detect_inside_virtualenv('prefix', 'real_prefix', 'base_prefix') |
| 35 |
self.assertTrue(resp) |
| 36 |
|
| 37 |
def test_no_baseprefix(self): |
| 38 |
resp = main.detect_inside_virtualenv('prefix', None, None) |
| 39 |
self.assertFalse(resp) |
| 40 |
|
| 41 |
def test_prefix_is_baseprefix(self): |
| 42 |
resp = main.detect_inside_virtualenv('prefix', None, 'prefix') |
| 43 |
self.assertFalse(resp) |
| 44 |
|
| 45 |
def test_prefix_is_not_baseprefix(self): |
| 46 |
resp = main.detect_inside_virtualenv('prefix', None, 'other prefix') |
| 47 |
self.assertTrue(resp) |
| 48 |
|
| 49 |
|
| 50 |
class DepsGatheringTestCase(unittest.TestCase): |
| 51 |
"""Tests for the gathering stage of consolidate_dependencies.""" |
| 52 |
|
| 53 |
def test_needs_ipython(self): |
| 54 |
d = main.consolidate_dependencies(needs_ipython=True, child_program=None, |
| 55 |
requirement_files=None, manual_dependencies=None) |
| 56 |
|
| 57 |
self.assertDictEqual(d, {'pypi': {Requirement('ipython')}}) |
| 58 |
|
| 59 |
def test_child_program(self): |
| 60 |
child_program = 'tests/test_files/req_module.py' |
| 61 |
|
| 62 |
d = main.consolidate_dependencies(needs_ipython=False, child_program=child_program, |
| 63 |
requirement_files=None, manual_dependencies=None) |
| 64 |
|
| 65 |
self.assertDictEqual(d, {'pypi': {Requirement('foo'), Requirement('bar')}}) |
| 66 |
|
| 67 |
def test_requirement_files(self): |
| 68 |
requirement_files = [create_tempfile(self, ['dep'])] |
| 69 |
|
| 70 |
d = main.consolidate_dependencies(needs_ipython=False, child_program=None, |
| 71 |
requirement_files=requirement_files, |
| 72 |
manual_dependencies=None) |
| 73 |
|
| 74 |
self.assertDictEqual(d, {'pypi': {Requirement('dep')}}) |
| 75 |
|
| 76 |
def test_manual_dependencies(self): |
| 77 |
manual_dependencies = ['dep'] |
| 78 |
|
| 79 |
d = main.consolidate_dependencies(needs_ipython=False, child_program=None, |
| 80 |
requirement_files=None, |
| 81 |
manual_dependencies=manual_dependencies) |
| 82 |
|
| 83 |
self.assertDictEqual(d, {'pypi': {Requirement('dep')}}) |
| 84 |
|
| 85 |
|
| 86 |
class DepsMergingTestCase(unittest.TestCase): |
| 87 |
"""Tests for the merging stage of consolidate_dependencies.""" |
| 88 |
|
| 89 |
def test_two_different(self): |
| 90 |
requirement_files = [create_tempfile(self, ['1', '2'])] |
| 91 |
manual_dependencies = ['vcs::3', 'vcs::4'] |
| 92 |
|
| 93 |
d = main.consolidate_dependencies(needs_ipython=False, child_program=None, |
| 94 |
requirement_files=requirement_files, |
| 95 |
manual_dependencies=manual_dependencies) |
| 96 |
|
| 97 |
self.assertEqual(d, { |
| 98 |
'pypi': {Requirement('1'), Requirement('2')}, |
| 99 |
'vcs': {parsing.VCSDependency('3'), parsing.VCSDependency('4')} |
| 100 |
}) |
| 101 |
|
| 102 |
def test_two_same_repo(self): |
| 103 |
requirement_files = [create_tempfile(self, ['1', '2'])] |
| 104 |
manual_dependencies = ['3', '4'] |
| 105 |
|
| 106 |
d = main.consolidate_dependencies(needs_ipython=False, child_program=None, |
| 107 |
requirement_files=requirement_files, |
| 108 |
manual_dependencies=manual_dependencies) |
| 109 |
|
| 110 |
self.assertDictEqual(d, { |
| 111 |
'pypi': {Requirement('1'), Requirement('2'), Requirement('3'), Requirement('4')} |
| 112 |
}) |
| 113 |
|
| 114 |
def test_complex_case(self): |
| 115 |
child_program = create_tempfile(self, ['"""fades:', '1', '2', '"""']) |
| 116 |
requirement_files = [create_tempfile(self, ['3', 'vcs::5'])] |
| 117 |
manual_dependencies = ['vcs::4', 'vcs::6'] |
| 118 |
|
| 119 |
d = main.consolidate_dependencies(needs_ipython=False, child_program=child_program, |
| 120 |
requirement_files=requirement_files, |
| 121 |
manual_dependencies=manual_dependencies) |
| 122 |
|
| 123 |
self.assertEqual(d, { |
| 124 |
'pypi': {Requirement('1'), Requirement('2'), Requirement('3')}, |
| 125 |
'vcs': {parsing.VCSDependency('5'), parsing.VCSDependency('4'), |
| 126 |
parsing.VCSDependency('6')} |
| 127 |
}) |
| 128 |
|
| 129 |
def test_one_duplicated(self): |
| 130 |
requirement_files = [create_tempfile(self, ['2', '2'])] |
| 131 |
manual_dependencies = None |
| 132 |
|
| 133 |
d = main.consolidate_dependencies(needs_ipython=False, child_program=None, |
| 134 |
requirement_files=requirement_files, |
| 135 |
manual_dependencies=manual_dependencies) |
| 136 |
|
| 137 |
self.assertDictEqual(d, { |
| 138 |
'pypi': {Requirement('2')} |
| 139 |
}) |
| 140 |
|
| 141 |
def test_two_different_with_dups(self): |
| 142 |
requirement_files = [create_tempfile(self, ['1', '2', '2', '2'])] |
| 143 |
manual_dependencies = ['vcs::3', 'vcs::4', 'vcs::1', 'vcs::2'] |
| 144 |
|
| 145 |
d = main.consolidate_dependencies(needs_ipython=False, child_program=None, |
| 146 |
requirement_files=requirement_files, |
| 147 |
manual_dependencies=manual_dependencies) |
| 148 |
|
| 149 |
self.assertEqual(d, { |
| 150 |
'pypi': {Requirement('1'), Requirement('2')}, |
| 151 |
'vcs': {parsing.VCSDependency('1'), parsing.VCSDependency('2'), |
| 152 |
parsing.VCSDependency('3'), parsing.VCSDependency('4')} |
| 153 |
}) |
| 154 |
|
| 155 |
|
| 156 |
class MiscTestCase(unittest.TestCase): |
| 157 |
"""Miscellaneous tests.""" |
| 158 |
|
| 159 |
def test_version_show(self): |
| 160 |
self.assertEqual( |
| 161 |
__version__, |
| 162 |
'.'.join([str(v) for v in VERSION]), |
| 163 |
) |
| 164 |
|
| 165 |
|
| 166 |
class ChildProgramDeciderTestCase(unittest.TestCase): |
| 167 |
"""Check how the child program is decided.""" |
| 168 |
|
| 169 |
def test_indicated_with_executable_flag(self): |
| 170 |
analyzable, child = main.decide_child_program(True, False, "foobar.py") |
| 171 |
self.assertIsNone(analyzable) |
| 172 |
self.assertEqual(child, "foobar.py") |
| 173 |
|
| 174 |
def test_no_child_at_all(self): |
| 175 |
analyzable, child = main.decide_child_program(False, False, None) |
| 176 |
self.assertIsNone(analyzable) |
| 177 |
self.assertIsNone(child) |
| 178 |
|
| 179 |
def test_normal_child_program(self): |
| 180 |
child_path = create_tempfile(self, "") |
| 181 |
analyzable, child = main.decide_child_program(False, False, child_path) |
| 182 |
self.assertEqual(analyzable, Path(child_path)) |
| 183 |
self.assertEqual(child, child_path) |
| 184 |
|
| 185 |
def test_normal_child_program_not_found(self): |
| 186 |
with self.assertRaises(FadesError): |
| 187 |
main.decide_child_program(False, False, 'does_not_exist.py') |
| 188 |
|
| 189 |
def test_normal_child_program_no_access(self): |
| 190 |
child_path = create_tempfile(self, "") |
| 191 |
os.chmod(child_path, 333) # Remove read permission. |
| 192 |
self.addCleanup(os.chmod, child_path, 644) |
| 193 |
with self.assertRaises(FadesError): |
| 194 |
main.decide_child_program(False, False, 'does_not_exist.py') |
| 195 |
|
| 196 |
def test_remote_child_program_simple(self): |
| 197 |
with patch('fades.helpers.download_remote_script') as mock: |
| 198 |
mock.return_value = "new_path_script" |
| 199 |
analyzable, child = main.decide_child_program( |
| 200 |
False, False, "http://scripts.com/foobar.py") |
| 201 |
mock.assert_called_with("http://scripts.com/foobar.py") |
| 202 |
|
| 203 |
# check that analyzable and child are the same, and that its content is the remote one |
| 204 |
self.assertEqual(analyzable, Path("new_path_script")) |
| 205 |
self.assertEqual(child, "new_path_script") |
| 206 |
|
| 207 |
def test_remote_child_program_ssl(self): |
| 208 |
with patch('fades.helpers.download_remote_script') as mock: |
| 209 |
mock.return_value = "new_path_script" |
| 210 |
analyzable, child = main.decide_child_program( |
| 211 |
False, False, "https://scripts.com/foobar.py") |
| 212 |
mock.assert_called_with("https://scripts.com/foobar.py") |
| 213 |
|
| 214 |
# check that analyzable and child are the same, and that its content is the remote one |
| 215 |
self.assertEqual(analyzable, Path("new_path_script")) |
| 216 |
self.assertEqual(child, "new_path_script") |
| 217 |
|
| 218 |
def test_indicated_with_executable_flag_with_relative_path(self): |
| 219 |
"""Relative paths not allowed when using --exec.""" |
| 220 |
with self.assertRaises(FadesError): |
| 221 |
main.decide_child_program(True, False, os.path.join("path", "../foobar.py")) |
| 222 |
|
| 223 |
def test_indicated_with_executable_flag_with_absolute_path(self): |
| 224 |
"""Absolute paths are allowed when using --exec.""" |
| 225 |
analyzable, child = main.decide_child_program(True, False, "/tmp/foo/bar.py") |
| 226 |
self.assertIsNone(analyzable) |
| 227 |
self.assertEqual(child, "/tmp/foo/bar.py") |
| 228 |
|
| 229 |
def test_module(self): |
| 230 |
child_path = 'foo.bar' |
| 231 |
analyzable, child = main.decide_child_program(False, True, child_path) |
| 232 |
self.assertIsNone(analyzable) |
| 233 |
self.assertEqual(child, child_path) |
| 234 |
|
| 235 |
|
| 236 |
# --------------------------------------- |
| 237 |
# autoimport tests |
| 238 |
|
| 239 |
def _autoimport_safe_call(*args, **kwargs): |
| 240 |
"""Call the tested function and always remove the tempfile after the test.""" |
| 241 |
fpath = main.get_autoimport_scriptname(*args, **kwargs) |
| 242 |
|
| 243 |
with open(fpath, "rt", encoding='utf8') as fh: |
| 244 |
content = fh.read() |
| 245 |
os.unlink(fpath) |
| 246 |
|
| 247 |
return content |
| 248 |
|
| 249 |
|
| 250 |
def test_autoimport_simple(): |
| 251 |
"""Simplest autoimport call.""" |
| 252 |
dependencies = { |
| 253 |
REPO_PYPI: {Requirement('mymod')}, |
| 254 |
} |
| 255 |
content = _autoimport_safe_call(dependencies, is_ipython=False) |
| 256 |
|
| 257 |
assert content.startswith(main.AUTOIMPORT_HEADER) |
| 258 |
assert main.AUTOIMPORT_MOD_IMPORTER.format(module='mymod') in content |
| 259 |
|
| 260 |
|
| 261 |
def test_autoimport_several_dependencies(): |
| 262 |
"""Indicate several dependencies.""" |
| 263 |
dependencies = { |
| 264 |
REPO_PYPI: {Requirement('mymod1'), Requirement('mymod2')}, |
| 265 |
} |
| 266 |
content = _autoimport_safe_call(dependencies, is_ipython=False) |
| 267 |
|
| 268 |
assert content.startswith(main.AUTOIMPORT_HEADER) |
| 269 |
assert main.AUTOIMPORT_MOD_IMPORTER.format(module='mymod1') in content |
| 270 |
assert main.AUTOIMPORT_MOD_IMPORTER.format(module='mymod2') in content |
| 271 |
|
| 272 |
|
| 273 |
def test_autoimport_including_ipython(): |
| 274 |
"""Call with ipython modifier.""" |
| 275 |
dependencies = { |
| 276 |
REPO_PYPI: { |
| 277 |
Requirement('mymod'), |
| 278 |
Requirement('ipython'), # this one is automatically added |
| 279 |
}, |
| 280 |
} |
| 281 |
content = _autoimport_safe_call(dependencies, is_ipython=True) |
| 282 |
|
| 283 |
assert main.AUTOIMPORT_HEADER not in content |
| 284 |
assert main.AUTOIMPORT_MOD_IMPORTER.format(module='mymod') in content |
| 285 |
assert 'ipython' not in content |
| 286 |
|
| 287 |
|
| 288 |
def test_autoimport_no_pypi_dep(): |
| 289 |
"""Case with no pypi dependencies.""" |
| 290 |
dependencies = { |
| 291 |
REPO_PYPI: {Requirement('my_pypi_mod')}, |
| 292 |
REPO_VCS: {'my_vcs_dependency'}, |
| 293 |
} |
| 294 |
content = _autoimport_safe_call(dependencies, is_ipython=False) |
| 295 |
|
| 296 |
assert main.AUTOIMPORT_MOD_IMPORTER.format(module='my_pypi_mod') in content |
| 297 |
assert main.AUTOIMPORT_MOD_SKIPPING.format(dependency='my_vcs_dependency') in content |
| 298 |
|
| 299 |
|
| 300 |
def test_autoimport_importer_mod_ok(capsys): |
| 301 |
"""Check the generated code to import a module when works fine.""" |
| 302 |
code = main.AUTOIMPORT_MOD_IMPORTER.format(module='time') # something from stdlib, always ok |
| 303 |
exec(code) |
| 304 |
assert capsys.readouterr().out == "::fades:: automatically imported 'time'\n" |
| 305 |
|
| 306 |
|
| 307 |
def test_autoimport_importer_mod_fail(capsys): |
| 308 |
"""Check the generated code to import a module when works fine.""" |
| 309 |
code = main.AUTOIMPORT_MOD_IMPORTER.format(module='not_there_should_explode') |
| 310 |
exec(code) |
| 311 |
assert capsys.readouterr().out == "::fades:: FAILED to autoimport 'not_there_should_explode'\n" |