PkgTemplates.jl/test/tests.jl

490 lines
17 KiB
Julia
Raw Normal View History

2018-09-24 18:25:40 +00:00
# A dummy GenericPlugin subtype.
struct Foo <: GenericPlugin
gitignore::Vector{AbstractString}
src::Union{AbstractString, Nothing}
dest::AbstractString
2017-08-18 04:06:33 +00:00
badges::Vector{Badge}
view::Dict{String, Any}
function Foo(; config_file=test_file)
new([], @__FILE__, config_file, [Badge("foo", "bar", "baz")], Dict{String, Any}())
end
end
2018-09-24 18:25:40 +00:00
# A dummy CustomPlugin subtype.
2017-08-18 04:06:33 +00:00
struct Bar <: CustomPlugin end
2018-09-24 18:25:40 +00:00
# A dummy Plugin subtype.
2017-08-18 04:06:33 +00:00
struct Baz <: Plugin end
2018-11-05 20:21:02 +00:00
# A Documenter with extra kwargs
struct Qux <: Documenter
assets::Vector{AbstractString}
additional_kwargs::Union{AbstractDict, NamedTuple}
end
2018-09-24 18:25:40 +00:00
# Various options to be passed into templates.
2017-08-18 21:08:48 +00:00
const me = "christopher-dG"
const test_pkg = "TestPkg"
const fake_path = "/dev/null/this/file/does/not/exist"
2017-08-14 22:57:49 +00:00
const test_file = tempname()
const default_dir = Pkg.devdir()
const gitconfig = GitConfig(joinpath(@__DIR__, "gitconfig"))
2017-08-23 17:50:52 +00:00
const template_text = """
2018-11-12 20:51:31 +00:00
PKGNAME: {{PKGNAME}}
VERSION: {{VERSION}}}
{{#DOCUMENTER}}Documenter{{/DOCUMENTER}}
{{#CODECOV}}Codecov{{/CODECOV}}
{{#COVERALLS}}Coveralls{{/COVERALLS}}
{{#AFTER}}After{{/AFTER}}
{{#OTHER}}Other{{/OTHER}}
"""
2017-08-14 18:13:26 +00:00
write(test_file, template_text)
@testset "Template creation" begin
2018-09-24 18:25:40 +00:00
# Checking default field assignments.
2019-05-15 15:03:46 +00:00
t = Template(; authors="foo", user=me)
2017-08-18 21:08:48 +00:00
@test t.user == me
@test t.license == "MIT"
2019-05-15 15:03:46 +00:00
@test t.authors == "foo"
@test t.dir == default_dir
2019-05-14 20:35:34 +00:00
@test t.julia_version == PkgTemplates.default_version()
2018-11-07 20:46:44 +00:00
@test !t.ssh
@test !t.manifest
2017-08-14 18:13:26 +00:00
@test isempty(t.plugins)
2018-09-24 18:25:40 +00:00
# Checking non-default field assignments.
2017-08-23 17:50:52 +00:00
t = Template(; user=me, license="")
@test t.license == ""
t = Template(; user=me, license="MPL")
@test t.license == "MPL"
2017-08-14 18:13:26 +00:00
2017-08-18 21:08:48 +00:00
t = Template(; user=me, authors="Some Guy")
2017-08-14 18:13:26 +00:00
@test t.authors == "Some Guy"
2018-09-24 18:25:40 +00:00
# Vectors of authors should be comma-joined.
2017-08-18 21:08:48 +00:00
t = Template(; user=me, authors=["Guy", "Gal"])
2017-08-14 18:13:26 +00:00
@test t.authors == "Guy, Gal"
2017-08-18 21:08:48 +00:00
t = Template(; user=me, dir=test_file)
@test t.dir == abspath(test_file)
if Sys.isunix() # ~ means temporary file on Windows, not $HOME.
2018-09-24 18:25:40 +00:00
# '~' should be replaced by home dir.
2017-08-25 05:09:49 +00:00
t = Template(; user=me, dir="~/$(basename(test_file))")
@test t.dir == joinpath(homedir(), basename(test_file))
end
2017-08-14 18:13:26 +00:00
2017-08-18 21:08:48 +00:00
t = Template(; user=me, julia_version=v"0.1.2")
2017-08-14 18:13:26 +00:00
@test t.julia_version == v"0.1.2"
2018-11-07 20:46:44 +00:00
t = Template(; user=me, ssh=true)
@test t.ssh
t = Template(; user=me, manifest=true)
@test t.manifest
2018-09-24 18:25:40 +00:00
# The template should contain whatever plugins you give it.
t = Template(;
2017-08-18 21:08:48 +00:00
user=me,
2018-11-05 22:05:30 +00:00
plugins = [GitHubPages(), TravisCI(), AppVeyor(), Codecov(), Coveralls()],
2017-08-14 18:13:26 +00:00
)
@test Set(keys(t.plugins)) == Set(map(typeof, values(t.plugins))) == Set(
2018-11-05 22:05:30 +00:00
[GitHubPages, TravisCI, AppVeyor, Codecov, Coveralls]
)
2017-08-14 18:13:26 +00:00
2018-09-24 18:25:40 +00:00
# Duplicate plugins should warn.
@test_logs (:warn, r"duplicates") match_mode=:any t = Template(;
2017-08-18 21:08:48 +00:00
user=me,
2017-08-14 19:16:04 +00:00
plugins=[TravisCI(), TravisCI()],
)
2017-08-16 22:58:12 +00:00
2018-09-24 18:25:40 +00:00
# If github.user is configured, use that as a default.
if isempty(LibGit2.getconfig("github.user", ""))
2017-08-28 23:28:44 +00:00
@test_throws ArgumentError Template()
else
t = Template()
@test t.user == LibGit2.getconfig("github.user", "")
end
2017-08-28 23:28:44 +00:00
@test_throws ArgumentError Template(; user=me, license="FakeLicense")
2017-08-14 19:16:04 +00:00
end
2017-12-05 20:03:57 +00:00
@testset "Show methods" begin
pkg_dir = replace(default_dir, homedir() => "~")
2019-05-14 20:35:34 +00:00
ver = PkgTemplates.version_floor(PkgTemplates.default_version())
2017-12-05 20:03:57 +00:00
buf = IOBuffer()
2019-05-15 15:03:46 +00:00
t = Template(; user=me, authors="foo")
2017-12-05 20:03:57 +00:00
show(buf, t)
text = String(take!(buf))
expected = """
Template:
User: $me
Host: github.com
2019-05-15 15:03:46 +00:00
License: MIT (foo $(year(today())))
Package directory: $pkg_dir
2019-05-14 20:35:34 +00:00
Minimum Julia version: v$ver
SSH remote: No
2018-11-07 20:46:44 +00:00
Commit Manifest.toml: No
2017-12-05 20:03:57 +00:00
Plugins: None
"""
@test text == rstrip(expected)
t = Template(
user=me,
license="",
ssh=true,
2018-11-07 20:46:44 +00:00
manifest=true,
2017-12-05 20:03:57 +00:00
plugins=[
TravisCI(),
2018-11-05 22:05:30 +00:00
Codecov(),
2017-12-05 20:03:57 +00:00
GitHubPages(),
],
)
show(buf, t)
text = String(take!(buf))
expected = """
Template:
User: $me
Host: github.com
License: None
Package directory: $pkg_dir
2019-05-14 20:35:34 +00:00
Minimum Julia version: v$ver
SSH remote: Yes
2018-11-07 20:46:44 +00:00
Commit Manifest.toml: Yes
2017-12-05 20:03:57 +00:00
Plugins:
2018-11-05 22:05:30 +00:00
Codecov:
2017-12-07 16:43:40 +00:00
Config file: None
3 gitignore entries: "*.jl.cov", "*.jl.*.cov", "*.jl.mem"
2017-12-05 20:03:57 +00:00
GitHubPages:
2017-12-07 16:43:40 +00:00
0 asset files
2 gitignore entries: "/docs/build/", "/docs/site/"
2017-12-05 20:03:57 +00:00
TravisCI:
2017-12-07 16:43:40 +00:00
Config file: Default
0 gitignore entries
2017-12-05 20:03:57 +00:00
"""
@test text == rstrip(expected)
end
2017-08-14 20:58:14 +00:00
@testset "File generation" begin
t = Template(;
2017-08-18 21:08:48 +00:00
user=me,
2017-08-14 20:58:14 +00:00
license="MPL",
2018-11-05 22:05:30 +00:00
plugins=[Coveralls(), TravisCI(), Codecov(), GitHubPages(), AppVeyor()],
2017-08-14 20:58:14 +00:00
)
2017-08-23 17:50:52 +00:00
temp_dir = mktempdir()
pkg_dir = joinpath(temp_dir, test_pkg)
2017-08-14 20:58:14 +00:00
temp_file = tempname()
gen_file(temp_file, "Hello, world")
@test isfile(temp_file)
@test read(temp_file, String) == "Hello, world\n"
2017-08-14 20:58:14 +00:00
rm(temp_file)
2018-09-24 18:25:40 +00:00
# Test the README generation.
@test gen_readme(pkg_dir, t) == ["README.md"]
@test isfile(joinpath(pkg_dir, "README.md"))
readme = readchomp(joinpath(pkg_dir, "README.md"))
rm(joinpath(pkg_dir, "README.md"))
@test occursin("# $test_pkg", readme)
for p in values(t.plugins)
2018-09-19 19:34:38 +00:00
@test occursin(join(badges(p, t.user, test_pkg), "\n"), readme)
2017-08-14 20:58:14 +00:00
end
# Check the order of the badges.
2018-09-19 19:57:21 +00:00
@test something(findfirst("github.io", readme)).start <
something(findfirst("travis", readme)).start <
something(findfirst("appveyor", readme)).start <
something(findfirst("codecov", readme)).start <
something(findfirst("coveralls", readme)).start
# Plugins with badges but not in BADGE_ORDER should appear at the far right side.
t.plugins[Foo] = Foo()
gen_readme(pkg_dir, t)
readme = readchomp(joinpath(pkg_dir, "README.md"))
rm(joinpath(pkg_dir, "README.md"))
2018-11-07 20:46:44 +00:00
@test findfirst("coveralls", readme).start < findfirst("baz", readme).start
2018-09-24 18:25:40 +00:00
# Test the gitignore generation.
@test gen_gitignore(pkg_dir, t) == [".gitignore"]
@test isfile(joinpath(pkg_dir, ".gitignore"))
gitignore = read(joinpath(pkg_dir, ".gitignore"), String)
rm(joinpath(pkg_dir, ".gitignore"))
@test occursin(".DS_Store", gitignore)
@test occursin("Manifest.toml", gitignore)
for p in values(t.plugins)
for entry in p.gitignore
@test occursin(entry, gitignore)
2017-08-14 20:58:14 +00:00
end
end
t = Template(; user=me, manifest=true)
@test gen_gitignore(pkg_dir, t) == [".gitignore", "Manifest.toml"]
gitignore = read(joinpath(pkg_dir, ".gitignore"), String)
@test !occursin("Manifest.toml", gitignore)
rm(joinpath(pkg_dir, ".gitignore"))
2017-08-14 20:58:14 +00:00
2018-09-24 18:25:40 +00:00
# Test the license generation.
@test gen_license(pkg_dir, t) == ["LICENSE"]
@test isfile(joinpath(pkg_dir, "LICENSE"))
license = readchomp(joinpath(pkg_dir, "LICENSE"))
rm(joinpath(pkg_dir, "LICENSE"))
@test occursin(t.authors, license)
@test occursin(read_license(t.license), license)
2018-09-24 18:25:40 +00:00
# Test the test generation.
@test gen_tests(pkg_dir, t) == ["test/"]
@test isfile(joinpath(pkg_dir, "Project.toml"))
project = read(joinpath(pkg_dir, "Project.toml"), String)
@test occursin("[extras]\nTest = ", project)
@test isdir(joinpath(pkg_dir, "test"))
@test isfile(joinpath(pkg_dir, "test", "runtests.jl"))
@test isfile(joinpath(pkg_dir, "Manifest.toml"))
runtests = read(joinpath(pkg_dir, "test", "runtests.jl"), String)
rm(joinpath(pkg_dir, "test"); recursive=true)
@test occursin("using $test_pkg", runtests)
@test occursin("using Test", runtests)
manifest = read(joinpath(pkg_dir, "Manifest.toml"), String)
@test !occursin("[[Test]]", manifest)
2017-08-16 22:58:12 +00:00
2017-08-28 23:28:44 +00:00
rm(temp_dir; recursive=true)
2017-08-14 20:58:14 +00:00
end
2017-08-14 22:57:49 +00:00
@testset "Package generation" begin
t = Template(; user=me)
generate(test_pkg, t; gitconfig=gitconfig)
pkg_dir = joinpath(default_dir, test_pkg)
2018-09-24 18:25:40 +00:00
# Check that the expected files all exist.
@test isfile(joinpath(pkg_dir, "LICENSE"))
@test isfile(joinpath(pkg_dir, "README.md"))
@test isfile(joinpath(pkg_dir, ".gitignore"))
@test isdir(joinpath(pkg_dir, "src"))
@test isfile(joinpath(pkg_dir, "src", "$test_pkg.jl"))
@test isfile(joinpath(pkg_dir, "Project.toml"))
@test isdir(joinpath(pkg_dir, "test"))
@test isfile(joinpath(pkg_dir, "test", "runtests.jl"))
@test isfile(joinpath(pkg_dir, "Manifest.toml"))
2018-09-24 18:25:40 +00:00
# Check the configured remote and branches.
2017-12-07 16:43:40 +00:00
# Note: This test will fail on your system if you've configured Git
# to replace all HTTPS URLs with SSH.
repo = LibGit2.GitRepo(pkg_dir)
remote = LibGit2.get(LibGit2.GitRemote, repo, "origin")
branches = map(b -> LibGit2.shortname(first(b)), LibGit2.GitBranchIter(repo))
2017-08-18 21:08:48 +00:00
@test LibGit2.url(remote) == "https://github.com/$me/$test_pkg.jl"
@test branches == ["master"]
@test !LibGit2.isdirty(repo)
2019-05-14 20:35:34 +00:00
# Check for the [compat] section.
project = read(joinpath(pkg_dir, "Project.toml"), String)
@test occursin("[compat]", project)
@test occursin("julia = " * PkgTemplates.repr_version(t.julia_version), project)
rm(pkg_dir; recursive=true)
# Check that the remote is an SSH URL when we want it to be.
t = Template(; user=me, ssh=true)
generate(t, test_pkg; gitconfig=gitconfig) # Test the reversed-arguments method here.
repo = LibGit2.GitRepo(pkg_dir)
remote = LibGit2.get(LibGit2.GitRemote, repo, "origin")
2017-08-18 21:08:48 +00:00
@test LibGit2.url(remote) == "git@github.com:$me/$test_pkg.jl.git"
rm(pkg_dir; recursive=true)
2018-09-24 18:25:40 +00:00
# Check that the remote is set correctly for non-default hosts.
t = Template(; user=me, host="gitlab.com")
generate(test_pkg, t; gitconfig=gitconfig)
repo = LibGit2.GitRepo(pkg_dir)
remote = LibGit2.get(LibGit2.GitRemote, repo, "origin")
2017-08-18 21:08:48 +00:00
@test LibGit2.url(remote) == "https://gitlab.com/$me/$test_pkg.jl"
rm(pkg_dir; recursive=true)
2018-09-24 18:25:40 +00:00
# Check that the package ends up in the right directory.
2017-08-16 06:47:46 +00:00
temp_dir = mktempdir()
t = Template(; user=me, dir=temp_dir)
generate(test_pkg, t; gitconfig=gitconfig)
2017-08-16 06:47:46 +00:00
@test isdir(joinpath(temp_dir, test_pkg))
rm(temp_dir; recursive=true)
2018-11-07 20:46:44 +00:00
# Check that the Manifest.toml is not commited by default.
t = Template(; user=me)
generate(test_pkg, t; gitconfig=gitconfig)
@test occursin("Manifest.toml", read(joinpath(pkg_dir, ".gitignore"), String))
# I'm not sure this is the "right" way to do this.
repo = GitRepo(pkg_dir)
idx = LibGit2.GitIndex(repo)
@test findall("Manifest.toml", idx) === nothing
rm(pkg_dir; recursive=true)
# And that it is when you tell it to be.
t = Template(; user=me, manifest=true)
generate(test_pkg, t; gitconfig=gitconfig)
@test !occursin("Manifest.toml", read(joinpath(pkg_dir, ".gitignore"), String))
# I'm not sure this is the "right" way to do this.
repo = GitRepo(pkg_dir)
idx = LibGit2.GitIndex(repo)
@test findall("Manifest.toml", idx) !== nothing
rm(pkg_dir; recursive=true)
# Check that the created package ends up developed in the current environment.
temp_dir = mktempdir()
Pkg.activate(temp_dir)
t = Template(; user=me)
generate(test_pkg, t; gitconfig=gitconfig)
@test haskey(Pkg.installed(), test_pkg)
rm(pkg_dir; recursive=true)
Pkg.activate()
rm(temp_dir; recursive=true)
2017-08-14 22:57:49 +00:00
end
2018-11-09 20:51:51 +00:00
@testset "Git-less template creation" begin
if isempty(LibGit2.getconfig("user.name", ""))
2018-11-12 20:51:31 +00:00
@test_logs Template(; user=me, git=false)
2018-11-09 20:51:51 +00:00
end
end
2018-11-07 20:20:58 +00:00
@testset "Git-less package generation" begin
t = Template(; user=me)
generate(test_pkg, t; git=false)
@test !ispath(joinpath(t.dir, ".git"))
@test !isfile(joinpath(t.dir, ".gitignore"))
end
@testset "Version floor" begin
@test version_floor(v"1.0.0") == "1.0"
@test version_floor(v"1.0.1") == "1.0"
@test version_floor(v"1.0.1-pre") == "1.0"
@test version_floor(v"1.0.0-pre") == "1.0-"
end
@testset "Mustache substitution" begin
view = Dict{String, Any}()
text = substitute(template_text, view)
@test !occursin("PKGNAME: $test_pkg", text)
@test !occursin("Documenter", text)
2018-11-05 22:05:30 +00:00
@test !occursin("Codecov", text)
@test !occursin("Coveralls", text)
@test !occursin("After", text)
@test !occursin("Other", text)
view["PKGNAME"] = test_pkg
view["OTHER"] = true
text = substitute(template_text, view)
@test occursin("PKGNAME: $test_pkg", text)
@test occursin("Other", text)
2017-08-18 21:08:48 +00:00
t = Template(; user=me)
view["OTHER"] = false
text = substitute(template_text, t; view=view)
@test occursin("PKGNAME: $test_pkg", text)
@test occursin("VERSION: $(t.julia_version.major).$(t.julia_version.minor)", text)
@test !occursin("Documenter", text)
@test !occursin("After", text)
@test !occursin("Other", text)
t.plugins[GitHubPages] = GitHubPages()
text = substitute(template_text, t; view=view)
@test occursin("Documenter", text)
@test occursin("After", text)
empty!(t.plugins)
2018-11-05 22:05:30 +00:00
t.plugins[Codecov] = Codecov()
text = substitute(template_text, t; view=view)
2018-11-05 22:05:30 +00:00
@test occursin("Codecov", text)
@test occursin("After", text)
empty!(t.plugins)
2017-08-23 17:50:52 +00:00
t.plugins[Coveralls] = Coveralls()
text = substitute(template_text, t; view=view)
@test occursin("Coveralls", text)
@test occursin("After", text)
empty!(t.plugins)
view["OTHER"] = true
text = substitute(template_text, t; view=view)
@test occursin("Other", text)
end
@testset "License display" begin
2018-09-24 18:25:40 +00:00
io = IOBuffer()
available_licenses(io)
licenses = String(take!(io))
show_license(io, "MIT")
mit = String(take!(io))
2018-09-24 18:25:40 +00:00
# Check that all licenses are included in the display.
for (short, long) in LICENSES
@test occursin("$short: $long", licenses)
2017-08-14 22:57:49 +00:00
end
@test strip(mit) == strip(read_license("MIT"))
@test strip(read_license("MIT")) == strip(read(joinpath(LICENSE_DIR, "MIT"), String))
@test_throws ArgumentError read_license(fake_path)
2018-09-24 18:25:40 +00:00
# Check that all licenses included with the package are displayed.
for license in readdir(LICENSE_DIR)
@test haskey(LICENSES, license)
end
2018-09-24 18:25:40 +00:00
# Check that all licenses displayed are included with the package.
@test length(readdir(LICENSE_DIR)) == length(LICENSES)
2017-08-14 22:57:49 +00:00
end
2017-08-16 22:58:12 +00:00
2017-10-02 01:43:03 +00:00
@testset "Plugins" begin
t = Template(; user=me)
2018-09-28 20:30:10 +00:00
pkg_dir = joinpath(t.dir, test_pkg)
2017-10-02 01:43:03 +00:00
2018-09-24 18:25:40 +00:00
# Check badge constructor and formatting.
2017-10-02 01:43:03 +00:00
badge = Badge("A", "B", "C")
@test badge.hover == "A"
@test badge.image == "B"
@test badge.link == "C"
@test format(badge) == "[![A](B)](C)"
p = Bar()
@test isempty(badges(p, me, test_pkg))
2018-09-28 20:30:10 +00:00
@test isempty(gen_plugin(p, t, test_pkg))
2017-10-02 01:43:03 +00:00
p = Baz()
@test isempty(badges(p, me, test_pkg))
2018-09-28 20:30:10 +00:00
@test isempty(gen_plugin(p, t, test_pkg))
2017-10-02 01:43:03 +00:00
include(joinpath("plugins", "travisci.jl"))
include(joinpath("plugins", "appveyor.jl"))
2017-10-02 03:21:07 +00:00
include(joinpath("plugins", "gitlabci.jl"))
2017-10-02 01:43:03 +00:00
include(joinpath("plugins", "codecov.jl"))
include(joinpath("plugins", "coveralls.jl"))
include(joinpath("plugins", "githubpages.jl"))
2019-02-01 03:31:30 +00:00
include(joinpath("plugins", "gitlabpages.jl"))
2019-05-14 19:31:34 +00:00
include(joinpath("plugins", "citation.jl"))
2017-10-02 01:43:03 +00:00
end
2018-11-02 22:23:40 +00:00
@testset "Documenter add kwargs" begin
t = Template(; user=me)
pkg_dir = joinpath(t.dir, test_pkg)
function check_kwargs(kwargs, warn_str)
2018-11-05 22:11:23 +00:00
p = Qux([], kwargs)
@test_logs (:warn, warn_str) gen_plugin(p, t, test_pkg)
make = readchomp(joinpath(pkg_dir, "docs", "make.jl"))
@test occursin("\n stringarg=\"string\",\n", make)
@test occursin("\n strict=true,\n", make)
@test occursin("\n checkdocs=:none,\n", make)
@test !occursin("format=:markdown", make)
@test occursin("format=Documenter.HTML()", make)
rm(pkg_dir; recursive=true)
end
# Test with string kwargs
kwargs = Dict("checkdocs" => :none,
"strict" => true,
"format" => :markdown,
"stringarg" => "string",
2018-11-02 22:23:40 +00:00
)
warn_str = "Ignoring predefined Documenter kwargs \"format\" from additional kwargs"
check_kwargs(kwargs, warn_str)
kwargs = Dict(:checkdocs => :none,
:strict => true,
:format => :markdown,
:stringarg => "string",
2018-11-02 22:23:40 +00:00
)
warn_str = "Ignoring predefined Documenter kwargs :format from additional kwargs"
check_kwargs(kwargs, warn_str)
kwargs = (checkdocs = :none, strict = true, format = :markdown, stringarg = "string")
warn_str = "Ignoring predefined Documenter kwargs :format from additional kwargs"
check_kwargs(kwargs, warn_str)
2018-11-02 22:23:40 +00:00
end
2018-11-05 20:21:02 +00:00
include(joinpath("interactive", "interactive.jl"))
2017-08-16 22:58:12 +00:00
rm(test_file)