Black Lives Matter. Support the Equal Justice Initiative.

Source file src/os/exec/lp_unix_test.go

Documentation: os/exec

     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  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     6  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
     7  
     8  package exec
     9  
    10  import (
    11  	"os"
    12  	"testing"
    13  )
    14  
    15  func TestLookPathUnixEmptyPath(t *testing.T) {
    16  	tmp, err := os.MkdirTemp("", "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  	t.Setenv("PATH", "")
    41  
    42  	path, err := LookPath("exec_me")
    43  	if err == nil {
    44  		t.Fatal("LookPath found exec_me in empty $PATH")
    45  	}
    46  	if path != "" {
    47  		t.Fatalf("LookPath path == %q when err != nil", path)
    48  	}
    49  }
    50  

View as plain text