<commit_before>"""
Tests for salt.loader
"""
import os
import shutil
import sys

import pytest
import salt.loader
import salt.loader_context
import salt.utils.files
from tests.support.helpers import dedent


@pytest.fixture
def loader_dir(tmp_path):
    """
    Create a simple directory with a couple modules to load and run tests
    against.
    """
    mod_content = dedent(
        """
    def __virtual__():
        return True

    def set_context(key, value):
        __context__[key] = value

    def get_context(key):
        return __context__[key]
    """
    )
    with salt.utils.files.fopen(os.path.join(tmp_path, "mod_a.py"), "w") as fp:
        fp.write(mod_content)
    with salt.utils.files.fopen(os.path.join(tmp_path, "mod_b.py"), "w") as fp:
        fp.write(mod_content)
    try:
        yield tmp_path
    finally:
        shutil.rmtree(tmp_path)


def test_loaders_have_uniq_context(loader_dir):
    """
    Loaded functions run in the LazyLoader's context.
    """
    opts = {"optimization_order": [0, 1, 2]}
    loader_1 = salt.loader.LazyLoader([loader_dir], opts,)
    loader_2 = salt.loader.LazyLoader([loader_dir], opts,)
    loader_1._load_all()
    loader_2._load_all()
    assert loader_1.pack["__context__"] == {}
    assert loader_2.pack["__context__"] == {}
    loader_1["mod_a.set_context"]("foo", "bar")
    assert loader_1.pack["__context__"] == {"foo": "bar"}
    assert loader_1["mod_b.get_context"]("foo") == "bar"
    with pytest.raises(KeyError):
        loader_2["mod_a.get_context"]("foo")<commit_msg>Fix older pytest runs<commit_after>"""
Tests for salt.loader
"""
import os
import shutil
import sys

import pytest
import salt.loader
import salt.loader_context
import salt.utils.files
from tests.support.helpers import dedent


@pytest.fixture
def loader_dir(tmp_path):
    """
    Create a simple directory with a couple modules to load and run tests
    against.
    """
    mod_content = dedent(
        """
    def __virtual__():
        return True

    def set_context(key, value):
        __context__[key] = value

    def get_context(key):
        return __context__[key]
    """
    )
    tmp_path = str(tmp_path)
    with salt.utils.files.fopen(os.path.join(tmp_path, "mod_a.py"), "w") as fp:
        fp.write(mod_content)
    with salt.utils.files.fopen(os.path.join(tmp_path, "mod_b.py"), "w") as fp:
        fp.write(mod_content)
    try:
        yield tmp_path
    finally:
        shutil.rmtree(tmp_path)


def test_loaders_have_uniq_context(loader_dir):
    """
    Loaded functions run in the LazyLoader's context.
    """
    opts = {"optimization_order": [0, 1, 2]}
    loader_1 = salt.loader.LazyLoader([loader_dir], opts,)
    loader_2 = salt.loader.LazyLoader([loader_dir], opts,)
    loader_1._load_all()
    loader_2._load_all()
    assert loader_1.pack["__context__"] == {}
    assert loader_2.pack["__context__"] == {}
    loader_1["mod_a.set_context"]("foo", "bar")
    assert loader_1.pack["__context__"] == {"foo": "bar"}
    assert loader_1["mod_b.get_context"]("foo") == "bar"
    with pytest.raises(KeyError):
        loader_2["mod_a.get_context"]("foo")
{'fraction_non_alphanumeric': 0.13620414673046252, 'fraction_numerical': 0.008293460925039872, 'mean_word_length': 3.313617606602476, 'pattern_counts': {'":': 4, '<': 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': '3608887', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1081, 'n_words': 226}