[lang-ref] ( accept_pipe_input_and_process_lines_one_by_one ) ( python )

def test_accept_pipe_input_and_process_lines_one_by_one(monkeypatch):
    # for line in sys.stdin: ..
    # This sample shows how to handle `cat a.txt | ./a.py`
    import sys, io

    text = 'aaaa\nbbbb\ncccc\n'

    monkeypatch.setattr(sys, 'stdin', io.StringIO(text))

    r = ''
    for line in sys.stdin:
        r += line.rstrip('\n') + ','

    assert r == 'aaaa,bbbb,cccc,'