github.com/bazelbuild/bazel-watcher@v0.25.2/internal/e2e/common.go (about)

     1  package e2e
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/bazelbuild/rules_go/go/tools/bazel"
    11  )
    12  
    13  var ibazelPath = getiBazelPath()
    14  
    15  func getiBazelPath() string {
    16  	path, ok := bazel.FindBinary("cmd/ibazel", "ibazel")
    17  	if !ok {
    18  		panic("Failed to locate binary //ibazel:ibazel, please add it as a data dependency")
    19  	}
    20  	return path
    21  }
    22  
    23  func Must(t *testing.T, e error) {
    24  	t.Helper()
    25  	if e != nil {
    26  		t.Fatalf("Error: %s", e)
    27  		//t.Logf("Stack trace:\n%s", string(debug.Stack()))
    28  	}
    29  }
    30  
    31  func MustMkdir(t *testing.T, path string, mode ...os.FileMode) {
    32  	t.Helper()
    33  
    34  	m := os.FileMode(0777)
    35  	if len(mode) == 1 {
    36  		m = mode[0]
    37  	}
    38  
    39  	if err := os.MkdirAll(path, m); err != nil {
    40  		t.Fatalf("os.MkdirAll(%q, %d): %v", path, m, err)
    41  	}
    42  }
    43  
    44  func MustWriteFile(t *testing.T, path, content string, mode ...os.FileMode) {
    45  	t.Helper()
    46  
    47  	m := os.FileMode(0777)
    48  	if len(mode) == 1 {
    49  		m = mode[0]
    50  	}
    51  
    52  	if err := ioutil.WriteFile(path, []byte(content), m); err != nil {
    53  		t.Fatalf("ioutil.WriteFile(%q, []byte(%q), 0777): %v", path, content, err)
    54  	}
    55  }
    56  
    57  func SetExecuteBit(t *testing.T) {
    58  	// Before doing anything, set the executable bit on all the .sh files
    59  	err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
    60  		if err != nil {
    61  			return err
    62  		}
    63  
    64  		if strings.HasSuffix(path, ".sh") {
    65  			if err := os.Chmod(path, 0777); err != nil {
    66  				t.Fatalf("Error os.Chmod(%q, 0777): %v", path, err)
    67  			}
    68  		}
    69  
    70  		return nil
    71  	})
    72  
    73  	if err != nil {
    74  		t.Fatalf("filpath.Walk(): %v", err)
    75  	}
    76  }
    77  
    78  func SetUp(t *testing.T) *IBazelTester {
    79  	SetExecuteBit(t)
    80  	return NewIBazelTester(t)
    81  }