Skip to content

Commit

Permalink
Test use of startup-file in Pkg.build and Pkg.test
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Jun 5, 2017
1 parent d85bed4 commit 7c63665
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 22 deletions.
3 changes: 2 additions & 1 deletion base/pkg/entry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ function test!(pkg::AbstractString,
push!(notests, pkg)
else
info("Testing $pkg")
code = "evalfile(\"$test_path\")"
cd(dirname(test_path)) do
try
cmd = ```
Expand All @@ -719,7 +720,7 @@ function test!(pkg::AbstractString,
--compilecache=$(Bool(Base.JLOptions().use_compilecache) ? "yes" : "no")
--check-bounds=yes
--startup-file=$(Base.JLOptions().startupfile != 2 ? "yes" : "no")
$test_path
--eval $code
```
run(cmd)
info("$pkg tests passed")
Expand Down
35 changes: 35 additions & 0 deletions test/TestHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,41 @@ module TestHelpers
include("dimensionful.jl")
export Furlong


function capture_stdout(f::Function)
local output::String
old_stdout = STDOUT
rd, wr = redirect_stdout()
try
f()
catch
rethrow()
finally
redirect_stdout(old_stdout)
close(wr)
output = readstring(rd)
close(rd)
end
return output
end

function capture_stderr(f::Function)
local output::String
old_stderr = STDERR
rd, wr = redirect_stderr()
try
f()
catch
rethrow()
finally
redirect_stderr(old_stderr)
close(wr)
output = readstring(rd)
close(rd)
end
return output
end

mutable struct FakeTerminal <: Base.Terminals.UnixTerminal
in_stream::Base.IO
out_stream::Base.IO
Expand Down
94 changes: 73 additions & 21 deletions test/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,8 @@

import Base.Pkg.PkgError

function capture_stdout(f::Function)
let fname = tempname()
try
open(fname, "w") do fout
redirect_stdout(fout) do
f()
end
end
return readstring(fname)
finally
rm(fname, force=true)
end
end
end

isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__), "TestHelpers.jl"))
import TestHelpers: capture_stdout, capture_stderr

function temp_pkg_dir(fn::Function, tmp_dir=joinpath(tempdir(), randstring()),
remove_tmp_dir::Bool=true; initialize::Bool=true)
Expand All @@ -39,6 +26,12 @@ function temp_pkg_dir(fn::Function, tmp_dir=joinpath(tempdir(), randstring()),
end
end

function write_build(pkg, content)
build_filename = Pkg.dir(pkg, "deps", "build.jl")
mkpath(dirname(build_filename))
write(build_filename, content)
end

# Test basic operations: adding or removing a package, status, free
# Also test for the existence of REQUIRE and META_BRANCH
temp_pkg_dir() do
Expand Down Expand Up @@ -531,6 +524,71 @@ temp_pkg_dir() do
"redirect_stderr(STDOUT); using Example; Pkg.update(\"$package\")"`))
@test contains(msg, "- $package\nRestart Julia to use the updated versions.")
end

# Verify that the --startup-file flag is respected by Pkg.build / Pkg.test
let package = "StartupFile"
content = """
info("JULIA_RC_LOADED defined \$(isdefined(:JULIA_RC_LOADED))")
info("Main.JULIA_RC_LOADED defined \$(isdefined(Main, :JULIA_RC_LOADED))")
"""

write_build(package, content)

test_filename = Pkg.dir(package, "test", "runtests.jl")
mkpath(dirname(test_filename))
write(test_filename, content)

# Make a .juliarc.jl
home = Pkg.dir(".home")
mkdir(home)
write(joinpath(home, ".juliarc.jl"), "const JULIA_RC_LOADED = true")

withenv("HOME" => home) do
code = "Pkg.build(\"$package\")"
err = capture_stderr() do
run(`$(Base.julia_cmd()) --startup-file=no -e $code`)
end

@test err == """
INFO: Building $package
INFO: JULIA_RC_LOADED defined false
INFO: Main.JULIA_RC_LOADED defined false
"""

err = capture_stderr() do
run(`$(Base.julia_cmd()) --startup-file=yes -e $code`)
end

@test err == """
INFO: Building $package
INFO: JULIA_RC_LOADED defined false
INFO: Main.JULIA_RC_LOADED defined true
"""

code = "Pkg.test(\"$package\")"
err = capture_stderr() do
run(`$(Base.julia_cmd()) --startup-file=no -e $code`)
end

@test err == """
INFO: Testing $package
INFO: JULIA_RC_LOADED defined false
INFO: Main.JULIA_RC_LOADED defined false
INFO: $package tests passed
"""

err = capture_stderr() do
run(`$(Base.julia_cmd()) --startup-file=yes -e $code`)
end

@test err == """
INFO: Testing $package
INFO: JULIA_RC_LOADED defined false
INFO: Main.JULIA_RC_LOADED defined true
INFO: $package tests passed
"""
end
end
end

@testset "Pkg functions with .jl extension" begin
Expand Down Expand Up @@ -594,12 +652,6 @@ end
end

temp_pkg_dir(initialize=false) do
function write_build(pkg, content)
build_filename = Pkg.dir(pkg, "deps", "build.jl")
mkpath(dirname(build_filename))
write(build_filename, content)
end

write_build("Normal", "")
write_build("Error", "error(\"An error has occurred while building a package\")")
write_build("Exit", "exit()")
Expand Down

0 comments on commit 7c63665

Please sign in to comment.