add MockEnv as more generic function.

pull/1067/head
shirou 4 years ago
parent abacce2177
commit 2fa855f295

@ -352,6 +352,16 @@ func HostDev(combineWith ...string) string {
return GetEnv("HOST_DEV", "/dev", combineWith...) return GetEnv("HOST_DEV", "/dev", combineWith...)
} }
// MockEnv set environment variable and return revert function.
// MockEnv should be used testing only.
func MockEnv(key string, value string) func() {
original := os.Getenv(key)
os.Setenv(key, value)
return func() {
os.Setenv(key, original)
}
}
// getSysctrlEnv sets LC_ALL=C in a list of env vars for use when running // getSysctrlEnv sets LC_ALL=C in a list of env vars for use when running
// sysctl commands (see DoSysctrl). // sysctl commands (see DoSysctrl).
func getSysctrlEnv(env []string) []string { func getSysctrlEnv(env []string) []string {

@ -5,9 +5,10 @@ package process
import ( import (
"context" "context"
"io/ioutil" "io/ioutil"
"os"
"strconv" "strconv"
"testing" "testing"
"github.com/shirou/gopsutil/internal/common"
) )
func Test_fillFromStatusWithContext(t *testing.T) { func Test_fillFromStatusWithContext(t *testing.T) {
@ -15,10 +16,8 @@ func Test_fillFromStatusWithContext(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
original := os.Getenv("HOST_PROC") f := common.MockEnv("HOST_PROC", "testdata/linux")
os.Setenv("HOST_PROC", "testdata/linux") defer f()
defer os.Setenv("HOST_PROC", original)
for _, pid := range pids { for _, pid := range pids {
pid, _ := strconv.ParseInt(pid.Name(), 0, 32) pid, _ := strconv.ParseInt(pid.Name(), 0, 32)
p, _ := NewProcess(int32(pid)) p, _ := NewProcess(int32(pid))

@ -352,6 +352,16 @@ func HostDev(combineWith ...string) string {
return GetEnv("HOST_DEV", "/dev", combineWith...) return GetEnv("HOST_DEV", "/dev", combineWith...)
} }
// MockEnv set environment variable and return revert function.
// MockEnv should be used testing only.
func MockEnv(key string, value string) func() {
original := os.Getenv(key)
os.Setenv(key, value)
return func() {
os.Setenv(key, original)
}
}
// getSysctrlEnv sets LC_ALL=C in a list of env vars for use when running // getSysctrlEnv sets LC_ALL=C in a list of env vars for use when running
// sysctl commands (see DoSysctrl). // sysctl commands (see DoSysctrl).
func getSysctrlEnv(env []string) []string { func getSysctrlEnv(env []string) []string {

@ -5,9 +5,10 @@ package process
import ( import (
"context" "context"
"io/ioutil" "io/ioutil"
"os"
"strconv" "strconv"
"testing" "testing"
"github.com/shirou/gopsutil/v3/internal/common"
) )
func Test_fillFromStatusWithContext(t *testing.T) { func Test_fillFromStatusWithContext(t *testing.T) {
@ -15,10 +16,8 @@ func Test_fillFromStatusWithContext(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
original := os.Getenv("HOST_PROC") f := common.MockEnv("HOST_PROC", "testdata/linux")
os.Setenv("HOST_PROC", "testdata/linux") defer f()
defer os.Setenv("HOST_PROC", original)
for _, pid := range pids { for _, pid := range pids {
pid, _ := strconv.ParseInt(pid.Name(), 0, 32) pid, _ := strconv.ParseInt(pid.Name(), 0, 32)
p, _ := NewProcess(int32(pid)) p, _ := NewProcess(int32(pid))

Loading…
Cancel
Save