github.com/bazelbuild/rules_webtesting@v0.2.0/go/bazel/bazel.go (about)

     1  // Copyright 2016 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  
    15  // Package bazel provides utilities for interacting with the surrounding Bazel environment.
    16  package bazel
    17  
    18  import (
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  )
    24  
    25  // DefaultWorkspace is the name of the default Bazel workspace.
    26  var DefaultWorkspace = "io_bazel_rules_webtesting"
    27  
    28  // Runfile returns an absolute path to the specified file in the runfiles directory of the running target.
    29  // It searches the current working directory, RunfilesPath() directory, and RunfilesPath()/TestWorkspace().
    30  // Returns an error if unable to locate RunfilesPath() or if the file does not exist.
    31  func Runfile(path string) (string, error) {
    32  	if _, err := os.Stat(path); err == nil {
    33  		// absolute path or found in current working directory
    34  		return path, nil
    35  	}
    36  
    37  	runfiles, err := RunfilesPath()
    38  	if err != nil {
    39  		return "", err
    40  	}
    41  
    42  	filename := filepath.Join(runfiles, path)
    43  	if _, err := os.Stat(filename); err == nil {
    44  		// found at RunfilesPath()/path
    45  		return filename, nil
    46  	}
    47  
    48  	filename = filepath.Join(runfiles, TestWorkspace(), path)
    49  	if _, err := os.Stat(filename); err == nil {
    50  		// found at RunfilesPath()/TestWorkspace()/path
    51  		return filename, nil
    52  	}
    53  
    54  	return "", fmt.Errorf("unable to find file %q", path)
    55  }
    56  
    57  // RunfilesPath return the path to the run files tree for this test.
    58  // It returns an error if TEST_SRCDIR does not exist.
    59  func RunfilesPath() (string, error) {
    60  	const srcEnv = "TEST_SRCDIR"
    61  	if src, ok := os.LookupEnv(srcEnv); ok {
    62  		return src, nil
    63  	}
    64  	return "", fmt.Errorf("environment variable %q is not defined, are you running with bazel test", srcEnv)
    65  }
    66  
    67  // NewTmpDir creates a new temporary directory in TestTmpDir().
    68  func NewTmpDir(prefix string) (string, error) {
    69  	return ioutil.TempDir(TestTmpDir(), prefix)
    70  }
    71  
    72  // TestTmpDir returns the path the Bazel test temp directory.
    73  // If TEST_TMPDIR is not defined, it returns the OS default temp dir.
    74  func TestTmpDir() string {
    75  	if tmp, ok := os.LookupEnv("TEST_TMPDIR"); ok {
    76  		return tmp
    77  	}
    78  	return os.TempDir()
    79  }
    80  
    81  // TestWorkspace returns the name of the Bazel workspace for this test.
    82  // If TEST_WORKSPACE is not defined, it returns DefaultWorkspace.
    83  func TestWorkspace() string {
    84  	if ws, ok := os.LookupEnv("TEST_WORKSPACE"); ok {
    85  		return ws
    86  	}
    87  	return DefaultWorkspace
    88  }