<commit_before>import os
import tempfile

import pytest

from flexmock import flexmock

from pyp2rpm.package_getters import *
from pyp2rpm.exceptions import *

tests_dir = os.path.split(os.path.abspath(__file__))[0]


class TestPypiFileGetter(object):
    client = flexmock(
        package_releases=lambda n: n == 'spam' and ['2', '1'] or [],
        release_urls=lambda n, v: n == 'spam' and v in ['2', '1'] and [{'url': 'spam'}] or []
    )

    @pytest.mark.parametrize(('name', 'version'), [
        ('eggs', '2'),
        ('spam', '3'),
    ])
    def test_init_bad_data(self, name, version):
        with pytest.raises(NoSuchPackageException):
            PypiDownloader(self.client, name, version)

    @pytest.mark.parametrize(('name', 'version', 'expected_ver'), [
        ('spam', '1', '1'),
        ('spam', None, '2'),
    ])
    def test_init_good_data(self, name, version, expected_ver):
        d = PypiDownloader(self.client, name, version)
        assert d.version == expected_ver


class TestLocalFileGetter(object):
    td_dir = '{0}/test_data/'.format(tests_dir)

    def setup_method(self, method):
        self.l = [LocalFileGetter('{0}plumbum-0.9.0.tar.gz'.format(self.td_dir)),
                  LocalFileGetter('{0}Sphinx-1.1.3-py2.6.egg'.format(self.td_dir)),
                  LocalFileGetter('{0}unextractable-1.tar'.format(self.td_dir)),
                  LocalFileGetter('{0}setuptools-19.6-py2.py3-none-any.whl'.format(self.td_dir)),
                  LocalFileGetter('{0}py2exe-0.9.2.2-py33.py34-none-any.whl'.format(self.td_dir)),
                  LocalFileGetter('python-foo-1.tar'),
                  LocalFileGetter('python-many-dashes-foo-1.tar'),
                  ]

    @pytest.mark.parametrize(('i', 'expected'), [
        (0, 'plumbum-0.9.0'),
        (1, 'Sphinx-1.1.3-py2.6'),
        (2, 'unextractable-1'),
        (3, 'setuptools-19.6-py2.py3-none-any'),
        (4, 'py2exe-0.9.2.2-py33.py34-none-any'),
    ])
    def test__stripped_name_version(self, i, expected):
        assert self.l[i]._stripped_name_version == expected

    @pytest.mark.parametrize(('i', 'expected'), [
        (0, 'plumbum-0.9.0'),
        (1, 'Sphinx-1.1.3-py2.6'),
        (2, 'unextractable-1'),
        (3, 'setuptools-19.6-py2.py3-none-any'),
        (4, 'py2exe-0.9.2.2-py33.py34-none-any'),
    ])
    def test__stripped_name_version(self, i, expected):
        assert self.l[i]._stripped_name_version == expected

    @pytest.mark.parametrize(('i', 'expected'), [
        (0, ('plumbum', '0.9.0')),
        (1, ('Sphinx', '1.1.3')),
        (3, ('setuptools', '19.6')),
        (4, ('py2exe', '0.9.2.2')),
        (5, ('python-foo', '1')),
        (6, ('python-many-dashes-foo', '1')),<commit_msg>Remove all temporary directories properly after the tests run<commit_after>import os
import tempfile
import shutil

import pytest

from flexmock import flexmock

from pyp2rpm.package_getters import *
from pyp2rpm.exceptions import *

tests_dir = os.path.split(os.path.abspath(__file__))[0]


class TestPypiFileGetter(object):
    client = flexmock(
        package_releases=lambda n: n == 'spam' and ['2', '1'] or [],
        release_urls=lambda n, v: n == 'spam' and v in ['2', '1'] and [{'url': 'spam'}] or []
    )

    @pytest.mark.parametrize(('name', 'version'), [
        ('eggs', '2'),
        ('spam', '3'),
    ])
    def test_init_bad_data(self, name, version):
        with pytest.raises(NoSuchPackageException):
            PypiDownloader(self.client, name, version)

    @pytest.mark.parametrize(('name', 'version', 'expected_ver'), [
        ('spam', '1', '1'),
        ('spam', None, '2'),
    ])
    def test_init_good_data(self, name, version, expected_ver):
        d = PypiDownloader(self.client, name, version)
        assert d.version == expected_ver


class TestLocalFileGetter(object):
    td_dir = '{0}/test_data/'.format(tests_dir)

    def setup_method(self, method):
        self.l = [LocalFileGetter('{0}plumbum-0.9.0.tar.gz'.format(self.td_dir)),
                  LocalFileGetter('{0}Sphinx-1.1.3-py2.6.egg'.format(self.td_dir)),
                  LocalFileGetter('{0}unextractable-1.tar'.format(self.td_dir)),
                  LocalFileGetter('{0}setuptools-19.6-py2.py3-none-any.whl'.format(self.td_dir)),
                  LocalFileGetter('{0}py2exe-0.9.2.2-py33.py34-none-any.whl'.format(self.td_dir)),
                  LocalFileGetter('python-foo-1.tar'),
                  LocalFileGetter('python-many-dashes-foo-1.tar'),
                  ]

    def teardown_method(self, method):
        for file_getter in self.l:
            if hasattr(file_getter, 'temp_dir'):
                shutil.rmtree(file_getter.temp_dir)

    @pytest.mark.parametrize(('i', 'expected'), [
        (0, 'plumbum-0.9.0'),
        (1, 'Sphinx-1.1.3-py2.6'),
        (2, 'unextractable-1'),
        (3, 'setuptools-19.6-py2.py3-none-any'),
        (4, 'py2exe-0.9.2.2-py33.py34-none-any'),
    ])
    def test__stripped_name_version(self, i, expected):
        assert self.l[i]._stripped_name_version == expected

    @pytest.mark.parametrize(('i', 'expected'), [
        (0, 'plumbum-0.9.0'),
        (1, 'Sphinx-1.1.3-py2.6'),
        (2, 'unextractable-1'),
        (3, 'setuptools-19.6-py2.py3-none-any'),
        (4, 'py2exe-0.9.2.2-py33.py34-none-any'),
    ])
    def test__stripped_name_version(self, i, expected):
        assert self.l[i]._stripped_name_version == expected

    @pytest.mark.parametrize(('i', 'expected'), [
        (0, ('plumbum', '0.9.0')),
        (1, ('Sphinx', '1.1.3')),
        (3, ('setuptools', '19.6')),
        (4, ('py2exe', '0.9.2.2')),
        (5, ('python-foo', '1')),
        (6, ('python-many-dashes-foo', '1')),
{'fraction_non_alphanumeric': 0.19052631578947368, 'fraction_numerical': 0.04245614035087719, 'mean_word_length': 2.621982210927573, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6486381', 'n_tokens_mistral': 2236, 'n_tokens_neox': 2192, 'n_words': 326}