Skip to content

Commit

Permalink
Refactor parse_package and add unit tests (#450)
Browse files Browse the repository at this point in the history
* Refactor `parse_package` and add unit tests

* Fold repl unit tests into repl.jl
  • Loading branch information
00vareladavid authored and KristofferC committed Jul 3, 2018
1 parent 0162855 commit 0a7e1ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion stdlib/Pkg/src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ preview_info() = printstyled("───── Preview mode ─────\n"; c

include("generate.jl")

parse_package(pkg) = Pkg.REPLMode.parse_package(pkg; context=Pkg.REPLMode.CMD_ADD)
parse_package(pkg) = Pkg.REPLMode.parse_package(pkg; add_or_develop=true)

add_or_develop(pkg::Union{String, PackageSpec}; kwargs...) = add_or_develop([pkg]; kwargs...)
add_or_develop(pkgs::Vector{String}; kwargs...) = add_or_develop([parse_package(pkg) for pkg in pkgs]; kwargs...)
Expand Down
24 changes: 10 additions & 14 deletions stdlib/Pkg/src/REPLMode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,24 @@ let uuid = raw"(?i)[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}(
global const name_uuid_re = Regex("^$name\\s*=\\s*($uuid)\$")
end

function parse_package(word::AbstractString; context=nothing)::PackageSpec
# packages can be identified through: uuid, name, or name+uuid
# additionally valid for add/develop are: local path, url
function parse_package(word::AbstractString; add_or_develop=false)::PackageSpec
word = replace(word, "~" => homedir())
if context in (CMD_ADD, CMD_DEVELOP) && casesensitive_isdir(word)
pkg = PackageSpec()
pkg.repo = Types.GitRepo(abspath(word))
return pkg
if add_or_develop && casesensitive_isdir(word)
return PackageSpec(Types.GitRepo(abspath(word)))
elseif occursin(uuid_re, word)
return PackageSpec(UUID(word))
elseif occursin(name_re, word)
return PackageSpec(String(match(name_re, word).captures[1]))
elseif occursin(name_uuid_re, word)
m = match(name_uuid_re, word)
return PackageSpec(String(m.captures[1]), UUID(m.captures[2]))
elseif add_or_develop
# Guess it is a url then
return PackageSpec(Types.GitRepo(word))
else
if context in (CMD_ADD, CMD_DEVELOP)
# Guess it is a url then
pkg = PackageSpec()
pkg.repo = Types.GitRepo(word)
return pkg
else
cmderror("`$word` cannot be parsed as a package")
end
cmderror("`$word` cannot be parsed as a package")
end
end

Expand Down Expand Up @@ -633,7 +629,7 @@ function do_add_or_develop!(ctx::Context, tokens::Vector{Token}, cmd::CommandKin
parsed_package = false
token = popfirst!(tokens)
if token isa String
push!(pkgs, parse_package(token; context=CMD_ADD))
push!(pkgs, parse_package(token; add_or_develop=true))
cmd == CMD_DEVELOP && pkgs[end].repo == nothing && (pkgs[end].repo = Types.GitRepo("", ""))
parsed_package = true
elseif token isa VersionRange
Expand Down
5 changes: 5 additions & 0 deletions stdlib/Pkg/src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ PackageSpec(name::AbstractString, version::VersionTypes=VersionSpec()) =
PackageSpec(name, UUID(zero(UInt128)), version)
PackageSpec(uuid::UUID, version::VersionTypes=VersionSpec()) =
PackageSpec("", uuid, version)
function PackageSpec(repo::GitRepo)
pkg = PackageSpec()
pkg.repo = repo
return pkg
end

has_name(pkg::PackageSpec) = !isempty(pkg.name)
has_uuid(pkg::PackageSpec) = pkg.uuid != UUID(zero(UInt128))
Expand Down

0 comments on commit 0a7e1ea

Please sign in to comment.