<commit_before>import os

import pytest

from twosheds import Shell
from twosheds.completer import Completer
from twosheds.grammar import Grammar
from twosheds.transform import (AliasTransform,
                                VariableTransform,
                                TildeTransform,)


@pytest.fixture
def aliases():
    return {
        "ls": "ls -G",
        "home": "cd ~",
    }


@pytest.fixture
def completer(grammar):
    return Completer(grammar)


@pytest.fixture
def environment():
    return {
        "HOME": "/user/twosheds",
        "LOGNAME": "twosheds",
    }


@pytest.fixture
def alias_transform(aliases):
    return AliasTransform(aliases)


@pytest.fixture
def variable_transform(environment):
    return VariableTransform(environment)


@pytest.fixture
def tilde_transform(environment):
    return TildeTransform(environment['HOME'])


@pytest.fixture
def grammar(alias_transform, tilde_transform, variable_transform):
    transforms = [
        alias_transform,
        variable_transform,
        tilde_transform,
    ]
    return Grammar(transforms=transforms)


@pytest.fixture
def shell():
    return Shell()


def test_shell(shell):
    assert shell.eval("echo") is None

def test_alias_substitution1(alias_transform):
    """Alias substitution should expand aliases."""
    text = "ls"
    assert alias_transform(text) == "ls -G"


def test_alias_substitution2(alias_transform):
    """Alias substitution should not expand arguments."""
    text = "echo ls"
    assert alias_transform(text) == text


def test_alias_substitution_inverse(alias_transform):
    """Alias substitution should have an inverse."""
    text = "ls"
    assert alias_transform(alias_transform(text), inverse=True) == text


def test_variable_substitution(variable_transform):
    text = "cd $HOME"
    assert variable_transform(text) == "cd /user/twosheds"


def test_variable_substitution_inverse(variable_transform):
    text = "cd /user/twosheds"
    assert variable_transform(text, inverse=True) == "cd $HOME"


def test_variable_substitution_order(variable_transform):
    """Variable substitution should substitute longer values first."""
    text = "cd /user/twosheds"
    assert variable_transform(text, inverse=True) == "cd $HOME"


def test_variable_substitution_id(variable_transform):
    text = "$HOME"
    assert variable_transform(variable_transform(text), inverse=True) == text


def test_tilde_substitution1(tilde_transform, environment):
    """Tilde substitution should expand ``~`` as ``$HOME``."""
    text = "cd ~"
    assert tilde_transform(text) == "cd %s" % environment['HOME']
    text = "cd ~/Desktop"
    assert tilde_transform(text) == "cd %s/Desktop" % environment['HOME']


def test_tilde_substitution2(tilde_transform):
    """
    Tilde substitution should not expand a tilde unless it is the prefix
    of a token.
    """
    text = "git rebase -i HEAD~3"
    assert tilde_transform(text) == text


def test_tilde_substitution_inverse(tilde_transform):
    """Tilde substitution should have an inverse."""
    text = "~"
    assert tilde_transform(tilde_transform(text), inverse=True) == text


def test_grammar_transform(grammar):
    text = "home"
    assert grammar.transform(text) == "cd /user/twosheds"


def test_grammar_transform_inverse(grammar):
    text = "cd /user/twosheds"
    assert grammar.transform(text, inverse=True) == "home"


def test_grammar_transform_id(grammar):
    text = "home"
    assert grammar.transform(grammar.transform(text), inverse=True) == text
<commit_msg>factor out global environment variables<commit_after>import os

import pytest

from twosheds import Shell
from twosheds.completer import Completer
from twosheds.grammar import Grammar
from twosheds.transform import (AliasTransform,
                                VariableTransform,
                                TildeTransform,)

EDITOR = "vim"
HOME = os.environ['HOME']
LOGNAME = os.environ['LOGNAME']

@pytest.fixture
def aliases():
    return {
        "ls": "ls -G",
        "home": "cd ~",
    }


@pytest.fixture
def completer(grammar):
    return Completer(grammar)


@pytest.fixture
def environment():
    return {
        "EDITOR": EDITOR,
        "HOME": HOME,
        "LOGNAME": LOGNAME,
    }


@pytest.fixture
def alias_transform(aliases):
    return AliasTransform(aliases)


@pytest.fixture
def variable_transform(environment):
    return VariableTransform(environment)


@pytest.fixture
def tilde_transform(environment):
    return TildeTransform(environment['HOME'])


@pytest.fixture
def grammar(alias_transform, tilde_transform, variable_transform):
    transforms = [
        alias_transform,
        variable_transform,
        tilde_transform,
    ]
    return Grammar(transforms=transforms)


@pytest.fixture
def shell():
    return Shell()


def test_shell(shell):
    assert shell.eval("echo") is None

def test_alias_substitution1(alias_transform):
    """Alias substitution should expand aliases."""
    text = "ls"
    assert alias_transform(text) == "ls -G"


def test_alias_substitution2(alias_transform):
    """Alias substitution should not expand arguments."""
    text = "echo ls"
    assert alias_transform(text) == text


def test_alias_substitution_inverse(alias_transform):
    """Alias substitution should have an inverse."""
    text = "ls"
    assert alias_transform(alias_transform(text), inverse=True) == text


def test_variable_substitution(variable_transform):
    text = "cd $HOME"
    assert variable_transform(text) == "cd %s" % HOME


def test_variable_substitution_inverse(variable_transform):
    text = "cd %s" % HOME
    assert variable_transform(text, inverse=True) == "cd $HOME"


def test_variable_substitution_order(variable_transform):
    """Variable substitution should substitute longer values first."""
    text = "cd %s" % HOME
    assert variable_transform(text, inverse=True) == "cd $HOME"


def test_variable_substitution_id(variable_transform):
    text = "$HOME"
    assert variable_transform(variable_transform(text), inverse=True) == text


def test_tilde_substitution1(tilde_transform, environment):
    """Tilde substitution should expand ``~`` as ``$HOME``."""
    text = "cd ~"
    assert tilde_transform(text) == "cd %s" % environment['HOME']
    text = "cd ~/Desktop"
    assert tilde_transform(text) == "cd %s/Desktop" % environment['HOME']


def test_tilde_substitution2(tilde_transform):
    """
    Tilde substitution should not expand a tilde unless it is the prefix
    of a token.
    """
    text = "git rebase -i HEAD~3"
    assert tilde_transform(text) == text


def test_tilde_substitution_inverse(tilde_transform):
    """Tilde substitution should have an inverse."""
    text = "~"
    assert tilde_transform(tilde_transform(text), inverse=True) == text


def test_grammar_transform(grammar):
    text = "home"
    assert grammar.transform(text) == "cd %s" % HOME


def test_grammar_transform_inverse(grammar):
    text = "cd %s" % HOME
    assert grammar.transform(text, inverse=True) == "home"


def test_grammar_transform_id(grammar):
    text = "home"
    assert grammar.transform(grammar.transform(text), inverse=True) == text

{'fraction_non_alphanumeric': 0.11039776692254012, 'fraction_numerical': 0.0013956734124214933, 'mean_word_length': 3.99721059972106, 'pattern_counts': {'":': 9, '<': 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': '6912744', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2135, 'n_words': 523}