Skip to content

Commit

Permalink
Make it easier to add or remove multiple packages w/ Vector{String} (#…
Browse files Browse the repository at this point in the history
…135)

* Make it easier to add or remove multiple packages w/ Vector{String}

* Separate multiple package test into different testitem

* Use jsonlines and cowpy

* Fix typos

* Update test/main.jl
  • Loading branch information
mkitti committed Jul 11, 2024
1 parent 26363a0 commit 2a90c67
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ These functions are intended to be used interactively when the Pkg REPL is not a
(e.g. if you are in a notebook):

- `status()` shows the Conda dependencies of the current project.
- `add(pkg; version="", channel="")` adds/replaces a dependency.
- `rm(pkg)` removes a dependency.
- `add(pkg; version="", channel="")` adds/replaces a dependency or a vector of dependencies.
- `rm(pkg)` removes a dependency or a vector of dependencies.
- `add_channel(channel)` adds a channel.
- `rm_channel(channel)` removes a channel.
- `add_pip(pkg; version="")` adds/replaces a pip dependency.
Expand Down
14 changes: 11 additions & 3 deletions src/deps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,16 @@ function status(; io::IO=stderr)
end
end

function add(pkgs::AbstractVector; resolve=true, file=cur_deps_file(), kw...)
# Do nothing for existing specs
_to_spec(s::Union{PkgSpec,PipPkgSpec,ChannelSpec}; channel="") = s
# Convert strings to PkgSpec
_to_spec(s::AbstractString; channel="") = PkgSpec(s; channel)

function add(pkgs::AbstractVector; channel="", resolve=true, file=cur_deps_file(), kw...)
toml = read_deps(; file)

for pkg in pkgs
add!(toml, pkg)
add!(toml, _to_spec(pkg; channel))
end
write_deps(toml; file)
STATE.resolved = false
Expand Down Expand Up @@ -271,7 +277,7 @@ end
function rm(pkgs::AbstractVector; resolve=true, file=cur_deps_file(), kw...)
toml = read_deps(; file)
for pkg in pkgs
rm!(toml, pkg)
rm!(toml, _to_spec(pkg))
end
write_deps(toml; file)
STATE.resolved = false
Expand Down Expand Up @@ -309,13 +315,15 @@ end

"""
add(pkg; version="", channel="", build="", resolve=true)
add([pkg1, pkg2, ...]; channel="", resolve=true)
Adds a dependency to the current environment.
"""
add(pkg::AbstractString; version="", channel="", build="", kw...) = add(PkgSpec(pkg, version=version, channel=channel, build=build); kw...)

"""
rm(pkg; resolve=true)
rm([pkg1, pkg2, ...]; resolve=true)
Removes a dependency from the current environment.
"""
Expand Down
26 changes: 26 additions & 0 deletions test/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ end
end
end

@testitem "install/remove multiple python packages" begin
include("setup.jl")
CondaPkg.add("python", version="==3.10.2")
# verify package isn't already installed
@test !occursin("jsonlines ", status())
@test !occursin("cowpy", status())
CondaPkg.withenv() do
isnull || @test_throws Exception run(`python -c "import jsonlines"`)
isnull || @test_throws Exception run(`python -c "import cowpy"`)
end

# install multiple packages
CondaPkg.add(["jsonlines", "cowpy"])
@test occursin("jsonlines", status())
@test occursin("cowpy", status())

# remove multiple packages
CondaPkg.rm(["jsonlines", "cowpy"])
@test !occursin("jsonlines ", status())
@test !occursin("cowpy", status())
CondaPkg.withenv() do
isnull || @test_throws Exception run(`python -c "import jsonlines"`)
isnull || @test_throws Exception run(`python -c "import cowpy"`)
end
end

@testitem "pip install/remove python package" begin
@testset "using $kind" for kind in ["pip", "uv"]
include("setup.jl")
Expand Down

0 comments on commit 2a90c67

Please sign in to comment.