Skip to content

Commit

Permalink
Add option to create test/Project.toml (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-dG committed Sep 25, 2019
1 parent 7a2a43f commit f6272e1
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
11 changes: 11 additions & 0 deletions src/PkgTemplates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ include("generate.jl")
include("plugin.jl")
include("interactive.jl")

# Run some function with a project activated at the given path.
function with_project(f::Function, path::AbstractString)
proj = current_project()
try
Pkg.activate(path)
f()
finally
proj === nothing ? Pkg.activate() : Pkg.activate(proj)
end
end

end
7 changes: 7 additions & 0 deletions src/generate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ function (t::Template)(pkg::AbstractString)
Pkg.develop(PackageSpec(; path=pkg_dir))
end

manifest = joinpath(pkg_dir, "Manifest.toml")
if t.manifest && !isfile(manifest)
# If the manifest is going to be committed, make sure it's generated.
touch(manifest)
with_project(Pkg.update, pkg_dir)
end

@info "New package is at $pkg_dir"
catch
rm(pkg_dir; recursive=true, force=true)
Expand Down
33 changes: 23 additions & 10 deletions src/plugins/defaults.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const TEST_UUID = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
const TEST_DEP = PackageSpec(; name="Test", uuid=TEST_UUID)
const LICENSE_DIR = normpath(joinpath(@__DIR__, "..", "..", "licenses"))
const LICENSES = Dict(
"MIT" => "MIT \"Expat\" License",
Expand Down Expand Up @@ -143,15 +144,21 @@ function gen_plugin(p::Gitignore, t::Template, pkg_dir::AbstractString)
end

"""
Tests(; file="$(contractuser(default_file("runtests.jl")))" -> Tests
Tests(; file="$(contractuser(default_file("runtests.jl")))", project=false)
Sets up testing for packages.
## Keyword Arguments
- `file::AbstractString`: Template file for the `runtests.jl`.
- `project::Bool`: Whether or not to create a new project for tests (`test/Project.toml`).
See [here](https://julialang.github.io/Pkg.jl/v1/creating-packages/#Test-specific-dependencies-in-Julia-1.2-and-above-1) for more details.
!!! note
Managing test dependencies with `test/Project.toml` is only supported in Julia 1.2 and later.
"""
@with_kw_noshow struct Tests <: BasicPlugin
file::String = default_file("runtests.jl")
project::Bool = false
end

source(p::Tests) = p.file
Expand All @@ -162,21 +169,27 @@ function gen_plugin(p::Tests, t::Template, pkg_dir::AbstractString)
# Do the normal BasicPlugin behaviour to create the test script.
invoke(gen_plugin, Tuple{BasicPlugin, Template, AbstractString}, p, t, pkg_dir)

# Add Test as a test-only dependency.
# Then set up the test depdendency in the chosen way.
f = p.project ? make_test_project : add_test_dependency
f(pkg_dir)
end

# Create a new test project.
function make_test_project(pkg_dir::AbstractString)
with_project(() -> Pkg.add(TEST_DEP), joinpath(pkg_dir, "test"))
end

# Add Test as a test-only dependency.
function add_test_dependency(pkg_dir::AbstractString)
# Add the dependency manually since there's no programmatic way to add to [extras].
path = joinpath(pkg_dir, "Project.toml")
toml = TOML.parsefile(path)
get!(toml, "extras", Dict())["Test"] = TEST_UUID
get!(toml, "targets", Dict())["test"] = ["Test"]
open(io -> TOML.print(io, toml), path, "w")

# Generate the manifest.
# Generate the manifest by updating the project.
# This also ensures that keys in Project.toml are sorted properly.
touch(joinpath(pkg_dir, "Manifest.toml")) # File must exist to be modified by Pkg.
proj = current_project()
try
Pkg.activate(pkg_dir)
Pkg.update()
finally
proj === nothing ? Pkg.activate() : Pkg.activate(proj)
end
with_project(Pkg.update, pkg_dir)
end
13 changes: 5 additions & 8 deletions src/plugins/documenter.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const DOCUMENTER_UUID = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
const DOCUMENTER_DEP = PackageSpec(;
name="Documenter",
uuid="e30172f5-a6a5-5a46-863b-614d45cd2de4",
)

"""
Documenter{T<:Union{TravisCI, GitLabCI, Nothing}}(;
Expand Down Expand Up @@ -98,13 +101,7 @@ function gen_plugin(p::Documenter, t::Template, pkg_dir::AbstractString)
foreach(a -> cp(a, joinpath(assets_dir, basename(a))), p.assets)

# Create the documentation project.
proj = current_project()
try
Pkg.activate(docs_dir)
Pkg.add(PackageSpec(; name="Documenter", uuid=DOCUMENTER_UUID))
finally
proj === nothing ? Pkg.activate() : Pkg.activate(proj)
end
with_project(() -> Pkg.add(DOCUMENTER_DEP), docs_dir)
end

github_pages_url(t::Template, pkg::AbstractString) = "https://$(t.user).github.io/$pkg.jl"
Expand Down
10 changes: 9 additions & 1 deletion src/template.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,21 @@ function Template(::Val{false}; kwargs...)
# which means that default plugins get replaced by user values.
plugins = Dict(typeof(p) => p for p in enabled)

# TODO: It might be nice to offer some kind of warn_incompatible function
# to be optionally implented by plugins instead of hardcoding this case here.
julia = getkw(kwargs, :julia_version)
julia < v"1.2" && haskey(plugins, Tests) && plugins[Tests].project && @warn string(
"The Tests plugin is set to create a project (supported in Julia 1.2 and later)",
"but a Julia version older than 1.2 is supported.",
)

return Template(
authors,
getkw(kwargs, :develop),
dir,
getkw(kwargs, :git),
host,
getkw(kwargs, :julia_version),
julia,
getkw(kwargs, :manifest),
plugins,
getkw(kwargs, :ssh),
Expand Down
4 changes: 0 additions & 4 deletions test/fixtures/AllPlugins/README.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@
[![Build Status](https://api.cirrus-ci.com/github/tester/.jl.svg)](https://cirrus-ci.com/github/tester/AllPlugins.jl)
[![Coverage](https://codecov.io/gh//.jl/branch/master/graph/badge.svg)](https://codecov.io/gh//.jl)
[![Coverage](https://coveralls.io/repos/github//.jl/badge.svg?branch=master)](https://coveralls.io/github//.jl?branch=master)

## Citing

See [`CITATION.bib`](CITATION.bib) for the relevant reference(s).
1 change: 1 addition & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const LICENSE_DIR = contractuser(PT.LICENSE_DIR)
inline_badges: false
Tests:
file: "$DEFAULTS_DIR/runtests.jl"
project: false
"""
@test sprint(show, MIME("text/plain"), tpl(; authors=USER)) == rstrip(expected)
end
Expand Down

0 comments on commit f6272e1

Please sign in to comment.