2020-03-31 22:35:29 +02:00
|
|
|
"""
|
|
|
|
Tests for the resolver
|
2017-08-07 18:03:43 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
import pytest
|
2020-03-31 22:35:29 +02:00
|
|
|
import yaml
|
2017-08-07 18:03:43 +02:00
|
|
|
|
|
|
|
from tests.lib import DATA_DIR, create_basic_wheel_for_package, path_to_url
|
|
|
|
|
2020-03-31 22:47:43 +02:00
|
|
|
_conflict_finder_pat = re.compile(
|
2017-08-07 18:03:43 +02:00
|
|
|
# Conflicting Requirements: \
|
|
|
|
# A 1.0.0 requires B == 2.0.0, C 1.0.0 requires B == 1.0.0.
|
2017-09-15 22:34:19 +02:00
|
|
|
r"""
|
2017-08-07 18:03:43 +02:00
|
|
|
(?P<package>[\w\-_]+?)
|
|
|
|
[ ]
|
|
|
|
(?P<version>\S+?)
|
|
|
|
[ ]requires[ ]
|
|
|
|
(?P<selector>.+?)
|
|
|
|
(?=,|\.$)
|
|
|
|
""",
|
|
|
|
re.X
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-31 22:35:29 +02:00
|
|
|
def generate_yaml_tests(directory):
|
|
|
|
"""
|
|
|
|
Generate yaml test cases from the yaml files in the given directory
|
|
|
|
"""
|
2020-04-22 22:23:35 +02:00
|
|
|
for yml_file in directory.glob("*.yml"):
|
2020-03-31 22:35:29 +02:00
|
|
|
data = yaml.safe_load(yml_file.read_text())
|
|
|
|
assert "cases" in data, "A fixture needs cases to be used in testing"
|
|
|
|
|
|
|
|
# Strip the parts of the directory to only get a name without
|
|
|
|
# extension and resolver directory
|
|
|
|
base_name = str(yml_file)[len(str(directory)) + 1:-4]
|
|
|
|
|
|
|
|
base = data.get("base", {})
|
|
|
|
cases = data["cases"]
|
|
|
|
|
2020-04-23 00:33:12 +02:00
|
|
|
for resolver in 'old', 'new':
|
|
|
|
for i, case_template in enumerate(cases):
|
|
|
|
case = base.copy()
|
|
|
|
case.update(case_template)
|
2020-03-31 22:35:29 +02:00
|
|
|
|
2020-04-23 00:50:01 +02:00
|
|
|
case[":name:"] = base_name
|
2020-04-23 00:33:12 +02:00
|
|
|
if len(cases) > 1:
|
|
|
|
case[":name:"] += "-" + str(i)
|
2020-04-23 00:50:01 +02:00
|
|
|
case[":name:"] += "*" + resolver
|
2020-04-23 00:33:12 +02:00
|
|
|
case[":resolver:"] = resolver
|
2020-03-31 22:35:29 +02:00
|
|
|
|
2020-04-23 00:57:20 +02:00
|
|
|
skip = case.pop("skip", False)
|
|
|
|
assert skip in [False, True, 'old', 'new']
|
2020-04-23 01:23:24 +02:00
|
|
|
if skip is True or skip == resolver:
|
2020-04-23 00:33:12 +02:00
|
|
|
case = pytest.param(case, marks=pytest.mark.xfail)
|
2020-03-31 22:35:29 +02:00
|
|
|
|
2020-04-23 00:33:12 +02:00
|
|
|
yield case
|
2020-03-31 22:35:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def id_func(param):
|
|
|
|
"""
|
|
|
|
Give a nice parameter name to the generated function parameters
|
|
|
|
"""
|
|
|
|
if isinstance(param, dict) and ":name:" in param:
|
|
|
|
return param[":name:"]
|
|
|
|
|
|
|
|
retval = str(param)
|
|
|
|
if len(retval) > 25:
|
|
|
|
retval = retval[:20] + "..." + retval[-2:]
|
|
|
|
return retval
|
|
|
|
|
|
|
|
|
|
|
|
def convert_to_dict(string):
|
2017-08-07 18:03:43 +02:00
|
|
|
|
|
|
|
def stripping_split(my_str, splitwith, count=None):
|
|
|
|
if count is None:
|
|
|
|
return [x.strip() for x in my_str.strip().split(splitwith)]
|
|
|
|
else:
|
|
|
|
return [x.strip() for x in my_str.strip().split(splitwith, count)]
|
|
|
|
|
|
|
|
parts = stripping_split(string, ";")
|
|
|
|
|
|
|
|
retval = {}
|
|
|
|
retval["depends"] = []
|
|
|
|
retval["extras"] = {}
|
|
|
|
|
|
|
|
retval["name"], retval["version"] = stripping_split(parts[0], " ")
|
|
|
|
|
|
|
|
for part in parts[1:]:
|
|
|
|
verb, args_str = stripping_split(part, " ", 1)
|
|
|
|
assert verb in ["depends"], "Unknown verb {!r}".format(verb)
|
|
|
|
|
|
|
|
retval[verb] = stripping_split(args_str, ",")
|
|
|
|
|
|
|
|
return retval
|
|
|
|
|
|
|
|
|
2020-04-22 22:20:59 +02:00
|
|
|
def handle_request(script, action, requirement, options, new_resolver=False):
|
2020-04-05 07:05:43 +02:00
|
|
|
if action == 'install':
|
2020-04-22 22:20:59 +02:00
|
|
|
args = ['install']
|
|
|
|
if new_resolver:
|
|
|
|
args.append("--unstable-feature=resolver")
|
|
|
|
args.extend(["--no-index", "--find-links",
|
|
|
|
path_to_url(script.scratch_path)])
|
2020-04-05 07:05:43 +02:00
|
|
|
elif action == 'uninstall':
|
|
|
|
args = ['uninstall', '--yes']
|
|
|
|
else:
|
|
|
|
raise "Did not excpet action: {!r}".format(action)
|
2020-04-29 06:32:55 +02:00
|
|
|
|
|
|
|
if isinstance(requirement, str):
|
|
|
|
args.append(requirement)
|
|
|
|
elif isinstance(requirement, list):
|
|
|
|
args.extend(requirement)
|
|
|
|
else:
|
|
|
|
raise "requirement neither str nor list {!r}".format(requirement)
|
|
|
|
|
2020-04-02 07:09:24 +02:00
|
|
|
args.extend(options)
|
|
|
|
args.append("--verbose")
|
|
|
|
|
|
|
|
result = script.pip(*args,
|
2020-04-02 07:34:55 +02:00
|
|
|
allow_stderr_error=True,
|
2020-04-29 06:15:57 +02:00
|
|
|
allow_stderr_warning=True,
|
|
|
|
allow_error=True)
|
2017-08-07 18:03:43 +02:00
|
|
|
|
2020-04-29 06:15:57 +02:00
|
|
|
# Check which packages got installed
|
|
|
|
state = []
|
|
|
|
for path in os.listdir(script.site_packages_path):
|
|
|
|
if path.endswith(".dist-info"):
|
|
|
|
name, version = (
|
|
|
|
os.path.basename(path)[:-len(".dist-info")]
|
|
|
|
).rsplit("-", 1)
|
|
|
|
# TODO: information about extras.
|
|
|
|
state.append(" ".join((name, version)))
|
2017-08-07 18:03:43 +02:00
|
|
|
|
2020-04-29 06:15:57 +02:00
|
|
|
return {"result": result, "state": sorted(state)}
|
2017-08-07 18:03:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.yaml
|
|
|
|
@pytest.mark.parametrize(
|
2019-07-02 07:00:32 +02:00
|
|
|
"case", generate_yaml_tests(DATA_DIR.parent / "yaml"), ids=id_func
|
2017-08-07 18:03:43 +02:00
|
|
|
)
|
|
|
|
def test_yaml_based(script, case):
|
|
|
|
available = case.get("available", [])
|
|
|
|
requests = case.get("request", [])
|
2020-04-04 21:00:16 +02:00
|
|
|
responses = case.get("response", [])
|
2017-08-07 18:03:43 +02:00
|
|
|
|
2020-04-04 21:00:16 +02:00
|
|
|
assert len(requests) == len(responses), (
|
|
|
|
"Expected requests and responses counts to be same"
|
2017-08-07 18:03:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Create a custom index of all the packages that are supposed to be
|
|
|
|
# available
|
|
|
|
# XXX: This doesn't work because this isn't making an index of files.
|
|
|
|
for package in available:
|
|
|
|
if isinstance(package, str):
|
2020-03-31 22:35:29 +02:00
|
|
|
package = convert_to_dict(package)
|
2017-08-07 18:03:43 +02:00
|
|
|
|
|
|
|
assert isinstance(package, dict), "Needs to be a dictionary"
|
|
|
|
|
|
|
|
create_basic_wheel_for_package(script, **package)
|
|
|
|
|
|
|
|
# use scratch path for index
|
2020-04-04 21:00:16 +02:00
|
|
|
for request, response in zip(requests, responses):
|
2017-08-07 18:03:43 +02:00
|
|
|
|
2020-04-05 07:05:43 +02:00
|
|
|
for action in 'install', 'uninstall':
|
|
|
|
if action in request:
|
|
|
|
break
|
2020-04-02 07:09:24 +02:00
|
|
|
else:
|
2020-04-05 07:05:43 +02:00
|
|
|
raise "Unsupported request {!r}".format(request)
|
2017-08-07 18:03:43 +02:00
|
|
|
|
2020-04-05 07:05:43 +02:00
|
|
|
# Perform the requested action
|
|
|
|
effect = handle_request(script, action,
|
|
|
|
request[action],
|
2020-04-23 00:33:12 +02:00
|
|
|
request.get('options', '').split(),
|
|
|
|
case[':resolver:'] == 'new')
|
2019-02-17 21:36:37 +01:00
|
|
|
|
2020-04-29 06:15:57 +02:00
|
|
|
if 0: # for analyzing output easier
|
2020-04-29 06:43:00 +02:00
|
|
|
with open(DATA_DIR.parent / "yaml" /
|
|
|
|
case[':name:'].replace('*', '-'), 'w') as fo:
|
2020-04-29 06:15:57 +02:00
|
|
|
result = effect['result']
|
|
|
|
fo.write("=== RETURNCODE = %d\n" % result.returncode)
|
|
|
|
fo.write("=== STDERR ===:\n%s\n" % result.stderr)
|
|
|
|
|
|
|
|
if 'state' in response:
|
|
|
|
assert effect['state'] == (response['state'] or []), \
|
|
|
|
str(effect["result"])
|
|
|
|
|
|
|
|
error = False
|
|
|
|
if 'conflicting' in response:
|
|
|
|
error = True
|
|
|
|
|
|
|
|
if error:
|
|
|
|
if case[":resolver:"] == 'old':
|
|
|
|
assert effect["result"].returncode == 0, str(effect["result"])
|
|
|
|
elif case[":resolver:"] == 'new':
|
|
|
|
assert effect["result"].returncode == 1, str(effect["result"])
|