Skip to content

Commit

Permalink
Merge pull request #36 from KristofferC/kc/tests
Browse files Browse the repository at this point in the history
make a basic functional API and add some very basic tests
  • Loading branch information
KristofferC authored Nov 26, 2017
2 parents 706a600 + b552904 commit 6a9806f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 26 deletions.
56 changes: 56 additions & 0 deletions src/API.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module API

import Pkg3
using Pkg3.Types
using Base.Random.UUID

add(pkg::String) = add([pkg])
add(pkgs::Vector{String}) = add([PackageSpec(pkg) for pkg in pkgs])
add(pkgs::Vector{PackageSpec}) = add(EnvCache(), pkgs)

function add(env::EnvCache, pkgs::Vector{PackageSpec})
project_resolve!(env, pkgs)
registry_resolve!(env, pkgs)
ensure_resolved(env, pkgs, true)
Pkg3.Operations.add(env, pkgs)
end


rm(pkg::String) = rm([pkg])
rm(pkgs::Vector{String}) = rm([PackageSpec(pkg) for pkg in pkgs])
rm(pkgs::Vector{PackageSpec}) = rm(EnvCache(), pkgs)

function rm(env::EnvCache, pkgs::Vector{PackageSpec})
project_resolve!(env, pkgs)
manifest_resolve!(env, pkgs)
Pkg3.Operations.rm(env, pkgs)
end


up(;kwargs...) = up(PackageSpec[], kwargs...)
up(pkg::String; kwargs...) = up([pkg]; kwargs...)
up(pkgs::Vector{String}; kwargs...) = up([PackageSpec(pkg) for pkg in pkgs]; kwargs...)
up(pkgs::Vector{PackageSpec}; kwargs...) = up(EnvCache(), pkgs; kwargs...)

function up(env::EnvCache, pkgs::Vector{PackageSpec};
level::UpgradeLevel=UpgradeLevel(:major), mode::Symbol=:project)
if isempty(pkgs)
if mode == :project
for (name::String, uuid::UUID) in env.project["deps"]
push!(pkgs, PackageSpec(name, uuid, level))
end
elseif mode == :manifest
for (name, infos) in env.manifest, info in infos
uuid = UUID(info["uuid"])
push!(pkgs, PackageSpec(name, uuid, level))
end
end
else
project_resolve!(env, pkgs)
manifest_resolve!(env, pkgs)
ensure_resolved(env, pkgs)
end
Pkg3.Operations.up(env, pkgs)
end

end
2 changes: 1 addition & 1 deletion src/Pkg3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ include("Types.jl")
include("Display.jl")
include("Operations.jl")
include("REPLMode.jl")

include("API.jl")

@enum LoadErrorChoice LOAD_ERROR_QUERY LOAD_ERROR_INSTALL LOAD_ERROR_ERROR

Expand Down
26 changes: 3 additions & 23 deletions src/REPLMode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,7 @@ function do_rm!(env::EnvCache, tokens::Vector{Tuple{Symbol,Vararg{Any}}})
end
isempty(pkgs) &&
cmderror("`rm` – list packages to remove")
project_resolve!(env, pkgs)
manifest_resolve!(env, pkgs)
Pkg3.Operations.rm(env, pkgs)
Pkg3.API.rm(env, pkgs)
end

function do_add!(env::EnvCache, tokens::Vector{Tuple{Symbol,Vararg{Any}}})
Expand All @@ -313,10 +311,7 @@ function do_add!(env::EnvCache, tokens::Vector{Tuple{Symbol,Vararg{Any}}})
cmderror("`add` doesn't take options: --$(join(token[2:end], '='))")
end
end
project_resolve!(env, pkgs)
registry_resolve!(env, pkgs)
ensure_resolved(env, pkgs, true)
Pkg3.Operations.add(env, pkgs)
Pkg3.API.add(env, pkgs)
end

function do_up!(env::EnvCache, tokens::Vector{Tuple{Symbol,Vararg{Any}}})
Expand Down Expand Up @@ -351,22 +346,7 @@ function do_up!(env::EnvCache, tokens::Vector{Tuple{Symbol,Vararg{Any}}})
end
last_token_type = token[1]
end
project_resolve!(env, pkgs)
manifest_resolve!(env, pkgs)
ensure_resolved(env, pkgs)
if isempty(pkgs)
if mode == :project
for (name::String, uuid::UUID) in env.project["deps"]
push!(pkgs, PackageSpec(name, uuid, level))
end
elseif mode == :manifest
for (name, infos) in env.manifest, info in infos
uuid = UUID(info["uuid"])
push!(pkgs, PackageSpec(name, uuid, level))
end
end
end
Pkg3.Operations.up(env, pkgs)
Pkg3.API.up(env, pkgs; level=level, mode=mode)
end

function do_status!(env::EnvCache, tokens::Vector{Tuple{Symbol,Vararg{Any}}})
Expand Down
4 changes: 4 additions & 0 deletions src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ struct CommandError <: Exception
end
cmderror(msg::String...) = throw(CommandError(join(msg)))

function Base.showerror(io::IO, ex::CommandError, bt; backtrace=true)
print_with_color(Base.error_color(), io, string(ex.msg))
end

## type for expressing operations ##

@enum UpgradeLevel fixed=0 patch=1 minor=2 major=3
Expand Down
27 changes: 25 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
using Pkg3
using Base.Test
using Pkg3.Types

function temp_pkg_dir(fn::Function)
local project_path
try
project_path = joinpath(tempdir(), randstring())
withenv("JULIA_ENV" => project_path) do
fn()
end
finally
rm(project_path, recursive=true, force=true)
end
end

temp_pkg_dir() do
Pkg3.API.add("Example")
@eval import Example
Pkg3.API.up()
Pkg3.API.rm("Example")

nonexisting_pkg = randstring(14)
@test_throws CommandError Pkg3.API.add(nonexisting_pkg)
@test_throws CommandError Pkg3.API.up(nonexisting_pkg)
@test_warn "not in project" Pkg3.API.rm(nonexisting_pkg)
end

# write your own tests here
@test 1 == 2

0 comments on commit 6a9806f

Please sign in to comment.