# Help Testing Repl Modes

**URL:** <https://discourse.julialang.org/t/help-testing-repl-modes/13773>\
**Category:** General Usage\
**Created:** [August 20, 2018, 4:41pm UTC](https://discourse.julialang.org/t/help-testing-repl-modes/13773 "2018-08-20T16:41:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [August 20, 2018, 4:41pm UTC](https://discourse.julialang.org/t/help-testing-repl-modes/13773/1 "2018-08-20T16:41:13Z")

</div>

I’m developing a [package for letting others define their own REPL modes](https://github.com/MasonProtter/ReplMaker.jl) but before I can put it on METADATA I need to develop tests for it. Unfortunately, in order to actually perform tests on it, I need a running REPL which is proving to be quite a hassle.

The main issue is that my tests rely on inspecting `Base.active_repl` and making sure that ReplMaker is properly modifying the active repl, but in a standard `pkg> test` script there is no REPL defined. Is there any way to make the test script get run with a REPL defined and active? Or is there some Julia command I can run from inside the test script to spawn the REPL?

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [August 20, 2018, 7:50pm UTC](https://discourse.julialang.org/t/help-testing-repl-modes/13773/2 "2018-08-20T19:50:50Z")

</div>

I use VT100.jl to test my RPEL modes:

> <https://github.com/JuliaDebug/DebuggerFramework.jl/blob/master/test/runtests.jl#L46-L58>

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 20, 2018, 8:20pm UTC](https://discourse.julialang.org/t/help-testing-repl-modes/13773/3 "2018-08-20T20:20:02Z")

</div>

Another way is to spawn a julia process that we write commands into. If we want to test for example the `st` command of the Pkg repl mode we could do something like

```julia
Base.include(@ __MODULE__ , joinpath(Sys.BINDIR, "..", "share", "julia", "test", "testhelpers", "FakePTYs.jl"))
import .FakePTYs: open_fake_pty

slave, master = open_fake_pty()

CTRL_C = '\x03'

# Script that we want the REPL to execute, here simply st for Pkg REPLMode
test_script = """
]st
$CTRL_C"""

function run_test()
    slave, master = open_fake_pty()
    # Start a julia process
    p = run(`$(Base.julia_cmd()) --history-file=no --startup-file=no`, slave, slave, slave; wait=false)
    
    # Read until the prompt
    readuntil(master, "julia>", keep=true)
    done = false
    repl_output_buffer = IOBuffer()

    # A task that just keeps reading the output
    @async begin
        while true
            done && break
            write(repl_output_buffer, readavailable(master))
        end
    end

    # Execute our "script"
    for l in split(test_script, '\n'; keepempty=false)
        write(master, l, '\n')
    end

    # Let the REPL exit
    write(master, "exit()\n")
    wait(p)
    done = true

    # Gather the output
    repl_output = String(take!(repl_output_buffer))
    println(repl_output)
end

```

The `repl_output` could be tested for what you expect.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [August 20, 2018, 11:42pm UTC](https://discourse.julialang.org/t/help-testing-repl-modes/13773/4 "2018-08-20T23:42:42Z")

</div>

Thanks so much for the thorough example and help! I really appreciate it. This was helpful.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 21, 2018, 12:02pm UTC](https://discourse.julialang.org/t/help-testing-repl-modes/13773/5 "2018-08-21T12:02:05Z")

</div>

The tests for the Julia REPL are also likely useful to look at [julia/repl.jl at master · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/master/stdlib/REPL/test/repl.jl#L93)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [August 21, 2018, 4:34pm UTC](https://discourse.julialang.org/t/help-testing-repl-modes/13773/6 "2018-08-21T16:34:45Z")

</div>

Yeah, my first efforts were directed at that but the fake repl implemented there was missing some important features for my purposes such as `repl.interface` and I was a bit over my head in understanding most of what the test set up was doing anyway.
