Skip to content

Commit 5606e9c

Browse files
committed
add test for JuliaLang/julia#34680
1 parent 01ec06a commit 5606e9c

File tree

1 file changed

+122
-60
lines changed

1 file changed

+122
-60
lines changed

test/runtests.jl

Lines changed: 122 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using PackageCompiler: PackageCompiler, create_sysimage, create_app
22
using Test
33
using Libdl
4+
using Pkg
45

56
ENV["JULIA_DEBUG"] = "PackageCompiler"
67

7-
# Make a new depot
8+
# Make a new depot to test stuff in
89
new_depot = mktempdir()
910
mkpath(joinpath(new_depot, "registries"))
1011
cp(joinpath(DEPOT_PATH[1], "registries", "General"), joinpath(new_depot, "registries", "General"))
@@ -17,67 +18,128 @@ if haskey(ENV, "CI")
1718
@show Sys.ARCH
1819
end
1920

20-
@testset "PackageCompiler.jl" begin
21-
tmp = mktempdir()
22-
sysimage_path = joinpath(tmp, "sys." * Libdl.dlext)
23-
script = tempname()
24-
write(script, "script_func() = println(\"I am a script\")")
25-
create_sysimage(:Example; sysimage_path=sysimage_path,
26-
precompile_execution_file="precompile_execution.jl",
27-
precompile_statements_file=["precompile_statements.jl",
28-
"precompile_statements2.jl"],
29-
script=script)
30-
# Check we can load sysimage and that Example is available in Main
31-
str = read(`$(Base.julia_cmd()) -J $(sysimage_path) -e 'println(Example.hello("foo")); script_func()'`, String)
32-
@test occursin("Hello, foo", str)
33-
@test occursin("I am a script", str)
34-
35-
# Test creating an app
36-
app_source_dir = joinpath(@__DIR__, "..", "examples/MyApp/")
37-
# TODO: Also test something that actually gives audit warnings
38-
@test_logs PackageCompiler.audit_app(app_source_dir)
39-
app_compiled_dir = joinpath(tmp, "MyAppCompiled")
40-
for incremental in (is_slow_ci ? (false,) : (true, false))
41-
if incremental == false
42-
filter_stdlibs = (is_slow_ci ? (true, ) : (true, false))
21+
22+
@testset "Sysimage" begin
23+
24+
tmp = mktempdir()
25+
sysimage_path = joinpath(tmp, "sys." * Libdl.dlext)
26+
script = tempname()
27+
write(script, "script_func() = println(\"I am a script\")")
28+
create_sysimage(:Example; sysimage_path=sysimage_path,
29+
precompile_execution_file="precompile_execution.jl",
30+
precompile_statements_file=["precompile_statements.jl",
31+
"precompile_statements2.jl"],
32+
script=script)
33+
# Check we can load sysimage and that Example is available in Main
34+
str = read(`$(Base.julia_cmd()) -J $(sysimage_path) -e 'println(Example.hello("foo")); script_func()'`, String)
35+
@test occursin("Hello, foo", str)
36+
@test occursin("I am a script", str)
37+
38+
end # sysimage
39+
40+
41+
@testset "App" begin
42+
43+
app_source_dir = joinpath(@__DIR__, "..", "examples/MyApp/")
44+
# TODO: Also test something that actually gives audit warnings
45+
@test_logs PackageCompiler.audit_app(app_source_dir)
46+
app_compiled_dir = joinpath(tmp, "MyAppCompiled")
47+
for incremental in (is_slow_ci ? (false,) : (true, false))
48+
if incremental == false
49+
filter_stdlibs = (is_slow_ci ? (true, ) : (true, false))
50+
else
51+
filter_stdlibs = (false,)
52+
end
53+
for filter in filter_stdlibs
54+
tmp_app_source_dir = joinpath(tmp, "MyApp")
55+
cp(app_source_dir, tmp_app_source_dir)
56+
create_app(tmp_app_source_dir, app_compiled_dir; incremental=incremental, force=true, filter_stdlibs=filter,
57+
precompile_execution_file=joinpath(app_source_dir, "precompile_app.jl"))
58+
rm(tmp_app_source_dir; recursive=true)
59+
# Get rid of some local state
60+
rm(joinpath(new_depot, "packages"); recursive=true)
61+
rm(joinpath(new_depot, "compiled"); recursive=true)
62+
app_path = abspath(app_compiled_dir, "bin", "MyApp" * (Sys.iswindows() ? ".exe" : ""))
63+
app_output = read(`$app_path`, String)
64+
65+
# Check stdlib filtering
66+
if filter == true
67+
@test !(occursin("LinearAlgebra", app_output))
4368
else
44-
filter_stdlibs = (false,)
45-
end
46-
for filter in filter_stdlibs
47-
tmp_app_source_dir = joinpath(tmp, "MyApp")
48-
cp(app_source_dir, tmp_app_source_dir)
49-
create_app(tmp_app_source_dir, app_compiled_dir; incremental=incremental, force=true, filter_stdlibs=filter,
50-
precompile_execution_file=joinpath(app_source_dir, "precompile_app.jl"))
51-
rm(tmp_app_source_dir; recursive=true)
52-
# Get rid of some local state
53-
rm(joinpath(new_depot, "packages"); recursive=true)
54-
rm(joinpath(new_depot, "compiled"); recursive=true)
55-
app_path = abspath(app_compiled_dir, "bin", "MyApp" * (Sys.iswindows() ? ".exe" : ""))
56-
app_output = read(`$app_path`, String)
57-
58-
# Check stdlib filtering
59-
if filter == true
60-
@test !(occursin("LinearAlgebra", app_output))
61-
else
62-
@test occursin("LinearAlgebra", app_output)
63-
end
64-
# Check dependency run
65-
@test occursin("Example.domath", app_output)
66-
# Check jll package runs
67-
@test occursin("Hello, World!", app_output)
68-
# Check artifact runs
69-
@test occursin("The result of 2*5^2 - 10 == 40.000000", app_output)
70-
# Check artifact gets run from the correct place
71-
@test occursin("HelloWorld artifact at $(realpath(app_compiled_dir))", app_output)
69+
@test occursin("LinearAlgebra", app_output)
7270
end
71+
# Check dependency run
72+
@test occursin("Example.domath", app_output)
73+
# Check jll package runs
74+
@test occursin("Hello, World!", app_output)
75+
# Check artifact runs
76+
@test occursin("The result of 2*5^2 - 10 == 40.000000", app_output)
77+
# Check artifact gets run from the correct place
78+
@test occursin("HelloWorld artifact at $(realpath(app_compiled_dir))", app_output)
7379
end
74-
# Test creating an empty sysimage
75-
if !is_slow_ci
76-
tmp = mktempdir()
77-
sysimage_path = joinpath(tmp, "empty." * Libdl.dlext)
78-
foreach(x -> touch(joinpath(tmp, x)), ["Project.toml", "Manifest.toml"])
79-
create_sysimage(; sysimage_path=sysimage_path, incremental=false, filter_stdlibs=true, project=tmp)
80-
hello = read(`$(Base.julia_cmd()) -J $(sysimage_path) -e 'print("hello, world")'`, String)
81-
@test hello == "hello, world"
80+
end
81+
82+
end # testset
83+
84+
85+
@testset "empty sysimage" begin
86+
87+
if !is_slow_ci
88+
tmp = mktempdir()
89+
sysimage_path = joinpath(tmp, "empty." * Libdl.dlext)
90+
foreach(x -> touch(joinpath(tmp, x)), ["Project.toml", "Manifest.toml"])
91+
create_sysimage(; sysimage_path=sysimage_path, incremental=false, filter_stdlibs=true, project=tmp)
92+
hello = read(`$(Base.julia_cmd()) -J $(sysimage_path) -e 'print("hello, world")'`, String)
93+
@test hello == "hello, world"
94+
end
95+
96+
end # testset
97+
98+
99+
# Test for https://github.com/JuliaLang/julia/issues/34680
100+
@testset "ccall absolute path" begin
101+
102+
tmpdir = mktempdir()
103+
open(joinpath(tmpdir, "libfoo.c"), "w") do io
104+
write(io, """
105+
#include <stdio.h>
106+
107+
void foo() {
108+
printf("Hello, Foo!\\n");
109+
}
110+
""")
111+
end
112+
libfoo_libname = string("libfoo", ".", Libdl.dlext)
113+
cd(tmpdir) do
114+
compiler = PackageCompiler.get_compiler()
115+
cmd = `$compiler -o $libfoo_libname libfoo.c -shared`
116+
PackageCompiler.run_with_env(cmd, compiler)
117+
118+
Pkg.generate("LibFoo")
119+
open(joinpath("LibFoo", "src", "LibFoo.jl"), "w") do io
120+
write(io, """
121+
module LibFoo
122+
const libfoo = $(repr(joinpath(tempdir, libfoo_libname)))
123+
f() = ccall((:foo, libfoo), Cvoid, ())
124+
function __init__()
125+
f()
126+
end
127+
end
128+
""")
82129
end
83130
end
131+
132+
prev = Base.ACTIVE_PROJECT[]
133+
Base.ACTIVE_PROJECT[] = joinpath(tmpdir, "LibFoo")
134+
135+
try
136+
sysimage_path = tempname()
137+
create_sysimage(:LibFoo; sysimage_path=sysimage_path)
138+
content = "using .LibFoo; LibFoo.f()"
139+
str = read(`$(Base.julia_cmd()) -J $(sysimage_path) -e $content`, String)
140+
@test occursin("Hello, Foo", str)
141+
finally
142+
Base.ACTIVE_PROJECT[] = prev
143+
end
144+
145+
end # testset

0 commit comments

Comments
 (0)