github.com/0xKiwi/rules_go@v0.24.3/go/tools/bazel/bazel_test.go (about)

     1  // Copyright 2017 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package bazel
    15  
    16  import (
    17  	"fmt"
    18  	"io/ioutil"
    19  	"os"
    20  	"path/filepath"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  // makeAndEnterTempdir creates a temporary directory and chdirs into it.
    26  func makeAndEnterTempdir() (func(), error) {
    27  	oldCwd, err := os.Getwd()
    28  	if err != nil {
    29  		return nil, fmt.Errorf("cannot get path to current directory: %v", err)
    30  	}
    31  
    32  	tempDir, err := ioutil.TempDir("", "test")
    33  	if err != nil {
    34  		return nil, fmt.Errorf("failed to create temporary directory: %v", err)
    35  	}
    36  
    37  	err = os.Chdir(tempDir)
    38  	if err != nil {
    39  		os.RemoveAll(tempDir)
    40  		return nil, fmt.Errorf("cannot enter temporary directory %s: %v", tempDir, err)
    41  	}
    42  
    43  	cleanup := func() {
    44  		defer os.RemoveAll(tempDir)
    45  		defer os.Chdir(oldCwd)
    46  	}
    47  	return cleanup, nil
    48  }
    49  
    50  // createPaths creates a collection of paths for testing purposes.  Paths can end with a /, in
    51  // which case a directory is created; or they can end with a *, in which case an executable file
    52  // is created.  (This matches the nomenclature of "ls -F".)
    53  func createPaths(paths []string) error {
    54  	for _, path := range paths {
    55  		if strings.HasSuffix(path, "/") {
    56  			if err := os.MkdirAll(path, 0755); err != nil {
    57  				return fmt.Errorf("failed to create directory %s: %v", path, err)
    58  			}
    59  		} else {
    60  			mode := os.FileMode(0644)
    61  			if strings.HasSuffix(path, "*") {
    62  				path = path[0 : len(path)-1]
    63  				mode |= 0111
    64  			}
    65  			if err := ioutil.WriteFile(path, []byte{}, mode); err != nil {
    66  				return fmt.Errorf("failed to create file %s with mode %v: %v", path, mode, err)
    67  			}
    68  		}
    69  	}
    70  	return nil
    71  }
    72  
    73  func TestRunfile(t *testing.T) {
    74  	file := "go/tools/bazel/empty.txt"
    75  	runfile, err := Runfile(file)
    76  	if err != nil {
    77  		t.Errorf("When reading file %s got error %s", file, err)
    78  	}
    79  
    80  	// Check that the file actually exist
    81  	if _, err := os.Stat(runfile); err != nil {
    82  		t.Errorf("File found by runfile doesn't exist")
    83  	}
    84  }
    85  
    86  func TestRunfilesPath(t *testing.T) {
    87  	path, err := RunfilesPath()
    88  	if err != nil {
    89  		t.Errorf("Error finding runfiles path: %s", err)
    90  	}
    91  
    92  	if path == "" {
    93  		t.Errorf("Runfiles path is empty: %s", path)
    94  	}
    95  }
    96  
    97  func TestNewTmpDir(t *testing.T) {
    98  	//prefix := "new/temp/dir"
    99  	prefix := "demodir"
   100  	tmpdir, err := NewTmpDir(prefix)
   101  	if err != nil {
   102  		t.Errorf("When creating temp dir %s got error %s", prefix, err)
   103  	}
   104  
   105  	// Check that the tempdir actually exist
   106  	if _, err := os.Stat(tmpdir); err != nil {
   107  		t.Errorf("New tempdir (%s) not created. Got error %s", tmpdir, err)
   108  	}
   109  }
   110  
   111  func TestTestTmpDir(t *testing.T) {
   112  	if TestTmpDir() == "" {
   113  		t.Errorf("TestTmpDir (TEST_TMPDIR) was left empty")
   114  	}
   115  }
   116  
   117  func TestTestWorkspace(t *testing.T) {
   118  	workspace, err := TestWorkspace()
   119  
   120  	if workspace == "" {
   121  		t.Errorf("Workspace is left empty")
   122  	}
   123  
   124  	if err != nil {
   125  		t.Errorf("Unable to get workspace with error %s", err)
   126  	}
   127  }
   128  
   129  func TestFindRunfiles(t *testing.T) {
   130  	testData := []struct {
   131  		name string
   132  
   133  		pathsToCreate []string
   134  		wantRunfiles  string
   135  		wantOk        bool
   136  	}{
   137  		{
   138  			"NoFiles",
   139  			[]string{},
   140  			"",
   141  			false,
   142  		},
   143  		{
   144  			"CurrentDirectory",
   145  			[]string{
   146  				"data-file",
   147  			},
   148  			".",
   149  			true,
   150  		},
   151  		{
   152  			"BazelBinNoConfigurationInPath",
   153  			[]string{
   154  				"bazel-bin/some/package/bin.runfiles/project/",
   155  				"bazel-bin/some/package/bin.runfiles/project/data-file",
   156  				"data-file", // bazel-bin should be preferred.
   157  			},
   158  			"bazel-bin/some/package/bin.runfiles/project",
   159  			true,
   160  		},
   161  		{
   162  			"BazelBinConfigurationInPath",
   163  			[]string{
   164  				"bazel-bin/some/package/amd64/bin.runfiles/project/",
   165  				"bazel-bin/some/package/arm64/bin.runfiles/project/",
   166  				"bazel-bin/some/package/arm64/bin.runfiles/project/data-file",
   167  				"bazel-bin/some/package/powerpc/bin.runfiles/project/",
   168  				"data-file", // bazel-bin should be preferred.
   169  			},
   170  			"bazel-bin/some/package/arm64/bin.runfiles/project",
   171  			true,
   172  		},
   173  	}
   174  	for _, d := range testData {
   175  		t.Run(d.name, func(t *testing.T) {
   176  			cleanup, err := makeAndEnterTempdir()
   177  			if err != nil {
   178  				t.Fatal(err)
   179  			}
   180  			defer cleanup()
   181  
   182  			if err := createPaths(d.pathsToCreate); err != nil {
   183  				t.Fatal(err)
   184  			}
   185  
   186  			runfiles, ok := findRunfiles("project", "some/package", "bin", "data-file")
   187  			if filepath.Clean(runfiles) != filepath.Clean(d.wantRunfiles) || ok != d.wantOk {
   188  				t.Errorf("Got %s, %v; want %s, %v", runfiles, ok, d.wantRunfiles, d.wantOk)
   189  			}
   190  		})
   191  	}
   192  }
   193  
   194  func TestEnterRunfiles(t *testing.T) {
   195  	cleanup, err := makeAndEnterTempdir()
   196  	if err != nil {
   197  		t.Fatal(err)
   198  	}
   199  	defer cleanup()
   200  
   201  	pathsToCreate := []string{
   202  		"bazel-bin/some/package/bin.runfiles/project/",
   203  		"bazel-bin/some/package/bin.runfiles/project/data-file",
   204  	}
   205  	if err := createPaths(pathsToCreate); err != nil {
   206  		t.Fatal(err)
   207  	}
   208  
   209  	if err := EnterRunfiles("project", "some/package", "bin", "data-file"); err != nil {
   210  		t.Fatalf("Cannot enter runfiles tree: %v", err)
   211  	}
   212  	// The cleanup routine returned by makeAndEnterTempdir restores the working directory from
   213  	// the beginning of the test, so we don't have to worry about it here.
   214  
   215  	if _, err := os.Lstat("data-file"); err != nil {
   216  		wd, err := os.Getwd()
   217  		if err != nil {
   218  			t.Errorf("failed to get current working directory: %v", err)
   219  		}
   220  		t.Errorf("data-file not found in current directory (%s); entered invalid runfiles tree?", wd)
   221  	}
   222  }