-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_test.go
More file actions
48 lines (38 loc) · 1.18 KB
/
path_test.go
File metadata and controls
48 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func createExecutable(t *testing.T, file string) {
t.Helper()
f, err := os.OpenFile(file, os.O_RDONLY|os.O_CREATE, 0755)
require.NoError(t, err)
require.NoError(t, f.Close())
}
func TestLookPath(t *testing.T) {
const cmd = "prog"
path1 := t.TempDir()
path2 := t.TempDir()
pathEnv := fmt.Sprintf("%s:%s", path1, path2)
_, err := lookPath(cmd, "", pathEnv)
assert.ErrorIs(t, err, exec.ErrNotFound)
// executable is located in path2
createExecutable(t, filepath.Join(path2, cmd))
executable, err := lookPath(cmd, "", pathEnv)
require.NoError(t, err)
assert.Equal(t, filepath.Join(path2, cmd), executable)
// executable is located in both path1 and path2
createExecutable(t, filepath.Join(path1, cmd))
executable, err = lookPath(cmd, "", pathEnv)
require.NoError(t, err)
assert.Equal(t, filepath.Join(path1, cmd), executable)
// executable is located in both path1 and path2, path1 is excluded
executable, err = lookPath(cmd, path1, pathEnv)
require.NoError(t, err)
assert.Equal(t, filepath.Join(path2, cmd), executable)
}