<commit_before>import os
import subprocess
from glob import glob
from nose.plugins.skip import SkipTest
from nose.tools import eq_
from os.path import join
from temporary import in_temp_dir, temp_file
from textwrap import dedent
from bashup.test.common import assert_eq


# Compile some bashup and run it!
#
# This will obviously only work if bash exists on the system. Otherwise
# the test is skipped.
#
def test_compiled_bash():
    bash_binaries = __find_bash_binaries()

    if not bash_binaries:
        raise SkipTest('bash executable not found')  # pragma: no cover

    @in_temp_dir()
    def assert_compiled_bash(bash_binary):
        with temp_file(__BASHUP_STR) as in_file:
            subprocess.check_call(args=(
                'bashup', '--in', in_file, '--out', 'hi.sh'))

        p = subprocess.Popen(
            args=(bash_binary, 'hi.sh'),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)

        stdout, _ = [o.decode('UTF-8').strip() for o in p.communicate()]

        assert_eq(stdout, __EXPECTED_OUTPUT)
        eq_(p.returncode, 55)

    for b in bash_binaries:
        yield assert_compiled_bash, b


def test_direct_run():
    if not __is_bash_in_path():
        raise SkipTest('bash executable not found')  # pragma: no cover

    with temp_file(__BASHUP_STR) as in_file:
        p = subprocess.Popen(
            args=('bashup', '--run', in_file),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)

        stdout, _ = [o.decode('UTF-8').strip() for o in p.communicate()]

    assert_eq(stdout, __EXPECTED_OUTPUT)
    eq_(p.returncode, 55)


#
# Test Helpers
#

__BASHUP_STR = dedent("""
    @fn hi greeting='Hello', target='World' {
        echo "${greeting}, ${target}!$@"<commit_msg>Don't expect skippable tests to be covered.<commit_after>import os
import subprocess
from glob import glob
from nose.plugins.skip import SkipTest
from nose.tools import eq_
from os.path import join
from temporary import in_temp_dir, temp_file
from textwrap import dedent
from bashup.test.common import assert_eq


# Compile some bashup and run it against multiple versions of bash. The
# versions are expected to be found in $BASH_VERSIONS_DIR. If none are
# found, or the environment variable is not set, the tests are skipped.
#
def test_compiled_bash():
    bash_binaries = __find_bash_binaries()

    if not bash_binaries:
        raise SkipTest('bash executable not found')  # pragma: no cover

    @in_temp_dir()                                   # pragma: no cover
    def assert_compiled_bash(bash_binary):           # pragma: no cover
        with temp_file(__BASHUP_STR) as in_file:     # pragma: no cover
            subprocess.check_call(args=(
                'bashup',
                '--in', in_file,
                '--out', 'hi.sh'))                   # pragma: no cover

        p = subprocess.Popen(
            args=(bash_binary, 'hi.sh'),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)                  # pragma: no cover

        stdout, _ = [
            o.decode('UTF-8').strip() for
            o in p.communicate()]                    # pragma: no cover

        assert_eq(stdout, __EXPECTED_OUTPUT)         # pragma: no cover
        eq_(p.returncode, 55)                        # pragma: no cover

    for b in bash_binaries:                          # pragma: no cover
        yield assert_compiled_bash, b                # pragma: no cover


# Compile some bashup and run it!
#
# This will obviously only work if bash exists on the system. Otherwise
# the test is skipped.
#
def test_direct_run():
    if not __is_bash_in_path():
        raise SkipTest('bash executable not found')  # pragma: no cover

    with temp_file(__BASHUP_STR) as in_file:         # pragma: no cover
        p = subprocess.Popen(
            args=('bashup', '--run', in_file),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)                  # pragma: no cover

        stdout, _ = [
            o.decode('UTF-8').strip() for
            o in p.communicate()]                    # pragma: no cover

    assert_eq(stdout, __EXPECTED_OUTPUT)             # pragma: no cover
    eq_(p.returncode, 55)                            # pragma: no cover


#
# Test Helpers
#

__BASHUP_STR = dedent("""
    @fn hi greeting='Hello', target='World' {
        echo "${greeting}, ${target}!$@"
{'fraction_non_alphanumeric': 0.09744990892531877, 'fraction_numerical': 0.00273224043715847, 'mean_word_length': 2.2686011904761907, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1973466', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1306, 'n_words': 387}