github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/src/os/exec/lp_unix_test.go (about)

     1  // Copyright 2013 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package exec
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  )
    14  
    15  func TestLookPathUnixEmptyPath(t *testing.T) {
    16  	tmp, err := ioutil.TempDir("", "TestLookPathUnixEmptyPath")
    17  	if err != nil {
    18  		t.Fatal("TempDir failed: ", err)
    19  	}
    20  	defer os.RemoveAll(tmp)
    21  	wd, err := os.Getwd()
    22  	if err != nil {
    23  		t.Fatal("Getwd failed: ", err)
    24  	}
    25  	err = os.Chdir(tmp)
    26  	if err != nil {
    27  		t.Fatal("Chdir failed: ", err)
    28  	}
    29  	defer os.Chdir(wd)
    30  
    31  	f, err := os.OpenFile("exec_me", os.O_CREATE|os.O_EXCL, 0700)
    32  	if err != nil {
    33  		t.Fatal("OpenFile failed: ", err)
    34  	}
    35  	err = f.Close()
    36  	if err != nil {
    37  		t.Fatal("Close failed: ", err)
    38  	}
    39  
    40  	pathenv := os.Getenv("PATH")
    41  	defer os.Setenv("PATH", pathenv)
    42  
    43  	err = os.Setenv("PATH", "")
    44  	if err != nil {
    45  		t.Fatal("Setenv failed: ", err)
    46  	}
    47  
    48  	path, err := LookPath("exec_me")
    49  	if err == nil {
    50  		t.Fatal("LookPath found exec_me in empty $PATH")
    51  	}
    52  	if path != "" {
    53  		t.Fatalf("LookPath path == %q when err != nil", path)
    54  	}
    55  }