Skip to content

Commit

Permalink
Support rename of --compilecache to --compile-modules (#26)
Browse files Browse the repository at this point in the history
* Support rename of compilecache to compile-modules

* Add is_precompile_enabled function
  • Loading branch information
omus authored Oct 26, 2017
1 parent 911faa8 commit 6c7a2fa
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ notifications:
on_failure: change
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- OPTIONS=$(julia -e 'isdefined(Base.JLOptions(), :use_compilecache) && print("--compilecache=no")')
- julia $OPTIONS -e 'Pkg.clone(pwd()); Pkg.build("Mocking"); Pkg.test("Mocking"; coverage=true)';
- julia -e 'Pkg.clone(pwd()); Pkg.build("Mocking")'
- OPTIONS=$(julia -e 'using Mocking; print(DISABLE_PRECOMPILE_STR)')
- julia $OPTIONS -e 'Pkg.test("Mocking"; coverage=true)';
after_success:
- julia -e 'cd(Pkg.dir("Mocking")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ Notes
Mocking.jl is intended to be used for testing only and will not affect the performance of
your code when using `@mock`. In fact the `@mock` is actually a no-op when `Mocking.enable`
is not called. One side effect of this behaviour is that pre-compiled packages won't test
correctly with Mocking unless you start Julia with `--compilecache=no`.
correctly with Mocking unless you start Julia with `--compiled-modules=no` (>=0.7) or
`--compilecache=no` (<=0.6).

```
$ julia --compilecache=no -e Pkg.test("...")
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ build_script:
Pkg.clone(pwd(), \"Mocking\"); Pkg.build(\"Mocking\")"

test_script:
- C:\projects\julia\bin\julia --check-bounds=yes -e "opts = ifelse(isdefined(Base.JLOptions(), :use_compilecache), `--compilecache=no`, ``); run(`$(Base.julia_cmd()) $opts -e Pkg.test(\\\"Mocking\\\")`)"
- C:\projects\julia\bin\julia --check-bounds=yes -e "using Mocking; run(`$(Base.julia_cmd()) $DISABLE_PRECOMPILE_CMD -e Pkg.test(\\\"Mocking\\\")`)"
40 changes: 35 additions & 5 deletions src/Mocking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,51 @@ import Compat: invokelatest
include("expr.jl")
include("bindings.jl")

export @patch, @mock, Patch, apply
export @patch, @mock, Patch, apply, DISABLE_PRECOMPILE_STR, DISABLE_PRECOMPILE_CMD

# When ENABLED is false the @mock macro is a noop.
global ENABLED = false
global PATCH_ENV = nothing

const PRECOMPILE_FLAG = if VERSION >= v"0.7.0-DEV.1698"
Symbol("compiled-modules")
elseif VERSION >= v"0.5.0-dev+977"
:compilecache
else
Symbol()
end

const PRECOMPILE_FIELD = if VERSION >= v"0.7.0-DEV.1698"
:use_compiled_modules
elseif VERSION >= v"0.5.0-dev+977"
:use_compilecache
else
Symbol()
end

const DISABLE_PRECOMPILE_STR = PRECOMPILE_FLAG == Symbol() ? "" : "--$PRECOMPILE_FLAG=no"
const DISABLE_PRECOMPILE_CMD = isempty(DISABLE_PRECOMPILE_STR) ? `` : `$DISABLE_PRECOMPILE_STR`

function is_precompile_enabled()
opts = Base.JLOptions()
field = PRECOMPILE_FIELD

# When the pre-compile field is empty it means pre-compilation is unsupported. If the
# pre-compile field is missing that means pre-compilation to be assumed to be enabled.
return field != Symbol() && (!isdefined(opts, field) || Bool(getfield(opts, field)))
end

function enable()
ENABLED::Bool && return # Abend early if enabled has already been set
global ENABLED = true
global PATCH_ENV = PatchEnv()

# TODO: Support programatically disabling the use of the compilecache.
opts = Base.JLOptions()
if isdefined(opts, :use_compilecache) && Bool(opts.use_compilecache)
warn("Mocking.jl will probably not work when compilecache is enabled. Please start Julia with `--compilecache=no`")
# TODO: Support programatically disabling the use of the pre-compilation flag.
if is_precompile_enabled()
warn(
"Mocking.jl will probably not work when $PRECOMPILE_FLAG is enabled. " *
"Please start Julia with `$DISABLE_PRECOMPILE_STR`",
)
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/precompile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if VERSION >= v"0.7.0-DEV.1698"
@test Mocking.is_precompile_enabled() == Bool(Base.JLOptions().use_compiled_modules)
elseif VERSION >= v"0.5.0-dev+977"
@test Mocking.is_precompile_enabled() == Bool(Base.JLOptions().use_compilecache)
else
@test Mocking.is_precompile_enabled() == false
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function next_gensym(str::AbstractString, offset::Integer=1)
return Symbol(string(m.captures[1], parse(Int, m.captures[2]) + offset))
end

include("precompile.jl")
include("expr.jl")
include("bindings/bindings.jl")
include("patch.jl")
Expand Down

0 comments on commit 6c7a2fa

Please sign in to comment.