<commit_before>#!/usr/bin/env python

from __future__ import absolute_import

import os
import shutil
import sys
import tempfile

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from contextlib import contextmanager

import pip

import bootstrapper


DIRNAME = os.path.abspath(os.path.dirname(__file__))
TEST_CONFIG = """[bootstrapper]
pre_requirements = python
quiet = True

[pip]
allow_external = elementtree PIL polib
allow_unverified = elementtree PIL polib
"""


class TestBootstrapper(unittest.TestCase):

    config = None
    requirements = 'test-requirements.txt'
    venv = 'test-env'

    def setUp(self):
        os.environ[bootstrapper.BOOTSTRAPPER_TEST_KEY] = '1'

    def tearDown(self):
        if bootstrapper.BOOTSTRAPPER_TEST_KEY in os.environ:
            os.environ.pop(bootstrapper.BOOTSTRAPPER_TEST_KEY)
        if self.config is not None:
            self.delete(self.config)
        self.delete(self.requirements, bootstrapper.safe_path(self.venv))

    def delete(self, *files):
        for item in files:
            if os.path.isfile(item):
                os.unlink(item)
            elif os.path.isdir(item):
                shutil.rmtree(item)

    def init_requirements(self, *lines):
        with open(self.requirements, 'w+') as handler:
            handler.write('\n'.join(lines))
        self.assertTrue(os.path.isfile(self.requirements))

    def message(self, out, err, echo=False):
        output = '\n'.join(('[STDOUT]', out, '', '[STDERR]', err))
        if echo:
            print(output)
        else:
            return output

    @contextmanager
    def redirect_streams(self, out, err):
        original_out, original_err = sys.stdout, sys.stderr
        sys.stdout, sys.stderr = out, err
        yield
        sys.stdout, sys.stderr = original_out, original_err

        out.seek(0)
        err.seek(0)

    def run_cmd(self, cmd):
        kwargs = {}
        on_travis = 'TRAVIS_PYTHON_VERSION' in os.environ
        out, err = bootstrapper.get_temp_streams()

        if cmd == 'bootstrap':
            func = bootstrapper.main
            args = ('-e', self.venv, '-r', self.requirements)
            if self.config:
                args += ('-c', self.config)
            if on_travis:
                args += ('--ignore-activated', )
        elif cmd.startswith('pip '):
            func = bootstrapper.pip_cmd
            args = (self.venv, cmd[4:].split(), on_travis, )
            kwargs['echo'] = True
        else:
            assert False, 'Command {0!r} is not supported!'.format(cmd)

        with self.redirect_streams(out, err):
            func(*args, **kwargs)

        return (out.read(), err.read())

    def test_install_error(self):
        os.environ.pop(bootstrapper.BOOTSTRAPPER_TEST_KEY)

        self.assertFalse(os.path.isdir(self.venv))
        self.init_requirements('ordereddict==1.1')

        os.mkdir(self.venv)<commit_msg>Close temporary files to avoid warning on Python 3.<commit_after>#!/usr/bin/env python

from __future__ import absolute_import

import os
import shutil
import sys
import tempfile

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from contextlib import contextmanager

import pip

import bootstrapper


DIRNAME = os.path.abspath(os.path.dirname(__file__))
TEST_CONFIG = """[bootstrapper]
pre_requirements = python
quiet = True

[pip]
allow_external = elementtree PIL polib
allow_unverified = elementtree PIL polib
"""


class TestBootstrapper(unittest.TestCase):

    config = None
    requirements = 'test-requirements.txt'
    venv = 'test-env'

    def setUp(self):
        os.environ[bootstrapper.BOOTSTRAPPER_TEST_KEY] = '1'

    def tearDown(self):
        if bootstrapper.BOOTSTRAPPER_TEST_KEY in os.environ:
            os.environ.pop(bootstrapper.BOOTSTRAPPER_TEST_KEY)
        if self.config is not None:
            self.delete(self.config)
        self.delete(self.requirements, bootstrapper.safe_path(self.venv))

    def delete(self, *files):
        for item in files:
            if os.path.isfile(item):
                os.unlink(item)
            elif os.path.isdir(item):
                shutil.rmtree(item)

    def init_requirements(self, *lines):
        with open(self.requirements, 'w+') as handler:
            handler.write('\n'.join(lines))
        self.assertTrue(os.path.isfile(self.requirements))

    def message(self, out, err, echo=False):
        output = '\n'.join(('[STDOUT]', out, '', '[STDERR]', err))
        if echo:
            print(output)
        else:
            return output

    @contextmanager
    def redirect_streams(self, out, err):
        original_out, original_err = sys.stdout, sys.stderr
        sys.stdout, sys.stderr = out, err
        yield
        sys.stdout, sys.stderr = original_out, original_err

        out.seek(0)
        err.seek(0)

    def run_cmd(self, cmd):
        kwargs = {}
        on_travis = 'TRAVIS_PYTHON_VERSION' in os.environ
        out, err = bootstrapper.get_temp_streams()

        if cmd == 'bootstrap':
            func = bootstrapper.main
            args = ('-e', self.venv, '-r', self.requirements)
            if self.config:
                args += ('-c', self.config)
            if on_travis:
                args += ('--ignore-activated', )
        elif cmd.startswith('pip '):
            func = bootstrapper.pip_cmd
            args = (self.venv, cmd[4:].split(), on_travis, )
            kwargs['echo'] = True
        else:
            assert False, 'Command {0!r} is not supported!'.format(cmd)

        with self.redirect_streams(out, err):
            func(*args, **kwargs)

        try:
            return (out.read(), err.read())
        finally:
            out.close()
            err.close()

    def test_install_error(self):
        os.environ.pop(bootstrapper.BOOTSTRAPPER_TEST_KEY)

        self.assertFalse(os.path.isdir(self.venv))
        self.init_requirements('ordereddict==1.1')

        os.mkdir(self.venv)
{'fraction_non_alphanumeric': 0.11535893155258764, 'fraction_numerical': 0.0028380634390651085, 'mean_word_length': 2.332035595105673, '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': '5718230', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1874, 'n_words': 403}