<commit_before>from __future__ import print_function
from __future__ import unicode_literals
from py._path.local import LocalPath as Path
import pytest

from testing import run, strip_coverage_warnings, venv_update

TOP = Path(__file__) / '../../..'
SCENARIOS = TOP/'tests/scenarios'

from sys import builtin_module_names
PYPY = ('__pypy__' in builtin_module_names)


def test_trivial(tmpdir):
    tmpdir.chdir()

    # Trailing slash is essential to rsync
    run('rsync', '-a', str(SCENARIOS) + '/trivial/', '.')
    venv_update()


@pytest.mark.flaky(reruns=10)
def test_second_install_faster(tmpdir):
    """install twice, and the second one should be faster, due to whl caching"""
    tmpdir.chdir()

    # Trailing slash is essential to rsync
    run('rsync', '-a', str(SCENARIOS) + '/trivial/', '.')
    with open('requirements.txt', 'w') as requirements:
        # An arbitrary package that takes a bit of time to install: twisted
        # Should I make my own fake c-extention just to remove this dependency?
        requirements.write('''\
simplejson
pyyaml
coverage
pylint
pytest
''')

    from time import time
    start = time()
    venv_update()
    time1 = time() - start

    start = time()
    venv_update()
    time2 = time() - start

    # second install should be at least twice as fast
    ratio = time1 / time2
    print('%.2fx speedup' % ratio)
    if PYPY:
        # pypy is too fast :(
        assert ratio > 1.5
    else:
        assert ratio > 2


def test_arguments_version(capfd):
    """Show that we can pass arguments through to virtualenv"""

    from subprocess import CalledProcessError
    with pytest.raises(CalledProcessError) as excinfo:
        # should show virtualenv version, then crash
        venv_update('--version')

    assert excinfo.value.returncode == 1
    out, err = capfd.readouterr()
    lasterr = strip_coverage_warnings(err).rsplit('\n', 2)[-2]
    assert lasterr == 'OSError: [Errno 2] No such file or directory', err

    lines = out.split('\n')
    assert lines[-3] == ('> virtualenv virtualenv_run --version'), out


def test_arguments_system_packages(tmpdir, capfd):
    """Show that we can pass arguments through to virtualenv"""
    tmpdir.chdir()
    run('rsync', '-a', str(SCENARIOS) + '/trivial/', '.')
    venv_update('--system-site-packages')
    out, err = capfd.readouterr()  # flush buffers

    run('virtualenv_run/bin/python', '-c', '''\
import sys
for p in sys.path:
    if p.startswith(sys.real_prefix) and p.endswith("-packages"):
        print(p)
        break<commit_msg>fix py3<commit_after>from __future__ import print_function
from __future__ import unicode_literals
from py._path.local import LocalPath as Path
import pytest

from testing import run, strip_coverage_warnings, venv_update

TOP = Path(__file__) / '../../..'
SCENARIOS = TOP/'tests/scenarios'

from sys import version_info, builtin_module_names
PY33 = (version_info >= (3, 3))
PYPY = ('__pypy__' in builtin_module_names)


def test_trivial(tmpdir):
    tmpdir.chdir()

    # Trailing slash is essential to rsync
    run('rsync', '-a', str(SCENARIOS) + '/trivial/', '.')
    venv_update()


@pytest.mark.flaky(reruns=10)
def test_second_install_faster(tmpdir):
    """install twice, and the second one should be faster, due to whl caching"""
    tmpdir.chdir()

    # Trailing slash is essential to rsync
    run('rsync', '-a', str(SCENARIOS) + '/trivial/', '.')
    with open('requirements.txt', 'w') as requirements:
        # An arbitrary package that takes a bit of time to install: twisted
        # Should I make my own fake c-extention just to remove this dependency?
        requirements.write('''\
simplejson
pyyaml
coverage
pylint
pytest
''')

    from time import time
    start = time()
    venv_update()
    time1 = time() - start

    start = time()
    venv_update()
    time2 = time() - start

    # second install should be at least twice as fast
    ratio = time1 / time2
    print('%.2fx speedup' % ratio)
    if PYPY:
        # pypy is too fast :(
        assert ratio > 1.5
    else:
        assert ratio > 2


def test_arguments_version(capfd):
    """Show that we can pass arguments through to virtualenv"""

    from subprocess import CalledProcessError
    with pytest.raises(CalledProcessError) as excinfo:
        # should show virtualenv version, then crash
        venv_update('--version')

    assert excinfo.value.returncode == 1
    out, err = capfd.readouterr()
    lasterr = strip_coverage_warnings(err).rsplit('\n', 2)[-2]
    if PY33:
        errname = 'FileNotFoundError'
    else:
        errname = 'OSError'
    assert lasterr == errname + ': [Errno 2] No such file or directory', err

    lines = out.split('\n')
    assert lines[-3] == ('> virtualenv virtualenv_run --version'), out


def test_arguments_system_packages(tmpdir, capfd):
    """Show that we can pass arguments through to virtualenv"""
    tmpdir.chdir()
    run('rsync', '-a', str(SCENARIOS) + '/trivial/', '.')
    venv_update('--system-site-packages')
    out, err = capfd.readouterr()  # flush buffers

    run('virtualenv_run/bin/python', '-c', '''\
import sys
for p in sys.path:
    if p.startswith(sys.real_prefix) and p.endswith("-packages"):
        print(p)
        break
{'fraction_non_alphanumeric': 0.12117737003058104, 'fraction_numerical': 0.007071865443425076, 'mean_word_length': 3.1998394863563404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3844963', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1682, 'n_words': 502}