From 004d84ecfa92fb4824720b7369f8531f8ed90862 Mon Sep 17 00:00:00 2001 From: Chris de Graaf Date: Thu, 26 Sep 2019 00:30:16 +0700 Subject: [PATCH] Fix rebase mistakes --- src/generate.jl | 89 ----------- test/tests.jl | 407 ------------------------------------------------ 2 files changed, 496 deletions(-) delete mode 100644 src/generate.jl delete mode 100644 test/tests.jl diff --git a/src/generate.jl b/src/generate.jl deleted file mode 100644 index 0c240e8..0000000 --- a/src/generate.jl +++ /dev/null @@ -1,89 +0,0 @@ -""" - (::Template)(pkg::AbstractString) - -Generate a package named `pkg` from a [`Template`](@ref). -""" -function (t::Template)(pkg::AbstractString) - endswith(pkg, ".jl") && (pkg = pkg[1:end-3]) - pkg_dir = joinpath(t.dir, pkg) - ispath(pkg_dir) && throw(ArgumentError("$pkg_dir already exists")) - repo = nothing - - try - # Create the directory with some boilerplate inside. - Pkg.generate(pkg_dir) - - # Replace the authors field with the template's authors, and add a compat entry. - if !isempty(t.authors) - path = joinpath(pkg_dir, "Project.toml") - toml = TOML.parsefile(path) - toml["authors"] = t.authors - get!(toml, "compat", Dict())["julia"] = compat_version(t.julia_version) - open(io -> TOML.print(io, toml), path, "w") - end - - if t.git - # Initialize the repo, make a commit, and set the remote. - repo = LibGit2.init(pkg_dir) - LibGit2.commit(repo, "Initial commit") - url = if t.ssh - "git@$(t.host):$(t.user)/$pkg.jl.git" - else - "https://$(t.host)/$(t.user)/$pkg.jl" - end - LibGit2.with(GitRemote(repo, "origin", url)) do remote - # TODO: `git pull` still requires some Git branch config. - LibGit2.add_push!(repo, remote, "refs/heads/master") - end - end - - # Generate the files. - foreach(p -> gen_plugin(p, t, pkg_dir), values(t.plugins)) - - if t.git - # Commit the files. - LibGit2.add!(repo, ".") - msg = "Files generated by PkgTemplates" - installed = Pkg.installed() - if haskey(installed, "PkgTemplates") - ver = string(installed["PkgTemplates"]) - msg *= "\n\nPkgTemplates version: $ver" - end - LibGit2.commit(repo, msg) - end - - if t.dev - # Add the new package to the current environment. - 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) - rethrow() - finally - repo === nothing || close(repo) - end -end - -""" - compat_version(v::VersionNumber) -> String - -Format a `VersionNumber` to exclude trailing zero components. -""" -function compat_version(v::VersionNumber) - return if v.patch == 0 && v.minor == 0 - "$(v.major)" - elseif v.patch == 0 - "$(v.major).$(v.minor)" - else - "$(v.major).$(v.minor).$(v.patch)" - end -end diff --git a/test/tests.jl b/test/tests.jl deleted file mode 100644 index 1b3e5ae..0000000 --- a/test/tests.jl +++ /dev/null @@ -1,407 +0,0 @@ -# A dummy GenericPlugin subtype. -struct Foo <: GenericPlugin - gitignore::Vector{AbstractString} - src::Union{AbstractString, Nothing} - dest::AbstractString - 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 -# A dummy CustomPlugin subtype. -struct Bar <: CustomPlugin end -# A dummy Plugin subtype. -struct Baz <: Plugin end -# A Documenter with extra kwargs -struct Qux <: Documenter - assets::Vector{AbstractString} - additional_kwargs::Union{AbstractDict, NamedTuple} -end - -# Various options to be passed into templates. -const me = "christopher-dG" -const test_pkg = "TestPkg" -const fake_path = "/dev/null/this/file/does/not/exist" -const test_file = tempname() -const default_dir = Pkg.devdir() -const gitconfig = GitConfig(joinpath(@__DIR__, "gitconfig")) -const template_text = """ - PKGNAME: {{PKGNAME}} - VERSION: {{VERSION}}} - {{#DOCUMENTER}}Documenter{{/DOCUMENTER}} - {{#CODECOV}}Codecov{{/CODECOV}} - {{#COVERALLS}}Coveralls{{/COVERALLS}} - {{#AFTER}}After{{/AFTER}} - {{#OTHER}}Other{{/OTHER}} - """ -write(test_file, template_text) - -@testset "Template creation" begin - # Checking default field assignments. - t = Template(; authors="foo", user=me) - @test t.user == me - @test t.license == "MIT" - @test t.authors == "foo" - @test t.dir == default_dir - @test t.julia_version == PkgTemplates.default_version - @test !t.ssh - @test !t.manifest - @test isempty(t.plugins) - - # Checking non-default field assignments. - - t = Template(; user=me, license="") - @test t.license == "" - - t = Template(; user=me, license="MPL") - @test t.license == "MPL" - - t = Template(; user=me, authors="Some Guy") - @test t.authors == "Some Guy" - # Vectors of authors should be comma-joined. - t = Template(; user=me, authors=["Guy", "Gal"]) - @test t.authors == "Guy, Gal" - - t = Template(; user=me, dir=test_file) - @test t.dir == abspath(test_file) - if Sys.isunix() # ~ means temporary file on Windows, not $HOME. - # '~' should be replaced by home dir. - t = Template(; user=me, dir="~/$(basename(test_file))") - @test t.dir == joinpath(homedir(), basename(test_file)) - end - - t = Template(; user=me, julia_version=v"0.1.2") - @test t.julia_version == v"0.1.2" - - t = Template(; user=me, ssh=true) - @test t.ssh - - t = Template(; user=me, manifest=true) - @test t.manifest - - # The template should contain whatever plugins you give it. - t = Template(; - user=me, - plugins = [GitHubPages(), TravisCI(), AppVeyor(), Codecov(), Coveralls()], - ) - @test Set(keys(t.plugins)) == Set(map(typeof, values(t.plugins))) == Set( - [GitHubPages, TravisCI, AppVeyor, Codecov, Coveralls] - ) - - # Duplicate plugins should warn. - @test_logs (:warn, r"duplicates") match_mode=:any t = Template(; - user=me, - plugins=[TravisCI(), TravisCI()], - ) - - # If github.user is configured, use that as a default. - if isempty(LibGit2.getconfig("github.user", "")) - @test_throws ArgumentError Template() - else - t = Template() - @test t.user == LibGit2.getconfig("github.user", "") - end - @test_throws ArgumentError Template(; user=me, license="FakeLicense") -end - -@testset "Show methods" begin - pkg_dir = replace(default_dir, homedir() => "~") - ver = PkgTemplates.version_floor(PkgTemplates.default_version) - buf = IOBuffer() - t = Template(; user=me, authors="foo") - show(buf, t) - text = String(take!(buf)) - expected = """ - Template: - → User: $me - → Host: github.com - → License: MIT (foo $(year(today()))) - → Package directory: $pkg_dir - → Minimum Julia version: v$ver - → SSH remote: No - → Add packages to main environment: Yes - → Commit Manifest.toml: No - → Plugins: None - """ - @test text == rstrip(expected) - t = Template( - user=me, - license="", - ssh=true, - manifest=true, - plugins=[ - TravisCI(), - Codecov(), - GitHubPages(), - ], - ) - show(buf, t) - text = String(take!(buf)) - expected = """ - Template: - → User: $me - → Host: github.com - → License: None - → Package directory: $pkg_dir - → Minimum Julia version: v$ver - → SSH remote: Yes - → Add packages to main environment: Yes - → Commit Manifest.toml: Yes - → Plugins: - • Codecov: - → Config file: None - → 3 gitignore entries: "*.jl.cov", "*.jl.*.cov", "*.jl.mem" - • GitHubPages: - → 0 asset files - → 2 gitignore entries: "/docs/build/", "/docs/site/" - • TravisCI: - → Config file: Default - → 0 gitignore entries - """ - @test text == rstrip(expected) -end - -@testset "File generation" begin - t = Template(; - user=me, - license="MPL", - plugins=[Coveralls(), TravisCI(), Codecov(), GitHubPages(), AppVeyor()], - ) - temp_dir = mktempdir() - pkg_dir = joinpath(temp_dir, test_pkg) - - temp_file = tempname() - gen_file(temp_file, "Hello, world") - @test isfile(temp_file) - @test read(temp_file, String) == "Hello, world\n" - rm(temp_file) - - # 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) - @test occursin(join(badges(p, t.user, test_pkg), "\n"), readme) - end - # Check the order of the badges. - @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")) - @test findfirst("coveralls", readme).start < findfirst("baz", readme).start - - # 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) - 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")) - - # 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) - - # 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) - - rm(temp_dir; recursive=true) -end - -@testset "Package generation" begin - t = Template(; user=me) - generate(test_pkg, t; gitconfig=gitconfig) - pkg_dir = joinpath(default_dir, test_pkg) - - # 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")) - # Check the configured remote and branches. - # 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)) - @test LibGit2.url(remote) == "https://github.com/$me/$test_pkg.jl" - @test branches == ["master"] - @test !LibGit2.isdirty(repo) - # 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") - @test LibGit2.url(remote) == "git@github.com:$me/$test_pkg.jl.git" - rm(pkg_dir; recursive=true) - - # 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") - @test LibGit2.url(remote) == "https://gitlab.com/$me/$test_pkg.jl" - rm(pkg_dir; recursive=true) - - # Check that the package ends up in the right directory. - temp_dir = mktempdir() - t = Template(; user=me, dir=temp_dir) - generate(test_pkg, t; gitconfig=gitconfig) - @test isdir(joinpath(temp_dir, test_pkg)) - rm(temp_dir; recursive=true) - - # 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) -end - -@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 "Plugins" begin - t = Template(; user=me) - pkg_dir = joinpath(t.dir, test_pkg) - - # Check badge constructor and formatting. - 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)) - @test isempty(gen_plugin(p, t, test_pkg)) - - p = Baz() - @test isempty(badges(p, me, test_pkg)) - @test isempty(gen_plugin(p, t, test_pkg)) - - include(joinpath("plugins", "travisci.jl")) - include(joinpath("plugins", "appveyor.jl")) - include(joinpath("plugins", "gitlabci.jl")) - include(joinpath("plugins", "cirrusci.jl")) - include(joinpath("plugins", "codecov.jl")) - include(joinpath("plugins", "coveralls.jl")) - include(joinpath("plugins", "githubpages.jl")) - include(joinpath("plugins", "gitlabpages.jl")) - include(joinpath("plugins", "citation.jl")) -end - -@testset "Documenter add kwargs" begin - t = Template(; user=me) - pkg_dir = joinpath(t.dir, test_pkg) - - function check_kwargs(kwargs, warn_str) - 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", - ) - 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", - ) - 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) -end - -include(joinpath("interactive", "interactive.jl"))