from __future__ import absolute_import, unicode_literals

import itertools
import os
import sys
from copy import copy

import pytest

from virtualenv.discovery.py_spec import PythonSpec


def test_bad_py_spec():
    text = "python2.3.4.5"
    spec = PythonSpec.from_string_spec(text)
    assert text in repr(spec)
    assert spec.str_spec == text
    assert spec.path == os.path.abspath(text)
    content = vars(spec)
    del content[str("str_spec")]
    del content[str("path")]
    assert all(v is None for v in content.values())


def test_py_spec_first_digit_only_major():
    spec = PythonSpec.from_string_spec("278")
    assert spec.major == 2
    assert spec.minor == 78


def test_spec_satisfies_path_ok():
    spec = PythonSpec.from_string_spec(sys.executable)
    assert spec.satisfies(spec) is True


def test_spec_satisfies_path_nok(tmp_path):
    spec = PythonSpec.from_string_spec(sys.executable)
    of = PythonSpec.from_string_spec(str(tmp_path))
    assert spec.satisfies(of) is False


def test_spec_satisfies_arch():
    spec_1 = PythonSpec.from_string_spec("python-32")
    spec_2 = PythonSpec.from_string_spec("python-64")

    assert spec_1.satisfies(spec_1) is True
    assert spec_2.satisfies(spec_1) is False


@pytest.mark.parametrize(
    "req, spec",
    list(itertools.combinations(["py", "CPython", "python"], 2)) + [("jython", "jython")] + [("CPython", "cpython")],
)
def test_spec_satisfies_implementation_ok(req, spec):
    spec_1 = PythonSpec.from_string_spec(req)
    spec_2 = PythonSpec.from_string_spec(spec)
    assert spec_1.satisfies(spec_1) is True
    assert spec_2.satisfies(spec_1) is True


def test_spec_satisfies_implementation_nok():
    spec_1 = PythonSpec.from_string_spec("python")
    spec_2 = PythonSpec.from_string_spec("jython")
    assert spec_2.satisfies(spec_1) is False
    assert spec_1.satisfies(spec_2) is False


def _version_satisfies_pairs():
    target = set()
    version = tuple(str(i) for i in sys.version_info[0:3])
    for i in range(len(version) + 1):
        req = ".".join(version[0:i])
        for j in range(i + 1):
            sat = ".".join(version[0:j])
            # can be satisfied in both directions
            target.add((req, sat))
            target.add((sat, req))
    return sorted(target)


@pytest.mark.parametrize("req, spec", _version_satisfies_pairs())
def test_version_satisfies_ok(req, spec):
    req_spec = PythonSpec.from_string_spec("python{}".format(req))
    sat_spec = PythonSpec.from_string_spec("python{}".format(spec))
    assert sat_spec.satisfies(req_spec) is True


def _version_not_satisfies_pairs():
    target = set()
    version = tuple(str(i) for i in sys.version_info[0:3])
    for i in range(len(version)):
        req = ".".join(version[0 : i + 1])
        for j in range(i + 1):
            sat_ver = list(sys.version_info[0 : j + 1])
            for l in range(j + 1):
                for o in [1, -1]:
                    temp = copy(sat_ver)
                    temp[l] += o
                    sat = ".".join(str(i) for i in temp)
                    target.add((req, sat))
    return sorted(target)


@pytest.mark.parametrize("req, spec", _version_not_satisfies_pairs())
def test_version_satisfies_nok(req, spec):
    req_spec = PythonSpec.from_string_spec("python{}".format(req))
    sat_spec = PythonSpec.from_string_spec("python{}".format(spec))
    assert sat_spec.satisfies(req_spec) is False


def test_relative_spec(tmp_path, monkeypatch):
    monkeypatch.chdir(tmp_path)
    a_relative_path = str((tmp_path / "a" / "b").relative_to(tmp_path))
    spec = PythonSpec.from_string_spec(a_relative_path)
    assert spec.path == os.path.abspath(str(tmp_path / a_relative_path))

[((15, 11, 15, 44), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(15, 39, 15, 43): 'text'}, {}), '(text)', False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((26, 11, 26, 45), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(26, 39, 26, 44): '"""278"""'}, {}), "('278')", False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((32, 11, 32, 54), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(32, 39, 32, 53): 'sys.executable'}, {}), '(sys.executable)', False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((37, 11, 37, 54), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(37, 39, 37, 53): 'sys.executable'}, {}), '(sys.executable)', False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((43, 13, 43, 53), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(43, 41, 43, 52): '"""python-32"""'}, {}), "('python-32')", False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((44, 13, 44, 53), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(44, 41, 44, 52): '"""python-64"""'}, {}), "('python-64')", False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((55, 13, 55, 45), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(55, 41, 55, 44): 'req'}, {}), '(req)', False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((56, 13, 56, 46), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(56, 41, 56, 45): 'spec'}, {}), '(spec)', False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((62, 13, 62, 50), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(62, 41, 62, 49): '"""python"""'}, {}), "('python')", False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((63, 13, 63, 50), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(63, 41, 63, 49): '"""jython"""'}, {}), "('jython')", False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((114, 11, 114, 55), 'virtualenv.discovery.py_spec.PythonSpec.from_string_spec', 'PythonSpec.from_string_spec', ({(114, 39, 114, 54): 'a_relative_path'}, {}), '(a_relative_path)', False, 'from virtualenv.discovery.py_spec import PythonSpec\n'), ((18, 24, 18, 45), 'os.path.abspath', 'os.path.abspath', ({(18, 40, 18, 44): 'text'}, {}), '(text)', False, 'import os\n'), ((52, 9, 52, 63), 'itertools.combinations', 'itertools.combinations', ({(52, 32, 52, 59): "['py', 'CPython', 'python']", (52, 61, 52, 62): '(2)'}, {}), "(['py', 'CPython', 'python'], 2)", False, 'import itertools\n'), ((97, 27, 97, 40), 'copy.copy', 'copy', ({(97, 32, 97, 39): 'sat_ver'}, {}), '(sat_ver)', False, 'from copy import copy\n')]