Skip to content

Commit ce53a7b

Browse files
authored
Merge pull request #1 from JuliaTesting/ox/doit
Create package
2 parents e2f33d2 + c84a676 commit ce53a7b

16 files changed

+497
-591
lines changed

.github/workflows/CI.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
version:
13-
- '1.0'
13+
#- '1.0'
14+
#- '1.1'
15+
#- '1.2'
16+
#- '1.3'
17+
- '1.4'
18+
- '1.5'
1419
- '1.6'
20+
- '1.7-nightly'
1521
- 'nightly'
1622
os:
1723
- ubuntu-latest

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.DS_Store
2+
*.jl.cov
3+
*.jl.*.cov
4+
*.jl.mem
5+
Manifest.toml
6+
docs/build
7+
docs/site
8+
.vscode/settings.json

LICENSE

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
1-
MIT License
1+
The TestEnv.jl package is licensed under the MIT "Expat" License:
22

3-
Copyright (c) 2021 Lyndon White <[email protected]> and contributors
3+
> Copyright (c) 2018-2021: Lyndon White and Malcolm Miller.
4+
>
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in all
13+
> copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
> SOFTWARE.
22+
>
423

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
24+
Some of the code (in particular /src/runner.jl), is based on code from the Julia Base library https://github.com/JuliaLang/julia
1125

12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
1426

15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
27+
> Copyright (c) 2009-2021: Jeff Bezanson, Stefan Karpinski, Viral B. Shah,
28+
> and other contributors:
29+
>
30+
> https://github.com/JuliaLang/julia/contributors
31+
>
32+
> Permission is hereby granted, free of charge, to any person obtaining
33+
> a copy of this software and associated documentation files (the
34+
> "Software"), to deal in the Software without restriction, including
35+
> without limitation the rights to use, copy, modify, merge, publish,
36+
> distribute, sublicense, and/or sell copies of the Software, and to
37+
> permit persons to whom the Software is furnished to do so, subject to
38+
> the following conditions:
39+
>
40+
> The above copyright notice and this permission notice shall be
41+
> included in all copies or substantial portions of the Software.
42+
>
43+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
44+
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45+
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
46+
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
47+
> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
48+
> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
49+
> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Manifest.toml

Lines changed: 0 additions & 110 deletions
This file was deleted.

Project.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
name = "TestEnv"
22
uuid = "1e6cf692-eddd-4d53-88a5-2d735e33781b"
3-
version = "0.1.0"
3+
version = "1.0.0"
44

55
[deps]
66
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
77

88
[compat]
9-
julia = "1"
9+
ChainRulesCore = "=1.0.2"
10+
julia = "1.4"
1011

1112
[extras]
12-
Example = "7876af07-990d-54b4-ab0e-23690620f79a"
13+
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
1314
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1415

1516
[targets]
16-
test = ["Example", "Test"]
17+
test = ["ChainRulesCore", "Test"]

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,30 @@
44
[![Coverage](https://codecov.io/gh/JuliaTesting/TestEnv.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/JuliaTesting/TestEnv.jl)
55
[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)
66
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
7+
8+
9+
This is a 1-function package: `TestEnv.activate()`.
10+
11+
Consider this package has as a test-only dependency of **ChainRulesCore.jl**.
12+
Not a main dependency
13+
14+
```julia
15+
julia> using TestEnv;
16+
17+
julia> TestEnv.activate();
18+
19+
julia> using ChainRulesCore
20+
```
21+
22+
You an also pass in the name of a package, to activate that package and it's test dependencies:
23+
`TestEnv.activate("Javis")` for example would activate Javis.jl's test environment.
24+
25+
Finally you can pass in a function to run in this environment.
26+
```julia
27+
using TestEnv, ReTest
28+
TestEnv.activate("Example") do
29+
retest()
30+
end
31+
```
32+
33+

src/TestEnv.jl

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
11
module TestEnv
22
using Pkg
33
using Pkg: PackageSpec
4-
using Pkg.Types: Context, ensure_resolved, is_project_uuid
4+
using Pkg.Types: Context, ensure_resolved, is_project_uuid, write_env
55
using Pkg.Operations: manifest_info, manifest_resolve!, project_deps_resolve!
66
using Pkg.Operations: project_rel_path, project_resolve!
77

8-
include("runner.jl")
8+
using Pkg.Types: Types, projectfile_path, manifestfile_path
9+
10+
# Version specific imports
11+
@static if VERSION >= v"1.4.0"
12+
using Pkg.Operations: gen_target_project
13+
else
14+
using Pkg.Operations: with_dependencies_loadable_at_toplevel
15+
end
16+
@static if isdefined(Pkg.Operations, :update_package_test!)
17+
using Pkg.Operations: update_package_test!
18+
else
19+
function update_package_test!(pkg, entry)
20+
is_stdlib(pkg.uuid) && return
21+
pkg.version = entry.version
22+
pkg.tree_hash = entry.tree_hash
23+
pkg.repo = entry.repo
24+
pkg.path = entry.path
25+
pkg.pinned = entry.pinned
26+
end
27+
end
28+
29+
@static if VERSION >= v"1.2.0"
30+
using Pkg.Types: is_stdlib
31+
using Pkg.Operations: sandbox, source_path, sandbox_preserve, abspath!
32+
else
33+
using Pkg.Operations: find_installed
34+
using Pkg.Types: SHA1
35+
end
36+
37+
38+
include("exceptions.jl")
39+
40+
include("activate.jl")
41+
include("make_test_env.jl")
42+
include("sandbox.jl")
43+
include("test_dir.jl")
944

1045
end

src/activate.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
TestEnv.activate(f, [pkg])
3+
4+
Activate the test enviroment of `pkg` (defaults to current enviroment), and run `f()`,
5+
then deactivate the enviroment.
6+
This is not useful for many people: Julia is not really designed to have the enviroment
7+
being changed while you are executing code.
8+
However, this *is* useful for anyone doing something like making a alternative to
9+
`Pkg.test()`.
10+
Indeed this is basically extracted from what `Pkg.test()` does.
11+
"""
12+
function activate(f, pkg::AbstractString=current_pkg_name())
13+
pkgspec = deepcopy(PackageSpec(pkg))
14+
ctx = Context()
15+
isinstalled!(ctx, pkgspec) || throw(TestEnvError("$pkg not installed 👻"))
16+
Pkg.instantiate(ctx)
17+
return _sandbox(f, ctx, pkgspec)
18+
end
19+
20+
"""
21+
TestEnv.activate([pkg])
22+
23+
Activate the test enviroment of `pkg` (defaults to current enviroment).
24+
"""
25+
function activate(pkg::AbstractString=current_pkg_name())
26+
pkgspec = deepcopy(PackageSpec(pkg))
27+
ctx = Context()
28+
isinstalled!(ctx, pkgspec) || throw(TestEnvError("$pkg not installed 👻"))
29+
Pkg.instantiate(ctx)
30+
return make_test_env(ctx, pkgspec)
31+
end
32+
33+
current_pkg_name() = Context().env.pkg.name
34+
35+
"""
36+
isinstalled!(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
37+
38+
Checks if the package is installed by using `ensure_resolved` from `Pkg/src/Types.jl`.
39+
This function fails if the package is not installed, but here we wrap it in a
40+
try-catch as we may want to test another package after the one that isn't installed.
41+
42+
For Julia versions V1.4 and later, the first arguments of the Pkg functions used
43+
is of type `Pkg.Types.Context`. For earlier versions, they are of type
44+
`Pkg.Types.EnvCache`.
45+
"""
46+
function isinstalled!(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
47+
var = @static if v"1.4.0" <= VERSION < v"1.7.0-"
48+
ctx
49+
else
50+
ctx.env
51+
end
52+
project_resolve!(var, [pkgspec])
53+
project_deps_resolve!(var, [pkgspec])
54+
55+
manifest = @static if VERSION < v"1.7.0-"
56+
var
57+
else
58+
var.manifest
59+
end
60+
manifest_resolve!(manifest, [pkgspec])
61+
62+
try
63+
ensure_resolved(manifest, [pkgspec])
64+
catch err
65+
err isa MethodError && rethrow()
66+
return false
67+
end
68+
return true
69+
end

src/exceptions.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct TestEnvError <: Exception
2+
msg::AbstractString
3+
end
4+
5+
function Base.showerror(io::IO, ex::TestEnvError, bt; backtrace=true)
6+
printstyled(io, ex.msg, color=Base.error_color())
7+
end

0 commit comments

Comments
 (0)