<commit_before>import os
import textwrap
import shutil
import pytest

sep = os.path.sep


@pytest.fixture(scope='function')
def theme_project_tmpdir(tmpdir):
    """Copy themes-project to a temp dir, and copy demo-project content to it."""
    themes_dir = os.path.join(os.path.dirname(__file__), 'themes-project')
    content_dir = os.path.join(os.path.dirname(__file__), 'demo-project', 'content')

    temp_dir = tmpdir.mkdir("temp").join('themes-project')

    shutil.copytree(themes_dir, str(temp_dir))
    shutil.copytree(content_dir, str(temp_dir.join('content')))

    return temp_dir


@pytest.fixture(scope='function')
def theme_project(theme_project_tmpdir, request):
    """Return the theme project created in a temp dir.

    Could be parametrize, if request.param=False themes variables won't be set
    """
    try:
        with_themes_var = request.param
    except AttributeError:
        with_themes_var = True

    from lektor.project import Project

    # Create the .lektorproject file
    lektorfile_text = textwrap.dedent(u"""
        [project]
        name = Themes Project
        {}
    """.format("themes = blog_theme, project_theme"
               if with_themes_var else ""))

    theme_project_tmpdir.join("themes.lektorproject").write_text(
        lektorfile_text, "utf8", ensure=True)
    return Project.from_path(str(theme_project_tmpdir))


@pytest.fixture(scope='function')
def theme_env(theme_project):
    from lektor.environment import Environment
    return Environment(theme_project)


@pytest.fixture(scope='function')
def theme_pad(theme_env):
    from lektor.db import Database
    return Database(theme_env).new_pad()


@pytest.fixture(scope='function')
def theme_builder(theme_pad, tmpdir):
    from lektor.builder import Builder
    return Builder(theme_pad, str(tmpdir.mkdir("output")))


@pytest.mark.parametrize(
    'theme_project, themes', [(True, ['blog_theme', 'project_theme']),
                              # when themes variables isn't set themes are only loaded in the env
                              (False, [])],
    indirect=['theme_project'])
def test_loading_theme_variable(theme_project, themes):
    assert theme_project.themes == themes


@pytest.mark.parametrize('theme_project', (True, False), indirect=True)
def test_loading_theme_path(theme_env):
    assert [os.path.basename(p)
            for p in theme_env.theme_paths] == ['blog_theme', 'project_theme']


@pytest.mark.parametrize(
    'asset_name, found_in',
    [
        # - themes-project/assets/dummy.txt
        # - themes-project/themes/blog_theme/assets/dummy.txt
        # wil be loaded from themes-project assets not from blog_theme assets
        ('dummy.txt', 'root'),
        # - themes-project/themes/blog_theme/static/blog.css<commit_msg>Split loading test_loading_theme_path, because order isn't preserved when the themes variable isn't set.<commit_after>import os
import textwrap
import shutil
import pytest

sep = os.path.sep


@pytest.fixture(scope='function')
def theme_project_tmpdir(tmpdir):
    """Copy themes-project to a temp dir, and copy demo-project content to it."""
    themes_dir = os.path.join(os.path.dirname(__file__), 'themes-project')
    content_dir = os.path.join(os.path.dirname(__file__), 'demo-project', 'content')

    temp_dir = tmpdir.mkdir("temp").join('themes-project')

    shutil.copytree(themes_dir, str(temp_dir))
    shutil.copytree(content_dir, str(temp_dir.join('content')))

    return temp_dir


@pytest.fixture(scope='function')
def theme_project(theme_project_tmpdir, request):
    """Return the theme project created in a temp dir.

    Could be parametrize, if request.param=False themes variables won't be set
    """
    try:
        with_themes_var = request.param
    except AttributeError:
        with_themes_var = True

    from lektor.project import Project

    # Create the .lektorproject file
    lektorfile_text = textwrap.dedent(u"""
        [project]
        name = Themes Project
        {}
    """.format("themes = blog_theme, project_theme"
               if with_themes_var else ""))

    theme_project_tmpdir.join("themes.lektorproject").write_text(
        lektorfile_text, "utf8", ensure=True)
    return Project.from_path(str(theme_project_tmpdir))


@pytest.fixture(scope='function')
def theme_env(theme_project):
    from lektor.environment import Environment
    return Environment(theme_project)


@pytest.fixture(scope='function')
def theme_pad(theme_env):
    from lektor.db import Database
    return Database(theme_env).new_pad()


@pytest.fixture(scope='function')
def theme_builder(theme_pad, tmpdir):
    from lektor.builder import Builder
    return Builder(theme_pad, str(tmpdir.mkdir("output")))


@pytest.mark.parametrize(
    'theme_project, themes', [(True, ['blog_theme', 'project_theme']),
                              # when themes variables isn't set themes are only loaded in the env
                              (False, [])],
    indirect=['theme_project'])
def test_loading_theme_variable(theme_project, themes):
    assert theme_project.themes == themes


@pytest.mark.parametrize('theme_project', (True,), indirect=True)
def test_loading_theme_path(theme_env):
    assert [os.path.basename(p)
            for p in theme_env.theme_paths] == ['blog_theme', 'project_theme']


@pytest.mark.parametrize('theme_project', (False,), indirect=True)
def test_loading_theme_path_variable_dont_set(theme_env):
    """Themes will be loaded, but the order could change."""
    paths = [os.path.basename(p) for p in theme_env.theme_paths]
    assert len(paths) == 2
    for path in paths:
        assert path in ['blog_theme', 'project_theme']


@pytest.mark.parametrize(
    'asset_name, found_in',
    [
        # - themes-project/assets/dummy.txt
        # - themes-project/themes/blog_theme/assets/dummy.txt
        # wil be loaded from themes-project assets not from blog_theme assets
        ('dummy.txt', 'root'),
        # - themes-project/themes/blog_theme/static/blog.css
{'fraction_non_alphanumeric': 0.11594202898550725, 'fraction_numerical': 0.0004997501249375312, 'mean_word_length': 3.701644479248238, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2445507', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1866, 'n_words': 408}