-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
cmdlineargs.jl
1155 lines (1041 loc) · 46.8 KB
/
cmdlineargs.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
import Libdl
# helper function for passing input to stdin
# and returning the stdout result
function writereadpipeline(input, exename; stderr=nothing)
p = open(pipeline(exename; stderr), "w+")
@async begin
write(p.in, input)
close(p.in)
end
return (read(p.out, String), success(p))
end
# helper function for returning stderr and stdout
# from running a command (ignoring failure status)
function readchomperrors(exename::Cmd)
out = Base.PipeEndpoint()
err = Base.PipeEndpoint()
p = run(exename, devnull, out, err, wait=false)
o = @async(readchomp(out))
e = @async(readchomp(err))
return (success(p), fetch(o), fetch(e))
end
function format_filename(s)
p = ccall(:jl_format_filename, Cstring, (Cstring,), s)
r = unsafe_string(p)
ccall(:free, Cvoid, (Cstring,), p)
return r
end
# Returns true if the given command errors, but doesn't signal
function errors_not_signals(cmd::Cmd)
p = run(pipeline(ignorestatus(cmd); stdout=devnull, stderr=devnull))
return errors_not_signals(p)
end
function errors_not_signals(p::Base.Process)
wait(p)
return process_exited(p) && !Base.process_signaled(p) && !success(p)
end
let
fn = format_filename("a%d %p %i %L %l %u z")
hd = withenv("HOME" => nothing) do
# get the homedir, as reported by uv_os_get_passwd, as used by jl_format_filename
try
homedir()
catch ex
(ex isa Base.IOError && ex.code == Base.UV_ENOENT) || rethrow(ex)
""
end
end
@test startswith(fn, "a$hd ")
@test endswith(fn, " z")
@test !occursin('%', fn)
@test occursin(" $(getpid()) ", fn)
@test occursin(" $(Libc.gethostname()) ", fn)
@test format_filename("%a%%b") == "a%b"
end
@testset "julia_cmd" begin
julia_basic = Base.julia_cmd()
function get_julia_cmd(arg)
io = Base.BufferStream()
cmd = `$julia_basic $arg -e 'print(repr(Base.julia_cmd()))'`
try
run(pipeline(cmd, stdout=io, stderr=io))
catch
@error "cmd failed" cmd read(io, String)
rethrow()
end
closewrite(io)
return read(io, String)
end
opts = Base.JLOptions()
for (arg, default) in (
# Use a Cmd to handle space nicely when
# interpolating inside another Cmd.
(`-C $(unsafe_string(opts.cpu_target))`, false),
("-J$(unsafe_string(opts.image_file))", false),
("--depwarn=yes", false),
("--depwarn=error", false),
("--depwarn=no", true),
("--check-bounds=yes", false),
("--check-bounds=no", false),
("--check-bounds=auto", true),
("--inline=no", false),
("--inline=yes", true),
("-O0", false),
("-O1", false),
("-O2", true),
("-O3", false),
("--min-optlevel=0", true),
("--min-optlevel=1", false),
("--min-optlevel=2", false),
("--min-optlevel=3", false),
("-g0", false),
("-g1", false),
("-g2", false),
("--compile=no", false),
("--compile=all", false),
("--compile=min", false),
("--compile=yes", true),
("--code-coverage=@", false),
("--code-coverage=user", false),
("--code-coverage=all", false),
("--code-coverage=none", true),
("--track-allocation=@", false),
("--track-allocation=user", false),
("--track-allocation=all", false),
("--track-allocation=none", true),
("--color=yes", false),
("--color=no", false),
("--startup-file=no", false),
("--startup-file=yes", true),
# ("--sysimage-native-code=no", false), # takes a lot longer (30s)
("--sysimage-native-code=yes", true),
("--pkgimages=yes", true),
("--pkgimages=no", false),
)
@testset "$arg" begin
str = arg isa Cmd ? join(arg.exec, ' ') : arg
if default
@test !occursin(str, get_julia_cmd(arg))
else
@test occursin(str, get_julia_cmd(arg))
end
end
end
# Test empty `cpu_target` gives a helpful error message, issue #52209.
io = IOBuffer()
p = run(pipeline(`$(Base.julia_cmd(; cpu_target="")) --startup-file=no -e ''`; stderr=io); wait=false)
wait(p)
@test p.exitcode == 1
@test occursin("empty CPU name", String(take!(io)))
end
let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
# tests for handling of ENV errors
let
io = IOBuffer()
v = writereadpipeline(
"println(\"REPL: \", @which(less), @isdefined(InteractiveUtils))",
setenv(`$exename -i -E '@assert isempty(LOAD_PATH); push!(LOAD_PATH, "@stdlib"); @isdefined InteractiveUtils'`,
"JULIA_LOAD_PATH" => "",
"JULIA_DEPOT_PATH" => ";:",
"HOME" => homedir());
stderr=io)
# @which is undefined
@test_broken v == ("false\nREPL: InteractiveUtilstrue\n", true)
stderr = String(take!(io))
@test_broken isempty(stderr)
end
let v = writereadpipeline("println(\"REPL: \", InteractiveUtils)",
setenv(`$exename -i -e 'const InteractiveUtils = 3'`,
"JULIA_LOAD_PATH" => ";;;:::",
"JULIA_DEPOT_PATH" => ";;;:::",
"HOME" => homedir()))
# TODO: ideally, `@which`, etc. would still work, but Julia can't handle `using $InteractiveUtils`
@test v == ("REPL: 3\n", true)
end
@testset let v = readchomperrors(`$exename -i -e '
empty!(LOAD_PATH)
@eval Sys STDLIB=mktempdir()
Base.unreference_module(Base.PkgId(Base.UUID(0xb77e0a4c_d291_57a0_90e8_8db25a27a240), "InteractiveUtils"))
'`)
# simulate not having a working version of InteractiveUtils,
# make sure this is a non-fatal error and the REPL still loads
@test v[1]
@test isempty(v[2])
# Can't load REPL if it's outside the sysimg if we break the load path.
# Need to rewrite this test nicer
# ┌ Warning: REPL provider not available: using basic fallback
# └ @ Base client.jl:459
@test_broken startswith(v[3], "┌ Warning: Failed to import InteractiveUtils into module Main\n")
end
real_threads = string(ccall(:jl_cpu_threads, Int32, ()))
for nc in ("0", "-2", "x", "2x", " ", "")
v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc, "HOME" => homedir()))
@test v == (true, real_threads,
"WARNING: couldn't parse `JULIA_CPU_THREADS` environment variable. Defaulting Sys.CPU_THREADS to $real_threads.")
end
for nc in ("1", " 1 ", " +1 ", " 0x1 ")
@testset let v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc, "HOME" => homedir()))
@test v[1]
@test v[2] == "1"
@test isempty(v[3])
end
end
@testset let v = readchomperrors(setenv(`$exename -e 0`, "JULIA_LLVM_ARGS" => "-print-options", "HOME" => homedir()))
@test v[1]
@test contains(v[2], r"print-options + = 1")
@test contains(v[2], r"combiner-store-merge-dependence-limit + = 4")
@test contains(v[2], r"enable-tail-merge + = 2")
@test isempty(v[3])
end
@testset let v = readchomperrors(setenv(`$exename -e 0`, "JULIA_LLVM_ARGS" => "-print-options -enable-tail-merge=1 -combiner-store-merge-dependence-limit=6", "HOME" => homedir()))
@test v[1]
@test contains(v[2], r"print-options + = 1")
@test contains(v[2], r"combiner-store-merge-dependence-limit + = 6")
@test contains(v[2], r"enable-tail-merge + = 1")
@test isempty(v[3])
end
if Base.libllvm_version < v"15" #LLVM over 15 doesn't care for multiple options
@testset let v = readchomperrors(setenv(`$exename -e 0`, "JULIA_LLVM_ARGS" => "-print-options -enable-tail-merge=1 -enable-tail-merge=1", "HOME" => homedir()))
@test !v[1]
@test isempty(v[2])
@test v[3] == "julia: for the --enable-tail-merge option: may only occur zero or one times!"
end
end
end
let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
# --version
let v = split(read(`$exename -v`, String), "julia version ")[end]
@test Base.VERSION_STRING == chomp(v)
end
@test read(`$exename -v`, String) == read(`$exename --version`, String)
# --help
let header = "\n julia [switches] -- [programfile] [args...]"
@test startswith(read(`$exename -h`, String), header)
@test startswith(read(`$exename --help`, String), header)
end
# ~ expansion in --project and JULIA_PROJECT
if !Sys.iswindows()
let expanded = abspath(expanduser("~/foo/Project.toml"))
@test expanded == readchomp(`$exename --project='~/foo' -e 'println(Base.active_project())'`)
@test expanded == readchomp(setenv(`$exename -e 'println(Base.active_project())'`, "JULIA_PROJECT" => "~/foo", "HOME" => homedir()))
end
end
# handling of @projectname in --project and JULIA_PROJECT
let expanded = abspath(Base.load_path_expand("@foo"))
@test expanded == readchomp(`$exename --project='@foo' -e 'println(Base.active_project())'`)
@test expanded == readchomp(addenv(`$exename -e 'println(Base.active_project())'`, "JULIA_PROJECT" => "@foo", "HOME" => homedir()))
end
# --quiet, --banner
let p = "print((Base.JLOptions().quiet, Base.JLOptions().banner))"
@test read(`$exename -e $p`, String) == "(0, -1)"
@test read(`$exename -q -e $p`, String) == "(1, 0)"
@test read(`$exename --quiet -e $p`, String) == "(1, 0)"
@test read(`$exename --banner=no -e $p`, String) == "(0, 0)"
@test read(`$exename --banner=yes -e $p`, String) == "(0, 1)"
@test read(`$exename --banner=short -e $p`, String) == "(0, 2)"
@test read(`$exename -q --banner=no -e $p`, String) == "(1, 0)"
@test read(`$exename -q --banner=yes -e $p`, String) == "(1, 1)"
@test read(`$exename -q --banner=short -e $p`, String) == "(1, 2)"
@test read(`$exename --banner=no -q -e $p`, String) == "(1, 0)"
@test read(`$exename --banner=yes -q -e $p`, String) == "(1, 1)"
@test read(`$exename --banner=short -q -e $p`, String) == "(1, 2)"
end
# --home
@test success(`$exename -H $(Sys.BINDIR)`)
@test success(`$exename --home=$(Sys.BINDIR)`)
# --eval
@test success(`$exename -e "exit(0)"`)
@test errors_not_signals(`$exename -e "exit(1)"`)
@test success(`$exename --eval="exit(0)"`)
@test errors_not_signals(`$exename --eval="exit(1)"`)
@test errors_not_signals(`$exename -e`)
@test errors_not_signals(`$exename --eval`)
# --eval --interactive (replaced --post-boot)
@test success(`$exename -i -e "exit(0)"`)
@test errors_not_signals(`$exename -i -e "exit(1)"`)
# issue #34924
@test success(`$exename -e 'const LOAD_PATH=1'`)
# --print
@test read(`$exename -E "1+1"`, String) == "2\n"
@test read(`$exename --print="1+1"`, String) == "2\n"
@test errors_not_signals(`$exename -E`)
@test errors_not_signals(`$exename --print`)
# --load
let testfile = tempname()
try
write(testfile, "testvar = :test\nprintln(\"loaded\")\n")
@test read(`$exename -i --load=$testfile -e "println(testvar)"`, String) == "loaded\ntest\n"
@test read(`$exename -i -L $testfile -e "println(testvar)"`, String) == "loaded\ntest\n"
# multiple, combined
@test read(```$exename
-e 'push!(ARGS, "hi")'
-E "1+1"
-E "2+2"
-L $testfile
-E '3+3'
-L $testfile
-E 'pop!(ARGS)'
-e 'show(ARGS); println()'
9 10
```, String) == """
2
4
loaded
6
loaded
"hi"
["9", "10"]
"""
finally
rm(testfile)
end
end
# -L, --load requires an argument
@test errors_not_signals(`$exename -L`)
@test errors_not_signals(`$exename --load`)
# --cpu-target (requires LLVM enabled)
# Strictly test for failed error, not a segfault, since we had a false positive with just `success()` before.
@test errors_not_signals(`$exename -C invalidtarget`)
@test errors_not_signals(`$exename --cpu-target=invalidtarget`)
if Sys.iswindows()
# -t, --threads
code = "print(Threads.threadpoolsize())"
cpu_threads = ccall(:jl_effective_threads, Int32, ())
@test string(cpu_threads) ==
read(`$exename --threads auto -e $code`, String) ==
read(`$exename --threads=auto -e $code`, String) ==
read(`$exename -tauto -e $code`, String) ==
read(`$exename -t auto -e $code`, String)
for nt in (nothing, "1")
withenv("JULIA_NUM_THREADS" => nt) do
@test read(`$exename --threads=2 -e $code`, String) ==
read(`$exename -t 2 -e $code`, String) == "2"
end
end
# We want to test oversubscription, but on manycore machines, this can
# actually exhaust limited PID spaces
cpu_threads = max(2*cpu_threads, min(50, 10*cpu_threads))
if Sys.WORD_SIZE == 32
cpu_threads = min(cpu_threads, 50)
end
@test read(`$exename -t $cpu_threads -e $code`, String) == string(cpu_threads)
withenv("JULIA_NUM_THREADS" => string(cpu_threads)) do
@test read(`$exename -e $code`, String) == string(cpu_threads)
end
@test errors_not_signals(`$exename -t 0`)
@test errors_not_signals(`$exename -t -1`)
# Combining --threads and --procs: --threads does propagate
withenv("JULIA_NUM_THREADS" => nothing) do
code = "print(sum(remotecall_fetch(Threads.threadpoolsize, x) for x in procs()))"
@test read(`$exename -p2 -t2 -e $code`, String) == "6"
end
else
@test_skip "Command line tests with -t are flakey on non-Windows OS"
# Known issue: https://github.com/JuliaLang/julia/issues/49154
# These tests should be fixed and reenabled on all operating systems.
end
# Combining --threads and invalid -C should yield a decent error
@test errors_not_signals(`$exename -t 2 -C invalidtarget`)
# --procs
@test readchomp(`$exename -q -p 2 -e "println(nworkers())"`) == "2"
@test errors_not_signals(`$exename -p 0`)
let p = run(`$exename --procs=1.0`, wait=false)
wait(p)
@test p.exitcode == 1 && p.termsignal == 0
end
# --gcthreads
code = "print(Threads.ngcthreads())"
cpu_threads = ccall(:jl_effective_threads, Int32, ())
@test (cpu_threads == 1 ? "1" : string(div(cpu_threads, 2))) ==
read(`$exename --threads auto -e $code`, String) ==
read(`$exename --threads=auto -e $code`, String) ==
read(`$exename -tauto -e $code`, String) ==
read(`$exename -t auto -e $code`, String)
for nt in (nothing, "1")
withenv("JULIA_NUM_GC_THREADS" => nt) do
@test read(`$exename --gcthreads=2 -e $code`, String) == "2"
end
withenv("JULIA_NUM_GC_THREADS" => nt) do
@test read(`$exename --gcthreads=2,1 -e $code`, String) == "3"
end
end
withenv("JULIA_NUM_GC_THREADS" => 2) do
@test read(`$exename -e $code`, String) == "2"
end
withenv("JULIA_NUM_GC_THREADS" => "2,1") do
@test read(`$exename -e $code`, String) == "3"
end
# --machine-file
# this does not check that machine file works,
# only that the filename gets correctly passed to the option struct
let fname = tempname()
touch(fname)
fname = realpath(fname)
try
@test readchomp(`$exename --machine-file $fname -e
"println(unsafe_string(Base.JLOptions().machine_file))"`) == fname
finally
rm(fname)
end
end
# -i, isinteractive
@test readchomp(`$exename -E "isinteractive()"`) == "false"
@test readchomp(`$exename -E "isinteractive()" -i`) == "true"
# --color
@test readchomp(`$exename --color=yes -E "Base.have_color"`) == "true"
@test readchomp(`$exename --color=no -E "Base.have_color"`) == "false"
@test errors_not_signals(`$exename --color=false`)
# --history-file
@test readchomp(`$exename -E "Bool(Base.JLOptions().historyfile)"
--history-file=yes`) == "true"
@test readchomp(`$exename -E "Bool(Base.JLOptions().historyfile)"
--history-file=no`) == "false"
@test errors_not_signals(`$exename --history-file=false`)
# --code-coverage
mktempdir() do dir
helperdir = joinpath(@__DIR__, "testhelpers")
inputfile = joinpath(helperdir, "coverage_file.jl")
expected = replace(read(joinpath(helperdir, "coverage_file.info"), String),
"<FILENAME>" => realpath(inputfile))
covfile = replace(joinpath(dir, "coverage.info"), "%" => "%%")
@test !isfile(covfile)
defaultcov = readchomp(`$exename -E "Base.JLOptions().code_coverage != 0" -L $inputfile`)
opts = Base.JLOptions()
coverage_file = (opts.output_code_coverage != C_NULL) ? unsafe_string(opts.output_code_coverage) : ""
@test !isfile(covfile)
@test defaultcov == string(opts.code_coverage != 0 && (isempty(coverage_file) || occursin("%p", coverage_file)))
@test readchomp(`$exename -E "Base.JLOptions().code_coverage" -L $inputfile
--code-coverage=$covfile --code-coverage=none`) == "0"
@test !isfile(covfile)
@test readchomp(`$exename -E "Base.JLOptions().code_coverage" -L $inputfile
--code-coverage=$covfile --code-coverage`) == "1"
@test isfile(covfile)
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
@test readchomp(`$exename -E "Base.JLOptions().code_coverage" -L $inputfile
--code-coverage=$covfile --code-coverage=user`) == "1"
@test isfile(covfile)
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
@test readchomp(`$exename -E "Base.JLOptions().code_coverage" -L $inputfile
--code-coverage=$covfile --code-coverage=all`) == "2"
@test isfile(covfile)
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
# Ask for coverage in specific file
tfile = realpath(inputfile)
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
--code-coverage=$covfile --code-coverage=@$tfile`) == "(3, $(repr(tfile)))"
@test isfile(covfile)
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
# Ask for coverage in directory
tdir = dirname(realpath(inputfile))
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
--code-coverage=$covfile --code-coverage=@$tdir`) == "(3, $(repr(tdir)))"
@test isfile(covfile)
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
# Ask for coverage in current directory
tdir = dirname(realpath(inputfile))
cd(tdir) do
# there may be atrailing separator here so use rstrip
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, rstrip(unsafe_string(Base.JLOptions().tracked_path), '/'))" -L $inputfile
--code-coverage=$covfile --code-coverage=@`) == "(3, $(repr(tdir)))"
end
@test isfile(covfile)
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
# Ask for coverage in relative directory
tdir = dirname(realpath(inputfile))
cd(dirname(tdir)) do
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
--code-coverage=$covfile --code-coverage=@testhelpers`) == "(3, $(repr(tdir)))"
end
@test isfile(covfile)
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
# Ask for coverage in relative directory with dot-dot notation
tdir = dirname(realpath(inputfile))
cd(tdir) do
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
--code-coverage=$covfile --code-coverage=@../testhelpers`) == "(3, $(repr(tdir)))"
end
@test isfile(covfile)
got = read(covfile, String)
rm(covfile)
@test occursin(expected, got) || (expected, got)
# Ask for coverage in a different directory
tdir = mktempdir() # a dir that contains no code
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
--code-coverage=$covfile --code-coverage=@$tdir`) == "(3, $(repr(realpath(tdir))))"
@test isfile(covfile)
got = read(covfile, String)
@test isempty(got)
rm(covfile)
function coverage_info_for(src::String)
mktemp(dir) do srcfile, io
write(io, src); close(io)
outfile = tempname(dir, cleanup=false)*".info"
run(`$exename --code-coverage=$outfile $srcfile`)
result = read(outfile, String)
rm(outfile, force=true)
result
end
end
@test contains(coverage_info_for("""
function cov_bug(x, p)
if p > 2
print("") # runs
end
if Base.compilerbarrier(:const, false)
println("Does not run")
end
end
function do_test()
cov_bug(5, 3)
end
do_test()
"""), """
DA:2,1
DA:3,1
DA:5,1
DA:6,0
DA:9,1
DA:10,1
LH:5
LF:6
""")
@test contains(coverage_info_for("""
function cov_bug()
if Base.compilerbarrier(:const, true)
if Base.compilerbarrier(:const, true)
if Base.compilerbarrier(:const, false)
println("Does not run")
end
else
print("Does not run either")
end
else
print("")
end
return nothing
end
cov_bug()
"""), """
DA:1,1
DA:2,1
DA:3,1
DA:4,1
DA:5,0
DA:8,0
DA:11,0
DA:13,1
LH:5
LF:8
""")
end
# --track-allocation
@test readchomp(`$exename -E "Base.JLOptions().malloc_log != 0"`) == "false"
@test readchomp(`$exename -E "Base.JLOptions().malloc_log != 0" --track-allocation=none`) == "false"
@test readchomp(`$exename -E "Base.JLOptions().malloc_log != 0" --track-allocation`) == "true"
@test readchomp(`$exename -E "Base.JLOptions().malloc_log != 0" --track-allocation=user`) == "true"
mktempdir() do dir
helperdir = joinpath(@__DIR__, "testhelpers")
inputfile = joinpath(dir, "allocation_file.jl")
cp(joinpath(helperdir,"allocation_file.jl"), inputfile)
pid = readchomp(`$exename -E "getpid()" -L $inputfile --track-allocation=user`)
memfile = "$inputfile.$pid.mem"
got = readlines(memfile)
rm(memfile)
@test popfirst!(got) == " 0 g(x) = x + 123456"
@test popfirst!(got) == " - function f(x)"
@test popfirst!(got) == " - []"
if Sys.WORD_SIZE == 64
# P64 pools with 64 bit tags
@test popfirst!(got) == " 16 Base.invokelatest(g, 0)"
@test popfirst!(got) == " 32 Base.invokelatest(g, x)"
elseif 12 == (() -> @allocated ccall(:jl_gc_allocobj, Ptr{Cvoid}, (Csize_t,), 8))()
# See if we have a 12-byte pool with 32 bit tags (MAX_ALIGN = 4)
@test popfirst!(got) == " 12 Base.invokelatest(g, 0)"
@test popfirst!(got) == " 24 Base.invokelatest(g, x)"
else # MAX_ALIGN >= 8
@test popfirst!(got) == " 8 Base.invokelatest(g, 0)"
@test popfirst!(got) == " 32 Base.invokelatest(g, x)"
end
if Sys.WORD_SIZE == 64
@test popfirst!(got) == " 32 []"
else
@test popfirst!(got) == " 16 []"
end
@test popfirst!(got) == " - end"
@test popfirst!(got) == " - f(1.23)"
@test isempty(got) || got
end
# --optimize
@test readchomp(`$exename -E "Base.JLOptions().opt_level"`) == "2"
@test readchomp(`$exename -E "Base.JLOptions().opt_level" -O`) == "3"
@test readchomp(`$exename -E "Base.JLOptions().opt_level" --optimize`) == "3"
@test readchomp(`$exename -E "Base.JLOptions().opt_level" -O0`) == "0"
@test readchomp(`$exename -E "Base.JLOptions().opt_level_min"`) == "0"
@test readchomp(`$exename -E "Base.JLOptions().opt_level_min" --min-optlevel=2`) == "2"
# -g
@test readchomp(`$exename -E "Base.JLOptions().debug_level" -g`) == "2"
# --print-before/--print-after with pass names is broken on Windows due to no-gnu-unique issues
if !Sys.iswindows()
withenv("JULIA_LLVM_ARGS" => "--print-before=BeforeOptimization") do
let code = readchomperrors(`$exename -g0 -E "@eval Int64(1)+Int64(1)"`)
@test code[1]
code = code[3]
@test occursin("llvm.module.flags", code)
@test !occursin("llvm.dbg.cu", code)
@test !occursin("int.jl", code)
@test !occursin("name: \"Int64\"", code)
end
let code = readchomperrors(`$exename -g1 -E "@eval Int64(1)+Int64(1)"`)
@test code[1]
code = code[3]
@test occursin("llvm.module.flags", code)
@test occursin("llvm.dbg.cu", code)
@test occursin("int.jl", code)
@test !occursin("name: \"Int64\"", code)
end
let code = readchomperrors(`$exename -g2 -E "@eval Int64(1)+Int64(1)"`)
@test code[1]
code = code[3]
@test occursin("llvm.module.flags", code)
@test occursin("llvm.dbg.cu", code)
@test occursin("int.jl", code)
@test occursin("name: \"Int64\"", code)
end
end
end
# --check-bounds
let JL_OPTIONS_CHECK_BOUNDS_DEFAULT = 0,
JL_OPTIONS_CHECK_BOUNDS_ON = 1,
JL_OPTIONS_CHECK_BOUNDS_OFF = 2
exename_default_checkbounds = `$exename`
filter!(a -> !startswith(a, "--check-bounds="), exename_default_checkbounds.exec)
@test parse(Int, readchomp(`$exename_default_checkbounds -E "Int(Base.JLOptions().check_bounds)"`)) ==
JL_OPTIONS_CHECK_BOUNDS_DEFAULT
@test parse(Int, readchomp(`$exename -E "Int(Base.JLOptions().check_bounds)"
--check-bounds=auto`)) == JL_OPTIONS_CHECK_BOUNDS_DEFAULT
@test parse(Int, readchomp(`$exename -E "Int(Base.JLOptions().check_bounds)"
--check-bounds=yes`)) == JL_OPTIONS_CHECK_BOUNDS_ON
@test parse(Int, readchomp(`$exename -E "Int(Base.JLOptions().check_bounds)"
--check-bounds=no`)) == JL_OPTIONS_CHECK_BOUNDS_OFF
end
# check-bounds takes yes/no as argument
@test errors_not_signals(`$exename -E "exit(0)" --check-bounds=false`)
# --depwarn
@test readchomp(`$exename --depwarn=no -E "Base.JLOptions().depwarn"`) == "0"
@test readchomp(`$exename --depwarn=yes -E "Base.JLOptions().depwarn"`) == "1"
@test errors_not_signals(`$exename --depwarn=false`)
# test deprecated syntax
@test errors_not_signals(`$exename -e "foo (x::Int) = x * x" --depwarn=error`)
# test deprecated method
@test errors_not_signals(`$exename -e "
foo() = :foo; bar() = :bar
@deprecate foo() bar()
foo()
" --depwarn=error`)
# test deprecated bindings, #13269
let code = """
module Foo
import Base: @deprecate_binding
const NotDeprecated = true
@deprecate_binding Deprecated NotDeprecated
end
Foo.Deprecated
"""
@test errors_not_signals(`$exename -E "$code" --depwarn=error`)
@test readchomperrors(`$exename -E "$code" --depwarn=yes`) ==
(true, "true", "WARNING: Foo.Deprecated is deprecated, use NotDeprecated instead.\n likely near none:8")
@test readchomperrors(`$exename -E "$code" --depwarn=no`) ==
(true, "true", "")
end
# --inline
@test readchomp(`$exename -E "Bool(Base.JLOptions().can_inline)"`) == "true"
@test readchomp(`$exename --inline=yes -E "Bool(Base.JLOptions().can_inline)"`) == "true"
@test readchomp(`$exename --inline=no -E "Bool(Base.JLOptions().can_inline)"`) == "false"
# --inline takes yes/no as argument
@test errors_not_signals(`$exename --inline=false`)
# --polly
@test readchomp(`$exename -E "Bool(Base.JLOptions().polly)"`) == "true"
@test readchomp(`$exename --polly=yes -E "Bool(Base.JLOptions().polly)"`) == "true"
@test readchomp(`$exename --polly=no -E "Bool(Base.JLOptions().polly)"`) == "false"
# --polly takes yes/no as argument
@test errors_not_signals(`$exename --polly=false`)
# --fast-math
let JL_OPTIONS_FAST_MATH_DEFAULT = 0,
JL_OPTIONS_FAST_MATH_ON = 1,
JL_OPTIONS_FAST_MATH_OFF = 2
@test parse(Int,readchomp(`$exename -E
"Int(Base.JLOptions().fast_math)"`)) == JL_OPTIONS_FAST_MATH_DEFAULT
@test parse(Int,readchomp(`$exename --math-mode=user -E
"Int(Base.JLOptions().fast_math)"`)) == JL_OPTIONS_FAST_MATH_DEFAULT
@test parse(Int,readchomp(`$exename --math-mode=ieee -E
"Int(Base.JLOptions().fast_math)"`)) == JL_OPTIONS_FAST_MATH_OFF
@test parse(Int,readchomp(`$exename --math-mode=fast -E
"Int(Base.JLOptions().fast_math)"`)) == JL_OPTIONS_FAST_MATH_DEFAULT
end
# --worker takes default / custom as argument (default/custom arguments
# tested in test/parallel.jl)
@test errors_not_signals(`$exename --worker=true`)
# test passing arguments
mktempdir() do dir
testfile, io = mktemp(dir)
# write a julia source file that just prints ARGS to stdout
write(io, """
println(ARGS)
""")
close(io)
mkpath(joinpath(dir, "config"))
cp(testfile, joinpath(dir, "config", "startup.jl"))
withenv("JULIA_DEPOT_PATH" => dir) do
output = "[\"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`) ==
output
@test readchomp(`$exename --startup-file=yes -e 'exit(0)' -- foo -bar --baz`) ==
output
output = "[\"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`) ==
output
@test readchomp(`$exename -L $testfile -e 'exit(0)' -- foo -- -bar --baz`) ==
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 errors_not_signals(`$exename --foo $testfile`)
end
end
# test the program name remains constant
mktempdir() do dir
# dir can be case-incorrect sometimes
dir = realpath(dir)
a = joinpath(dir, "a.jl")
b = joinpath(dir, "b.jl")
c = joinpath(dir, "config", "startup.jl")
write(a, """
println(@__FILE__)
println(PROGRAM_FILE)
include(\"$(escape_string(b))\")
""")
write(b, """
println(@__FILE__)
println(PROGRAM_FILE)
""")
mkpath(dirname(c))
cp(b, c)
readsplit(cmd) = split(readchomp(cmd), '\n')
withenv("JULIA_DEPOT_PATH" => dir) do
@test readsplit(`$exename $a`) ==
[a, a,
b, a]
@test readsplit(`$exename -L $b -e 'exit(0)'`) ==
[b, ""]
@test readsplit(`$exename -L $b $a`) ==
[b, a,
a, a,
b, a]
@test readsplit(`$exename --startup-file=yes -e 'exit(0)'`) ==
[c, ""]
@test readsplit(`$exename --startup-file=yes -L $b -e 'exit(0)'`) ==
[c, "",
b, ""]
@test readsplit(`$exename --startup-file=yes -L $b $a`) ==
[c, a,
b, a,
a, a,
b, a]
end
end
# issue #10562
@test readchomp(`$exename -e 'println(ARGS);' ''`) == "[\"\"]"
# issue #12679
@test readchomperrors(`$exename --startup-file=no --compile=yes -ioo`) ==
(false, "", "ERROR: unknown option `-o`")
@test readchomperrors(`$exename --startup-file=no -p`) ==
(false, "", "ERROR: option `-p/--procs` is missing an argument")
@test readchomperrors(`$exename --startup-file=no --inline`) ==
(false, "", "ERROR: option `--inline` is missing an argument")
@test readchomperrors(`$exename --startup-file=no -e "@show ARGS" -now -- julia RUN.jl`) ==
(false, "", "ERROR: unknown option `-n`")
@test readchomperrors(`$exename --interactive=yes`) ==
(false, "", "ERROR: option `-i/--interactive` does not accept an argument")
# --compiled-modules={yes|no}
@test readchomp(`$exename -E "Bool(Base.JLOptions().use_compiled_modules)"`) == "true"
@test readchomp(`$exename --compiled-modules=yes -E
"Bool(Base.JLOptions().use_compiled_modules)"`) == "true"
@test readchomp(`$exename --compiled-modules=no -E
"Bool(Base.JLOptions().use_compiled_modules)"`) == "false"
@test errors_not_signals(`$exename --compiled-modules=foo -e "exit(0)"`)
# issue #12671, starting from a non-directory
# rm(dir) fails on windows with Permission denied
# and was an upstream bug in llvm <= v3.3
if !Sys.iswindows() && Base.libllvm_version > v"3.3"
testdir = mktempdir()
cd(testdir) do
rm(testdir)
@test Base.current_project() === nothing
@test success(`$exename -e "exit(0)"`)
for load_path in ["", "@", "@."]
withenv("JULIA_LOAD_PATH" => load_path) do
@test success(`$exename -e "exit(!(Base.load_path() == []))"`)
end
end
end
end
end
# Object file with multiple cpu targets
@testset "Object file for multiple microarchitectures" begin
julia_path = joinpath(Sys.BINDIR, Base.julia_exename())
outputo_file = tempname()
write(outputo_file, "1")
object_file = tempname() * ".o"
# This is to test that even with `pkgimages=no`, we can create object file
# with multiple cpu-targets
# The cmd is checked for `--object-o` as soon as it is run. So, to avoid long
# testing times, intentionally don't pass `--sysimage`; when we reach the
# corresponding error, we know that `check_cmdline` has already passed
let v = readchomperrors(`$julia_path
--cpu-target='native;native'
--output-o=$object_file $outputo_file
--pkgimages=no`)
@test v[1] == false
@test v[2] == ""
@test !contains(v[3], "More than one command line CPU targets specified")
@test v[3] == "ERROR: File \"boot.jl\" not found"
end
# This is to test that with `pkgimages=yes`, multiple CPU targets are parsed.
# We intentionally fail fast due to a lack of an `--output-o` flag.
let v = readchomperrors(`$julia_path --cpu-target='native;native' --pkgimages=yes`)
@test v[1] == false
@test v[2] == ""
@test contains(v[3], "More than one command line CPU targets specified")
end
end
# Find the path of libjulia (or libjulia-debug, as the case may be)
# to use as a dummy shlib to open
libjulia = if Base.DARWIN_FRAMEWORK
abspath(Libdl.dlpath(Base.DARWIN_FRAMEWORK_NAME *
(Base.isdebugbuild() ? "_debug" : "")))
else
abspath(Libdl.dlpath(Base.isdebugbuild() ? "libjulia-debug" : "libjulia"))
end
# test error handling code paths of running --sysimage
let exename = `$(Base.julia_cmd().exec[1]) -t 1`
sysname = unsafe_string(Base.JLOptions().image_file)
for nonexist_image in (
joinpath(@__DIR__, "nonexistent"),
"$sysname.nonexistent",
)
let err = Pipe(),
p = run(pipeline(`$exename --sysimage=$nonexist_image`, stderr=err), wait=false)
close(err.in)
let s = read(err, String)
@test occursin("ERROR: could not load library \"$nonexist_image\"\n", s)
@test !occursin("Segmentation fault", s)
@test !occursin("EXCEPTION_ACCESS_VIOLATION", s)
end
@test errors_not_signals(p)
@test p.exitcode == 1
end
end
let err = Pipe(),
p = run(pipeline(`$exename --sysimage=$libjulia`, stderr=err), wait=false)
close(err.in)
let s = read(err, String)
@test s == "ERROR: System image file failed consistency check: maybe opened the wrong version?\n"
end
@test errors_not_signals(p)
@test p.exitcode == 1
end
end
let exename = Base.julia_cmd()
# --startup-file
let JL_OPTIONS_STARTUPFILE_ON = 1,
JL_OPTIONS_STARTUPFILE_OFF = 2
# `JULIA_DEPOT_PATH=$tmpdir` to avoid errors in the user startup.jl, which hangs the tests. Issue #17642
mktempdir() do tmpdir
withenv("JULIA_DEPOT_PATH"=>tmpdir) do
@test parse(Int,readchomp(`$exename -E "Base.JLOptions().startupfile" --startup-file=yes`)) == JL_OPTIONS_STARTUPFILE_ON
end
end
@test parse(Int,readchomp(`$exename -E "Base.JLOptions().startupfile"
--startup-file=no`)) == JL_OPTIONS_STARTUPFILE_OFF
end
@test errors_not_signals(`$exename --startup-file=false`)
end
# Make sure `julia --lisp` doesn't break
run(pipeline(devnull, `$(joinpath(Sys.BINDIR, Base.julia_exename())) --lisp`, devnull))
# Test that `julia [some other option] --lisp` is disallowed
@test readchomperrors(`$(joinpath(Sys.BINDIR, Base.julia_exename())) -Cnative --lisp`) ==
(false, "", "ERROR: --lisp must be specified as the first argument")
# --sysimage-native-code={yes|no}
let exename = `$(Base.julia_cmd()) --startup-file=no`
@test readchomp(`$exename --sysimage-native-code=yes -E
"Bool(Base.JLOptions().use_sysimage_native_code)"`) == "true"
@test readchomp(`$exename --sysimage-native-code=no -E
"Bool(Base.JLOptions().use_sysimage_native_code)"`) == "false"
end
# backtrace contains line number info (esp. on windows #17179)
for precomp in ("yes", "no")
succ, out, bt = readchomperrors(`$(Base.julia_cmd()) --startup-file=no --sysimage-native-code=$precomp -E 'sqrt(-2)'`)
@test !succ
@test out == ""
@test occursin(r"\.jl:(\d+)", bt)