"""ONLY BASIC TEST SET. The additional ones are in test_pythonpackage.py.These are in a separate file because these were picked to run in github-actions,while the other additional ones aren't (for build time reasons)."""import osimport shutilimport sysimport subprocessimport tempfileimport textwrapfrom unittest import mockfrom pythonforandroid.pythonpackage import (_extract_info_from_package,get_dep_names_of_package,get_package_name,_get_system_python_executable,is_filesystem_path,parse_as_folder_reference,transform_dep_for_pip,)def local_repo_folder():return os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))def fake_metadata_extract(dep_name, output_folder, debug=False):# Helper function to write out fake metadata.with open(os.path.join(output_folder, "METADATA"), "w") as f:f.write(textwrap.dedent("""\Metadata-Version: 2.1Name: testpackageVersion: 0.1Requires-Dist: testpkgRequires-Dist: testpkg2Lorem Ipsum"""))with open(os.path.join(output_folder, "metadata_source"), "w") as f:f.write(u"wheel") # since we have no pyproject.tomldef test__extract_info_from_package():import pythonforandroid.pythonpackage # noqawith mock.patch("pythonforandroid.pythonpackage.""extract_metainfo_files_from_package",fake_metadata_extract):assert _extract_info_from_package("whatever", extract_type="name") == "testpackage"assert set(_extract_info_from_package("whatever", extract_type="dependencies")) == {"testpkg", "testpkg2"}def test_get_package_name():# TEST 1 from external refwith mock.patch("pythonforandroid.pythonpackage.""extract_metainfo_files_from_package",fake_metadata_extract):assert get_package_name("TeStPackaGe") == "testpackage"# TEST 2 from a local folder, for which we'll create a fake package:temp_d = tempfile.mkdtemp(prefix="p4a-pythonpackage-test-tmp-")try:with open(os.path.join(temp_d, "setup.py"), "w") as f:f.write(textwrap.dedent("""\from setuptools import setupsetup(name="testpackage")"""))pkg_name = get_package_name(temp_d)assert pkg_name == "testpackage"finally:shutil.rmtree(temp_d)def test_get_dep_names_of_package():# TEST 1 from external ref:# Check that colorama is returned without the install condition when# just getting the names (it has a "; ..." conditional originally):dep_names = get_dep_names_of_package("python-for-android")assert "colorama" in dep_namesassert "setuptools" not in dep_namestry:dep_names = get_dep_names_of_package("python-for-android", include_build_requirements=True,verbose=True,)except NotImplementedError as e:# If python-for-android was fetched as wheel then build requirements# cannot be obtained (since that is not implemented for wheels).# Check for the correct error message:assert "wheel" in str(e)# (And yes it would be better to do a local test with something# that is guaranteed to be a wheel and not remote on pypi,# but that might require setting up a full local pypiserver.# Not worth the test complexity for now, but if anyone has an# idea in the future feel free to replace this subtest.)else:# We managed to obtain build requirements!# Check setuptools is in here:assert "setuptools" in dep_names# TEST 2 from local folder:assert "colorama" in get_dep_names_of_package(local_repo_folder())# Now test that exact version pins are kept, but others aren't:test_fake_package = tempfile.mkdtemp()try:with open(os.path.join(test_fake_package, "setup.py"), "w") as f:f.write(textwrap.dedent("""\from setuptools import setupsetup(name='fakeproject',description='fake for testing',install_requires=['buildozer==0.39','python-for-android>=0.5.1'],)"""))# See that we get the deps with the exact version pin kept but# the other version restriction gone:assert set(get_dep_names_of_package(test_fake_package, recursive=False,keep_version_pins=True, verbose=True)) == {"buildozer==0.39", "python-for-android"}# Make sure we also can get the fully cleaned up variant:assert set(get_dep_names_of_package(test_fake_package, recursive=False,keep_version_pins=False, verbose=True)) == {"buildozer", "python-for-android"}# Test with build requirements included:dep_names = get_dep_names_of_package(test_fake_package, recursive=False,keep_version_pins=False, verbose=True,include_build_requirements=True)assert len({"buildozer", "python-for-android", "setuptools"}.intersection(dep_names)) == 3 # all three must be includedfinally:shutil.rmtree(test_fake_package)def test_transform_dep_for_pip():# A reminder, this entire function we test here is just a workaround# for https://github.com/pypa/pip/issues/6097 (and not a nice one.)# As soon as upstream fixes it, we should throw it & this test outtransformed = (transform_dep_for_pip("python-for-android @ https://github.com/kivy/" +"python-for-android/archive/master.zip"),transform_dep_for_pip("python-for-android @ https://github.com/kivy/" +"python-for-android/archive/master.zip" +"#egg=python-for-android-master"),transform_dep_for_pip("python-for-android @ https://github.com/kivy/" +"python-for-android/archive/master.zip" +"#" # common hack variant used by others to make pip parse it),)expected = ("https://github.com/kivy/python-for-android/archive/master.zip" +"#egg=python-for-android")assert transformed == (expected, expected, expected)assert transform_dep_for_pip("https://a@b/") == "https://a@b/"def test_is_filesystem_path():assert is_filesystem_path("/some/test")assert not is_filesystem_path("https://blubb")assert not is_filesystem_path("test @ bla")assert is_filesystem_path("/abc/c@d")assert not is_filesystem_path("https://user:pw@host/")assert is_filesystem_path(".")assert is_filesystem_path("")def test_parse_as_folder_reference():assert parse_as_folder_reference("file:///a%20test") == "/a test"assert parse_as_folder_reference("https://github.com") is Noneassert parse_as_folder_reference("/a/folder") == "/a/folder"assert parse_as_folder_reference("test @ /abc") == "/abc"assert parse_as_folder_reference("test @ https://bla") is Noneclass TestGetSystemPythonExecutable():""" This contains all tests for _get_system_python_executable().ULTRA IMPORTANT THING TO UNDERSTAND: (if you touch this)This code runs things with other python interpreters NOT in the toxenvironment/virtualenv.E.g. _get_system_python_executable() is outside in the regularhost environment! That also means all dependencies can be possiblynot present!This is kind of absurd that we need this to run the test at all,but we can't test this inside tox's virtualenv:"""def test_basic(self):# Tests function inside tox env with no further special setup.# Get system-wide python bin:pybin = _get_system_python_executable()# The python binary needs to match our major version to be correct:pyversion = subprocess.check_output([pybin, "-c", "import sys; print(sys.version)"], stderr=subprocess.STDOUT).decode("utf-8", "replace")assert pyversion.strip() == sys.version.strip()def run__get_system_python_executable(self, pybin):""" Helper function to run our function.We want to see what _get_system_python_executable() does givena specific python, so we need to make it import it and run it,with that TARGET python, which this function does."""cmd = [pybin,"-c","import importlib\n""import json\n""import os\n""import sys\n""sys.path = [os.path.dirname(sys.argv[1])] + sys.path\n""m = importlib.import_module(\n"" os.path.basename(sys.argv[1]).partition('.')[0]\n"")\n""print(m._get_system_python_executable())",os.path.join(os.path.dirname(__file__), "..","pythonforandroid", "pythonpackage.py"),]# Actual call to python:try:return subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode("utf-8", "replace").strip()except subprocess.CalledProcessError as e:raise RuntimeError("call failed, with output: " + str(e.output))def test_systemwide_python(self):# Get system-wide python bin seen from here first:pybin = _get_system_python_executable()# (this call was not a test, we really just need the path here)# Check that in system-wide python, the system-wide python is returned:# IMPORTANT: understand that this runs OUTSIDE of any virtualenv.try:p1 = os.path.normpath(self.run__get_system_python_executable(pybin))p2 = os.path.normpath(pybin)assert p1 == p2except RuntimeError as e:if "pep517" in str(e.args):# System python probably doesn't have pep517 available!# (remember this is not in a virtualenv)# Not much we can do in that case since pythonpackage needs it,# so we'll skip this particular check.passelse:raisedef test_venv(self):""" Verifies that _get_system_python_executable() works correctlyin a 'venv' (Python 3 only feature)."""# Get system-wide python bin seen from here first:pybin = _get_system_python_executable()# (this call was not a test, we really just need the path here)test_dir = tempfile.mkdtemp()try:# Check that in a venv/pyvenv, the system-wide python is returned:subprocess.check_output([pybin, "-m", "venv", "--",os.path.join(test_dir, "venv")])subprocess.check_output([os.path.join(test_dir, "venv", "bin", "pip"),"install", "-U", "pip"])subprocess.check_output([os.path.join(test_dir, "venv", "bin", "pip"),"install", "-U", "pep517<0.7.0"])subprocess.check_output([os.path.join(test_dir, "venv", "bin", "pip"),"install", "-U", "toml"])sys_python_path = self.run__get_system_python_executable(os.path.join(test_dir, "venv", "bin", "python"))assert os.path.normpath(sys_python_path).startswith(os.path.normpath(pybin))finally:shutil.rmtree(test_dir)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。