Skip to content

Commit 4bb3d8e

Browse files
committed
tests and fix for cpu pc
1 parent 7a2a53e commit 4bb3d8e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

cpugrep.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const cpugrepcmd = "ps -m -o %%cpu,pid | grep %d | head -1"
1111
func Cpugrep(pid int) string {
1212
cmd := fmt.Sprintf(cpugrepcmd, pid)
1313
stdout, _, _ := Shellout(cmd)
14-
cpu := strings.Split(stdout, " ")[0]
14+
cpu := strings.Split(strings.TrimSpace(stdout), " ")[0]
1515
r, _ := strconv.ParseFloat(cpu, 8)
1616
return fmt.Sprintf("%.2f%%", r)
1717
}

cpugrep_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package l0la
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestCpuGrep(t *testing.T) {
10+
cpu := Cpugrep(os.Getpid())
11+
t.Logf("cpu percent was %v", cpu)
12+
if !strings.HasSuffix(cpu, "%") {
13+
t.Errorf("invalid cpu pc, got: %v", cpu)
14+
}
15+
}

0 commit comments

Comments
 (0)