github.com/direnv/go-lpenv@v0.0.0-20201224120758-54c33c5ae6e4/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 aix darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package lpenv
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  )
    14  
    15  func TestLookPathEnvUnixEmptyPath(t *testing.T) {
    16  	tmp, err := ioutil.TempDir("", "TestLookPathEnvUnixEmptyPath")
    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  	path, err := LookPathEnv("exec_me", ".", []string{})
    41  	if err == nil {
    42  		t.Fatal("LookPathEnv found exec_me in empty $PATH")
    43  	}
    44  	if path != "" {
    45  		t.Fatalf("LookPathEnv path == %q when err != nil", path)
    46  	}
    47  }