From 1adc42b24efd1ff1bfeb2d98b3b5fba3b56c42c8 Mon Sep 17 00:00:00 2001 From: David Gustavsson Date: Fri, 12 Jul 2024 18:05:11 +0200 Subject: [PATCH 1/2] Make DaemonMode aware of which script file is currently running --- .gitignore | 1 + src/DaemonMode.jl | 12 +++++------- test/fileandline.jl | 7 +++++++ test/runtests.jl | 11 +++++++++-- 4 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 test/fileandline.jl diff --git a/.gitignore b/.gitignore index 145cb1a..c8fa77a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store /Manifest.toml /dev/ +/test/*.log diff --git a/src/DaemonMode.jl b/src/DaemonMode.jl index 69ee10e..b9b6189 100644 --- a/src/DaemonMode.jl +++ b/src/DaemonMode.jl @@ -252,7 +252,7 @@ function serverRun(run, sock, shared, print_stack, fname, args, reviser) try reviser() - + if shared redirect_stdout(sock) do redirect_stderr(sock) do @@ -278,8 +278,7 @@ function serverRun(run, sock, shared, print_stack, fname, args, reviser) Base.eval(m, add_params) add_exit = Meta.parse("struct SystemExit <: Exception code::Int32 end; exit(x)=throw(SystemExit(x))") Base.eval(m, add_exit) - # Following code is not needed, the real problem was global ARGS, not - add_redirect = Meta.parse("const stdout=IOBuffer(); println(io, x...) = Base.println(io,x...); println(x)=Base.println(stdout, x); println(x...)=Base.println(stdout, x...); println(io, x...)=Base.println(io, x...); print(x...)=Base.print(stdout, x...); stdout") + add_redirect = Meta.parse("const stdout=IOBuffer(); stdout") out = Base.eval(m, add_redirect) add_redirect_err = Meta.parse("const stderr=IOBuffer(); stderr") err = Base.eval(m, add_redirect_err) @@ -379,7 +378,7 @@ Parse the argument string handling quoted arguments, and escaped quotes correctl # Parameters - shared: string of arguments separated by one or many spaces -""" +""" function parse_arguments(args_str::String) args_out = [] quotes = Set(['\'','"']) @@ -420,7 +419,7 @@ Run the source code of the filename push through the socket. - sock: socket in which is going to receive the dir, the filename, and args to run. - shared: Share the environment between calls. If it is false (default) each run has its own environment, so the variables/functions are not shared. -""" +""" function serverRunFile(sock, shared, print_stack, reviser) fname = "" @@ -444,9 +443,8 @@ function serverRunFile(sock, shared, print_stack, reviser) first_time[] = true cd(dir) do - content = read(fname, String) serverRun(sock, shared, print_stack, fname, args, reviser) do mod - include_string(mod, content) + Base.include(mod, fname) end end catch e diff --git a/test/fileandline.jl b/test/fileandline.jl new file mode 100644 index 0000000..d626798 --- /dev/null +++ b/test/fileandline.jl @@ -0,0 +1,7 @@ +println(@__FILE__) +#= + To truly test @__LINE__ + I add a couple comments + Seven is random +=# +println(@__LINE__) diff --git a/test/runtests.jl b/test/runtests.jl index a4a9466..b96b74e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -90,13 +90,13 @@ end output = String(take!(buffer)) @test output == "1\n2\n" - expr = "begin + expr = "begin x = 2 for i = 1:2 println(i) end end - + " runexpr(expr, output=buffer, port=port) @@ -177,3 +177,10 @@ end output = test_evalfile("eval.jl", port=3010) @test output == "3\n" end + +@testset "testCodeloc" begin + output = test_evalfile("fileandline.jl", port=3011) + l = split(output) + @test endswith(l[1], "/test/fileandline.jl") + @test l[2] == "7" +end From e3d3496d0c4ff1c63340e159b177bb10b60e70df Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Sat, 13 Jul 2024 13:31:40 +0200 Subject: [PATCH 2/2] Update runtests.jl Update to fix the error with Windows systems. --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index b96b74e..8d07bc5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -181,6 +181,6 @@ end @testset "testCodeloc" begin output = test_evalfile("fileandline.jl", port=3011) l = split(output) - @test endswith(l[1], "/test/fileandline.jl") + @test endswith(l[1], joinpath("test", "fileandline.jl")) @test l[2] == "7" end