PkgTemplates.jl/test/tests.jl

480 lines
16 KiB
Julia
Raw Normal View History

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
2017-08-18 04:06:33 +00:00
struct Bar <: CustomPlugin end
struct Baz <: Plugin end
2017-08-18 21:08:48 +00:00
const me = "christopher-dG"
2017-08-23 17:50:52 +00:00
const gitconfig = Dict(
2017-08-14 18:13:26 +00:00
"user.name" => "Tester McTestFace",
"user.email" => "email@web.site",
"github.user" => "TesterMcTestFace",
2017-08-14 18:13:26 +00:00
)
const test_pkg = "TestPkg"
const fake_path = string(hash("/this/file/does/not/exist"); base=2)
2017-08-14 22:57:49 +00:00
const test_file = tempname()
const default_dir = PkgTemplates.dev_dir()
2017-08-23 17:50:52 +00:00
const template_text = """
PKGNAME: {{PKGNAME}}
VERSION: {{VERSION}}}
2017-08-14 18:13:26 +00:00
{{#DOCUMENTER}}Documenter{{/DOCUMENTER}}
{{#CODECOV}}CodeCov{{/CODECOV}}
2017-08-23 17:50:52 +00:00
{{#COVERALLS}}Coveralls{{/COVERALLS}}
2017-08-14 18:13:26 +00:00
{{#AFTER}}After{{/AFTER}}
{{#OTHER}}Other{{/OTHER}}
"""
write(test_file, template_text)
@testset "Template creation" begin
2017-08-18 21:08:48 +00:00
t = Template(; user=me)
@test t.user == me
@test t.license == "MIT"
2017-08-14 18:13:26 +00:00
@test t.years == string(Dates.year(Dates.today()))
@test t.authors == LibGit2.getconfig("user.name", "")
@test t.dir == default_dir
2017-08-14 18:13:26 +00:00
@test t.julia_version == VERSION
2017-08-23 17:50:52 +00:00
@test isempty(t.gitconfig)
2017-08-14 18:13:26 +00:00
@test isempty(t.plugins)
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, years=2014)
2017-08-14 18:13:26 +00:00
@test t.years == "2014"
2017-08-18 21:08:48 +00:00
t = Template(user=me, years="2014-2015")
2017-08-14 18:13:26 +00:00
@test t.years == "2014-2015"
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"
2017-08-16 22:58:12 +00:00
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.
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-10-06 13:04:51 +00:00
t = Template(; user=me, precompile=false)
@test !t.precompile
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"
2017-08-18 21:08:48 +00:00
t = Template(; user=me, requirements=["$test_pkg 0.1"])
2017-08-18 07:08:03 +00:00
@test t.requirements == ["$test_pkg 0.1"]
@test_logs (:warn, r".+") t = Template(; user=me, requirements=[test_pkg, test_pkg])
2017-08-18 07:08:03 +00:00
@test t.requirements == [test_pkg]
2017-08-28 23:28:44 +00:00
@test_throws ArgumentError Template(;
2017-08-18 21:08:48 +00:00
user=me,
2017-08-18 07:08:03 +00:00
requirements=[test_pkg, "$test_pkg 0.1"]
)
2017-08-23 17:50:52 +00:00
t = Template(; user=me, gitconfig=gitconfig)
@test t.gitconfig == gitconfig
2017-08-14 18:13:26 +00:00
2017-08-23 17:50:52 +00:00
t = Template(; user=me, gitconfig=gitconfig)
@test t.authors == gitconfig["user.name"]
2017-08-14 20:58:14 +00:00
2017-08-23 17:50:52 +00:00
t = Template(; gitconfig=gitconfig)
@test t.user == gitconfig["github.user"]
@test t.authors == gitconfig["user.name"]
t = Template(;
2017-08-18 21:08:48 +00:00
user=me,
plugins = [GitHubPages(), TravisCI(), AppVeyor(), CodeCov(), Coveralls()],
2017-08-14 18:13:26 +00:00
)
@test Set(keys(t.plugins)) == Set(
[GitHubPages, TravisCI, AppVeyor, CodeCov, Coveralls]
)
@test Set(values(t.plugins)) == Set(
[GitHubPages(), TravisCI(), AppVeyor(), CodeCov(), Coveralls()]
)
2017-08-14 18:13:26 +00:00
@test_logs (:warn, r".+") 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
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
if get(ENV, "TRAVIS_OS_NAME", "") != "osx"
2017-10-02 03:21:35 +00:00
include(joinpath("interactive", "interactive.jl"))
2017-10-06 13:08:35 +00:00
@testset "Interactive plugin creation" begin
include(joinpath("interactive", "plugins.jl"))
end
2017-10-02 03:21:35 +00:00
else
info("Skipping tests that require TerminalMenus")
end
2017-10-02 03:21:35 +00:00
2017-12-05 20:03:57 +00:00
@testset "Show methods" begin
pkg_dir = replace(default_dir, homedir() => "~")
2017-12-05 20:03:57 +00:00
buf = IOBuffer()
t = Template(; user=me, gitconfig=gitconfig)
show(buf, t)
text = String(take!(buf))
expected = """
Template:
User: $me
Host: github.com
2017-12-07 16:43:40 +00:00
License: MIT ($(gitconfig["user.name"]) $(Dates.year(now())))
Package directory: $pkg_dir
2017-12-07 16:43:40 +00:00
Precompilation enabled: Yes
2017-12-12 15:15:55 +00:00
Minimum Julia version: v$(PkgTemplates.version_floor())
2017-12-07 16:43:40 +00:00
0 package requirements
2017-12-05 20:03:57 +00:00
Git configuration options:
github.user = $(gitconfig["github.user"])
user.email = $(gitconfig["user.email"])
user.name = $(gitconfig["user.name"])
Plugins: None
"""
@test text == rstrip(expected)
t = Template(
user=me,
license="",
requirements=["Foo", "Bar"],
gitconfig=gitconfig,
plugins=[
TravisCI(),
CodeCov(),
GitHubPages(),
],
)
show(buf, t)
text = String(take!(buf))
expected = """
Template:
User: $me
Host: github.com
License: None
Package directory: $pkg_dir
2017-12-07 16:43:40 +00:00
Precompilation enabled: Yes
2017-12-12 15:15:55 +00:00
Minimum Julia version: v$(PkgTemplates.version_floor())
2017-12-07 16:43:40 +00:00
2 package requirements: Bar, Foo
2017-12-05 20:03:57 +00:00
Git configuration options:
github.user = $(gitconfig["github.user"])
user.email = $(gitconfig["user.email"])
user.name = $(gitconfig["user.name"])
Plugins:
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",
2017-08-18 07:08:03 +00:00
requirements=[test_pkg],
2017-08-23 17:50:52 +00:00
gitconfig=gitconfig,
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)
2017-08-23 17:50:52 +00:00
@test gen_readme(temp_dir, test_pkg, 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.
@test search(readme, "github.io").start <
search(readme, "travis").start <
search(readme, "appveyor").start <
search(readme, "codecov").start <
search(readme, "coveralls").start
# Plugins with badges but not in BADGE_ORDER should appear at the far right side.
t.plugins[Foo] = Foo()
2017-08-23 17:50:52 +00:00
gen_readme(temp_dir, test_pkg, t)
readme = readchomp(joinpath(pkg_dir, "README.md"))
rm(joinpath(pkg_dir, "README.md"))
@test search(readme, "coveralls").start < search(readme, "baz").start
2017-08-23 17:50:52 +00:00
@test gen_gitignore(temp_dir, test_pkg, 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)
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
2017-08-23 17:50:52 +00:00
@test gen_license(temp_dir, test_pkg, 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(t.years, license)
@test occursin(read_license(t.license), license)
2017-08-23 17:50:52 +00:00
@test gen_entrypoint(temp_dir, test_pkg, t) == ["src/"]
@test isdir(joinpath(pkg_dir, "src"))
@test isfile(joinpath(pkg_dir, "src", "$test_pkg.jl"))
entrypoint = readchomp(joinpath(pkg_dir, "src", "$test_pkg.jl"))
rm(joinpath(pkg_dir, "src"); recursive=true)
@test occursin("__precompile__()", entrypoint)
@test occursin("module $test_pkg", entrypoint)
2017-10-06 13:04:51 +00:00
t2 = Template(; user=me, precompile=false)
gen_entrypoint(temp_dir, test_pkg, t2)
entrypoint = readchomp(joinpath(pkg_dir, "src", "$test_pkg.jl"))
@test !occursin("__precompile__()", entrypoint)
@test occursin("module $test_pkg", entrypoint)
2017-10-06 13:04:51 +00:00
rm(joinpath(pkg_dir, "src"); recursive=true)
2017-08-23 17:50:52 +00:00
@test gen_require(temp_dir, test_pkg, t) == ["REQUIRE"]
@test isfile(joinpath(pkg_dir, "REQUIRE"))
vf = version_floor(t.julia_version)
2017-08-18 07:08:03 +00:00
@test readchomp(joinpath(pkg_dir, "REQUIRE")) == "julia $vf\n$test_pkg"
rm(joinpath(pkg_dir, "REQUIRE"))
2017-08-23 17:50:52 +00:00
@test gen_tests(temp_dir, test_pkg, t) == ["test/"]
@test isdir(joinpath(pkg_dir, "test"))
@test isfile(joinpath(pkg_dir, "test", "runtests.jl"))
runtests = readchomp(joinpath(pkg_dir, "test", "runtests.jl"))
rm(joinpath(pkg_dir, "test"); recursive=true)
@test occursin("using $test_pkg", runtests)
@test occursin("using Base.Test", runtests)
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
2017-08-23 17:50:52 +00:00
t = Template(; user=me, gitconfig=gitconfig)
generate(test_pkg, t)
pkg_dir = joinpath(default_dir, test_pkg)
@test isfile(joinpath(pkg_dir, "LICENSE"))
@test isfile(joinpath(pkg_dir, "README.md"))
@test isfile(joinpath(pkg_dir, "REQUIRE"))
@test isfile(joinpath(pkg_dir, ".gitignore"))
@test isdir(joinpath(pkg_dir, "src"))
@test isfile(joinpath(pkg_dir, "src", "$test_pkg.jl"))
@test isdir(joinpath(pkg_dir, "test"))
@test isfile(joinpath(pkg_dir, "test", "runtests.jl"))
repo = LibGit2.GitRepo(pkg_dir)
remote = LibGit2.get(LibGit2.GitRemote, repo, "origin")
2017-12-12 15:15:55 +00:00
branches = map(b -> LibGit2.shortname(first(b)), LibGit2.GitBranchIter(repo))
2017-08-23 17:50:52 +00:00
@test LibGit2.getconfig(repo, "user.name", "") == gitconfig["user.name"]
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.
2017-08-18 21:08:48 +00:00
@test LibGit2.url(remote) == "https://github.com/$me/$test_pkg.jl"
@test in("master", branches)
@test !in("gh-pages", branches)
@test !LibGit2.isdirty(repo)
rm(pkg_dir; recursive=true)
2017-09-21 14:51:19 +00:00
generate(t, test_pkg; ssh=true) # Test the reversed-arguments method.
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)
2017-08-23 17:50:52 +00:00
t = Template(; user=me, host="gitlab.com", gitconfig=gitconfig)
generate(test_pkg, t)
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)
2017-08-16 06:47:46 +00:00
temp_dir = mktempdir()
2017-08-23 17:50:52 +00:00
t = Template(; user=me, dir=temp_dir, gitconfig=gitconfig)
2017-08-16 06:47:46 +00:00
generate(test_pkg, t)
@test isdir(joinpath(temp_dir, test_pkg))
rm(temp_dir; recursive=true)
t = Template(;
2017-08-18 21:08:48 +00:00
user=me,
2017-08-23 17:50:52 +00:00
license="",
gitconfig=gitconfig,
plugins=[AppVeyor(), GitHubPages(), Coveralls(), CodeCov(), TravisCI()],
)
generate(test_pkg, t)
@test isdir(pkg_dir)
@test !isfile(joinpath(pkg_dir, "LICENSE"))
@test isfile(joinpath(pkg_dir, ".travis.yml"))
@test isfile(joinpath(pkg_dir, ".appveyor.yml"))
@test isdir(joinpath(pkg_dir, "docs"))
@test isfile(joinpath(pkg_dir, "docs", "make.jl"))
@test isdir(joinpath(pkg_dir, "docs", "src"))
@test isfile(joinpath(pkg_dir, "docs", "src", "index.md"))
repo = LibGit2.GitRepo(pkg_dir)
2017-08-23 17:50:52 +00:00
@test LibGit2.getconfig(repo, "user.name", "") == gitconfig["user.name"]
2017-12-12 15:15:55 +00:00
branches = map(b -> LibGit2.shortname(first(b)), LibGit2.GitBranchIter(repo))
@test in("gh-pages", branches)
2017-08-14 22:57:49 +00:00
@test !LibGit2.isdirty(repo)
rm(pkg_dir; recursive=true)
2017-08-14 22:57:49 +00:00
mkdir(pkg_dir)
@test_throws ArgumentError generate(test_pkg, t)
generate(test_pkg, t; force=true)
@test isfile(joinpath(pkg_dir, "README.md"))
rm(pkg_dir; recursive=true)
temp_file, io = mktemp()
close(io)
2017-08-31 14:51:28 +00:00
temp_dir = mktempdir()
t = Template(; user=me, dir=temp_file)
@test_logs (:warn, r".+") match_mode=:any generate(test_pkg, t; backup_dir=temp_dir)
2017-08-31 14:51:28 +00:00
rm(temp_dir; recursive=true)
temp_dir = mktempdir()
t = Template(; user=me, dir=joinpath(temp_file, "dir"))
@test_logs (:warn, r".+") match_mode=:any generate(test_pkg, t; backup_dir=temp_dir)
2017-08-31 14:51:28 +00:00
rm(temp_dir; recursive=true)
2017-08-28 23:28:44 +00:00
rm(temp_file)
2017-08-23 17:50:52 +00:00
t = Template(; user=me, gitconfig=gitconfig, plugins=[GitHubPages()])
generate(test_pkg, t)
readme = read(joinpath(pkg_dir, "README.md"), String)
index = read(joinpath(pkg_dir, "docs", "src", "index.md"), String)
@test readme == index
rm(pkg_dir; recursive=true)
2017-08-14 22:57:49 +00:00
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)
@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)
t.plugins[CodeCov] = CodeCov()
text = substitute(template_text, t; view=view)
@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-19 19:25:09 +00:00
old_stdout = stdout
out_read, out_write = redirect_stdout()
2017-08-23 17:50:52 +00:00
available_licenses()
licenses = String(readavailable(out_read))
show_license("MIT")
mit = String(readavailable(out_read))
close(out_write)
close(out_read)
redirect_stdout(old_stdout)
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)
for license in readdir(LICENSE_DIR)
@test haskey(LICENSES, license)
end
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
user = gitconfig["github.user"]
t = Template(; user=me)
temp_dir = mktempdir()
pkg_dir = joinpath(temp_dir, test_pkg)
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, user, test_pkg))
@test isempty(gen_plugin(p, t, temp_dir, test_pkg))
p = Baz()
@test isempty(badges(p, user, test_pkg))
@test isempty(gen_plugin(p, t, temp_dir, test_pkg))
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"))
rm(temp_dir; recursive=true)
end
2017-08-16 22:58:12 +00:00
rm(test_file)