Skip to content

Commit 89355fb

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

File tree

1 file changed

+123
-60
lines changed

1 file changed

+123
-60
lines changed

test/runtests.jl

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

0 commit comments

Comments
 (0)