This repository was archived by the owner on Jan 22, 2025. It is now read-only.
forked from mitchellh/go-ps
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprocess_test.go
More file actions
72 lines (61 loc) · 1.52 KB
/
process_test.go
File metadata and controls
72 lines (61 loc) · 1.52 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package ps
import (
"errors"
"os"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testFindProcess(t *testing.T, name string) Process {
proc, err := FindProcess(os.Getpid())
require.NoError(t, err)
require.NotNil(t, proc)
assert.Equal(t, os.Getpid(), proc.Pid())
if name != "" {
assert.Equal(t, name, proc.Executable())
path, err := proc.Path()
require.NoError(t, err)
t.Logf("Path: %s", path)
assert.True(t, strings.HasSuffix(path, string(os.PathSeparator)+name))
}
return proc
}
func testProcesses(t *testing.T, name string) {
// This test works because there will always be SOME processes running
procs, err := Processes()
require.NoError(t, err)
require.True(t, len(procs) > 0)
if name != "" {
found := false
for _, p := range procs {
if p.Executable() == name {
found = true
break
}
}
assert.True(t, found)
}
}
func TestFindProcess(t *testing.T) {
testFindProcess(t, "")
}
func TestFindProcessGo(t *testing.T) {
var exe = "go-ps.test"
if runtime.GOOS == "windows" {
exe += ".exe"
}
testFindProcess(t, exe)
}
func TestProcesses(t *testing.T) {
testProcesses(t, "")
}
func TestFindProcessesWithFnError(t *testing.T) {
ps, err := findProcessesWithFn(func() ([]Process, error) { return nil, errors.New("TestFindProcessesWithFn Error") }, nil, 0)
require.Nil(t, ps)
require.NotNil(t, err)
ps, err = findProcessesWithFn(func() ([]Process, error) { return nil, nil }, nil, 0)
require.Nil(t, ps)
require.Nil(t, err)
}