<commit_before># -*- coding: utf-8 -*-
from bottle import static_file, request
import mock
import os
import platform
import unittest

from collections import namedtuple

import six
from mock.mock import patch, mock_open
from six import StringIO

from conans.client.client_cache import CONAN_CONF

from conans import tools
from conans.client.conan_api import ConanAPIV1
from conans.client.conf import default_settings_yml, default_client_conf
from conans.client.output import ConanOutput
from conans.client.tools.win import vcvars_dict, vswhere
from conans.client.tools.scm import Git

from conans.errors import ConanException, NotFoundException
from conans.model.settings import Settings

from conans.test.utils.runner import TestRunner
from conans.test.utils.test_files import temp_folder
from conans.test.utils.tools import TestClient, TestBufferConanOutput, create_local_git_repo, \
    StoppableThreadBottle

from conans.tools import which
from conans.tools import OSInfo, SystemPackageTool, replace_in_file, AptTool, ChocolateyTool,\
    set_global_instances
from conans.util.files import save, load, md5
import requests

from nose.plugins.attrib import attr


class SystemPackageToolTest(unittest.TestCase):
    def setUp(self):
        out = TestBufferConanOutput()
        set_global_instances(out, requests)

    def verify_update_test(self):
        # https://github.com/conan-io/conan/issues/3142
        with tools.environment_append({"CONAN_SYSREQUIRES_SUDO": "False",
                                       "CONAN_SYSREQUIRES_MODE": "Verify"}):
            runner = RunnerMock()
            # fake os info to linux debian, default sudo
            os_info = OSInfo()
            os_info.is_macos = False
            os_info.is_linux = True
            os_info.is_windows = False
            os_info.linux_distro = "debian"
            spt = SystemPackageTool(runner=runner, os_info=os_info)
            spt.update()
            self.assertEquals(runner.command_called, None)
            self.assertIn('Not updating system_requirements. CONAN_SYSREQUIRES_MODE=verify',
                          tools.system_pm._global_output)

    def add_repositories_exception_cases_test(self):
        os_info = OSInfo()
        os_info.is_macos = False
        os_info.is_linux = True
        os_info.is_windows = False
        os_info.linux_distro = "fedora"  # Will instantiate YumTool

        with self.assertRaisesRegexp(ConanException, "add_repository not implemented"):
            spt = SystemPackageTool(os_info=os_info)
            spt.add_repository(repository="deb http://repo/url/ saucy universe multiverse",
                               repo_key=None)

    def add_repositories_test(self):
        class RunnerOrderedMock:
            commands = []  # Command + return value

            def __call__(runner_self, command, output, win_bash=False, subsystem=None):
                if not len(runner_self.commands):
                    self.fail("Commands list exhausted, but runner called with '%s'" % command)
                expected, ret = runner_self.commands.pop(0)
                self.assertEqual(expected, command)
                return ret

        with tools.environment_append({"CONAN_SYSREQUIRES_SUDO": "True"}):
            repository = "deb http://repo/url/ saucy universe multiverse"
            gpg_key = 'http://one/key.gpg'

            # Add one repository giving a gpg key for it
            runner = RunnerOrderedMock()
            runner.commands.append(("sudo apt-add-repository {}".format(repository), 0))
            runner.commands.append(("wget -qO - {} | sudo apt-key add -".format(gpg_key), 0))

            os_info = OSInfo()
            os_info.is_macos = False
            os_info.is_linux = True
            os_info.is_windows = False
            os_info.linux_distro = "debian"
            spt = SystemPackageTool(runner=runner, os_info=os_info)

            spt.add_repository(repository=repository, repo_key=gpg_key, update=False)
            self.assertTrue(len(runner.commands) == 0)

            # Add one repository without gpg_key, but updating
            runner.commands.append(("sudo apt-add-repository {}".format(repository), 0))
            spt.add_repository(repository=repository, repo_key=None, update=False)
            self.assertTrue(len(runner.commands) == 0)

        with tools.environment_append({"CONAN_SYSREQUIRES_SUDO": "False"}):
            repository = "deb http://repo/url/ saucy universe multiverse"
            gpg_key = 'http://one/key.gpg'

            # Add one repository giving a gpg key for it
            runner = RunnerOrderedMock()
            runner.commands.append(("apt-add-repository {}".format(repository), 0))
            runner.commands.append(("wget -qO - {} | apt-key add -".format(gpg_key), 0))
            runner.commands.append(("apt-get update", 0))

            os_info = OSInfo()
            os_info.is_macos = False
            os_info.is_linux = True
            os_info.is_windows = False
            os_info.linux_distro = "debian"
            spt = SystemPackageTool(runner=runner, os_info=os_info)

            spt.add_repository(repository=repository, repo_key=gpg_key, update=True)
            self.assertTrue(len(runner.commands) == 0)

    def system_package_tool_test(self):

        with tools.environment_append({"CONAN_SYSREQUIRES_SUDO": "True"}):
            runner = RunnerMock()
            # fake os info to linux debian, default sudo
            os_info = OSInfo()
            os_info.is_macos = False
            os_info.is_linux = True
            os_info.is_windows = False
            os_info.linux_distro = "debian"
            spt = SystemPackageTool(runner=runner, os_info=os_info)
            spt.update()
            self.assertEquals(runner.command_called, "sudo apt-get update")

            os_info.linux_distro = "ubuntu"
            spt = SystemPackageTool(runner=runner, os_info=os_info)
            spt.update()
            self.assertEquals(runner.command_called, "sudo apt-get update")

            os_info.linux_distro = "knoppix"
            spt = SystemPackageTool(runner=runner, os_info=os_info)
            spt.update()
            self.assertEquals(runner.command_called, "sudo apt-get update")<commit_msg>create a helper function to run  test<commit_after># -*- coding: utf-8 -*-
from bottle import static_file, request
import mock
import os
import platform
import unittest

from collections import namedtuple

import six
from mock.mock import patch, mock_open
from six import StringIO

from conans.client.client_cache import CONAN_CONF

from conans import tools
from conans.client.conan_api import ConanAPIV1
from conans.client.conf import default_settings_yml, default_client_conf
from conans.client.output import ConanOutput
from conans.client.tools.win import vcvars_dict, vswhere
from conans.client.tools.scm import Git

from conans.errors import ConanException, NotFoundException
from conans.model.settings import Settings

from conans.test.utils.runner import TestRunner
from conans.test.utils.test_files import temp_folder
from conans.test.utils.tools import TestClient, TestBufferConanOutput, create_local_git_repo, \
    StoppableThreadBottle

from conans.tools import which
from conans.tools import OSInfo, SystemPackageTool, replace_in_file, AptTool, ChocolateyTool,\
    set_global_instances
from conans.util.files import save, load, md5
import requests

from nose.plugins.attrib import attr


class SystemPackageToolTest(unittest.TestCase):
    def setUp(self):
        out = TestBufferConanOutput()
        set_global_instances(out, requests)

    def verify_update_test(self):
        # https://github.com/conan-io/conan/issues/3142
        with tools.environment_append({"CONAN_SYSREQUIRES_SUDO": "False",
                                       "CONAN_SYSREQUIRES_MODE": "Verify"}):
            runner = RunnerMock()
            # fake os info to linux debian, default sudo
            os_info = OSInfo()
            os_info.is_macos = False
            os_info.is_linux = True
            os_info.is_windows = False
            os_info.linux_distro = "debian"
            spt = SystemPackageTool(runner=runner, os_info=os_info)
            spt.update()
            self.assertEquals(runner.command_called, None)
            self.assertIn('Not updating system_requirements. CONAN_SYSREQUIRES_MODE=verify',
                          tools.system_pm._global_output)

    def add_repositories_exception_cases_test(self):
        os_info = OSInfo()
        os_info.is_macos = False
        os_info.is_linux = True
        os_info.is_windows = False
        os_info.linux_distro = "fedora"  # Will instantiate YumTool

        with self.assertRaisesRegexp(ConanException, "add_repository not implemented"):
            spt = SystemPackageTool(os_info=os_info)
            spt.add_repository(repository="deb http://repo/url/ saucy universe multiverse",
                               repo_key=None)

    def add_repository_test(self):
        class RunnerOrderedMock:
            commands = []  # Command + return value

            def __call__(runner_self, command, output, win_bash=False, subsystem=None):
                if not len(runner_self.commands):
                    self.fail("Commands list exhausted, but runner called with '%s'" % command)
                expected, ret = runner_self.commands.pop(0)
                self.assertEqual(expected, command)
                return ret

        def _run_add_repository_test(repository, gpg_key, sudo, update):
            sudo_cmd = "sudo " if sudo else ""
            runner = RunnerOrderedMock()
            runner.commands.append(("{}apt-add-repository {}".format(sudo_cmd, repository), 0))
            if gpg_key:
                runner.commands.append(
                    ("wget -qO - {} | {}apt-key add -".format(gpg_key, sudo_cmd), 0))
            if update:
                runner.commands.append(("{}apt-get update".format(sudo_cmd), 0))

            with tools.environment_append({"CONAN_SYSREQUIRES_SUDO": str(sudo)}):
                os_info = OSInfo()
                os_info.is_macos = False
                os_info.is_linux = True
                os_info.is_windows = False
                os_info.linux_distro = "debian"
                spt = SystemPackageTool(runner=runner, os_info=os_info)

                spt.add_repository(repository=repository, repo_key=gpg_key, update=update)
                self.assertTrue(len(runner.commands) == 0)

        # Run several test cases
        repository = "deb http://repo/url/ saucy universe multiverse"
        gpg_key = 'http://one/key.gpg'
        _run_add_repository_test(repository, gpg_key, sudo=True, update=True)
        _run_add_repository_test(repository, gpg_key, sudo=True, update=False)
        _run_add_repository_test(repository, gpg_key, sudo=False, update=True)
        _run_add_repository_test(repository, gpg_key, sudo=False, update=False)
        _run_add_repository_test(repository, gpg_key=None, sudo=True, update=True)
        _run_add_repository_test(repository, gpg_key=None, sudo=False, update=False)

    def system_package_tool_test(self):

        with tools.environment_append({"CONAN_SYSREQUIRES_SUDO": "True"}):
            runner = RunnerMock()
            # fake os info to linux debian, default sudo
            os_info = OSInfo()
            os_info.is_macos = False
            os_info.is_linux = True
            os_info.is_windows = False
            os_info.linux_distro = "debian"
            spt = SystemPackageTool(runner=runner, os_info=os_info)
            spt.update()
            self.assertEquals(runner.command_called, "sudo apt-get update")

            os_info.linux_distro = "ubuntu"
            spt = SystemPackageTool(runner=runner, os_info=os_info)
            spt.update()
            self.assertEquals(runner.command_called, "sudo apt-get update")

            os_info.linux_distro = "knoppix"
            spt = SystemPackageTool(runner=runner, os_info=os_info)
            spt.update()
            self.assertEquals(runner.command_called, "sudo apt-get update")
{'fraction_non_alphanumeric': 0.08818910653773039, 'fraction_numerical': 0.0023968923051491857, 'mean_word_length': 2.5947712418300655, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4415144', 'n_tokens_mistral': 3575, 'n_tokens_neox': 3473, 'n_words': 754}