From 9e0be07fa5cb3cf2e0b6d5e72659c0c40492c1f3 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Fri, 26 May 2017 18:18:30 -0500 Subject: [PATCH 1/7] Assign PROGRAM_FILE earlier I discovered a use case where it would be nice to be able to access `PROGRAM_FILE` while within the .juliarc.jl. The global variable is now assigned and available within `--load` file or the .juliarc.jl. Additionally, PROGRAM_FILE is now a constant. --- base/client.jl | 9 ++++++--- base/initdefs.jl | 2 +- test/cmdlineargs.jl | 5 +++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/base/client.jl b/base/client.jl index 2f22a24ca6e7e..c2e5e4de82cd7 100644 --- a/base/client.jl +++ b/base/client.jl @@ -257,6 +257,11 @@ function process_options(opts::JLOptions) color_set = (opts.color != 0) global have_color = (opts.color == 1) global is_interactive = (opts.isinteractive != 0) + + # remove filename from ARGS + arg_is_program = opts.eval == C_NULL && opts.print == C_NULL && !isempty(ARGS) + global const PROGRAM_FILE = arg_is_program ? shift!(ARGS) : "" + while true # startup worker. # opts.startupfile, opts.load, etc should should not be processed for workers. @@ -296,11 +301,9 @@ function process_options(opts::JLOptions) break end # load file - if !isempty(ARGS) && !isempty(ARGS[1]) + if !isempty(PROGRAM_FILE) # program repl = false - # remove filename from ARGS - global PROGRAM_FILE = shift!(ARGS) if !is_interactive ccall(:jl_exit_on_sigint, Void, (Cint,), 1) end diff --git a/base/initdefs.jl b/base/initdefs.jl index 20437a4e57cba..4535a718a5fd7 100644 --- a/base/initdefs.jl +++ b/base/initdefs.jl @@ -9,7 +9,7 @@ A string containing the script name passed to Julia from the command line. Note script name remains unchanged from within included files. Alternatively see [`@__FILE__`](@ref). """ -PROGRAM_FILE = "" +:PROGRAM_FILE """ ARGS diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index f5ca5f9698dd0..3d7ad5bb41d1f 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -264,7 +264,7 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` @test readchomp(`$exename -L $testfile -e 'exit(0)' -- foo -bar --baz`) == "String[\"foo\", \"-bar\", \"--baz\"]" @test split(readchomp(`$exename -L $testfile $testfile`), '\n') == - ["String[\"$(escape(testfile))\"]", "String[]"] + ["String[]", "String[]"] @test !success(`$exename --foo $testfile`) @test readchomp(`$exename -L $testfile -e 'exit(0)' -- foo -bar -- baz`) == "String[\"foo\", \"-bar\", \"--\", \"baz\"]" finally @@ -273,6 +273,7 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` end # test the script name + # TODO: Add tests to make sure PROGRAM_FILE is available within the `.juliarc.jl` let a = tempname(), b = tempname() try write(a, """ @@ -291,7 +292,7 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` @test split(readchomp(`$exename -L $b -e 'exit(0)'`), '\n') == ["$(realpath(b))", "", "0"] @test split(readchomp(`$exename -L $b $a`), '\n') == - ["$(realpath(b))", "", "1", "$a", "$a", "0", "$b", "$a", "0"] + ["$(realpath(b))", "$a", "0", "$a", "$a", "0", "$b", "$a", "0"] finally rm(a) rm(b) From 3b4428614135e3ccb3f7b7497d4334d497fce8bd Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Mon, 29 May 2017 09:52:55 -0500 Subject: [PATCH 2/7] Add to NEWS [ci skip] --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 787a916a6c6d8..f0673fc225b3c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -40,6 +40,10 @@ This section lists changes that do not have deprecation warnings. of the socket. Previously the address of the remote endpoint was being returned ([#21825]). + * Using `ARGS` within the ~/.juliarc.jl or within a .jl file loaded with `--load` will no + longer contain the script name as the first argument. Instead the script name will be + assigned to `PROGRAM_FILE`. ([#22092]) + Library improvements -------------------- From ebf6b03dd24cce1875a43fdc68723d68dd5c8214 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Mon, 5 Jun 2017 10:32:30 -0500 Subject: [PATCH 3/7] Test startup-file and PROGRAM_FILE --- test/cmdlineargs.jl | 67 ++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index 3d7ad5bb41d1f..2fb6c59e1d2b4 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -256,7 +256,8 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` # write a julia source file that just prints ARGS to STDOUT write(testfile, """ println(ARGS) - """) + """ + ) @test readchomp(`$exename $testfile foo -bar --baz`) == "String[\"foo\", \"-bar\", \"--baz\"]" @test readchomp(`$exename $testfile -- foo -bar --baz`) == @@ -272,30 +273,46 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` end end - # test the script name - # TODO: Add tests to make sure PROGRAM_FILE is available within the `.juliarc.jl` - let a = tempname(), b = tempname() - try - write(a, """ - println(@__FILE__) - println(PROGRAM_FILE) - println(length(ARGS)) - include(\"$(escape(b))\") - """) - write(b, """ - println(@__FILE__) - println(PROGRAM_FILE) - println(length(ARGS)) - """) - @test split(readchomp(`$exename $a`), '\n') == - ["$a", "$a", "0", "$b", "$a", "0"] - @test split(readchomp(`$exename -L $b -e 'exit(0)'`), '\n') == - ["$(realpath(b))", "", "0"] - @test split(readchomp(`$exename -L $b $a`), '\n') == - ["$(realpath(b))", "$a", "0", "$a", "$a", "0", "$b", "$a", "0"] - finally - rm(a) - rm(b) + # test the program name remains constant + mktempdir() do dir + a = joinpath(dir, "a.jl") + b = joinpath(dir, "b.jl") + c = joinpath(dir, ".juliarc.jl") + + write(a, """ + println(@__FILE__) + println(PROGRAM_FILE) + include(\"$(escape(b))\") + """ + ) + write(b, """ + println(@__FILE__) + println(PROGRAM_FILE) + """ + ) + cp(b, c) + + @test split(readchomp(`$exename $a`), '\n') == + [a, a, + b, a] + @test split(readchomp(`$exename -L $b -e 'exit(0)'`), '\n') == + [realpath(b), ""] + @test split(readchomp(`$exename -L $b $a`), '\n') == + [realpath(b), a, + a, a, + b, a] + + withenv("HOME" => dir) do + @test split(readchomp(`$exename --startup-file=yes -e 'exit(0)'`), '\n') == + [c, ""] + @test split(readchomp(`$exename --startup-file=yes -L $b -e 'exit(0)'`), '\n') == + [c, "", + realpath(b), ""] + @test split(readchomp(`$exename --startup-file=yes -L $b $a`), '\n') == + [c, a, + realpath(b), a, + a, a, + b, a] end end From 32c7e79dc8bed616194011f803c11d5715ce9d72 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 6 Jun 2017 09:00:12 -0500 Subject: [PATCH 4/7] Modify user home properly on Windows --- test/cmdlineargs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index 2fb6c59e1d2b4..dcc410d0ac431 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -302,7 +302,7 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` a, a, b, a] - withenv("HOME" => dir) do + withenv((is_windows() ? "USERPROFILE" : "HOME") => dir) do @test split(readchomp(`$exename --startup-file=yes -e 'exit(0)'`), '\n') == [c, ""] @test split(readchomp(`$exename --startup-file=yes -L $b -e 'exit(0)'`), '\n') == From c5a1506d0968fc768e0d7810ac62aae0655a4142 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 7 Jun 2017 16:46:32 -0500 Subject: [PATCH 5/7] Style correction and remove escape function --- test/cmdlineargs.jl | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index dcc410d0ac431..7e7b40b14cdb1 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -248,16 +248,13 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` # tested in test/parallel.jl, test/examples.jl) @test !success(`$exename --worker=true`) - escape(str) = replace(str, "\\", "\\\\") - # test passing arguments let testfile = tempname() try # write a julia source file that just prints ARGS to STDOUT write(testfile, """ println(ARGS) - """ - ) + """) @test readchomp(`$exename $testfile foo -bar --baz`) == "String[\"foo\", \"-bar\", \"--baz\"]" @test readchomp(`$exename $testfile -- foo -bar --baz`) == @@ -282,14 +279,12 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` write(a, """ println(@__FILE__) println(PROGRAM_FILE) - include(\"$(escape(b))\") - """ - ) + include(\"$(escape_string(b))\") + """) write(b, """ println(@__FILE__) println(PROGRAM_FILE) - """ - ) + """) cp(b, c) @test split(readchomp(`$exename $a`), '\n') == From 8d4d7ac03f61b49774efa4d4b5916efd5cd4e399 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Mon, 12 Jun 2017 13:50:55 -0500 Subject: [PATCH 6/7] Expand arguments tests to include the startup-file --- test/cmdlineargs.jl | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index 7e7b40b14cdb1..960e3363c3086 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -249,24 +249,30 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` @test !success(`$exename --worker=true`) # test passing arguments - let testfile = tempname() - try - # write a julia source file that just prints ARGS to STDOUT - write(testfile, """ - println(ARGS) - """) - @test readchomp(`$exename $testfile foo -bar --baz`) == - "String[\"foo\", \"-bar\", \"--baz\"]" - @test readchomp(`$exename $testfile -- foo -bar --baz`) == - "String[\"foo\", \"-bar\", \"--baz\"]" + mktempdir() do dir + testfile = joinpath(dir, tempname()) + # write a julia source file that just prints ARGS to STDOUT + write(testfile, """ + println(ARGS) + """) + cp(testfile, joinpath(dir, ".juliarc.jl")) + + withenv((is_windows() ? "USERPROFILE" : "HOME") => dir) do + output = "String[\"foo\", \"-bar\", \"--baz\"]" + @test readchomp(`$exename $testfile foo -bar --baz`) == output + @test readchomp(`$exename $testfile -- foo -bar --baz`) == output @test readchomp(`$exename -L $testfile -e 'exit(0)' -- foo -bar --baz`) == - "String[\"foo\", \"-bar\", \"--baz\"]" - @test split(readchomp(`$exename -L $testfile $testfile`), '\n') == - ["String[]", "String[]"] + output + @test readchomp(`$exename --startup-file=yes -e 'exit(0)' -- foo -bar --baz`) == + output + + output = "String[]\nString[]" + @test readchomp(`$exename -L $testfile $testfile`) == output + @test readchomp(`$exename --startup-file=yes $testfile`) == output + @test !success(`$exename --foo $testfile`) - @test readchomp(`$exename -L $testfile -e 'exit(0)' -- foo -bar -- baz`) == "String[\"foo\", \"-bar\", \"--\", \"baz\"]" - finally - rm(testfile) + @test readchomp(`$exename -L $testfile -e 'exit(0)' -- foo -bar -- baz`) == + "String[\"foo\", \"-bar\", \"--\", \"baz\"]" end end From 7db08b506b1f280952b058484a9ee06dceacaf1d Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Mon, 12 Jun 2017 13:51:37 -0500 Subject: [PATCH 7/7] Refactor PROGRAM_FILE tests --- test/cmdlineargs.jl | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index 960e3363c3086..3bcdd0a7d2c9e 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -293,23 +293,24 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` """) cp(b, c) - @test split(readchomp(`$exename $a`), '\n') == - [a, a, - b, a] - @test split(readchomp(`$exename -L $b -e 'exit(0)'`), '\n') == - [realpath(b), ""] - @test split(readchomp(`$exename -L $b $a`), '\n') == - [realpath(b), a, - a, a, - b, a] + readsplit(cmd) = split(readchomp(cmd), '\n') withenv((is_windows() ? "USERPROFILE" : "HOME") => dir) do - @test split(readchomp(`$exename --startup-file=yes -e 'exit(0)'`), '\n') == + @test readsplit(`$exename $a`) == + [a, a, + b, a] + @test readsplit(`$exename -L $b -e 'exit(0)'`) == + [realpath(b), ""] + @test readsplit(`$exename -L $b $a`) == + [realpath(b), a, + a, a, + b, a] + @test readsplit(`$exename --startup-file=yes -e 'exit(0)'`) == [c, ""] - @test split(readchomp(`$exename --startup-file=yes -L $b -e 'exit(0)'`), '\n') == + @test readsplit(`$exename --startup-file=yes -L $b -e 'exit(0)'`) == [c, "", realpath(b), ""] - @test split(readchomp(`$exename --startup-file=yes -L $b $a`), '\n') == + @test readsplit(`$exename --startup-file=yes -L $b $a`) == [c, a, realpath(b), a, a, a,