Skip to content

Commit 2a90c67

Browse files
authored
Make it easier to add or remove multiple packages w/ Vector{String} (#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
1 parent 26363a0 commit 2a90c67

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ These functions are intended to be used interactively when the Pkg REPL is not a
5353
(e.g. if you are in a notebook):
5454

5555
- `status()` shows the Conda dependencies of the current project.
56-
- `add(pkg; version="", channel="")` adds/replaces a dependency.
57-
- `rm(pkg)` removes a dependency.
56+
- `add(pkg; version="", channel="")` adds/replaces a dependency or a vector of dependencies.
57+
- `rm(pkg)` removes a dependency or a vector of dependencies.
5858
- `add_channel(channel)` adds a channel.
5959
- `rm_channel(channel)` removes a channel.
6060
- `add_pip(pkg; version="")` adds/replaces a pip dependency.

src/deps.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,16 @@ function status(; io::IO=stderr)
212212
end
213213
end
214214

215-
function add(pkgs::AbstractVector; resolve=true, file=cur_deps_file(), kw...)
215+
# Do nothing for existing specs
216+
_to_spec(s::Union{PkgSpec,PipPkgSpec,ChannelSpec}; channel="") = s
217+
# Convert strings to PkgSpec
218+
_to_spec(s::AbstractString; channel="") = PkgSpec(s; channel)
219+
220+
function add(pkgs::AbstractVector; channel="", resolve=true, file=cur_deps_file(), kw...)
216221
toml = read_deps(; file)
222+
217223
for pkg in pkgs
218-
add!(toml, pkg)
224+
add!(toml, _to_spec(pkg; channel))
219225
end
220226
write_deps(toml; file)
221227
STATE.resolved = false
@@ -271,7 +277,7 @@ end
271277
function rm(pkgs::AbstractVector; resolve=true, file=cur_deps_file(), kw...)
272278
toml = read_deps(; file)
273279
for pkg in pkgs
274-
rm!(toml, pkg)
280+
rm!(toml, _to_spec(pkg))
275281
end
276282
write_deps(toml; file)
277283
STATE.resolved = false
@@ -309,13 +315,15 @@ end
309315

310316
"""
311317
add(pkg; version="", channel="", build="", resolve=true)
318+
add([pkg1, pkg2, ...]; channel="", resolve=true)
312319
313320
Adds a dependency to the current environment.
314321
"""
315322
add(pkg::AbstractString; version="", channel="", build="", kw...) = add(PkgSpec(pkg, version=version, channel=channel, build=build); kw...)
316323

317324
"""
318325
rm(pkg; resolve=true)
326+
rm([pkg1, pkg2, ...]; resolve=true)
319327
320328
Removes a dependency from the current environment.
321329
"""

test/main.jl

+26
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,32 @@ end
6262
end
6363
end
6464

65+
@testitem "install/remove multiple python packages" begin
66+
include("setup.jl")
67+
CondaPkg.add("python", version="==3.10.2")
68+
# verify package isn't already installed
69+
@test !occursin("jsonlines ", status())
70+
@test !occursin("cowpy", status())
71+
CondaPkg.withenv() do
72+
isnull || @test_throws Exception run(`python -c "import jsonlines"`)
73+
isnull || @test_throws Exception run(`python -c "import cowpy"`)
74+
end
75+
76+
# install multiple packages
77+
CondaPkg.add(["jsonlines", "cowpy"])
78+
@test occursin("jsonlines", status())
79+
@test occursin("cowpy", status())
80+
81+
# remove multiple packages
82+
CondaPkg.rm(["jsonlines", "cowpy"])
83+
@test !occursin("jsonlines ", status())
84+
@test !occursin("cowpy", status())
85+
CondaPkg.withenv() do
86+
isnull || @test_throws Exception run(`python -c "import jsonlines"`)
87+
isnull || @test_throws Exception run(`python -c "import cowpy"`)
88+
end
89+
end
90+
6591
@testitem "pip install/remove python package" begin
6692
@testset "using $kind" for kind in ["pip", "uv"]
6793
include("setup.jl")

0 commit comments

Comments
 (0)