"""
integration tests that check setuptools version support
"""
import sys
import os
import subprocess
import pytest
pytestmark = pytest.mark.skipif(
    "sys.version_info >= (3,6,0)",
    reason="integration with old versions no longer needed on py3.6+")


@pytest.fixture(scope='session')
def get_setuptools_packagedir(request):
    targets = request.config.cache.makedir('setuptools_installs')

    def makeinstall(version):
        target = targets.ensure(version, dir=1)
        subprocess.check_call([
            sys.executable, '-m', 'pip',
            'install', '--no-binary', 'setuptools', 'setuptools==' + version,
            '-t', str(target),
        ])
        return target
    return makeinstall


SCRIPT = """
import setuptools
print(setuptools.__version__)
import setuptools_scm.version
from setuptools_scm.__main__ import main
main()
"""


def check(packagedir, **env):
    subprocess.check_call(
        [sys.executable, '-c', SCRIPT],
        env=dict(os.environ, PYTHONPATH=".:" + str(packagedir), **env))


def test_old_setuptools_fails(get_setuptools_packagedir):
    packagedir = get_setuptools_packagedir("0.9.8")
    with pytest.raises(subprocess.CalledProcessError):
        check(packagedir)


def test_old_setuptools_allows_with_warnings(get_setuptools_packagedir):

    packagedir = get_setuptools_packagedir("0.9.8")
    # filter using warning since in the early python startup
    check(
        packagedir,
        PYTHONWARNINGS="once::Warning")


def test_distlib_setuptools_works(get_setuptools_packagedir):
    packagedir = get_setuptools_packagedir("12.0.1")
    check(packagedir)

@@ -718,24 +718,73 @@
 CRIPT = %22%22%22%0A
+from __future__ import print_function%0Aimport sys%0A
 import setup
@@ -817,16 +817,41 @@
 ersion__
+, 'expected', sys.argv%5B1%5D
 )%0Aimport
@@ -949,16 +949,34 @@
 kagedir,
+ expected_version,
  **env):
@@ -1040,16 +1040,34 @@
 , SCRIPT
+, expected_version
 %5D,%0A     
@@ -1324,16 +1324,25 @@
 ckagedir
+, %220.9.8%22
 )%0A%0A%0Adef 
@@ -1554,16 +1554,25 @@
 kagedir,
+ %220.9.8%22,
 %0A       
@@ -1741,10 +1741,20 @@
 ckagedir
+, %2212.0.1%22
 )%0A

enhance setuptools version loading debugging