<commit_before>import os
import sys
import subprocess
import pytest
import prophyc

main_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))


empty_python_output = """\
import prophy
"""


def call_as_subprocess(call_args):

    popen = subprocess.Popen([sys.executable, "-m", "prophyc"] + call_args,
                             cwd=main_dir,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    out, err = popen.communicate()
    return popen.returncode, out.decode(), err.decode()


@pytest.fixture(params=["subprocess", "py_code"])
def call(request, mocker):

    if request.param == "subprocess":
        return call_as_subprocess
    else:
        if request.node.name == "test_sack_parse_warnings[py_code]":
            pytest.xfail("Its not worth of effort to simulate warns comming from source file in this case.")

        if request.node.name == "test_showing_version[py_code]":
            pytest.xfail("It's too tricky to mock sys.stdout.")

        warn_mock = mocker.patch.object(prophyc.Emit, "warn")

        def simulate_Emit_warn(msg):
            return 'prophyc: warning: ' + str(msg) + '\n'

        def call_from_py_code(call_args):

            ret_code = 0
            std_err = ""

            try:
                prophyc.main(call_args)
            except SystemExit as err:
                ret_code = err.code
                if isinstance(ret_code, prophyc.six.string_types):
                    std_err = ret_code + '\n'
                    ret_code = 1

            std_err = ''.join(simulate_Emit_warn(line[0][0]) for line in warn_mock.call_args_list) + std_err
            return ret_code, "", std_err

        return call_from_py_code


def test_showing_version(call):
    ret, out, err = call(["--version"])
    expected_version = '1.0.2'
    assert ret == 0
    assert out == 'prophyc %s\n' % expected_version
    assert err == ""


def test_missing_input(call):
    ret, out, err = call([])
    assert ret == 1
    assert out == ""
    assert err == "prophyc: error: missing input file\n"


def test_no_output_directory(call, tmpdir_cwd):
    tmpdir_cwd.join("input.xml").write("")
    ret, out, err = call(["--python_out", "no_dir",
                          os.path.join(str(tmpdir_cwd), "input_xml")])
    assert ret == 1
    assert out == ""
    assert err == "prophyc: error: argument --python_out: no_dir directory not found\n"


def test_missing_output(call, tmpdir_cwd):
    tmpdir_cwd.join("input.xml").write('')
    ret, out, err = call(["--isar", os.path.join(str(tmpdir_cwd), "input.xml")])
    assert ret == 1
    assert out == ""
    assert err == "prophyc: error: missing output directives\n"


def test_passing_isar_and_sack(call, tmpdir_cwd):
    tmpdir_cwd.join("input").write('')
    ret, out, err = call(["--isar", "--sack", "--python_out", ".",
                          os.path.join(str(tmpdir_cwd), "input")])
    assert ret == 1
    assert out == ""
    assert err == "prophyc: error: argument --sack: not allowed with argument --isar\n"


def test_isar_compiles_single_empty_xml(call, tmpdir_cwd):
    tmpdir_cwd.join("input.xml").write("<struct/>")
    ret, out, err = call(["--isar", "--python_out", str(tmpdir_cwd),
                          os.path.join(str(tmpdir_cwd), "input.xml")])
    assert ret == 0
    assert out == ""
    assert err == ""
    with tmpdir_cwd.join("input.py") as f:
        contents = f.read()
    assert empty_python_output == contents

<commit_msg>Bringing back accidentally commited code<commit_after>import os
import sys
import subprocess
import pytest
import prophyc

main_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))


empty_python_output = """\
import prophy
"""


def call_as_subprocess(call_args):

    popen = subprocess.Popen([sys.executable, "-m", "prophyc"] + call_args,
                             cwd=main_dir,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    out, err = popen.communicate()
    return popen.returncode, out.decode(), err.decode()


@pytest.fixture(params=["subprocess", "py_code"])
def call(request, mocker):

    if request.param == "subprocess":
        return call_as_subprocess
    else:
        if request.node.name == "test_sack_parse_warnings[py_code]":
            pytest.xfail("Its not worth of effort to simulate warns comming from source file in this case.")

        if request.node.name == "test_showing_version[py_code]":
            pytest.xfail("It's too tricky to mock sys.stdout.")

        warn_mock = mocker.patch.object(prophyc.Emit, "warn")

        def simulate_Emit_warn(msg):
            return 'prophyc: warning: ' + str(msg) + '\n'

        def call_from_py_code(call_args):

            ret_code = 0
            std_err = ""

            try:
                prophyc.main(call_args)
            except SystemExit as err:
                ret_code = err.code
                if isinstance(ret_code, prophyc.six.string_types):
                    std_err = ret_code + '\n'
                    ret_code = 1

            std_err = ''.join(simulate_Emit_warn(line[0][0]) for line in warn_mock.call_args_list) + std_err
            return ret_code, "", std_err

        return call_from_py_code


def test_showing_version(call):
    ret, out, err = call(["--version"])
    expected_version = '1.0.2'
    assert ret == 0
    assert out == 'prophyc %s\n' % expected_version
    assert err == ""


def test_missing_input(call):
    ret, out, err = call([])
    assert ret == 1
    assert out == ""
    assert err == "prophyc: error: missing input file\n"


def test_no_output_directory(call, tmpdir_cwd):
    tmpdir_cwd.join("input.xml").write("")
    ret, out, err = call(["--python_out", "no_dir",
                          os.path.join(str(tmpdir_cwd), "input_xml")])
    assert ret == 1
    assert out == ""
    assert err == "prophyc: error: argument --python_out: no_dir directory not found\n"


def test_missing_output(call, tmpdir_cwd):
    tmpdir_cwd.join("input.xml").write('')
    ret, out, err = call(["--isar", os.path.join(str(tmpdir_cwd), "input.xml")])
    assert ret == 1
    assert out == ""
    assert err == "prophyc: error: missing output directives\n"


def test_passing_isar_and_sack(call, tmpdir_cwd):
    tmpdir_cwd.join("input").write('')
    ret, out, err = call(["--isar", "--sack", "--python_out", ".",
                          os.path.join(str(tmpdir_cwd), "input")])
    assert ret == 1
    assert out == ""
    assert err == "prophyc: error: argument --sack: not allowed with argument --isar\n"


def test_isar_compiles_single_empty_xml(call, tmpdir_cwd):
    tmpdir_cwd.join("input.xml").write("<struct/>")
    ret, out, err = call(["--isar", "--python_out", str(tmpdir_cwd),
                          os.path.join(str(tmpdir_cwd), "input.xml")])
    assert ret == 0
    assert out == ""
    assert err == ""
    assert empty_python_output == tmpdir_cwd.join("input.py").read()


{'fraction_non_alphanumeric': 0.1382344592676696, 'fraction_numerical': 0.0036900369003690036, 'mean_word_length': 2.5235, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '5603655', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2302, 'n_words': 504}