<commit_before># coding: utf-8
'''Common test fixtures

@author:     <NAME> (:truber)

@license:

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
'''
import logging
import os
import shutil
import subprocess
import tempfile
import pytest
from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType
from django.core.files import File
from covmanager.models import Collection, CollectionFile, Repository
from crashmanager.models import Client, Tool, User as cmUser


LOG = logging.getLogger("fm.covmanager.tests")


def _check_git():
    try:
        proc = subprocess.Popen(["git"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output = proc.communicate()
        if output and proc.wait() == 1:
            return True
    except OSError:  # FileNotFoundError
        pass
    return False


def _check_hg():
    try:
        proc = subprocess.Popen(["hg"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output = proc.communicate()
        if output and proc.wait() == 0:
            return True
    except OSError:  # FileNotFoundError
        pass
    return False


HAVE_GIT = _check_git()
HAVE_HG = _check_hg()


@pytest.fixture
def covmanager_test(db):  # pylint: disable=invalid-name,unused-argument
    """Common setup/teardown tasks for all server unittests"""
    user = User.objects.create_user('test', '<EMAIL>', 'test')
    user.user_permissions.clear()
    content_type = ContentType.objects.get_for_model(cmUser)
    perm = Permission.objects.get(content_type=content_type, codename='view_covmanager')
    user.user_permissions.add(perm)
    user_np = User.objects.create_user('test-noperm', '<EMAIL>', 'test')
    user_np.user_permissions.clear()


@pytest.fixture
def cm(request):

    class _result(object):
        have_git = HAVE_GIT
        have_hg = HAVE_HG

        @classmethod
        def create_repository(cls, repotype, name="testrepo"):
            location = tempfile.mkdtemp(prefix='testrepo', dir=os.path.dirname(__file__))
            request.addfinalizer(lambda: shutil.rmtree(location))
            if repotype == "git":
                if not HAVE_GIT:
                    pytest.skip("git is not available")
                classname = "GITSourceCodeProvider"
            elif repotype == "hg":
                if not HAVE_HG:
                    pytest.skip("hg is not available")
                classname = "HGSourceCodeProvider"
            else:
                raise Exception("unknown repository type: %s (expecting git or hg)" % repotype)
            result = Repository.objects.create(classname=classname, name=name, location=location)
            LOG.debug("Created Repository pk=%d", result.pk)
            if repotype == "git":
                cls.git(result, "init")
            elif repotype == "hg":
                cls.hg(result, "init")
            return result

        @staticmethod
        def create_collection_file(data):
            location = tempfile.mkdtemp(prefix='testcov_', dir=os.path.dirname(__file__))
            request.addfinalizer(lambda: shutil.rmtree(location))
            tmp_fd, path = tempfile.mkstemp(suffix=".data", dir=location)
            os.close(tmp_fd)
            with open(path, "w") as fp:
                fp.write(data)
            result = CollectionFile.objects.create(file=File(open(path)))
            LOG.debug("Created CollectionFile pk=%d", result.pk)
            return result

        @classmethod
        def create_collection(cls,
                              created=None,<commit_msg>Fix failing tests due to Django 2.2.21 (#657)<commit_after># coding: utf-8
'''Common test fixtures

@author:     <NAME> (:truber)

@license:

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
'''
import logging
import os
import shutil
import subprocess
import tempfile
import pytest
from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType
from covmanager.models import Collection, CollectionFile, Repository
from crashmanager.models import Client, Tool, User as cmUser


LOG = logging.getLogger("fm.covmanager.tests")


def _check_git():
    try:
        proc = subprocess.Popen(["git"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output = proc.communicate()
        if output and proc.wait() == 1:
            return True
    except OSError:  # FileNotFoundError
        pass
    return False


def _check_hg():
    try:
        proc = subprocess.Popen(["hg"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output = proc.communicate()
        if output and proc.wait() == 0:
            return True
    except OSError:  # FileNotFoundError
        pass
    return False


HAVE_GIT = _check_git()
HAVE_HG = _check_hg()


@pytest.fixture
def covmanager_test(db):  # pylint: disable=invalid-name,unused-argument
    """Common setup/teardown tasks for all server unittests"""
    user = User.objects.create_user('test', '<EMAIL>', 'test')
    user.user_permissions.clear()
    content_type = ContentType.objects.get_for_model(cmUser)
    perm = Permission.objects.get(content_type=content_type, codename='view_covmanager')
    user.user_permissions.add(perm)
    user_np = User.objects.create_user('test-noperm', '<EMAIL>', 'test')
    user_np.user_permissions.clear()


@pytest.fixture
def cm(request, settings, tmpdir):

    class _result(object):
        have_git = HAVE_GIT
        have_hg = HAVE_HG

        @classmethod
        def create_repository(cls, repotype, name="testrepo"):
            location = tempfile.mkdtemp(prefix='testrepo', dir=os.path.dirname(__file__))
            request.addfinalizer(lambda: shutil.rmtree(location))
            if repotype == "git":
                if not HAVE_GIT:
                    pytest.skip("git is not available")
                classname = "GITSourceCodeProvider"
            elif repotype == "hg":
                if not HAVE_HG:
                    pytest.skip("hg is not available")
                classname = "HGSourceCodeProvider"
            else:
                raise Exception("unknown repository type: %s (expecting git or hg)" % repotype)
            result = Repository.objects.create(classname=classname, name=name, location=location)
            LOG.debug("Created Repository pk=%d", result.pk)
            if repotype == "git":
                cls.git(result, "init")
            elif repotype == "hg":
                cls.hg(result, "init")
            return result

        @staticmethod
        def create_collection_file(data):

            # Use a specific temporary directory to upload covmanager files
            # This is required as Django now needs a path relative to that folder in FileField
            location = str(tmpdir)
            CollectionFile.file.field.storage.location = location

            tmp_fd, path = tempfile.mkstemp(suffix=".data", dir=location)
            os.close(tmp_fd)
            with open(path, "w") as fp:
                fp.write(data)
            result = CollectionFile.objects.create(file=os.path.basename(path))
            LOG.debug("Created CollectionFile pk=%d", result.pk)
            return result

        @classmethod
        def create_collection(cls,
                              created=None,
{'fraction_non_alphanumeric': 0.10223515408014813, 'fraction_numerical': 0.002777410395450337, 'mean_word_length': 2.57203589985829, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 9, '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': '4805478', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2144, 'n_words': 559}