chore: add tests

Add simple tests to verify that environment variables are read.
This commit is contained in:
Max Altgelt 2021-07-13 16:38:49 +02:00
parent 5337ed8408
commit 73fee847e6
No known key found for this signature in database
GPG Key ID: 52C16AF8F6B69C5A
2 changed files with 123 additions and 0 deletions

View File

@ -691,6 +691,60 @@ func Test_IsRunning(t *testing.T) {
}
}
func Test_Process_Environ(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unable to create temp dir %v", err)
}
defer os.RemoveAll(tmpdir) // clean up
tmpfilepath := filepath.Join(tmpdir, "test.go")
tmpfile, err := os.Create(tmpfilepath)
if err != nil {
t.Fatalf("unable to create temp file %v", err)
}
tmpfilecontent := []byte("package main\nimport(\n\"time\"\n)\nfunc main(){\nfor range time.Tick(time.Second) {}\n}")
if _, err := tmpfile.Write(tmpfilecontent); err != nil {
tmpfile.Close()
t.Fatalf("unable to write temp file %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("unable to close temp file %v", err)
}
err = exec.Command("go", "build", "-o", tmpfile.Name()+".exe", tmpfile.Name()).Run()
if err != nil {
t.Fatalf("unable to build temp file %v", err)
}
cmd := exec.Command(tmpfile.Name() + ".exe")
cmd.Env = []string{"testkey=envvalue"}
assert.Nil(t, cmd.Start())
defer cmd.Process.Kill()
time.Sleep(100 * time.Millisecond)
p, err := NewProcess(int32(cmd.Process.Pid))
skipIfNotImplementedErr(t, err)
assert.Nil(t, err)
envs, err := p.Environ()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("getting environ error %v", err)
}
var envvarFound bool
for _, envvar := range envs {
if envvar == "testkey=envvalue" {
envvarFound = true
break
}
}
if !envvarFound {
t.Error("environment variable not found")
}
}
func Test_AllProcesses_cmdLine(t *testing.T) {
procs, err := Processes()
if err == nil {

View File

@ -693,6 +693,60 @@ func Test_IsRunning(t *testing.T) {
}
}
func Test_Process_Environ(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unable to create temp dir %v", err)
}
defer os.RemoveAll(tmpdir) // clean up
tmpfilepath := filepath.Join(tmpdir, "test.go")
tmpfile, err := os.Create(tmpfilepath)
if err != nil {
t.Fatalf("unable to create temp file %v", err)
}
tmpfilecontent := []byte("package main\nimport(\n\"time\"\n)\nfunc main(){\nfor range time.Tick(time.Second) {}\n}")
if _, err := tmpfile.Write(tmpfilecontent); err != nil {
tmpfile.Close()
t.Fatalf("unable to write temp file %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("unable to close temp file %v", err)
}
err = exec.Command("go", "build", "-o", tmpfile.Name()+".exe", tmpfile.Name()).Run()
if err != nil {
t.Fatalf("unable to build temp file %v", err)
}
cmd := exec.Command(tmpfile.Name() + ".exe")
cmd.Env = []string{"testkey=envvalue"}
assert.Nil(t, cmd.Start())
defer cmd.Process.Kill()
time.Sleep(100 * time.Millisecond)
p, err := NewProcess(int32(cmd.Process.Pid))
skipIfNotImplementedErr(t, err)
assert.Nil(t, err)
envs, err := p.Environ()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("getting environ error %v", err)
}
var envvarFound bool
for _, envvar := range envs {
if envvar == "testkey=envvalue" {
envvarFound = true
break
}
}
if !envvarFound {
t.Error("environment variable not found")
}
}
func Test_AllProcesses_cmdLine(t *testing.T) {
procs, err := Processes()
if err == nil {
@ -711,6 +765,21 @@ func Test_AllProcesses_cmdLine(t *testing.T) {
}
}
func Test_AllProcesses_environ(t *testing.T) {
procs, err := Processes()
if err == nil {
for _, proc := range procs {
exeName, _ := proc.Exe()
environ, err := proc.Environ()
if err != nil {
environ = []string{"Error: " + err.Error() }
}
t.Logf("Process #%v: Name: %v / Environment Variables: %v\n", proc.Pid, exeName, environ)
}
}
}
func BenchmarkNewProcess(b *testing.B) {
checkPid := os.Getpid()
for i := 0; i < b.N; i++ {