Skip to content

Commit

Permalink
Address further comments, add a few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
staticfloat committed Jun 8, 2016
1 parent 2647454 commit 0ddade3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Returns a list of all installed packages that are out of date as a `Vector{BrewP
"""
function outdated()
json_str = brewchomp(`outdated --json=v1`)
if length(json_str) == 0
if isempty(json_str)
return BrewPkg[]
end
brew_outdated = JSON.parse(json_str)
Expand Down Expand Up @@ -151,6 +151,7 @@ function upgrade()
end
end

const json_cache = Dict{String,Dict{AbstractString,Any}}()
"""
json(names::Vector{AbstractString})
Expand All @@ -163,7 +164,6 @@ Note that running `brew info --json=v1` is somewhat expensive, so we cache the
results in a global dictionary, and batching larger requests with this function
similarly increases performance.
"""
const json_cache = Dict{String,Dict{AbstractString,Any}}()
function json{T<:AbstractString}(names::Vector{T})
# First, normalize all names
names = String[normalize_name(n) for n in names]
Expand All @@ -182,15 +182,14 @@ function json{T<:AbstractString}(names::Vector{T})
end

# Now ask for all these names if we have any
if length(ask_names) > 0
if !isempty(ask_names)
try
jdata = JSON.parse(brewchomp(Cmd(String["info", "--json=v1", ask_names...])))
for idx in 1:length(jdata)
json_cache[ask_names[idx]] = jdata[idx]
objs[ask_names[idx]] = jdata[idx]
end
catch
rethrow()
throw(ArgumentError("`brew info` failed for $(name)!"))
end
end
Expand Down Expand Up @@ -304,7 +303,7 @@ end
deps_tree(name::AbstractString)
Return a dictionary mapping every dependency (both direct and indirect) of `name`
to a `Vector{String}` of all of its dependencies. Used in `deps_sorted()`.
to a `Vector{BrewPkg}` of all of its dependencies. Used in `deps_sorted()`.
"""
function deps_tree(name::AbstractString)
# First, get all the knowledge we need about dependencies
Expand Down Expand Up @@ -336,24 +335,23 @@ end
deps_tree(pkg::BrewPkg)
Return a dictionary mapping every dependency (both direct and indirect) of `pkg`
to a `Vector{String}` of all of its dependencies. Used in `deps_sorted()`.
to a `Vector{BrewPkg}` of all of its dependencies. Used in `deps_sorted()`.
"""
function deps_tree(pkg::BrewPkg)
return deps_tree(pkg.name)
end

function find_depidx(sorted_deps::Vector{BrewPkg}, name::AbstractString)
for idx in 1:length(sorted_deps)
if sorted_deps[idx].name == name
return idx
end
end
return 0
end
"""
insert_after_dependencies(tree::Dict, sorted_deps::Vector{BrewPkg}, name::String)
Given a mapping from names to dependencies in `tree`, and a list of sorted
dependencies in `sorted_deps`, insert a new dependency `name` into `sorted_deps`
after all dependencies of `name`. If a dependency of `name` is not already in
`sorted_deps`, then recursively add that dependency as well.
"""
function insert_after_dependencies(tree::Dict, sorted_deps::Vector{BrewPkg}, name::AbstractString)
# First off, are we already in sorted_deps? If so, back out!
self_idx = find_depidx(sorted_deps, name)
self_idx = findfirst(x -> (x.name == name), sorted_deps)
if self_idx != 0
return self_idx
end
Expand All @@ -363,7 +361,7 @@ function insert_after_dependencies(tree::Dict, sorted_deps::Vector{BrewPkg}, nam
# Iterate over all dependencies
for dpkg in tree[name]
# Is this dependency already in the sorted_deps?
idx = find_depidx(sorted_deps, dpkg.name)
idx = findfirst(x -> (x.name == dpkg.name), sorted_deps)

# If the dependency is not already in this list, then recurse into it!
if idx == 0
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,20 @@ println(" installed to: $(Homebrew.prefix(pkgconfig))")
@test Homebrew.linked(pkgconfig) == true

@test Homebrew.deps("pkg-config") == []
@test Homebrew.deps(pkgconfig) == []
@test Homebrew.deps("nettle") == [Homebrew.info("gmp")]
@test Homebrew.deps(Homebrew.info("nettle")) == [Homebrew.info("gmp")]

# Run through our sorted deps routines, ensuring that everything is sorted
sortdeps = Homebrew.deps_sorted("pango")
for idx in 1:length(sortdeps)
for dep in Homebrew.deps(sortdeps[idx])
depidx = findfirst(x -> (x.name == dep.name), sortdeps)
@test depidx != 0
@test depidx < idx
end
end


# Can't really do anything useful with these, but can at least run them to ensure they work
Homebrew.outdated()
Expand Down

0 comments on commit 0ddade3

Please sign in to comment.