Skip to content

RFC: test, fix, and redesign Requires #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
### Note: changes in v0.7

Requires now needs a UUID, and must be called from within your packages `__init__` function. For example:

```julia
function __init__()
@require JSON="682c06a0-de6a-54ab-a142-c8b1cf79cde6" do_stuff()
end
```

# Requires.jl

[![Build Status](https://travis-ci.org/MikeInnes/Requires.jl.svg?branch=master)](https://travis-ci.org/MikeInnes/Requires.jl)
Expand Down
5 changes: 4 additions & 1 deletion src/Requires.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ __precompile__()
module Requires

include("init.jl")
include("getthing.jl")
include("require.jl")

function __init__()
push!(package_callbacks, loadpkg)
end

end # module
17 changes: 0 additions & 17 deletions src/getthing.jl

This file was deleted.

5 changes: 0 additions & 5 deletions src/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,3 @@ end
macro init(args...)
initm(args...)
end

"Prevent init fns being called multiple times during precompilation."
macro guard(ex)
:(!isprecompiling() && $(esc(ex)))
end
35 changes: 13 additions & 22 deletions src/require.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ export @require

isprecompiling() = ccall(:jl_generating_output, Cint, ()) == 1

@init begin
push!(package_callbacks, loadpkg)
end

loaded(pkg) = haskey(Base.loaded_modules, pkg)

const _callbacks = Dict{PkgId, Vector{Function}}()
Expand Down Expand Up @@ -52,33 +48,28 @@ function parsepkg(ex)
isexpr(ex, :(=)) || @goto fail
mod, id = ex.args
(mod isa Symbol && id isa String) || @goto fail
return Base.PkgId(Base.UUID(id), String(mod))
return id, String(mod)
@label fail
error("Requires syntax is: `@require Pkg=\"uuid\"`")
end

macro require(pkg, expr)
pkg isa Symbol &&
return Expr(:macrocall, Symbol("@warn"), __source__,
"Requires now needs a UUID: `@require $pkg=\"uuid\"`")
pkg = parsepkg(pkg)
ex = quote
listenpkg($pkg) do
withpath(@__FILE__) do
err($__module__, $(pkg.name)) do
$(esc(:(eval($(Expr(:quote, Expr(:block,
:(const $(Symbol(pkg.name)) = Base.require($pkg)),
expr)))))))
"Requires now needs a UUID; please see the readme for changes in 0.7.")
id, modname = parsepkg(pkg)
pkg = Base.PkgId(Base.UUID(id), modname)
quote
if !isprecompiling()
listenpkg(Base.PkgId(Base.UUID($id), $modname)) do
withpath($(string(__source__.file))) do
err($__module__, $(pkg.name)) do
$(esc(:(eval($(Expr(:quote, Expr(:block,
:(const $(Symbol(pkg.name)) = Base.require($pkg)),
expr)))))))
end
end
end
end
end
quote
if isprecompiling()
@init @guard $(ex)
else
$(ex)
end
nothing
end
end
69 changes: 59 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,67 @@
module Foo
using Test

using Requires, Test
function writepkg(name, precomp::Bool)
open("$name.jl", "w") do io
println(io, """
__precompile__($precomp)

beforeflag = false
afterflag = false
module $name

@require JSON="682c06a0-de6a-54ab-a142-c8b1cf79cde6" global beforeflag = true
using Requires

@test !beforeflag
using JSON
@test beforeflag
flag = false

@require JSON="682c06a0-de6a-54ab-a142-c8b1cf79cde6" global afterflag = true
function __init__()
@require JSON="682c06a0-de6a-54ab-a142-c8b1cf79cde6" global flag = true
end

end
""")
end
end

@testset "Requires" begin
mktempdir() do pkgsdir
cd(pkgsdir) do
npcdir = joinpath("FooNPC", "src")
mkpath(npcdir)
cd(npcdir) do
writepkg("FooNPC", false)
end
npcdir = joinpath("FooPC", "src")
mkpath(npcdir)
cd(npcdir) do
writepkg("FooPC", true)
end
end
push!(LOAD_PATH, pkgsdir)

@eval using FooNPC
@test !FooNPC.flag
@eval using FooPC
@test !FooPC.flag

@eval using JSON

@test FooNPC.flag
@test FooPC.flag

@test afterflag
cd(pkgsdir) do
npcdir = joinpath("FooAfterNPC", "src")
mkpath(npcdir)
cd(npcdir) do
writepkg("FooAfterNPC", false)
end
pcidr = joinpath("FooAfterPC", "src")
mkpath(pcidr)
cd(pcidr) do
writepkg("FooAfterPC", true)
end
end

@eval using FooAfterNPC
@eval using FooAfterPC
@test FooAfterNPC.flag
@test FooAfterPC.flag
end
end