Files
lancet/system/os_test.go
T
Lee Sang Gyu b4302b2d15 fix(package): [system] keep the whole command name in GetProcessInfo (#426)
* fix(package): [system] keep the whole command name in GetProcessInfo

* test(package): [system] cover a command name with spaces
2026-11-09 12:30:04 +08:00

164 lines
3.2 KiB
Go

package system
import (
"os/exec"
"runtime"
"strings"
"testing"
"time"
"github.com/duke-git/lancet/v2/internal"
)
func TestOsDetection(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestOsJudgment")
osType, _, _ := ExecCommand("echo $OSTYPE")
if strings.Contains(osType, "linux") {
assert.Equal(true, IsLinux())
}
if strings.Contains(osType, "darwin") {
assert.Equal(true, IsMac())
}
}
func TestOsEnvOperation(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestOsEnvOperation")
envNotExist := GetOsEnv("foo")
assert.Equal("", envNotExist)
err := SetOsEnv("foo", "foo_value")
assert.IsNil(err)
envExist := GetOsEnv("foo")
assert.Equal("foo_value", envExist)
assert.Equal(true, CompareOsEnv("foo", "foo_value"))
assert.Equal(false, CompareOsEnv("foo", "abc"))
assert.Equal(false, CompareOsEnv("abc", "abc"))
assert.Equal(false, CompareOsEnv("abc", "abc"))
err = RemoveOsEnv("foo")
if err != nil {
t.Fail()
}
assert.Equal(false, CompareOsEnv("foo", "foo_value"))
}
func TestExecCommand(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestExecCommand")
// linux or mac
stdout, stderr, err := ExecCommand("ls", func(cmd *exec.Cmd) {
cmd.Dir = "/"
})
t.Log("std out: ", stdout)
t.Log("std err: ", stderr)
assert.Equal("", stderr)
assert.IsNil(err)
// windows
stdout, stderr, err = ExecCommand("dir")
t.Log("std out: ", stdout)
t.Log("std err: ", stderr)
if IsWindows() {
assert.IsNil(err)
}
// error command
stdout, stderr, err = ExecCommand("abc")
t.Log("std out: ", stdout)
t.Log("std err: ", stderr)
assert.IsNotNil(err)
}
func TestGetOsBits(t *testing.T) {
t.Parallel()
osBits := GetOsBits()
switch osBits {
case 32, 64:
t.Logf("os is %d", osBits)
default:
t.Error("os is not 32 or 64 bits")
}
}
func TestStartProcess(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestStartProcess")
pid, err := StartProcess("ls", "-a")
assert.IsNil(err)
assert.Equal(true, pid > 0)
}
func TestKillProcess(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestKillProcess")
pid, err := StartProcess("ls")
assert.IsNil(err)
assert.Equal(true, pid > 0)
err = KillProcess(pid)
assert.IsNil(err)
}
func TestStopProcess(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestStopProcess")
pid, err := StartProcess("ls")
assert.IsNil(err)
assert.Equal(true, pid > 0)
err = StopProcess(pid)
assert.IsNil(err)
}
func TestGetProcessInfo(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestGetProcessInfo")
pid, err := StartProcess("ls", "-a")
assert.IsNil(err)
time.Sleep(1 * time.Second)
processInfo, err := GetProcessInfo(pid)
assert.IsNil(err)
t.Log(processInfo)
}
func TestParseProcessInfo_commandWithSpaces(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" {
t.Skip("parses ps output")
}
assert := internal.NewAssert(t, "TestParseProcessInfo_commandWithSpaces")
output := []byte(" PID %CPU %MEM STAT USER COMM\n 4242 0.1 0.2 S root Google Chrome Helper\n")
info, err := parseProcessInfo(output, 4242)
assert.IsNil(err)
assert.Equal("Google Chrome Helper", info.Cmd)
assert.Equal("root", info.User)
assert.Equal("S", info.State)
}