<commit_before>"""Tests against full Pylons projects created from scratch"""
import os
import sys
import time
from shutil import rmtree

import pkg_resources
import nose
import nose.config
import pylons
from nose import SkipTest
from paste.fixture import *

is_jython = sys.platform.startswith('java')

TEST_OUTPUT_DIRNAME = 'output'

for spec in ['PasteScript', 'Paste', 'PasteDeploy', 'pylons']:
    pkg_resources.require(spec)

template_path = os.path.join(
    os.path.dirname(__file__), 'filestotest').replace('\\','/')

test_environ = os.environ.copy()
test_environ['PASTE_TESTING'] = 'true'

testenv = TestFileEnvironment(
    os.path.join(os.path.dirname(__file__), TEST_OUTPUT_DIRNAME).replace('\\','/'),
    template_path=template_path,
    environ=test_environ)

projenv = None

def _get_script_name(script):
    if sys.platform == 'win32':
        script += '.exe'
    return script

def svn_repos_setup():
    res = testenv.run(_get_script_name('svnadmin'), 'create', 'REPOS',
                      printresult=False)
    path = testenv.base_path.replace('\\','/').replace(' ','%20')
    base = 'file://'
    if ':' in path:
        base = 'file:///'
    testenv.svn_url = base + path + '/REPOS'
    assert 'REPOS' in res.files_created
    testenv.ignore_paths.append('REPOS')

def paster_create(template_engine='mako', overwrite=False):
    global projenv
    paster_args = ['create', '--verbose', '--no-interactive']
    if overwrite:
        paster_args.append('-f')
    paster_args.extend(['--template=pylons',
                        'ProjectName',
                        'version=0.1',
                        'sqlalchemy=False',
                        'zip_safe=False',
                        'template_engine=%s' % template_engine])
    res = testenv.run(_get_script_name('paster'), *paster_args)
    expect_fn = ['projectname', 'development.ini', 'setup.cfg', 'README.txt',
                 'setup.py']
    for fn in expect_fn:
        fn = os.path.join('ProjectName', fn)
        if not overwrite:
            assert fn in res.files_created.keys()
        assert fn in res.stdout

    if not overwrite:
        setup = res.files_created[os.path.join('ProjectName','setup.py')]
        setup.mustcontain('0.1')
        setup.mustcontain('projectname.config.middleware:make_app')
        setup.mustcontain('main = pylons.util:PylonsInstaller')
        setup.mustcontain("include_package_data=True")
        assert '0.1' in setup
    testenv.run(_get_script_name(sys.executable)+' setup.py egg_info',
                cwd=os.path.join(testenv.cwd, 'ProjectName').replace('\\','/'),
                expect_stderr=True)
    #testenv.run(_get_script_name('svn'), 'commit', '-m', 'Created project', 'ProjectName')
    # A new environment with a new
    projenv = TestFileEnvironment(
        os.path.join(testenv.base_path, 'ProjectName').replace('\\','/'),
        start_clear=False,
        template_path=template_path,
        environ=test_environ)
    projenv.environ['PYTHONPATH'] = (
        projenv.environ.get('PYTHONPATH', '') + ':'
        + projenv.base_path)

def make_controller():
    res = projenv.run(_get_script_name('paster')+' controller sample')
    assert os.path.join('projectname','controllers','sample.py') in res.files_created
    assert os.path.join('projectname','tests','functional','test_sample.py') in res.files_created
    #res = projenv.run(_get_script_name('svn')+' status')
    # Make sure all files are added to the repository:
    assert '?' not in res.stdout

def make_restcontroller():
    res = projenv.run(_get_script_name('paster')+' restcontroller restsample restsamples')
    assert os.path.join('projectname','controllers','restsamples.py') in res.files_created
    assert os.path.join('projectname','tests','functional','test_restsamples.py') in res.files_created
    #res = projenv.run(_get_script_name('svn')+' status')
    # Make sure all files are added to the repository:
    assert '?' not in res.stdout

def _do_proj_test(copydict, emptyfiles=None):
    """Given a dict of files, where the key is a filename in filestotest, the value is
    the destination in the new projects dir. emptyfiles is a list of files that should
    be created and empty."""
    if is_jython:
        # Hack for Jython .py/bytecode mtime handling:
        # http://bugs.jython.org/issue1024 (the issue actually describes
        # this test)
        time.sleep(1)
    
    if pylons.test.pylonsapp:
        pylons.test.pylonsapp = None
    
    if not emptyfiles:
        emptyfiles = []
    for original, newfile in copydict.iteritems():
        projenv.writefile(newfile, frompath=original)
    for fi in emptyfiles:
        projenv.writefile(fi)
    
    # here_dir = os.getcwd()
    # test_dir = os.path.join(testenv.cwd, 'ProjectName').replace('\\','/')
    # os.chdir(test_dir)
    # sys.path.append(test_dir)
    # nose.run(argv=['nosetests', '-d', test_dir])
    # 
    # sys.path.pop(-1)
    # os.chdir(here_dir)
    
    res = projenv.run(_get_script_name('nosetests')+' -d --with-coverage --cover-package=pylons',<commit_msg>Jython includes an mtime in its bytecode as of beta2, remove the workaround (transplanted from ebe55a3122522b9bf43c096dfbcefbf0c7644ee4) (transplanted from 5bf3b4ce0e9e8a3d06d8c7b46a1d0cfdaea92502)<commit_after>"""Tests against full Pylons projects created from scratch"""
import os
import sys
from shutil import rmtree

import pkg_resources
import nose
import nose.config
import pylons
from nose import SkipTest
from paste.fixture import *

is_jython = sys.platform.startswith('java')

TEST_OUTPUT_DIRNAME = 'output'

for spec in ['PasteScript', 'Paste', 'PasteDeploy', 'pylons']:
    pkg_resources.require(spec)

template_path = os.path.join(
    os.path.dirname(__file__), 'filestotest').replace('\\','/')

test_environ = os.environ.copy()
test_environ['PASTE_TESTING'] = 'true'

testenv = TestFileEnvironment(
    os.path.join(os.path.dirname(__file__), TEST_OUTPUT_DIRNAME).replace('\\','/'),
    template_path=template_path,
    environ=test_environ)

projenv = None

def _get_script_name(script):
    if sys.platform == 'win32':
        script += '.exe'
    return script

def svn_repos_setup():
    res = testenv.run(_get_script_name('svnadmin'), 'create', 'REPOS',
                      printresult=False)
    path = testenv.base_path.replace('\\','/').replace(' ','%20')
    base = 'file://'
    if ':' in path:
        base = 'file:///'
    testenv.svn_url = base + path + '/REPOS'
    assert 'REPOS' in res.files_created
    testenv.ignore_paths.append('REPOS')

def paster_create(template_engine='mako', overwrite=False):
    global projenv
    paster_args = ['create', '--verbose', '--no-interactive']
    if overwrite:
        paster_args.append('-f')
    paster_args.extend(['--template=pylons',
                        'ProjectName',
                        'version=0.1',
                        'sqlalchemy=False',
                        'zip_safe=False',
                        'template_engine=%s' % template_engine])
    res = testenv.run(_get_script_name('paster'), *paster_args)
    expect_fn = ['projectname', 'development.ini', 'setup.cfg', 'README.txt',
                 'setup.py']
    for fn in expect_fn:
        fn = os.path.join('ProjectName', fn)
        if not overwrite:
            assert fn in res.files_created.keys()
        assert fn in res.stdout

    if not overwrite:
        setup = res.files_created[os.path.join('ProjectName','setup.py')]
        setup.mustcontain('0.1')
        setup.mustcontain('projectname.config.middleware:make_app')
        setup.mustcontain('main = pylons.util:PylonsInstaller')
        setup.mustcontain("include_package_data=True")
        assert '0.1' in setup
    testenv.run(_get_script_name(sys.executable)+' setup.py egg_info',
                cwd=os.path.join(testenv.cwd, 'ProjectName').replace('\\','/'),
                expect_stderr=True)
    #testenv.run(_get_script_name('svn'), 'commit', '-m', 'Created project', 'ProjectName')
    # A new environment with a new
    projenv = TestFileEnvironment(
        os.path.join(testenv.base_path, 'ProjectName').replace('\\','/'),
        start_clear=False,
        template_path=template_path,
        environ=test_environ)
    projenv.environ['PYTHONPATH'] = (
        projenv.environ.get('PYTHONPATH', '') + ':'
        + projenv.base_path)

def make_controller():
    res = projenv.run(_get_script_name('paster')+' controller sample')
    assert os.path.join('projectname','controllers','sample.py') in res.files_created
    assert os.path.join('projectname','tests','functional','test_sample.py') in res.files_created
    #res = projenv.run(_get_script_name('svn')+' status')
    # Make sure all files are added to the repository:
    assert '?' not in res.stdout

def make_restcontroller():
    res = projenv.run(_get_script_name('paster')+' restcontroller restsample restsamples')
    assert os.path.join('projectname','controllers','restsamples.py') in res.files_created
    assert os.path.join('projectname','tests','functional','test_restsamples.py') in res.files_created
    #res = projenv.run(_get_script_name('svn')+' status')
    # Make sure all files are added to the repository:
    assert '?' not in res.stdout

def _do_proj_test(copydict, emptyfiles=None):
    """Given a dict of files, where the key is a filename in filestotest, the value is
    the destination in the new projects dir. emptyfiles is a list of files that should
    be created and empty."""
    if pylons.test.pylonsapp:
        pylons.test.pylonsapp = None
    
    if not emptyfiles:
        emptyfiles = []
    for original, newfile in copydict.iteritems():
        projenv.writefile(newfile, frompath=original)
    for fi in emptyfiles:
        projenv.writefile(fi)
    
    # here_dir = os.getcwd()
    # test_dir = os.path.join(testenv.cwd, 'ProjectName').replace('\\','/')
    # os.chdir(test_dir)
    # sys.path.append(test_dir)
    # nose.run(argv=['nosetests', '-d', test_dir])
    # 
    # sys.path.pop(-1)
    # os.chdir(here_dir)
    
    res = projenv.run(_get_script_name('nosetests')+' -d --with-coverage --cover-package=pylons',
{'fraction_non_alphanumeric': 0.13926670609107036, 'fraction_numerical': 0.006800709639266706, 'mean_word_length': 3.2226383687057845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1997217', 'n_tokens_mistral': 3348, 'n_tokens_neox': 3285, 'n_words': 671}