<commit_before>"""
Copyright (c) 2019 Red Hat, Inc
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.
"""

from copy import deepcopy
from io import BytesIO
from textwrap import dedent
import base64
import json
import os
import responses
import tarfile

from atomic_reactor import util
from atomic_reactor.constants import REMOTE_SOURCE_DIR, CACHITO_ENV_FILENAME, CACHITO_ENV_ARG_ALIAS
from atomic_reactor.inner import DockerBuildWorkflow
from atomic_reactor.source import SourceConfig
from atomic_reactor.plugins.pre_reactor_config import (
    ReactorConfigPlugin, WORKSPACE_CONF_KEY, ReactorConfig)
from atomic_reactor.utils.cachito import CFG_TYPE_B64
from tests.stubs import StubInsideBuilder
from atomic_reactor.plugins.pre_download_remote_source import (
    DownloadRemoteSourcePlugin,
)
import pytest
from tests.stubs import StubSource


CFG_CONTENT = b'configContent'
CACHITO_ENV_SHEBANG = "#!/bin/bash\n"
CACHITO_ENV_VARIABLES1 = (
    "export spam=maps\n"
    "export foo='somefile; rm -rf ~'\n"
)
CACHITO_ENV_VARIABLES2 = (
    "export varkey=varval\n"
    "export fioo='newfile; rm -rf ~'\n"
)


def mock_reactor_config(workflow, insecure=False):
    data = dedent("""\
        version: 1
        koji:
            hub_url: /
            root_url: ''
            auth: {{}}
        cachito:
           api_url: 'example.com'
           insecure: {}
           auth:
               ssl_certs_dir: /some/dir
        """.format(insecure))

    workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
    config = util.read_yaml(data, 'schemas/config.json')
    workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] = ReactorConfig(config)


def mock_repo_config(workflow, tmpdir, multiple_remote_sources=False):
    data = ""

    if multiple_remote_sources:
        data = dedent("""\
            remote_sources:
            - name: first
              remote-source:
                repo: test_repo
                ref: e1be527f39ec31323f0454f7d1422c6260b00580
            """)

    if not os.path.exists(tmpdir):
        os.makedirs(tmpdir)

    with open(os.path.join(tmpdir, 'container.yaml'), 'w') as f:
        f.write(data)
        f.flush()

    class MockSource(StubSource):
        def __init__(self, workdir):
            super(MockSource, self).__init__()<commit_msg>Fix mocking of repo config for multiple remote sources<commit_after>"""
Copyright (c) 2019 Red Hat, Inc
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.
"""

from copy import deepcopy
from io import BytesIO
from textwrap import dedent
import base64
import json
import os
import responses
import tarfile

from atomic_reactor import util
from atomic_reactor.constants import REMOTE_SOURCE_DIR, CACHITO_ENV_FILENAME, CACHITO_ENV_ARG_ALIAS
from atomic_reactor.inner import DockerBuildWorkflow
from atomic_reactor.source import SourceConfig
from atomic_reactor.plugins.pre_reactor_config import (
    ReactorConfigPlugin, WORKSPACE_CONF_KEY, ReactorConfig)
from atomic_reactor.utils.cachito import CFG_TYPE_B64
from tests.stubs import StubInsideBuilder
from atomic_reactor.plugins.pre_download_remote_source import (
    DownloadRemoteSourcePlugin,
)
import pytest
from tests.stubs import StubSource


CFG_CONTENT = b'configContent'
CACHITO_ENV_SHEBANG = "#!/bin/bash\n"
CACHITO_ENV_VARIABLES1 = (
    "export spam=maps\n"
    "export foo='somefile; rm -rf ~'\n"
)
CACHITO_ENV_VARIABLES2 = (
    "export varkey=varval\n"
    "export fioo='newfile; rm -rf ~'\n"
)


def mock_reactor_config(workflow, insecure=False):
    data = dedent("""\
        version: 1
        koji:
            hub_url: /
            root_url: ''
            auth: {{}}
        cachito:
           api_url: 'example.com'
           insecure: {}
           auth:
               ssl_certs_dir: /some/dir
        """.format(insecure))

    workflow.plugin_workspace[ReactorConfigPlugin.key] = {}
    config = util.read_yaml(data, 'schemas/config.json')
    workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] = ReactorConfig(config)


def mock_repo_config(workflow, tmpdir, multiple_remote_sources=False):
    data = ""

    if multiple_remote_sources:
        data = dedent("""\
            remote_sources:
            - name: first
              remote_source:
                repo: test_repo
                ref: e1be527f39ec31323f0454f7d1422c6260b00580
            """)

    if not os.path.exists(tmpdir):
        os.makedirs(tmpdir)

    with open(os.path.join(tmpdir, 'container.yaml'), 'w') as f:
        f.write(data)
        f.flush()

    class MockSource(StubSource):
        def __init__(self, workdir):
            super(MockSource, self).__init__()
{'fraction_non_alphanumeric': 0.08800993994615862, 'fraction_numerical': 0.016566576931041622, 'mean_word_length': 3.1927083333333335, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7382085', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1542, 'n_words': 357}