Skip to content

Commit ddfdccd

Browse files
authored
Fix tests when DocStringExtensions is tested in a non-repo (#70)
Fix tests when DocStringExtensions is tested in a non-repo environment: - try-catch around LibGit2 operations - move testmodule M to its own and setup a repository there when testing
1 parent 87a244f commit ddfdccd

File tree

4 files changed

+84
-61
lines changed

4 files changed

+84
-61
lines changed

src/utilities.jl

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -319,21 +319,26 @@ function url(mod::Module, file::AbstractString, line::Integer)
319319
else
320320
if isfile(file)
321321
local d = dirname(file)
322-
return LibGit2.with(LibGit2.GitRepoExt(d)) do repo
323-
LibGit2.with(LibGit2.GitConfig(repo)) do cfg
324-
local u = LibGit2.get(cfg, "remote.origin.url", "")
325-
local m = match(LibGit2.GITHUB_REGEX, u)
326-
u = m === nothing ? get(ENV, "TRAVIS_REPO_SLUG", "") : m.captures[1]
327-
local commit = string(LibGit2.head_oid(repo))
328-
local root = LibGit2.path(repo)
329-
if startswith(file, root) || startswith(realpath(file), root)
330-
local base = "https://github.com/$u/tree"
331-
local filename = file[(length(root) + 1):end]
332-
return "$base/$commit/$filename#L$line"
333-
else
334-
return ""
322+
try # might not be in a git repo
323+
LibGit2.with(LibGit2.GitRepoExt(d)) do repo
324+
LibGit2.with(LibGit2.GitConfig(repo)) do cfg
325+
local u = LibGit2.get(cfg, "remote.origin.url", "")
326+
local m = match(LibGit2.GITHUB_REGEX, u)
327+
u = m === nothing ? get(ENV, "TRAVIS_REPO_SLUG", "") : m.captures[1]
328+
local commit = string(LibGit2.head_oid(repo))
329+
local root = LibGit2.path(repo)
330+
if startswith(file, root) || startswith(realpath(file), root)
331+
local base = "https://github.com/$u/tree"
332+
local filename = file[(length(root) + 1):end]
333+
return "$base/$commit/$filename#L$line"
334+
else
335+
return ""
336+
end
335337
end
336338
end
339+
catch err
340+
isa(err, LibGit2.GitError) || rethrow()
341+
return ""
337342
end
338343
else
339344
return ""

test/TestModule/M.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module M
2+
3+
export f
4+
5+
f(x) = x
6+
7+
g(x = 1, y = 2, z = 3; kwargs...) = x
8+
9+
const A{T} = Union{Vector{T}, Matrix{T}}
10+
11+
h_1(x::A) = x
12+
h_2(x::A{Int}) = x
13+
h_3(x::A{T}) where {T} = x
14+
15+
i_1(x; y = x) = x * y
16+
i_2(x::Int; y = x) = x * y
17+
i_3(x::T; y = x) where {T} = x * y
18+
i_4(x; y::T = zero(T), z::U = zero(U)) where {T, U} = x + y + z
19+
20+
j_1(x, y) = x * y # two arguments, no keyword arguments
21+
j_1(x; y = x) = x * y # one argument, one keyword argument
22+
23+
mutable struct T
24+
a
25+
b
26+
c
27+
end
28+
29+
struct K
30+
K(; a = 1) = new()
31+
end
32+
33+
34+
abstract type AbstractType <: Integer end
35+
36+
struct CustomType{S, T <: Integer} <: Integer
37+
end
38+
39+
primitive type BitType8 8 end
40+
41+
primitive type BitType32 <: Real 32 end
42+
43+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DocStringExtensions
22
using Test
33
import Markdown
4+
import LibGit2
45

56
include("tests.jl")

test/tests.jl

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,20 @@
11
const DSE = DocStringExtensions
22

33
include("templates.jl")
4-
5-
module M
6-
7-
export f
8-
9-
f(x) = x
10-
11-
g(x = 1, y = 2, z = 3; kwargs...) = x
12-
13-
const A{T} = Union{Vector{T}, Matrix{T}}
14-
15-
h_1(x::A) = x
16-
h_2(x::A{Int}) = x
17-
h_3(x::A{T}) where {T} = x
18-
19-
i_1(x; y = x) = x * y
20-
i_2(x::Int; y = x) = x * y
21-
i_3(x::T; y = x) where {T} = x * y
22-
i_4(x; y::T = zero(T), z::U = zero(U)) where {T, U} = x + y + z
23-
24-
j_1(x, y) = x * y # two arguments, no keyword arguments
25-
j_1(x; y = x) = x * y # one argument, one keyword argument
26-
27-
mutable struct T
28-
a
29-
b
30-
c
31-
end
32-
33-
struct K
34-
K(; a = 1) = new()
35-
end
36-
37-
38-
abstract type AbstractType <: Integer end
39-
40-
struct CustomType{S, T <: Integer} <: Integer
41-
end
42-
43-
primitive type BitType8 8 end
44-
45-
primitive type BitType32 <: Real 32 end
46-
4+
include("TestModule/M.jl")
5+
6+
# initialize a test repo in test/TestModule which is needed for some tests
7+
function with_test_repo(f)
8+
repo = LibGit2.init(joinpath(@__DIR__, "TestModule"))
9+
LibGit2.add!(repo, "M.jl")
10+
sig = LibGit2.Signature("zeptodoctor", "[email protected]", round(time()), 0)
11+
LibGit2.commit(repo, "M.jl", committer = sig, author = sig)
12+
LibGit2.GitRemote(repo, "origin", "https://github.com/JuliaDocs/NonExistent.jl.git")
13+
try
14+
f()
15+
finally
16+
rm(joinpath(@__DIR__, "TestModule", ".git"); force = true, recursive = true)
17+
end
4718
end
4819

4920
@testset "DocStringExtensions" begin
@@ -136,11 +107,13 @@ end
136107
:typesig => Tuple{Any},
137108
:module => M,
138109
)
139-
DSE.format(METHODLIST, buf, doc)
110+
with_test_repo() do
111+
DSE.format(METHODLIST, buf, doc)
112+
end
140113
str = String(take!(buf))
141114
@test occursin("```julia", str)
142115
@test occursin("f(x)", str)
143-
@test occursin(joinpath("test", "tests.jl"), str)
116+
@test occursin(joinpath("test", "TestModule", "M.jl"), str)
144117
end
145118

146119
@testset "method signatures" begin
@@ -407,9 +380,10 @@ end
407380
end
408381
@testset "url" begin
409382
@test !isempty(DSE.url(first(methods(sin))))
410-
@test !isempty(DSE.url(first(methods(DSE.parsedocs))))
411-
@test !isempty(DSE.url(first(methods(M.f))))
412-
@test !isempty(DSE.url(first(methods(M.K))))
383+
with_test_repo() do
384+
@test occursin("github.com/JuliaDocs/NonExistent", DSE.url(first(methods(M.f))))
385+
@test occursin("github.com/JuliaDocs/NonExistent", DSE.url(first(methods(M.K))))
386+
end
413387
end
414388
@testset "comparemethods" begin
415389
let f = first(methods(M.f)),

0 commit comments

Comments
 (0)