Black Lives Matter. Support the Equal Justice Initiative.

Source file src/internal/execabs/execabs_test.go

Documentation: internal/execabs

     1  // Copyright 2020 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  package execabs
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"internal/testenv"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"runtime"
    15  	"testing"
    16  )
    17  
    18  func TestFixCmd(t *testing.T) {
    19  	cmd := &exec.Cmd{Path: "hello"}
    20  	fixCmd("hello", cmd)
    21  	if cmd.Path != "" {
    22  		t.Error("fixCmd didn't clear cmd.Path")
    23  	}
    24  	expectedErr := fmt.Sprintf("hello resolves to executable relative to current directory (.%chello)", filepath.Separator)
    25  	if err := cmd.Run(); err == nil {
    26  		t.Fatal("Command.Run didn't fail")
    27  	} else if err.Error() != expectedErr {
    28  		t.Fatalf("Command.Run returned unexpected error: want %q, got %q", expectedErr, err.Error())
    29  	}
    30  }
    31  
    32  func TestCommand(t *testing.T) {
    33  	testenv.MustHaveExec(t)
    34  
    35  	for _, cmd := range []func(string) *Cmd{
    36  		func(s string) *Cmd { return Command(s) },
    37  		func(s string) *Cmd { return CommandContext(context.Background(), s) },
    38  	} {
    39  		tmpDir := t.TempDir()
    40  		executable := "execabs-test"
    41  		if runtime.GOOS == "windows" {
    42  			executable += ".exe"
    43  		}
    44  		if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
    45  			t.Fatalf("os.WriteFile failed: %s", err)
    46  		}
    47  		cwd, err := os.Getwd()
    48  		if err != nil {
    49  			t.Fatalf("os.Getwd failed: %s", err)
    50  		}
    51  		defer os.Chdir(cwd)
    52  		if err = os.Chdir(tmpDir); err != nil {
    53  			t.Fatalf("os.Chdir failed: %s", err)
    54  		}
    55  		if runtime.GOOS != "windows" {
    56  			// add "." to PATH so that exec.LookPath looks in the current directory on
    57  			// non-windows platforms as well
    58  			origPath := os.Getenv("PATH")
    59  			defer os.Setenv("PATH", origPath)
    60  			os.Setenv("PATH", fmt.Sprintf(".:%s", origPath))
    61  		}
    62  		expectedErr := fmt.Sprintf("execabs-test resolves to executable relative to current directory (.%c%s)", filepath.Separator, executable)
    63  		if err = cmd("execabs-test").Run(); err == nil {
    64  			t.Fatalf("Command.Run didn't fail when exec.LookPath returned a relative path")
    65  		} else if err.Error() != expectedErr {
    66  			t.Errorf("Command.Run returned unexpected error: want %q, got %q", expectedErr, err.Error())
    67  		}
    68  	}
    69  }
    70  
    71  func TestLookPath(t *testing.T) {
    72  	testenv.MustHaveExec(t)
    73  
    74  	tmpDir := t.TempDir()
    75  	executable := "execabs-test"
    76  	if runtime.GOOS == "windows" {
    77  		executable += ".exe"
    78  	}
    79  	if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
    80  		t.Fatalf("os.WriteFile failed: %s", err)
    81  	}
    82  	cwd, err := os.Getwd()
    83  	if err != nil {
    84  		t.Fatalf("os.Getwd failed: %s", err)
    85  	}
    86  	defer os.Chdir(cwd)
    87  	if err = os.Chdir(tmpDir); err != nil {
    88  		t.Fatalf("os.Chdir failed: %s", err)
    89  	}
    90  	if runtime.GOOS != "windows" {
    91  		// add "." to PATH so that exec.LookPath looks in the current directory on
    92  		// non-windows platforms as well
    93  		origPath := os.Getenv("PATH")
    94  		defer os.Setenv("PATH", origPath)
    95  		os.Setenv("PATH", fmt.Sprintf(".:%s", origPath))
    96  	}
    97  	expectedErr := fmt.Sprintf("execabs-test resolves to executable relative to current directory (.%c%s)", filepath.Separator, executable)
    98  	if _, err := LookPath("execabs-test"); err == nil {
    99  		t.Fatalf("LookPath didn't fail when finding a non-relative path")
   100  	} else if err.Error() != expectedErr {
   101  		t.Errorf("LookPath returned unexpected error: want %q, got %q", expectedErr, err.Error())
   102  	}
   103  }
   104  

View as plain text