Skip to content

Commit 86afc96

Browse files
authored
Merge pull request #84 from timholy/teh/testcoverage
Improve test coverage, add module API summary
2 parents c490872 + e5e77bd commit 86afc96

File tree

6 files changed

+63
-17
lines changed

6 files changed

+63
-17
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ jobs:
4747
run: julia -e 'using Pkg; Pkg.develop(PackageSpec(path=".")); Pkg.add(PackageSpec(url="https://github.com/timholy/Revise.jl")); Pkg.test("Revise")'
4848
- name: Test while running Revise
4949
if: ${{ matrix.os == 'ubuntu-latest' && matrix.version != '1.0' }}
50-
run: julia --project -e 'using Pkg;
51-
Pkg.add("Revise");
52-
Pkg.test("CodeTracking"; coverage=true, test_args=["revise"])'
50+
run: TERM="xterm" julia --project -i --code-coverage -e 'using InteractiveUtils, REPL, Revise, Pkg;
51+
Pkg.add("ColorTypes");
52+
@async(Base.run_main_repl(true, true, false, true, false));
53+
sleep(2);
54+
cd("test");
55+
include("runtests.jl");
56+
REPL.eval_user_input(:(exit()), Base.active_repl_backend)'
5357
- uses: julia-actions/julia-processcoverage@v1
5458
- uses: codecov/codecov-action@v1
5559
with:

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ julia = "1"
1313
[extras]
1414
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
1515
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
16+
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
1617
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1718
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1819

1920
[targets]
20-
test = ["ColorTypes", "LinearAlgebra", "SparseArrays", "Test"]
21+
test = ["ColorTypes", "LinearAlgebra", "REPL", "SparseArrays", "Test"]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CodeTracking
22

3+
[![Build status](https://github.com/timholy/CodeTracking.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/timholy/CodeTracking.jl/actions/workflows/ci.yml)
4+
[![Coverage](https://codecov.io/gh/timholy/CodeTracking.jl/branch/master/graph/badge.svg?token=bBzCYyj19O)](https://codecov.io/gh/timholy/CodeTracking.jl)
5+
36
CodeTracking can be thought of as an extension of Julia's
47
[InteractiveUtils library](https://docs.julialang.org/en/latest/stdlib/InteractiveUtils/).
58
It provides an interface for obtaining:

src/CodeTracking.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
"""
2+
CodeTracking can be thought of as an extension of InteractiveUtils, and pairs well with Revise.jl.
3+
4+
- `code_string`, `@code_string`: fetch the source code (as a string) for a method definition
5+
- `code_expr`, `@code_expr`: fetch the expression for a method definition
6+
- `definition`: a lower-level variant of the above
7+
- `pkgfiles`: return information about the source files that define a package
8+
- `whereis`: Return location information about methods (with Revise, it updates as you edit files)
9+
- `signatures_at`: return the signatures of all methods whose definition spans the specified location
10+
"""
111
module CodeTracking
212

313
using Base: PkgId
@@ -159,7 +169,7 @@ function signatures_at(filename::AbstractString, line::Integer)
159169
end
160170
end
161171
end
162-
error("$filename not found in internal data, perhaps the package is not loaded (or not loaded with `includet`)")
172+
throw(ArgumentError("$filename not found in internal data, perhaps the package is not loaded (or not loaded with `includet`)"))
163173
end
164174

165175
"""

src/utils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# This should stay as the first method because it's used in a test
2+
# (or change the test)
13
function checkname(fdef::Expr, name)
24
fproto = fdef.args[1]
35
fdef.head === :where && return checkname(fproto, name)

test/runtests.jl

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Note: some of CodeTracking's functionality can only be tested by Revise
22

33
using CodeTracking
4-
using Test, InteractiveUtils, LinearAlgebra, SparseArrays
4+
using Test, InteractiveUtils, REPL, LinearAlgebra, SparseArrays
55
# Note: ColorTypes needs to be installed, but note the intentional absence of `using ColorTypes`
66

77
using CodeTracking: line_is_decl
@@ -67,6 +67,10 @@ isdefined(Main, :Revise) ? Main.Revise.includet("script.jl") : include("script.j
6767
show(io, info)
6868
str = String(take!(io))
6969
@test startswith(str, "PkgFiles(CodeTracking [da1fd8a2-8d9e-5ec2-8556-3022fb5608a2]):\n basedir:")
70+
ioctx = IOContext(io, :compact=>true)
71+
show(ioctx, info)
72+
str = String(take!(io))
73+
@test match(r"PkgFiles\(CodeTracking, .*CodeTracking(\.jl)?, String\[\]\)", str) !== nothing
7074

7175
@test pkgfiles("ColorTypes") === nothing
7276
@test_throws ErrorException pkgfiles("NotAPkg")
@@ -86,17 +90,6 @@ isdefined(Main, :Revise) ? Main.Revise.includet("script.jl") : include("script.j
8690
oldlookup = CodeTracking.method_lookup_callback[]
8791
CodeTracking.method_lookup_callback[] = m -> error("oops")
8892
@test whereis(m) == ("REPL[1]", 1)
89-
# Test with definition(String, m)
90-
if isdefined(Base, :active_repl)
91-
hp = Base.active_repl.interface.modes[1].hist
92-
fstr = "__fREPL__(x::Int16) = 0"
93-
histidx = length(hp.history) + 1 - hp.start_idx
94-
ex = Base.parse_input_line(fstr; filename="REPL[$histidx]")
95-
f = Core.eval(Main, ex)
96-
push!(hp.history, fstr)
97-
@test definition(String, first(methods(f))) == (fstr, 1)
98-
pop!(hp.history)
99-
end
10093
CodeTracking.method_lookup_callback[] = oldlookup
10194

10295
m = first(methods(Test.eval))
@@ -109,6 +102,10 @@ isdefined(Main, :Revise) ? Main.Revise.includet("script.jl") : include("script.j
109102
end
110103
m = first(methods(f150))
111104
src = Base.uncompressed_ast(m)
105+
idx = findfirst(lin -> String(lin.file) == @__FILE__, src.linetable)
106+
lin = src.linetable[idx]
107+
file, line = whereis(lin, m)
108+
@test endswith(file, String(lin.file))
112109
idx = findfirst(lin -> String(lin.file) != @__FILE__, src.linetable)
113110
lin = src.linetable[idx]
114111
file, line = whereis(lin, m)
@@ -167,6 +164,35 @@ end
167164

168165
# issue #23
169166
@test !isempty(signatures_at("script.jl", 9))
167+
168+
@test_throws ArgumentError signatures_at("nofile.jl", 1)
169+
170+
if isdefined(Revise, :add_revise_deps)
171+
Revise.add_revise_deps()
172+
sigs = signatures_at(CodeTracking, "src/utils.jl", 5)
173+
@test length(sigs) == 1 # only isn't available on julia 1.0
174+
@test first(sigs) == Tuple{typeof(CodeTracking.checkname), Expr, Any}
175+
@test pkgfiles(CodeTracking).id == Base.PkgId(CodeTracking)
176+
end
177+
178+
# REPL (test copied from Revise)
179+
if isdefined(Base, :active_repl)
180+
hp = Base.active_repl.interface.modes[1].hist
181+
fstr = "__fREPL__(x::Int16) = 0"
182+
histidx = length(hp.history) + 1 - hp.start_idx
183+
ex = Base.parse_input_line(fstr; filename="REPL[$histidx]")
184+
f = Core.eval(Main, ex)
185+
if ex.head === :toplevel
186+
ex = ex.args[end]
187+
end
188+
push!(hp.history, fstr)
189+
m = first(methods(f))
190+
@test definition(String, first(methods(f))) == (fstr, 1)
191+
@test !isempty(signatures_at(String(m.file), m.line))
192+
pop!(hp.history)
193+
elseif haskey(ENV, "CI")
194+
error("CI Revise tests must be run with -i")
195+
end
170196
end
171197
end
172198

0 commit comments

Comments
 (0)