Skip to content

Commit 9941729

Browse files
authored
Informative error for un-devved packages (#3)
Modules defined at the REPL or packages being used in `add` mode are not amenable to updates. Rather than throw a cryptic error it's better to suggest the solution. Also adds a test that `write` works.
1 parent bf54a08 commit 9941729

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ version = "0.1.0"
77
julia = "1.3"
88

99
[extras]
10+
Example = "7876af07-990d-54b4-ab0e-23690620f79a"
11+
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1012
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1113

1214
[targets]
13-
test = ["Test"]
15+
test = ["Example", "Pkg", "Test"]

src/ModuleDocstrings.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ The package should be checked out in `develop` mode before calling `write`.
7272
"""
7373
function write(mod::Module, str)
7474
path = pathof(mod)
75+
(path === nothing || !iswritable(path)) && error_write(mod, path)
7576
modstr = read(path, String)
7677
idxs = findfirst("module $mod", modstr)
7778
idxs === nothing && error("could not identify start of module")
@@ -90,4 +91,10 @@ The package should be checked out in `develop` mode before calling `write`.
9091
"""
9192
write(mod::Module) = write(mod, generate(mod))
9293

94+
# this is replacing, not extending, the Base function of the same name
95+
iswritable(filename::AbstractString) = isfile(filename) && (uperm(filename) & 0x02) != 0x00
96+
97+
error_write(mod, ::Nothing) = error("$mod must be a writable package, but there is no corresponding file, suggesting it wasn't loaded from a package.")
98+
error_write(mod, path::AbstractString) = error("$mod must be a writable package, but the path \"$path\" is not writable.\nDid you forget to `Pkg.develop` the package?")
99+
93100
end

test/runtests.jl

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using ModuleDocstrings
2+
using Example # test error on a non-devved package
3+
using Pkg
24
using Test
35

46
@testset "ModuleDocstrings.jl" begin
57
str = ModuleDocstrings.generate(ModuleDocstrings)
68
@test occursin("ModuleDocstrings.generate", str)
79
@test occursin("ModuleDocstrings.write", str)
810

9-
str = ModuleDocstrings.generate(@eval Module() begin
11+
m = @eval Module() begin
1012
"""
1113
foo1()
1214
@@ -27,8 +29,49 @@ using Test
2729
foo3()
2830

2931
@__MODULE__
30-
end)
32+
end
33+
str = ModuleDocstrings.generate(m)
3134
@test occursin("- `Main.anonymous.foo1`: `foo1` is pretty useful.", str)
3235
@test occursin("- `Main.anonymous.foo2`: `foo2` doesn't show the signature.", str)
3336
@test occursin("- `Main.anonymous.foo3`: `foo3` contains a [`Main.anonymous.foo1`](@ref) that contains a period.", str)
37+
38+
if Base.VERSION >= v"1.8.0-DEV.363" # use strings in @test_throws; we don't care what type of error this is
39+
@test_throws "must be a writable package, but there is no corresponding file" ModuleDocstrings.write(m)
40+
@test_throws r"must be a writable package, but the path \".*\" is not writable" ModuleDocstrings.write(Example)
41+
else
42+
@test_throws Exception ModuleDocstrings.write(m)
43+
@test_throws Exception ModuleDocstrings.write(Example)
44+
end
45+
46+
mktempdir() do pkgs
47+
push!(LOAD_PATH, pkgs)
48+
newpkgdir = joinpath(pkgs, "DevDummy")
49+
Pkg.generate(newpkgdir)
50+
open(joinpath(newpkgdir, "src", "DevDummy.jl"), "w") do io
51+
print(io,
52+
"""
53+
module DevDummy
54+
55+
\"\"\"
56+
greet()
57+
58+
Print a delightful greeting.
59+
\"\"\"
60+
greet() = print("Hello World!")
61+
62+
end # module
63+
"""
64+
)
65+
end
66+
@eval using DevDummy
67+
ModuleDocstrings.write(DevDummy)
68+
str = read(joinpath(newpkgdir, "src", "DevDummy.jl"), String)
69+
@test occursin(
70+
"""
71+
\"\"\"
72+
- `DevDummy.greet`: Print a delightful greeting.
73+
\"\"\"
74+
module DevDummy
75+
""", str)
76+
end
3477
end

0 commit comments

Comments
 (0)