diff --git a/v3/process/process_linux_test.go b/v3/process/process_linux_test.go new file mode 100644 index 0000000..2e1350c --- /dev/null +++ b/v3/process/process_linux_test.go @@ -0,0 +1,54 @@ +// +build linux + +package process + +import ( + "fmt" + "strconv" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_Process_splitProcStat(t *testing.T) { + expectedFieldsNum := 53 + statLineContent := make([]string, expectedFieldsNum-1) + for i := 0; i < expectedFieldsNum-1; i++ { + statLineContent[i] = strconv.Itoa(i + 1) + } + + cases := []string{ + "ok", + "ok)", + "(ok", + "ok )", + "ok )(", + "ok )()", + "() ok )()", + "() ok (()", + " ) ok )", + "(ok) (ok)", + } + + consideredFields := []int{4, 7, 10, 11, 12, 13, 14, 15, 18, 22, 42} + + commandNameIndex := 2 + for _, expectedName := range cases { + statLineContent[commandNameIndex-1] = "(" + expectedName + ")" + statLine := strings.Join(statLineContent, " ") + t.Run(fmt.Sprintf("name: %s", expectedName), func(t *testing.T) { + parsedStatLine := splitProcStat([]byte(statLine)) + assert.Equal(t, expectedName, parsedStatLine[commandNameIndex]) + for _, idx := range consideredFields { + expected := strconv.Itoa(idx) + parsed := parsedStatLine[idx] + assert.Equal( + t, expected, parsed, + "field %d (index from 1 as in man proc) must be %q but %q is received", + idx, expected, parsed, + ) + } + }) + } +} diff --git a/v3/process/process_test.go b/v3/process/process_test.go index b82030c..47045af 100644 --- a/v3/process/process_test.go +++ b/v3/process/process_test.go @@ -303,48 +303,6 @@ func Test_Process_Threads(t *testing.T) { } } -func Test_Process_splitProcStat(t *testing.T) { - expectedFieldsNum := 53 - statLineContent := make([]string, expectedFieldsNum-1) - for i := 0; i < expectedFieldsNum-1; i++ { - statLineContent[i] = strconv.Itoa(i + 1) - } - - cases := []string{ - "ok", - "ok)", - "(ok", - "ok )", - "ok )(", - "ok )()", - "() ok )()", - "() ok (()", - " ) ok )", - "(ok) (ok)", - } - - consideredFields := []int{4, 7, 10, 11, 12, 13, 14, 15, 18, 22, 42} - - commandNameIndex := 2 - for _, expectedName := range cases { - statLineContent[commandNameIndex-1] = "(" + expectedName + ")" - statLine := strings.Join(statLineContent, " ") - t.Run(fmt.Sprintf("name: %s", expectedName), func(t *testing.T) { - parsedStatLine := splitProcStat([]byte(statLine)) - assert.Equal(t, expectedName, parsedStatLine[commandNameIndex]) - for _, idx := range consideredFields { - expected := strconv.Itoa(idx) - parsed := parsedStatLine[idx] - assert.Equal( - t, expected, parsed, - "field %d (index from 1 as in man proc) must be %q but %q is received", - idx, expected, parsed, - ) - } - }) - } -} - func Test_Process_Name(t *testing.T) { p := testGetProcess()