Run artifact

build/jq/tests/test_input_diagnostics.py

import io

from jq_interpreter.command import main


def run(program, data, monkeypatch=None):
    output, errors = io.StringIO(), io.StringIO()
    if monkeypatch is not None:
        monkeypatch.setenv("JQ_IO_TEST", "startup")
    status = main(["-c", program], io.StringIO(data), output, errors)
    return status, output.getvalue().splitlines(), errors.getvalue()


def test_input_and_inputs_share_the_remaining_record_stream():
    status, output, errors = run("., input, inputs", "1\n2\n3\n")
    assert (status, output, errors) == (0, ["1", "2", "3"], "")


def test_input_exhaustion_is_catchable():
    status, output, errors = run("try input catch .", "1\n")
    assert (status, output, errors) == (0, ['"break"'], "")


def test_environment_binding_and_filter_are_available(monkeypatch):
    status, output, errors = run("[$ENV.JQ_IO_TEST, env.JQ_IO_TEST]", "null\n", monkeypatch)
    assert status == 0
    assert output == ['["startup","startup"]']
    assert errors == ""


def test_location_reports_source_line():
    status, output, errors = run("1,\n$__loc__", "null\n")
    assert (status, output, errors) == (0, ["1", '{"file":"<top-level>","line":2}'], "")


def test_debug_and_stderr_preserve_values_and_use_stderr():
    status, output, errors = run("debug, stderr", '{"a":1}\n')
    assert status == 0
    assert output == ['{"a":1}', '{"a":1}']
    assert errors == '["DEBUG:",{"a":1}]\n{"a":1}'