Piping directly to STDIN

I’m writing an integration test and want to know the best way to pipe to STDIN.

The closest I’ve gotten is print(STDIN, "blah"), but this still required me to press enter.

What’s the best way to accomplish this?

I know redirect_stdin() exists, but the docs and googlable posts don’t really say much.

Not necessarily the best solution, but did you try adding \n to the print statement?

That didn’t work. :cry:

The problem is I have to type enter before anything gets added to the buffer


edit: for reference, here’s the thing that’s waiting for input

function user_input(prompt)
  print(prompt)
  answer = readline()
  chomp(answer)
end

Just guessing here, but maybe this could help:
http://docs.julialang.org/en/stable/stdlib/io-network/#Base.flush

Is this something like what you want?

let filename = tempname()
    open(filename,"w") do f
        println(f,"my answer!")
    end
    ret = open(filename) do f
        redirect_stdin(f) do
            user_input("what do you say?")
        end
    end
   
    println("\nresult was: ",ret) # for testing the test

    rm(filename)
    contains(ret, "my answer!") || error("test failed!")
end

(cribbed from the Julia test suite)

1 Like

Bam. Solved.

Thanks man!