|
| 1 | +using Test |
| 2 | +import IJulia |
| 3 | + |
| 4 | +@testset "@main app entry point" begin |
| 5 | + # Test help output |
| 6 | + @testset "help" begin |
| 7 | + # Capture stdout using a pipe |
| 8 | + old_stdout = stdout |
| 9 | + rd, wr = redirect_stdout() |
| 10 | + |
| 11 | + ret = IJulia.main(["--help"]) |
| 12 | + |
| 13 | + # Restore stdout and read the captured output |
| 14 | + redirect_stdout(old_stdout) |
| 15 | + close(wr) |
| 16 | + help_text = read(rd, String) |
| 17 | + close(rd) |
| 18 | + |
| 19 | + @test ret == 0 |
| 20 | + @test occursin("IJulia Launcher", help_text) |
| 21 | + @test occursin("notebook", help_text) |
| 22 | + @test occursin("lab", help_text) |
| 23 | + @test occursin("--dir=PATH", help_text) |
| 24 | + @test occursin("--port=N", help_text) |
| 25 | + @test occursin("--detached", help_text) |
| 26 | + @test occursin("--verbose", help_text) |
| 27 | + end |
| 28 | + |
| 29 | + # Test invalid subcommand |
| 30 | + @testset "invalid subcommand" begin |
| 31 | + ret = @test_logs (:error, r"Unknown subcommand.*Use 'notebook' or 'lab'") IJulia.main(["invalid"]) |
| 32 | + @test ret == 1 |
| 33 | + end |
| 34 | + |
| 35 | + # Test argument parsing without actually launching |
| 36 | + # We use mutable function references (notebook_cmd/jupyterlab_cmd) to test parsing |
| 37 | + @testset "argument parsing" begin |
| 38 | + # Save original functions |
| 39 | + orig_notebook = IJulia.notebook_cmd |
| 40 | + orig_jupyterlab = IJulia.jupyterlab_cmd |
| 41 | + |
| 42 | + # Test notebook (default) |
| 43 | + called_with = nothing |
| 44 | + IJulia.notebook_cmd = function(args=``; dir=homedir(), detached=false, port=nothing, verbose=false) |
| 45 | + called_with = (; args, dir, detached, port, verbose) |
| 46 | + return nothing |
| 47 | + end |
| 48 | + |
| 49 | + try |
| 50 | + ret = IJulia.main(["--dir=/test", "--port=9999", "--detached", "--verbose", "--no-browser"]) |
| 51 | + @test ret == 0 |
| 52 | + @test !isnothing(called_with) |
| 53 | + @test called_with.dir == "/test" |
| 54 | + @test called_with.port == 9999 |
| 55 | + @test called_with.detached == true |
| 56 | + @test called_with.verbose == true |
| 57 | + @test "--no-browser" in called_with.args |
| 58 | + finally |
| 59 | + IJulia.notebook_cmd = orig_notebook |
| 60 | + end |
| 61 | + |
| 62 | + # Test explicit notebook subcommand |
| 63 | + called_with = nothing |
| 64 | + IJulia.notebook_cmd = function(args=``; dir=homedir(), detached=false, port=nothing, verbose=false) |
| 65 | + called_with = (; args, dir, detached, port, verbose) |
| 66 | + return nothing |
| 67 | + end |
| 68 | + |
| 69 | + try |
| 70 | + ret = IJulia.main(["notebook", "--port=8888"]) |
| 71 | + @test ret == 0 |
| 72 | + @test !isnothing(called_with) |
| 73 | + @test called_with.port == 8888 |
| 74 | + @test called_with.dir == homedir() |
| 75 | + @test called_with.detached == false |
| 76 | + finally |
| 77 | + IJulia.notebook_cmd = orig_notebook |
| 78 | + end |
| 79 | + |
| 80 | + # Test lab subcommand |
| 81 | + called_with = nothing |
| 82 | + IJulia.jupyterlab_cmd = function(args=``; dir=homedir(), detached=false, port=nothing, verbose=false) |
| 83 | + called_with = (; args, dir, detached, port, verbose) |
| 84 | + return nothing |
| 85 | + end |
| 86 | + |
| 87 | + try |
| 88 | + ret = IJulia.main(["lab", "--dir=/lab", "--verbose"]) |
| 89 | + @test ret == 0 |
| 90 | + @test !isnothing(called_with) |
| 91 | + @test called_with.dir == "/lab" |
| 92 | + @test called_with.verbose == true |
| 93 | + finally |
| 94 | + IJulia.jupyterlab_cmd = orig_jupyterlab |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + # Test InterruptException handling |
| 99 | + @testset "interrupt handling" begin |
| 100 | + orig_notebook = IJulia.notebook_cmd |
| 101 | + IJulia.notebook_cmd = function(args=``; kwargs...) |
| 102 | + throw(InterruptException()) |
| 103 | + end |
| 104 | + |
| 105 | + try |
| 106 | + ret = IJulia.main([]) |
| 107 | + @test ret == 0 # InterruptException should return 0 |
| 108 | + finally |
| 109 | + IJulia.notebook_cmd = orig_notebook |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + # Test error handling |
| 114 | + @testset "error handling" begin |
| 115 | + orig_notebook = IJulia.notebook_cmd |
| 116 | + IJulia.notebook_cmd = function(args=``; kwargs...) |
| 117 | + error("Test error") |
| 118 | + end |
| 119 | + |
| 120 | + try |
| 121 | + ret = @test_logs (:error, r"Failed to launch") IJulia.main([]) |
| 122 | + @test ret == 1 # Errors should return 1 |
| 123 | + finally |
| 124 | + IJulia.notebook_cmd = orig_notebook |
| 125 | + end |
| 126 | + end |
| 127 | +end |
0 commit comments