mirror of
https://github.com/Jguer/yay.git
synced 2025-10-09 00:07:14 -04:00
feat(yay): Add support for PACMAN_AUTH (#1706)
This commit is contained in:
parent
7f7b69447d
commit
7f5a060324
@ -143,6 +143,14 @@ func (c *Configuration) String() string {
|
|||||||
|
|
||||||
// check privilege elevator exists otherwise try to find another one.
|
// check privilege elevator exists otherwise try to find another one.
|
||||||
func (c *Configuration) setPrivilegeElevator() error {
|
func (c *Configuration) setPrivilegeElevator() error {
|
||||||
|
if auth := os.Getenv("PACMAN_AUTH"); auth != "" {
|
||||||
|
c.SudoBin = auth
|
||||||
|
if auth != "sudo" {
|
||||||
|
c.SudoFlags = ""
|
||||||
|
c.SudoLoop = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, bin := range [...]string{c.SudoBin, "sudo"} {
|
for _, bin := range [...]string{c.SudoBin, "sudo"} {
|
||||||
if _, err := exec.LookPath(bin); err == nil {
|
if _, err := exec.LookPath(bin); err == nil {
|
||||||
c.SudoBin = bin
|
c.SudoBin = bin
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GIVEN a non existing build dir in the config
|
// GIVEN a non existing build dir in the config
|
||||||
@ -221,3 +222,77 @@ func TestConfiguration_setPrivilegeElevator_custom_script(t *testing.T) {
|
|||||||
assert.Equal(t, "-v", config.SudoFlags)
|
assert.Equal(t, "-v", config.SudoFlags)
|
||||||
assert.True(t, config.SudoLoop)
|
assert.True(t, config.SudoLoop)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GIVEN default config and sudo loop enabled
|
||||||
|
// GIVEN doas as PACMAN_AUTH env variable
|
||||||
|
// WHEN setPrivilegeElevator gets called
|
||||||
|
// THEN sudobin should be changed to "doas"
|
||||||
|
func TestConfiguration_setPrivilegeElevator_pacman_auth_doas(t *testing.T) {
|
||||||
|
oldPath := os.Getenv("PATH")
|
||||||
|
|
||||||
|
path, err := os.MkdirTemp("", "yay-test")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
doas := filepath.Join(path, "doas")
|
||||||
|
_, err = os.Create(doas)
|
||||||
|
os.Chmod(doas, 0o755)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
sudo := filepath.Join(path, "sudo")
|
||||||
|
_, err = os.Create(sudo)
|
||||||
|
os.Chmod(sudo, 0o755)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
defer os.RemoveAll(path)
|
||||||
|
|
||||||
|
config := DefaultConfig()
|
||||||
|
config.SudoBin = "sudo"
|
||||||
|
config.SudoLoop = true
|
||||||
|
config.SudoFlags = "-v"
|
||||||
|
|
||||||
|
os.Setenv("PACMAN_AUTH", "doas")
|
||||||
|
os.Setenv("PATH", path)
|
||||||
|
err = config.setPrivilegeElevator()
|
||||||
|
os.Setenv("PATH", oldPath)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "doas", config.SudoBin)
|
||||||
|
assert.Equal(t, "", config.SudoFlags)
|
||||||
|
assert.False(t, config.SudoLoop)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GIVEN config with doas configed and sudo loop enabled
|
||||||
|
// GIVEN sudo as PACMAN_AUTH env variable
|
||||||
|
// WHEN setPrivilegeElevator gets called
|
||||||
|
// THEN sudobin should be changed to "sudo"
|
||||||
|
func TestConfiguration_setPrivilegeElevator_pacman_auth_sudo(t *testing.T) {
|
||||||
|
oldPath := os.Getenv("PATH")
|
||||||
|
|
||||||
|
path, err := os.MkdirTemp("", "yay-test")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
doas := filepath.Join(path, "doas")
|
||||||
|
_, err = os.Create(doas)
|
||||||
|
os.Chmod(doas, 0o755)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
sudo := filepath.Join(path, "sudo")
|
||||||
|
_, err = os.Create(sudo)
|
||||||
|
os.Chmod(sudo, 0o755)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
defer os.RemoveAll(path)
|
||||||
|
|
||||||
|
config := DefaultConfig()
|
||||||
|
config.SudoBin = "doas"
|
||||||
|
config.SudoLoop = true
|
||||||
|
config.SudoFlags = "-v"
|
||||||
|
|
||||||
|
os.Setenv("PACMAN_AUTH", "sudo")
|
||||||
|
os.Setenv("PATH", path)
|
||||||
|
err = config.setPrivilegeElevator()
|
||||||
|
os.Setenv("PATH", oldPath)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "sudo", config.SudoBin)
|
||||||
|
assert.Equal(t, "-v", config.SudoFlags)
|
||||||
|
assert.True(t, config.SudoLoop)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user