Skip to content

Commit a0a2bb6

Browse files
cmdlineargs test tweaks (#59286)
1 parent 07716cb commit a0a2bb6

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

test/cmdlineargs.jl

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ function readchomperrors(exename::Cmd)
2424
return (success(p), fetch(o), fetch(e))
2525
end
2626

27+
# helper function for tests that expect successful command execution
28+
# logs detailed error information if the command fails
29+
function test_read_success(cmd::Cmd, expected_type::Type=String)
30+
success, out, err = readchomperrors(cmd)
31+
if !success
32+
println("---- Command failed: $cmd")
33+
println("stdout:$out")
34+
println("stderr: $err")
35+
println("----")
36+
end
37+
@test success
38+
return expected_type == String ? out : parse(expected_type, out)
39+
end
40+
2741
function format_filename(s)
2842
p = ccall(:jl_format_filename, Cstring, (Cstring,), s)
2943
r = unsafe_string(p)
@@ -482,33 +496,35 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
482496

483497
# --code-coverage
484498
mktempdir() do dir
499+
# don't inherit global coverage settings that could confuse these tests
500+
cov_exename = `$(Base.julia_cmd()[1]) --startup-file=no --color=no`
485501
helperdir = joinpath(@__DIR__, "testhelpers")
486502
inputfile = joinpath(helperdir, "coverage_file.jl")
487503
expected = replace(read(joinpath(helperdir, "coverage_file.info"), String),
488504
"<FILENAME>" => realpath(inputfile))
489505
covfile = replace(joinpath(dir, "coverage.info"), "%" => "%%")
490506
@test !isfile(covfile)
491-
defaultcov = readchomp(`$exename -E "Base.JLOptions().code_coverage != 0" -L $inputfile`)
507+
defaultcov = readchomp(`$cov_exename -E "Base.JLOptions().code_coverage != 0" -L $inputfile`)
492508
opts = Base.JLOptions()
493509
coverage_file = (opts.output_code_coverage != C_NULL) ? unsafe_string(opts.output_code_coverage) : ""
494510
@test !isfile(covfile)
495511
@test defaultcov == string(opts.code_coverage != 0 && (isempty(coverage_file) || occursin("%p", coverage_file)))
496-
@test readchomp(`$exename -E "Base.JLOptions().code_coverage" -L $inputfile
512+
@test readchomp(`$cov_exename -E "Base.JLOptions().code_coverage" -L $inputfile
497513
--code-coverage=$covfile --code-coverage=none`) == "0"
498514
@test !isfile(covfile)
499-
@test readchomp(`$exename -E "Base.JLOptions().code_coverage" -L $inputfile
515+
@test readchomp(`$cov_exename -E "Base.JLOptions().code_coverage" -L $inputfile
500516
--code-coverage=$covfile --code-coverage`) == "1"
501517
@test isfile(covfile)
502518
got = read(covfile, String)
503519
rm(covfile)
504520
@test occursin(expected, got) || (expected, got)
505-
@test readchomp(`$exename -E "Base.JLOptions().code_coverage" -L $inputfile
521+
@test readchomp(`$cov_exename -E "Base.JLOptions().code_coverage" -L $inputfile
506522
--code-coverage=$covfile --code-coverage=user`) == "1"
507523
@test isfile(covfile)
508524
got = read(covfile, String)
509525
rm(covfile)
510526
@test occursin(expected, got) || (expected, got)
511-
@test readchomp(`$exename -E "Base.JLOptions().code_coverage" -L $inputfile
527+
@test readchomp(`$cov_exename -E "Base.JLOptions().code_coverage" -L $inputfile
512528
--code-coverage=$covfile --code-coverage=all`) == "2"
513529
@test isfile(covfile)
514530
got = read(covfile, String)
@@ -517,7 +533,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
517533

518534
# Ask for coverage in specific file
519535
tfile = realpath(inputfile)
520-
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
536+
@test readchomp(`$cov_exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
521537
--code-coverage=$covfile --code-coverage=@$tfile`) == "(3, $(repr(tfile)))"
522538
@test isfile(covfile)
523539
got = read(covfile, String)
@@ -526,7 +542,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
526542

527543
# Ask for coverage in directory
528544
tdir = dirname(realpath(inputfile))
529-
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
545+
@test readchomp(`$cov_exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
530546
--code-coverage=$covfile --code-coverage=@$tdir`) == "(3, $(repr(tdir)))"
531547
@test isfile(covfile)
532548
got = read(covfile, String)
@@ -537,7 +553,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
537553
tdir = dirname(realpath(inputfile))
538554
cd(tdir) do
539555
# there may be atrailing separator here so use rstrip
540-
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, rstrip(unsafe_string(Base.JLOptions().tracked_path), '/'))" -L $inputfile
556+
@test readchomp(`$cov_exename -E "(Base.JLOptions().code_coverage, rstrip(unsafe_string(Base.JLOptions().tracked_path), '/'))" -L $inputfile
541557
--code-coverage=$covfile --code-coverage=@`) == "(3, $(repr(tdir)))"
542558
end
543559
@test isfile(covfile)
@@ -548,7 +564,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
548564
# Ask for coverage in relative directory
549565
tdir = dirname(realpath(inputfile))
550566
cd(dirname(tdir)) do
551-
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
567+
@test readchomp(`$cov_exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
552568
--code-coverage=$covfile --code-coverage=@testhelpers`) == "(3, $(repr(tdir)))"
553569
end
554570
@test isfile(covfile)
@@ -559,7 +575,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
559575
# Ask for coverage in relative directory with dot-dot notation
560576
tdir = dirname(realpath(inputfile))
561577
cd(tdir) do
562-
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
578+
@test readchomp(`$cov_exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
563579
--code-coverage=$covfile --code-coverage=@../testhelpers`) == "(3, $(repr(tdir)))"
564580
end
565581
@test isfile(covfile)
@@ -569,7 +585,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
569585

570586
# Ask for coverage in a different directory
571587
tdir = mktempdir() # a dir that contains no code
572-
@test readchomp(`$exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
588+
@test readchomp(`$cov_exename -E "(Base.JLOptions().code_coverage, unsafe_string(Base.JLOptions().tracked_path))" -L $inputfile
573589
--code-coverage=$covfile --code-coverage=@$tdir`) == "(3, $(repr(realpath(tdir))))"
574590
@test isfile(covfile)
575591
got = read(covfile, String)
@@ -580,7 +596,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
580596
mktemp(dir) do srcfile, io
581597
write(io, src); close(io)
582598
outfile = tempname(dir, cleanup=false)*".info"
583-
run(`$exename --code-coverage=$outfile $srcfile`)
599+
run(`$cov_exename --code-coverage=$outfile $srcfile`)
584600
result = read(outfile, String)
585601
rm(outfile, force=true)
586602
result
@@ -640,16 +656,17 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
640656
end
641657

642658
# --track-allocation
643-
@test readchomp(`$exename -E "Base.JLOptions().malloc_log != 0"`) == "false"
644-
@test readchomp(`$exename -E "Base.JLOptions().malloc_log != 0" --track-allocation=none`) == "false"
659+
alloc_exename = `$(Base.julia_cmd()) --startup-file=no --color=no --track-allocation=none`
660+
@test readchomp(`$alloc_exename -E "Base.JLOptions().malloc_log != 0"`) == "false"
661+
@test readchomp(`$alloc_exename -E "Base.JLOptions().malloc_log != 0" --track-allocation=none`) == "false"
645662

646-
@test readchomp(`$exename -E "Base.JLOptions().malloc_log != 0" --track-allocation`) == "true"
647-
@test readchomp(`$exename -E "Base.JLOptions().malloc_log != 0" --track-allocation=user`) == "true"
663+
@test readchomp(`$alloc_exename -E "Base.JLOptions().malloc_log != 0" --track-allocation`) == "true"
664+
@test readchomp(`$alloc_exename -E "Base.JLOptions().malloc_log != 0" --track-allocation=user`) == "true"
648665
mktempdir() do dir
649666
helperdir = joinpath(@__DIR__, "testhelpers")
650667
inputfile = joinpath(dir, "allocation_file.jl")
651668
cp(joinpath(helperdir,"allocation_file.jl"), inputfile)
652-
pid = readchomp(`$exename -E "getpid()" -L $inputfile --track-allocation=user`)
669+
pid = readchomp(`$alloc_exename -E "getpid()" -L $inputfile --track-allocation=user`)
653670
memfile = "$inputfile.$pid.mem"
654671
got = readlines(memfile)
655672
rm(memfile)
@@ -1291,19 +1308,19 @@ end
12911308
("100000000", 100000000), ("1e8", 1e8), ("100MB", 100m), ("100m", 100m), ("1e5kB", 1e5k),
12921309
]
12931310
@testset "--hard-heap-limit=$str" for (str, val) in one_hundred_mb_strs_and_vals
1294-
@test parse(UInt64,read(`$exename --hard-heap-limit=$str -E "Base.JLOptions().hard_heap_limit"`, String)) == val
1311+
@test test_read_success(`$exename --hard-heap-limit=$str -E "Base.JLOptions().hard_heap_limit"`, UInt64) == val
12951312
end
12961313
two_and_a_half_gigabytes_strs_and_vals = [
12971314
("2500000000", 2500000000), ("2.5e9", 2.5e9), ("2.5g", 2.5g), ("2.5GB", 2.5g), ("2.5e6mB", 2.5e6m),
12981315
]
12991316
@testset "--hard-heap-limit=$str" for (str, val) in two_and_a_half_gigabytes_strs_and_vals
1300-
@test parse(UInt64,read(`$exename --hard-heap-limit=$str -E "Base.JLOptions().hard_heap_limit"`, String)) == val
1317+
@test test_read_success(`$exename --hard-heap-limit=$str -E "Base.JLOptions().hard_heap_limit"`, UInt64) == val
13011318
end
13021319
one_terabyte_strs_and_vals = [
13031320
("1TB", 1t), ("1024GB", 1t),
13041321
]
13051322
@testset "--hard-heap-limit=$str" for (str, val) in one_terabyte_strs_and_vals
1306-
@test parse(UInt64,read(`$exename --hard-heap-limit=$str -E "Base.JLOptions().hard_heap_limit"`, String)) == val
1323+
@test test_read_success(`$exename --hard-heap-limit=$str -E "Base.JLOptions().hard_heap_limit"`, UInt64) == val
13071324
end
13081325
end
13091326

@@ -1321,26 +1338,26 @@ end
13211338
("100000000", 100000000), ("1e8", 1e8), ("100MB", 100m), ("100m", 100m), ("1e5kB", 1e5k),
13221339
]
13231340
@testset "--heap-target-increment=$str" for (str, val) in one_hundred_mb_strs_and_vals
1324-
@test parse(UInt64,read(`$exename --heap-target-increment=$str -E "Base.JLOptions().heap_target_increment"`, String)) == val
1341+
@test test_read_success(`$exename --heap-target-increment=$str -E "Base.JLOptions().heap_target_increment"`, UInt64) == val
13251342
end
13261343
two_and_a_half_gigabytes_strs_and_vals = [
13271344
("2500000000", 2500000000), ("2.5e9", 2.5e9), ("2.5g", 2.5g), ("2.5GB", 2.5g), ("2.5e6mB", 2.5e6m),
13281345
]
13291346
@testset "--heap-target-increment=$str" for (str, val) in two_and_a_half_gigabytes_strs_and_vals
1330-
@test parse(UInt64,read(`$exename --heap-target-increment=$str -E "Base.JLOptions().heap_target_increment"`, String)) == val
1347+
@test test_read_success(`$exename --heap-target-increment=$str -E "Base.JLOptions().heap_target_increment"`, UInt64) == val
13311348
end
13321349
one_terabyte_strs_and_vals = [
13331350
("1TB", 1t), ("1024GB", 1t),
13341351
]
13351352
@testset "--heap-target-increment=$str" for (str, val) in one_terabyte_strs_and_vals
1336-
@test parse(UInt64,read(`$exename --heap-target-increment=$str -E "Base.JLOptions().heap_target_increment"`, String)) == val
1353+
@test test_read_success(`$exename --heap-target-increment=$str -E "Base.JLOptions().heap_target_increment"`, UInt64) == val
13371354
end
13381355
end
13391356

13401357
@testset "--timeout-for-safepoint-straggler" begin
13411358
exename = `$(Base.julia_cmd())`
13421359
timeout = 120
1343-
@test parse(Int,read(`$exename --timeout-for-safepoint-straggler=$timeout -E "Base.JLOptions().timeout_for_safepoint_straggler_s"`, String)) == timeout
1360+
@test test_read_success(`$exename --timeout-for-safepoint-straggler=$timeout -E "Base.JLOptions().timeout_for_safepoint_straggler_s"`, Int) == timeout
13441361
end
13451362

13461363
@testset "--strip-metadata" begin

0 commit comments

Comments
 (0)