mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-08-02 03:28:28 -04:00
78f2ea4642
- Fixes #2220
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package utils
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCommandExistsFallsBackToLocalBin(t *testing.T) {
|
|
home := t.TempDir()
|
|
binDir := filepath.Join(home, ".local", "bin")
|
|
require.NoError(t, os.MkdirAll(binDir, 0o755))
|
|
require.NoError(t, os.WriteFile(filepath.Join(binDir, "pywalfox"), []byte("#!/bin/sh\n"), 0o755))
|
|
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("PATH", t.TempDir())
|
|
|
|
assert.True(t, CommandExists("pywalfox"))
|
|
}
|
|
|
|
func TestCommandExistsIgnoresNonExecutableLocalBinFile(t *testing.T) {
|
|
home := t.TempDir()
|
|
binDir := filepath.Join(home, ".local", "bin")
|
|
require.NoError(t, os.MkdirAll(binDir, 0o755))
|
|
require.NoError(t, os.WriteFile(filepath.Join(binDir, "pywalfox"), []byte("not executable"), 0o644))
|
|
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("PATH", t.TempDir())
|
|
|
|
assert.False(t, CommandExists("pywalfox"))
|
|
}
|
|
|
|
func TestEnvWithUserBinPathPrependsLocalBin(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
|
|
env := EnvWithUserBinPath([]string{"PATH=/usr/bin", "OTHER=value"})
|
|
var pathValue string
|
|
for _, entry := range env {
|
|
if strings.HasPrefix(entry, "PATH=") {
|
|
pathValue = strings.TrimPrefix(entry, "PATH=")
|
|
break
|
|
}
|
|
}
|
|
|
|
parts := filepath.SplitList(pathValue)
|
|
require.NotEmpty(t, parts)
|
|
assert.Equal(t, filepath.Join(home, ".local", "bin"), parts[0])
|
|
assert.Contains(t, parts, "/usr/local/bin")
|
|
assert.Contains(t, env, "OTHER=value")
|
|
}
|