Skip to content

Commit 92f87ed

Browse files
Merge #1226
1226: [RFC] Allow Pkg.test to pass command-line arguments to test/runtests.jl r=fredrikekre a=DilumAluthge Fixes #518 Fixes #981 Fixes #1241 Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
2 parents dcb43d5 + 105e1c6 commit 92f87ed

File tree

8 files changed

+55
-15
lines changed

8 files changed

+55
-15
lines changed

src/API.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,11 @@ test(pkg::Union{AbstractString, PackageSpec}; kwargs...) = test([pkg]; kwargs...
229229
test(pkgs::Vector{<:AbstractString}; kwargs...) = test([PackageSpec(pkg) for pkg in pkgs]; kwargs...)
230230
test(pkgs::Vector{PackageSpec}; kwargs...) = test(Context(), pkgs; kwargs...)
231231
function test(ctx::Context, pkgs::Vector{PackageSpec};
232-
coverage=false, test_fn=nothing, kwargs...)
232+
coverage=false, test_fn=nothing,
233+
julia_args::AbstractVector{<:Base.AbstractCmd}=Cmd[],
234+
test_args::AbstractVector{<:AbstractString}=String[], kwargs...)
235+
julia_args = convert(Vector{Cmd}, julia_args)
236+
test_args = convert(Vector{String}, test_args)
233237
pkgs = deepcopy(pkgs) # deepcopy for avoid mutating PackageSpec members
234238
Context!(ctx; kwargs...)
235239
ctx.preview && preview_info()
@@ -242,7 +246,7 @@ function test(ctx::Context, pkgs::Vector{PackageSpec};
242246
manifest_resolve!(ctx.env, pkgs)
243247
ensure_resolved(ctx.env, pkgs)
244248
end
245-
Operations.test(ctx, pkgs; coverage=coverage, test_fn=test_fn)
249+
Operations.test(ctx, pkgs; coverage=coverage, test_fn=test_fn, julia_args=julia_args, test_args=test_args)
246250
return
247251
end
248252

src/Operations.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,10 +1142,15 @@ function free(ctx::Context, pkgs::Vector{PackageSpec})
11421142
end
11431143
end
11441144

1145-
function gen_test_code(testfile::String; coverage=false)
1145+
function gen_test_code(testfile::String;
1146+
coverage=false,
1147+
julia_args::Vector{Cmd}=Cmd[],
1148+
test_args::Vector{String}=String[])
1149+
julia_args_str::Vector{String} = vcat([x.exec for x in julia_args]...)
11461150
code = """
11471151
$(Base.load_path_setup_code(false))
11481152
cd($(repr(dirname(testfile))))
1153+
append!(empty!(ARGS), $(repr(test_args)))
11491154
include($(repr(testfile)))
11501155
"""
11511156
return ```
@@ -1157,7 +1162,8 @@ function gen_test_code(testfile::String; coverage=false)
11571162
--inline=$(Bool(Base.JLOptions().can_inline) ? "yes" : "no")
11581163
--startup-file=$(Base.JLOptions().startupfile == 1 ? "yes" : "no")
11591164
--track-allocation=$(("none", "user", "all")[Base.JLOptions().malloc_log + 1])
1160-
--eval $code
1165+
$(julia_args_str)
1166+
--eval $(code)
11611167
```
11621168
end
11631169

@@ -1240,7 +1246,10 @@ end
12401246

12411247
testdir(source_path::String) = joinpath(source_path, "test")
12421248
testfile(source_path::String) = joinpath(testdir(source_path), "runtests.jl")
1243-
function test(ctx::Context, pkgs::Vector{PackageSpec}; coverage=false, test_fn=nothing)
1249+
function test(ctx::Context, pkgs::Vector{PackageSpec};
1250+
coverage=false, test_fn=nothing,
1251+
julia_args::Vector{Cmd}=Cmd[],
1252+
test_args::Vector{String}=String[])
12441253
ctx.preview || Pkg.instantiate(ctx)
12451254

12461255
# load manifest data
@@ -1273,7 +1282,7 @@ function test(ctx::Context, pkgs::Vector{PackageSpec}; coverage=false, test_fn=n
12731282
for (pkg, source_path) in zip(pkgs, source_paths)
12741283
if !isfile(projectfile_path(testdir(source_path)))
12751284
backwards_compatibility_for_test(ctx, pkg, testfile(source_path),
1276-
pkgs_errored, coverage)
1285+
pkgs_errored, coverage; julia_args=julia_args, test_args=test_args)
12771286
continue
12781287
end
12791288

@@ -1287,7 +1296,7 @@ function test(ctx::Context, pkgs::Vector{PackageSpec}; coverage=false, test_fn=n
12871296
test_fn !== nothing && test_fn()
12881297
Display.status(Context(), mode=PKGMODE_PROJECT)
12891298
try
1290-
run(gen_test_code(testfile(source_path); coverage=coverage))
1299+
run(gen_test_code(testfile(source_path); coverage=coverage, julia_args=julia_args, test_args=test_args))
12911300
printpkgstyle(ctx, :Testing, pkg.name * " tests passed ")
12921301
catch err
12931302
push!(pkgs_errored, pkg.name)

src/Pkg.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,18 @@ See also [`PackageSpec`](@ref), [`PackageMode`](@ref), [`UpgradeLevel`](@ref).
137137
"""
138138
const update = API.up
139139

140-
141140
"""
142-
Pkg.test(; coverage::Bool=false)
143-
Pkg.test(pkg::Union{String, Vector{String}; coverage::Bool=false)
144-
Pkg.test(pkgs::Union{PackageSpec, Vector{PackageSpec}}; coverage::Bool=false)
141+
Pkg.test(; kwargs...)
142+
Pkg.test(pkg::Union{String, Vector{String}; kwargs...)
143+
Pkg.test(pkgs::Union{PackageSpec, Vector{PackageSpec}}; kwargs...)
144+
145+
**Keyword arguments:**
146+
- `coverage::Bool=false`: enable or disable generation of coverage statistics.
147+
- `julia_args`: vector of `Cmd` passed to the test process.
148+
- `test_args`: vector of test arguments (`ARGS`) available in the test process.
149+
150+
!!! compat "Julia 1.3"
151+
`julia_args` and `test_args` requires at least Julia 1.3.
145152
146153
Run the tests for package `pkg`, or for the current project (which thus needs to be a package) if no
147154
positional argument is given to `Pkg.test`. A package is tested by running its
@@ -162,9 +169,6 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
162169
test = ["Test"]
163170
```
164171
165-
Coverage statistics for the packages may be generated by
166-
passing `coverage=true`. The default behavior is to not run coverage.
167-
168172
The tests are executed in a new process with `check-bounds=yes` and by default `startup-file=no`.
169173
If using the startup file (`~/.julia/config/startup.jl`) is desired, start julia with `--startup-file=yes`.
170174
Inlining of functions during testing can be disabled (for better coverage accuracy)

src/backwards_compatible_isolation.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ end
572572

573573
function backwards_compatibility_for_test(
574574
ctx::Context, pkg::PackageSpec, testfile::String, pkgs_errored::Vector{String},
575-
coverage
575+
coverage; julia_args=Cmd[], test_args=String[]
576576
)
577577
printpkgstyle(ctx, :Testing, pkg.name)
578578
if ctx.preview
@@ -582,8 +582,10 @@ function backwards_compatibility_for_test(
582582
code = """
583583
$(Base.load_path_setup_code(false))
584584
cd($(repr(dirname(testfile))))
585+
append!(empty!(ARGS), $(repr(test_args)))
585586
include($(repr(testfile)))
586587
"""
588+
julia_args_str = vcat([x.exec for x in julia_args]...)
587589
cmd = ```
588590
$(Base.julia_cmd())
589591
--code-coverage=$(coverage ? "user" : "none")
@@ -593,6 +595,7 @@ function backwards_compatibility_for_test(
593595
--inline=$(Bool(Base.JLOptions().can_inline) ? "yes" : "no")
594596
--startup-file=$(Base.JLOptions().startupfile == 1 ? "yes" : "no")
595597
--track-allocation=$(("none", "user", "all")[Base.JLOptions().malloc_log + 1])
598+
$(julia_args_str)
596599
--eval $code
597600
```
598601
run_test = () -> begin

test/api.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,16 @@ end
256256
end end
257257
end
258258

259+
@testset "Pkg.test" begin
260+
temp_pkg_dir() do tmp
261+
copy_test_package(tmp, "TestArguments")
262+
Pkg.activate(joinpath(tmp, "TestArguments"))
263+
# test the old code path (no test/Project.toml)
264+
Pkg.test("TestArguments"; test_args=["a", "b"], julia_args = [`--quiet --check-bounds=no`])
265+
# test new code path
266+
touch(joinpath(tmp, "TestArguments", "test", "Project.toml"))
267+
Pkg.test("TestArguments"; test_args=["a", "b"], julia_args = [`--quiet --check-bounds=no`])
268+
end
269+
end
259270

260271
end # module APITests
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name = "TestArguments"
2+
uuid = "265b0eca-b78c-42af-9929-ddebf847c026"
3+
version = "0.1.0"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TestArguments
2+
3+
end # module
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@assert ARGS == ["a", "b"]
2+
@assert Base.JLOptions().quiet == 1 # --quiet
3+
@assert Base.JLOptions().check_bounds == 2 # overriden default when testing

0 commit comments

Comments
 (0)