Skip to content

Commit

Permalink
refactor(pip3): use with_paths to set venv path (chipsalliance#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamboman authored May 29, 2022
1 parent 63cde74 commit 1c85bf2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
9 changes: 3 additions & 6 deletions lua/nvim-lsp-installer/core/managers/pip3/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ function M.install(packages)
"-U",
settings.current.pip.install_args,
pkgs,
env = M.env(ctx.cwd:get()), -- use venv env
check_executable = false,
with_paths = { M.venv_path(ctx.cwd:get()) },
}
end)
:or_else_throw "Unable to create python3 venv environment."
Expand Down Expand Up @@ -95,8 +94,7 @@ function M.check_outdated_primary_package(receipt, install_dir)
"--outdated",
"--format=json",
cwd = install_dir,
env = M.env(install_dir), -- use venv
check_executable = false,
with_paths = { M.venv_path(install_dir) },
}):map_catching(function(result)
---@alias PipOutdatedPackage {name: string, version: string, latest_version: string}
---@type PipOutdatedPackage[]
Expand Down Expand Up @@ -132,8 +130,7 @@ function M.get_installed_primary_package_version(receipt, install_dir)
"list",
"--format=json",
cwd = install_dir,
env = M.env(install_dir), -- use venv env
check_executable = false,
with_paths = { M.venv_path(install_dir) },
}):map_catching(function(result)
local pip_packages = vim.json.decode(result.stdout)
local normalized_pip_package = M.normalize_package(receipt.primary_source.package)
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-lsp-installer/core/spawn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ setmetatable(spawn, {

local cmd = self._aliases[normalized_cmd] or normalized_cmd

if args.with_paths == nil and args.check_executable ~= false and not is_executable(cmd) then
if (env and env.PATH) == nil and args.check_executable ~= false and not is_executable(cmd) then
return Failure({
stderr = ("%s is not executable"):format(cmd),
}, cmd)
Expand Down
37 changes: 16 additions & 21 deletions tests/core/managers/pip3_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local mock = require "luassert.mock"
local spy = require "luassert.spy"
local match = require "luassert.match"

local pip3 = require "nvim-lsp-installer.core.managers.pip3"
local Optional = require "nvim-lsp-installer.core.optional"
Expand Down Expand Up @@ -41,20 +40,19 @@ describe("pip3 manager", function()
"venv",
}
assert.spy(ctx.spawn.python).was_called(1)
assert.spy(ctx.spawn.python).was_called_with(match.tbl_containing {
assert.spy(ctx.spawn.python).was_called_with {
"-m",
"pip",
"install",
"-U",
match.table(),
match.tbl_containing {
{},
{
"main-package==42.13.37",
"supporting-package",
"supporting-package2",
},
env = match.is_table(),
check_executable = false,
})
with_paths = { "/tmp/install-dir/venv/bin" },
}
end)
)

Expand Down Expand Up @@ -106,16 +104,15 @@ describe("pip3 manager", function()
}
installer.run_installer(ctx, pip3.packages { "package" })
settings.set(settings._DEFAULT_SETTINGS)
assert.spy(ctx.spawn.python).was_called_with(match.tbl_containing {
assert.spy(ctx.spawn.python).was_called_with {
"-m",
"pip",
"install",
"-U",
match.tbl_containing { "--proxy", "http://localhost:8080" },
match.tbl_containing { "package" },
env = match.is_table(),
check_executable = false,
})
{ "--proxy", "http://localhost:8080" },
{ "package" },
with_paths = { "/tmp/install-dir/venv/bin" },
}
end)
)

Expand Down Expand Up @@ -165,15 +162,14 @@ describe("pip3 version check", function()
)

assert.spy(spawn.python).was_called(1)
assert.spy(spawn.python).was_called_with(match.tbl_containing {
assert.spy(spawn.python).was_called_with {
"-m",
"pip",
"list",
"--format=json",
cwd = "/tmp/install/dir",
env = match.table(),
check_executable = false,
})
with_paths = { "/tmp/install/dir/venv/bin" },
}
assert.is_true(result:is_success())
assert.equals("1.3.0", result:get_or_nil())

Expand Down Expand Up @@ -203,16 +199,15 @@ describe("pip3 version check", function()
)

assert.spy(spawn.python).was_called(1)
assert.spy(spawn.python).was_called_with(match.tbl_containing {
assert.spy(spawn.python).was_called_with {
"-m",
"pip",
"list",
"--outdated",
"--format=json",
cwd = "/tmp/install/dir",
env = match.table(),
check_executable = false,
})
with_paths = { "/tmp/install/dir/venv/bin" },
}
assert.is_true(result:is_success())
assert.same({
name = "python-lsp-server",
Expand Down

0 comments on commit 1c85bf2

Please sign in to comment.