mirror of
https://hubproxy.babadafafafafa.cn/https://github.com/1Panel-dev/1Panel.git
synced 2026-09-20 08:03:55 +08:00
cmd.Which() previously shelled out to `which <name>` to determine whether a binary was on PATH. On distributions that do not ship a `which` package by default — Arch Linux is the canonical example, but the same applies to several minimal container images — `which` itself is missing, so every call to Which() returned false and 1Panel reported core dependencies (notably Docker) as 'not installed' even when they were running normally. Switch the primary path to Go's exec.LookPath, which uses the process PATH directly and has no external dependency. The original shell-out is preserved as a fallback so any environment where the agent's PATH differs from the user's interactive shell PATH (the original reason the shell-out existed) keeps working unchanged. Both copies are updated (agent/utils/cmd/cmd.go and core/utils/cmd/cmd.go) and a small unit test is added to each package to guard the regression. Fixes #12605 Signed-off-by: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com>
24 lines
844 B
Go
24 lines
844 B
Go
package cmd
|
|
|
|
import "testing"
|
|
|
|
// TestWhich_ExistingBinary verifies that Which() returns true for a
|
|
// binary that is guaranteed to exist on every Unix-like build host
|
|
// (`sh`). Regression test for #12605: the previous implementation
|
|
// shelled out to `which`, which is not always available on minimal
|
|
// distributions like Arch Linux. The new implementation tries
|
|
// exec.LookPath first, so this assertion holds regardless of whether
|
|
// `which` itself is on PATH.
|
|
func TestWhich_ExistingBinary(t *testing.T) {
|
|
if !Which("sh") {
|
|
t.Errorf("Which(\"sh\") = false, want true")
|
|
}
|
|
}
|
|
|
|
func TestWhich_MissingBinary(t *testing.T) {
|
|
// A binary name that is extremely unlikely to exist on any host.
|
|
if Which("definitely-not-a-real-binary-xyzzy-1panel") {
|
|
t.Errorf("Which(\"definitely-not-a-real-binary-xyzzy-1panel\") = true, want false")
|
|
}
|
|
}
|