from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import pytest

from testing import Path
from testing import requirements
from testing import run
from testing import venv_update


@pytest.mark.usefixtures('pypi_server')
def test_relocatable(tmpdir):
    tmpdir.chdir()
    requirements('')
    venv_update('--python=python')  # this makes pypy work right. derp.

    Path('virtualenv_run').rename('relocated')

    python = 'relocated/bin/python'
    assert Path(python).exists()
    run(python, '-m', 'pip.__main__', '--version')


@pytest.mark.usefixtures('pypi_server')
def test_python_versions(tmpdir):
    tmpdir.chdir()
    requirements('pure_python_package')

    venv_update('--python=python2.6')
    run('sh', '-c', '. virtualenv_run/bin/activate && pure-python-script')
    out, err = run('sh', '-c', '. virtualenv_run/bin/activate && python --version')
    assert out == ''
    assert err.startswith('Python 2.6')

    venv_update('--python=python2.7')
    run('sh', '-c', '. virtualenv_run/bin/activate && pure-python-script')
    out, err = run('sh', '-c', '. virtualenv_run/bin/activate && python --version')
    assert out == ''
    assert err.startswith('Python 2.7')

    venv_update('--python=python2.6')
    run('sh', '-c', '. virtualenv_run/bin/activate && pure-python-script')
    out, err = run('sh', '-c', '. virtualenv_run/bin/activate && python --version')
    assert out == ''
    assert err.startswith('Python 2.6')


def test_virtualenv_moved(tmpdir):
    """if you move the virtualenv and venv-update again, the old will be blown away, and things will work"""
    original_path = 'original'
    new_path = 'new_dir'

    tmpdir.mkdir(original_path).chdir()
    requirements('flake8==2.4.0\n')
    Path('run.py').write('')
    venv_update()
    run('virtualenv_run/bin/flake8', 'run.py')
    run('virtualenv_run/bin/python', 'virtualenv_run/bin/flake8', 'run.py')

    tmpdir.chdir()
    Path(original_path).rename(new_path)
    tmpdir.join(new_path).chdir()
    venv_update()
    run('virtualenv_run/bin/flake8', 'run.py')
    run('virtualenv_run/bin/python', 'virtualenv_run/bin/flake8', 'run.py')

@@ -1507,16 +1507,56 @@
 2.6')%0A%0A%0A
+@pytest.mark.usefixtures('pypi_server')%0A
 def test
@@ -1748,24 +1748,29 @@
 w_dir'%0A%0A    
+with 
 tmpdir.mkdir
@@ -1785,28 +1785,34 @@
 l_path).
-chdir()%0A
+as_cwd():%0A   
     
+ 
 requirem
@@ -1821,55 +1821,34 @@
 ts('
-flake8==2.4.0%5Cn')%0A    Path('run.py').write('')%0A
+pure_python_package')%0A    
     
@@ -1853,32 +1853,36 @@
   venv_update()%0A
+    
     run('virtual
@@ -1893,35 +1893,41 @@
 run/bin/
-flake8', 'run.py')%0A
+pure-python-script')%0A    
     run(
@@ -1979,32 +1979,34 @@
 bin/
-flake8', 'run.py
+pure-python-script
 ')%0A%0A    
 tmpd
@@ -1997,39 +1997,50 @@
 ript')%0A%0A    
+with 
 tmpdir.
-chdir()%0A
+as_cwd():%0A    
     Path(ori
@@ -2068,20 +2068,26 @@
 w_path)%0A
+%0A
     
+with 
 tmpdir.j
@@ -2104,153 +2104,456 @@
 th).
-chdir()%0A    venv_update()%0A    run('virtualenv_run/bin/flake8', 'run.py')%0A    run('virtualenv_run/bin/python', 'virtualenv_run/bin/flake8', 'run.py
+as_cwd():%0A        with pytest.raises(OSError) as excinfo:%0A            run('virtualenv_run/bin/pure-python-script')%0A        assert excinfo.type is OSError%0A        assert excinfo.value.args%5B0%5D == 2  # no such file%0A        run('virtualenv_run/bin/python', 'virtualenv_run/bin/pure-python-script')%0A%0A        venv_update()%0A        run('virtualenv_run/bin/pure-python-script')%0A        run('virtualenv_run/bin/python', 'virtualenv_run/bin/pure-python-script
 ')%0A

fix virtualenv_moved