forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg.jl
165 lines (144 loc) · 5.57 KB
/
pkg.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# This file is a part of Julia. License is MIT: http://julialang.org/license
function temp_pkg_dir(fn::Function)
# Used in tests below to setup and teardown a sandboxed package directory
const tmpdir = ENV["JULIA_PKGDIR"] = joinpath(tempdir(),randstring())
@test !isdir(Pkg.dir())
try
Pkg.init()
@test isdir(Pkg.dir())
Pkg.resolve()
fn()
finally
rm(tmpdir, recursive=true)
end
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
@test isfile(joinpath(Pkg.dir(),"REQUIRE"))
@test isfile(joinpath(Pkg.dir(),"META_BRANCH"))
@test isempty(Pkg.installed())
Pkg.add("Example")
@test [keys(Pkg.installed())...] == ["Example"]
iob = IOBuffer()
Pkg.checkout("Example")
Pkg.status("Example", iob)
str = chomp(takebuf_string(iob))
@test startswith(str, " - Example")
@test endswith(str, "master")
Pkg.free("Example")
Pkg.status("Example", iob)
str = chomp(takebuf_string(iob))
@test endswith(str, string(Pkg.installed("Example")))
Pkg.checkout("Example")
Pkg.free(("Example",))
Pkg.status("Example", iob)
str = chomp(takebuf_string(iob))
@test endswith(str, string(Pkg.installed("Example")))
Pkg.rm("Example")
@test isempty(Pkg.installed())
@test !isempty(Pkg.available("Example"))
@test_throws ErrorException Pkg.available("FakePackageDoesn'tExist")
Pkg.clone("https://github.com/JuliaLang/Example.jl.git")
@test [keys(Pkg.installed())...] == ["Example"]
Pkg.status("Example", iob)
str = chomp(takebuf_string(iob))
@test startswith(str, " - Example")
@test endswith(str, "master")
Pkg.free("Example")
Pkg.status("Example", iob)
str = chomp(takebuf_string(iob))
@test endswith(str, string(Pkg.installed("Example")))
Pkg.checkout("Example")
Pkg.free(("Example",))
Pkg.status("Example", iob)
str = chomp(takebuf_string(iob))
@test endswith(str, string(Pkg.installed("Example")))
Pkg.rm("Example")
@test isempty(Pkg.installed())
end
# testing a package with test dependencies causes them to be installed for the duration of the test
temp_pkg_dir() do
Pkg.generate("PackageWithTestDependencies", "MIT", config=Dict("user.name"=>"Julia Test", "user.email"=>"[email protected]"))
@test [keys(Pkg.installed())...] == ["PackageWithTestDependencies"]
@test readall(Pkg.dir("PackageWithTestDependencies","REQUIRE")) == "julia $(Pkg.Generate.versionfloor(VERSION))\n"
isdir(Pkg.dir("PackageWithTestDependencies","test")) || mkdir(Pkg.dir("PackageWithTestDependencies","test"))
open(Pkg.dir("PackageWithTestDependencies","test","REQUIRE"),"w") do f
println(f,"Example")
end
open(Pkg.dir("PackageWithTestDependencies","test","runtests.jl"),"w") do f
println(f,"using Base.Test")
println(f,"@test haskey(Pkg.installed(), \"Example\")")
end
Pkg.resolve()
@test [keys(Pkg.installed())...] == ["PackageWithTestDependencies"]
Pkg.test("PackageWithTestDependencies")
@test [keys(Pkg.installed())...] == ["PackageWithTestDependencies"]
end
# testing a package with no runtests.jl errors
temp_pkg_dir() do
Pkg.generate("PackageWithNoTests", "MIT", config=Dict("user.name"=>"Julia Test", "user.email"=>"[email protected]"))
if isfile(Pkg.dir("PackageWithNoTests", "test", "runtests.jl"))
rm(Pkg.dir("PackageWithNoTests", "test", "runtests.jl"))
end
try
Pkg.test("PackageWithNoTests")
catch err
@test err.msg == "PackageWithNoTests did not provide a test/runtests.jl file"
end
end
# testing a package with failing tests errors
temp_pkg_dir() do
Pkg.generate("PackageWithFailingTests", "MIT", config=Dict("user.name"=>"Julia Test", "user.email"=>"[email protected]"))
isdir(Pkg.dir("PackageWithFailingTests","test")) || mkdir(Pkg.dir("PackageWithFailingTests","test"))
open(Pkg.dir("PackageWithFailingTests", "test", "runtests.jl"),"w") do f
println(f,"using Base.Test")
println(f,"@test false")
end
try
Pkg.test("PackageWithFailingTests")
catch err
@test err.msg == "PackageWithFailingTests had test errors"
end
end
# Testing with code-coverage
temp_pkg_dir() do
Pkg.generate("PackageWithCodeCoverage", "MIT", config=Dict("user.name"=>"Julia Test", "user.email"=>"[email protected]"))
src = """
module PackageWithCodeCoverage
export f1, f2, f3, untested
f1(x) = 2x
f2(x) = f1(x)
function f3(x)
3x
end
untested(x) = 7
end"""
linetested = [false, false, false, false, true, true, false, true, false, false]
open(Pkg.dir("PackageWithCodeCoverage", "src", "PackageWithCodeCoverage.jl"), "w") do f
println(f, src)
end
isdir(Pkg.dir("PackageWithCodeCoverage","test")) || mkdir(Pkg.dir("PackageWithCodeCoverage","test"))
open(Pkg.dir("PackageWithCodeCoverage", "test", "runtests.jl"),"w") do f
println(f,"using PackageWithCodeCoverage, Base.Test")
println(f,"@test f2(2) == 4")
println(f,"@test f3(5) == 15")
end
Pkg.test("PackageWithCodeCoverage")
covdir = Pkg.dir("PackageWithCodeCoverage","src")
covfiles = filter!(x -> contains(x, "PackageWithCodeCoverage.jl") && contains(x,".cov"), readdir(covdir))
@test isempty(covfiles)
Pkg.test("PackageWithCodeCoverage", coverage=true)
covfiles = filter!(x -> contains(x, "PackageWithCodeCoverage.jl") && contains(x,".cov"), readdir(covdir))
@test !isempty(covfiles)
for file in covfiles
@test isfile(joinpath(covdir,file))
covstr = readall(joinpath(covdir,file))
srclines = split(src, '\n')
covlines = split(covstr, '\n')
for i = 1:length(linetested)
covline = (linetested[i] ? " 1 " : " - ")*srclines[i]
@test covlines[i] == covline
end
end
end