<commit_before>import os
import os.path
import tempfile
import shutil
from nose.tools import eq_
from nose.tools import with_setup
from compile_helpers import setup_htdocs_if_it_doesnt_exist
from compile_helpers import convert_php_extensions


class TestCompileHelpers(object):
    def setUp(self):
        self.build_dir = tempfile.mkdtemp(prefix='build-')
        self.cache_dir = tempfile.mkdtemp(prefix='cache-')
        os.rmdir(self.build_dir)  # delete otherwise copytree complains
        os.rmdir(self.cache_dir)  # cache dir does not exist normally

    def tearDown(self):
        if os.path.exists(self.build_dir):
            shutil.rmtree(self.build_dir)
        if os.path.exists(self.cache_dir):
            shutil.rmtree(self.cache_dir)
        for name in os.listdir(os.environ['TMPDIR']):
            if name.startswith('httpd-') and name.endswith('.gz'):
                os.remove(os.path.join(os.environ['TMPDIR'], name))
            if name.startswith('php-') and name.endswith('.gz'):
                os.remove(os.path.join(os.environ['TMPDIR'], name))

    def assert_exists(self, *args):
        eq_(True, os.path.exists(os.path.join(*args)),
            "Does not exists: %s" % os.path.join(*args))

    @with_setup(setup=setUp, teardown=tearDown)
    def test_setup_if_htdocs_exists(self):
        shutil.copytree('tests/data/app-1', self.build_dir)
        setup_htdocs_if_it_doesnt_exist({
            'BUILD_DIR': self.build_dir
        })
        self.assert_exists(self.build_dir, 'htdocs')
        self.assert_exists(self.build_dir, 'htdocs', 'index.php')
        self.assert_exists(self.build_dir, 'htdocs', 'info.php')
        self.assert_exists(self.build_dir, 'htdocs',
                           'technical-difficulties1.jpg')
        self.assert_exists(self.build_dir, 'config')
        self.assert_exists(self.build_dir, 'config', 'options.json')
        self.assert_exists(self.build_dir, 'config', 'httpd', 'extra',
                           'httpd-remoteip.conf')
        eq_(2, len(os.listdir(self.build_dir)))
        eq_(3, len(os.listdir(os.path.join(self.build_dir, 'htdocs'))))

    @with_setup(setup=setUp, teardown=tearDown)
    def test_setup_if_htdocs_does_not_exist(self):
        shutil.copytree('tests/data/app-2', self.build_dir)
        setup_htdocs_if_it_doesnt_exist({
            'BUILD_DIR': self.build_dir
        })
        self.assert_exists(self.build_dir, 'htdocs')
        self.assert_exists(self.build_dir, 'htdocs', 'index.php')
        self.assert_exists(self.build_dir, 'htdocs', 'info.php')
        self.assert_exists(self.build_dir, 'htdocs',
                           'technical-difficulties1.jpg')
        self.assert_exists(self.build_dir, 'config')
        self.assert_exists(self.build_dir, 'config', 'options.json')
        self.assert_exists(self.build_dir, 'config', 'httpd', 'extra',
                           'httpd-remoteip.conf')
        eq_(2, len(os.listdir(self.build_dir)))
        eq_(3, len(os.listdir(os.path.join(self.build_dir, 'htdocs'))))

    def test_convert_php_extensions(self):
        ctx = {
            'PHP_EXTENSIONS': ['mod1', 'mod2', 'mod3'],
            'ZEND_EXTENSIONS': ['zmod1', 'zmod2']
        }
        convert_php_extensions(ctx)
        eq_('extension=mod1.so\nextension=mod2.so\nextension=mod3.so',
            ctx['PHP_EXTENSIONS'])
        eq_('zend_extension="@{HOME}/php/lib/php/extensions/'
            'no-debug-non-zts-20100525/zmod1.so"\n'
            'zend_extension="@{HOME}/php/lib/php/extensions/'
            'no-debug-non-zts-20100525/zmod2.so"',
            ctx['ZEND_EXTENSIONS'])

    def test_convert_php_extensions_none(self):
        ctx = {
            'PHP_EXTENSIONS': [],
            'ZEND_EXTENSIONS': []
        }
        convert_php_extensions(ctx)
        eq_('', ctx['PHP_EXTENSIONS'])
        eq_('', ctx['ZEND_EXTENSIONS'])

    def test_convert_php_extensions_one(self):
        ctx = {
            'PHP_EXTENSIONS': ['mod1'],
            'ZEND_EXTENSIONS': ['zmod1']
        }
        convert_php_extensions(ctx)
        eq_('extension=mod1.so', ctx['PHP_EXTENSIONS'])
        eq_('zend_extension="@{HOME}/php/lib/php/extensions/'
            'no-debug-non-zts-20100525/zmod1.so"',
            ctx['ZEND_EXTENSIONS'])
<commit_msg>Modified test to use FormattedDict istead of regular dict.<commit_after>import os
import os.path
import tempfile
import shutil
from nose.tools import eq_
from nose.tools import with_setup
from build_pack_utils import utils
from compile_helpers import setup_htdocs_if_it_doesnt_exist
from compile_helpers import convert_php_extensions


class TestCompileHelpers(object):
    def setUp(self):
        self.build_dir = tempfile.mkdtemp(prefix='build-')
        self.cache_dir = tempfile.mkdtemp(prefix='cache-')
        os.rmdir(self.build_dir)  # delete otherwise copytree complains
        os.rmdir(self.cache_dir)  # cache dir does not exist normally

    def tearDown(self):
        if os.path.exists(self.build_dir):
            shutil.rmtree(self.build_dir)
        if os.path.exists(self.cache_dir):
            shutil.rmtree(self.cache_dir)
        for name in os.listdir(os.environ['TMPDIR']):
            if name.startswith('httpd-') and name.endswith('.gz'):
                os.remove(os.path.join(os.environ['TMPDIR'], name))
            if name.startswith('php-') and name.endswith('.gz'):
                os.remove(os.path.join(os.environ['TMPDIR'], name))

    def assert_exists(self, *args):
        eq_(True, os.path.exists(os.path.join(*args)),
            "Does not exists: %s" % os.path.join(*args))

    @with_setup(setup=setUp, teardown=tearDown)
    def test_setup_if_htdocs_exists(self):
        shutil.copytree('tests/data/app-1', self.build_dir)
        setup_htdocs_if_it_doesnt_exist(utils.FormattedDict({
            'BUILD_DIR': self.build_dir
        }))
        self.assert_exists(self.build_dir, 'htdocs')
        self.assert_exists(self.build_dir, 'htdocs', 'index.php')
        self.assert_exists(self.build_dir, 'htdocs', 'info.php')
        self.assert_exists(self.build_dir, 'htdocs',
                           'technical-difficulties1.jpg')
        self.assert_exists(self.build_dir, 'config')
        self.assert_exists(self.build_dir, 'config', 'options.json')
        self.assert_exists(self.build_dir, 'config', 'httpd', 'extra',
                           'httpd-remoteip.conf')
        eq_(2, len(os.listdir(self.build_dir)))
        eq_(3, len(os.listdir(os.path.join(self.build_dir, 'htdocs'))))

    @with_setup(setup=setUp, teardown=tearDown)
    def test_setup_if_htdocs_does_not_exist(self):
        shutil.copytree('tests/data/app-2', self.build_dir)
        setup_htdocs_if_it_doesnt_exist(utils.FormattedDict({
            'BUILD_DIR': self.build_dir
        }))
        self.assert_exists(self.build_dir, 'htdocs')
        self.assert_exists(self.build_dir, 'htdocs', 'index.php')
        self.assert_exists(self.build_dir, 'htdocs', 'info.php')
        self.assert_exists(self.build_dir, 'htdocs',
                           'technical-difficulties1.jpg')
        self.assert_exists(self.build_dir, 'config')
        self.assert_exists(self.build_dir, 'config', 'options.json')
        self.assert_exists(self.build_dir, 'config', 'httpd', 'extra',
                           'httpd-remoteip.conf')
        eq_(2, len(os.listdir(self.build_dir)))
        eq_(3, len(os.listdir(os.path.join(self.build_dir, 'htdocs'))))

    def test_convert_php_extensions(self):
        ctx = {
            'PHP_EXTENSIONS': ['mod1', 'mod2', 'mod3'],
            'ZEND_EXTENSIONS': ['zmod1', 'zmod2']
        }
        convert_php_extensions(ctx)
        eq_('extension=mod1.so\nextension=mod2.so\nextension=mod3.so',
            ctx['PHP_EXTENSIONS'])
        eq_('zend_extension="@{HOME}/php/lib/php/extensions/'
            'no-debug-non-zts-20100525/zmod1.so"\n'
            'zend_extension="@{HOME}/php/lib/php/extensions/'
            'no-debug-non-zts-20100525/zmod2.so"',
            ctx['ZEND_EXTENSIONS'])

    def test_convert_php_extensions_none(self):
        ctx = {
            'PHP_EXTENSIONS': [],
            'ZEND_EXTENSIONS': []
        }
        convert_php_extensions(ctx)
        eq_('', ctx['PHP_EXTENSIONS'])
        eq_('', ctx['ZEND_EXTENSIONS'])

    def test_convert_php_extensions_one(self):
        ctx = {
            'PHP_EXTENSIONS': ['mod1'],
            'ZEND_EXTENSIONS': ['zmod1']
        }
        convert_php_extensions(ctx)
        eq_('extension=mod1.so', ctx['PHP_EXTENSIONS'])
        eq_('zend_extension="@{HOME}/php/lib/php/extensions/'
            'no-debug-non-zts-20100525/zmod1.so"',
            ctx['ZEND_EXTENSIONS'])

{'fraction_non_alphanumeric': 0.14067172762824937, 'fraction_numerical': 0.010582010582010581, 'mean_word_length': 2.7, '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': '751317', 'n_tokens_mistral': 2990, 'n_tokens_neox': 2926, 'n_words': 390}