Piping directly to STDIN


#1

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.


#2

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


#3

That didn’t work. :’(

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

#4

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


#5

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)


#6

Bam. Solved.

Thanks man!