Skip to content

Commit ff233b3

Browse files
authored
Test Cthulhu extension (#392)
`ascend` is a terminal program so it takes a bit of effort to test it. xref JuliaDebug/Cthulhu.jl#581
1 parent 710dc6c commit ff233b3

File tree

4 files changed

+109
-7
lines changed

4 files changed

+109
-7
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ jobs:
4949
- run: julia --project -e 'using Pkg; Pkg.develop([PackageSpec(path="SnoopCompileCore")])'
5050
- uses: julia-actions/julia-buildpkg@latest
5151
- uses: julia-actions/julia-runtest@latest
52+
- run: julia --check-bounds=yes --project -e 'using Pkg; Pkg.test(; test_args=["cthulhu"], coverage=true)'
5253
- uses: julia-actions/julia-processcoverage@v1
5354
with:
5455
directories: src,SnoopCompileCore/src

Project.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ PyPlot = "2"
4343
Random = "1"
4444
Serialization = "1"
4545
SnoopCompileCore = "3"
46+
REPL = "1"
4647
Test = "1"
4748
YAML = "0.4"
4849
julia = "1.10"
@@ -55,7 +56,8 @@ MethodAnalysis = "85b6ec6f-f7df-4429-9514-a64bcd9ee824"
5556
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
5657
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
5758
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
59+
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
5860
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5961

6062
[targets]
61-
test = ["Cthulhu", "PrettyTables", "Documenter", "JET", "MethodAnalysis", "Pkg", "Random", "Test"]
63+
test = ["Cthulhu", "PrettyTables", "Documenter", "JET", "MethodAnalysis", "Pkg", "Random", "REPL", "Test"]

test/extensions/cthulhu.jl

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
module CthulhuExtTest
2+
3+
using SnoopCompileCore, SnoopCompile
4+
using Cthulhu
5+
using Pkg
6+
using Test
7+
8+
if !isdefined(@__MODULE__, :fake_terminal)
9+
@eval (@__MODULE__) begin
10+
Base.include(@__MODULE__, normpath(pkgdir(Cthulhu), "test", "FakeTerminals.jl"))
11+
using .FakeTerminals
12+
end
13+
end
14+
15+
macro with_try_stderr(out, expr)
16+
quote
17+
try
18+
$(esc(expr))
19+
catch err
20+
bt = catch_backtrace()
21+
Base.display_error(stderr, err, bt)
22+
end
23+
end
24+
end
25+
26+
# Test functions
27+
myplus(x, y) = x + y
28+
function f(x)
29+
x < 0.25 ? 1 :
30+
x < 0.5 ? 1.0 :
31+
x < 0.75 ? 0x01 : Float16(1)
32+
end
33+
g(c) = myplus(f(c[1]), f(c[2]))
34+
35+
36+
@testset "Cthulhu extension" begin
37+
@testset "ascend for invalidations" begin
38+
cproj = Base.active_project()
39+
cd(joinpath(dirname(@__DIR__), "testmodules", "Invalidation")) do
40+
Pkg.activate(pwd())
41+
Pkg.develop(path="./PkgC")
42+
Pkg.develop(path="./PkgD")
43+
Pkg.precompile()
44+
invalidations = @snoop_invalidations begin
45+
@eval begin
46+
using PkgC
47+
PkgC.nbits(::UInt8) = 8
48+
using PkgD
49+
end
50+
end
51+
tree = only(invalidation_trees(invalidations))
52+
sig, root = only(tree.mt_backedges)
53+
54+
fake_terminal() do term, in, out, _
55+
t = @async begin
56+
@with_try_stderr out ascend(term, root; interruptexc=false)
57+
end
58+
lines = String(readavailable(out)) # this gets the header
59+
sleep(0.1) # some platforms seem to need this
60+
lines = String(readavailable(out))
61+
sleep(0.1)
62+
@test occursin("call_nbits", lines)
63+
@test occursin("map_nbits(::Vector{Integer})", lines)
64+
# the job of the extension is done once we've written the menu, so we can quit here
65+
write(in, 'q')
66+
wait(t)
67+
end
68+
end
69+
70+
Pkg.activate(cproj)
71+
end
72+
73+
@testset "ascend for inference triggers" begin
74+
tinf = @snoop_inference g([0.7, 0.8])
75+
itrigs = inference_triggers(tinf; exclude_toplevel=false)
76+
itrig = last(itrigs)
77+
78+
fake_terminal() do term, in, out, _
79+
t = @async begin
80+
@with_try_stderr out ascend(term, itrig; interruptexc=false)
81+
end
82+
lines = String(readavailable(out)) # this gets the header
83+
sleep(0.1)
84+
lines = String(readavailable(out))
85+
sleep(0.1)
86+
@test occursin("myplus(::UInt8, ::Float16)", lines)
87+
@test occursin("g(::Vector{Float64})", lines)
88+
# the job of the extension is done once we've written the menu, so we can quit here
89+
write(in, 'q')
90+
wait(t)
91+
end
92+
end
93+
end
94+
95+
end

test/runtests.jl

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
using Test
22
using SnoopCompile
33

4-
include("snoop_inference.jl")
5-
include("snoop_llvm.jl")
6-
include("snoop_invalidations.jl")
4+
if !isempty(ARGS)
5+
"cthulhu" ARGS && include("extensions/cthulhu.jl")
6+
else
7+
include("snoop_inference.jl")
8+
include("snoop_llvm.jl")
9+
include("snoop_invalidations.jl")
710

8-
# otherwise-untested demos
9-
retflat = SnoopCompile.flatten_demo()
10-
@test !isempty(retflat.children)
11+
# otherwise-untested demos
12+
retflat = SnoopCompile.flatten_demo()
13+
@test !isempty(retflat.children)
14+
end

0 commit comments

Comments
 (0)