Skip to content

Commit

Permalink
Add support for tools/bazel.ps1 and tools/bazel.bat on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Jan 24, 2023
1 parent b484ae2 commit 7275167
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 10 deletions.
8 changes: 7 additions & 1 deletion core/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
Expand All @@ -16,3 +16,9 @@ go_library(
"@com_github_mitchellh_go_homedir//:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["core_test.go"],
embed = [":go_default_library"],
)
35 changes: 26 additions & 9 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os/signal"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -456,23 +457,39 @@ func linkLocalBazel(baseDirectory string, bazelPath string) (string, error) {
return destinationPath, nil
}

func maybeDelegateToWrapper(bazel string) string {
if GetEnvOrConfig(skipWrapperEnv) != "" {
return bazel
}

wd, err := os.Getwd()
if err != nil {
func maybeDelegateToWrapperFromDir(bazel string, wd string, ignoreEnv bool) string {
if !ignoreEnv && GetEnvOrConfig(skipWrapperEnv) != "" {
return bazel
}

root := findWorkspaceRoot(wd)
wrapper := filepath.Join(root, wrapperPath)
if stat, err := os.Stat(wrapper); err != nil || stat.IsDir() || stat.Mode().Perm()&0111 == 0 {
if stat, err := os.Stat(wrapper); err == nil && !stat.Mode().IsDir() && stat.Mode().Perm()&0111 != 0 {
return wrapper
}

if runtime.GOOS == "windows" {
powershell_wrapper := filepath.Join(root, wrapperPath + ".ps1")
if stat, err := os.Stat(powershell_wrapper); err == nil && !stat.Mode().IsDir() {
return powershell_wrapper
}

batch_wrapper := filepath.Join(root, wrapperPath + ".bat")
if stat, err := os.Stat(batch_wrapper); err == nil && !stat.Mode().IsDir() {
return batch_wrapper
}
}

return bazel
}

func maybeDelegateToWrapper(bazel string) string {
wd, err := os.Getwd()
if err != nil {
return bazel
}

return wrapper
return maybeDelegateToWrapperFromDir(bazel, wd, false)
}

func prependDirToPathList(cmd *exec.Cmd, dir string) {
Expand Down
155 changes: 155 additions & 0 deletions core/core_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package core

import (
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"testing"
)

func TestMaybeDelegateToNoWrapper(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "TestMaybeDelegateToNoWrapper")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)

os.MkdirAll(tmpDir, os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "WORKSPACE"), []byte(""), 0600)
ioutil.WriteFile(filepath.Join(tmpDir, "BUILD"), []byte(""), 0600)

entrypoint := maybeDelegateToWrapperFromDir("bazel_real", tmpDir, true)
expected := "bazel_real"

if entrypoint != expected {
t.Fatalf("Expected to delegate bazel to %q, but got %q", expected, entrypoint)
}
}

func TestMaybeDelegateToNoNonExecutableWrapper(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "TestMaybeDelegateToNoNonExecutableWrapper")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)

os.MkdirAll(tmpDir, os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "WORKSPACE"), []byte(""), 0600)
ioutil.WriteFile(filepath.Join(tmpDir, "BUILD"), []byte(""), 0600)

os.MkdirAll(filepath.Join(tmpDir, "tools"), os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "tools", "bazel"), []byte(""), 0600)

entrypoint := maybeDelegateToWrapperFromDir("bazel_real", tmpDir, true)
expected := "bazel_real"

if entrypoint != expected {
t.Fatalf("Expected to delegate bazel to %q, but got %q", expected, entrypoint)
}
}

func TestMaybeDelegateToStandardWrapper(t *testing.T) {
var tmpDir, err = ioutil.TempDir("", "TestMaybeDelegateToStandardWrapper")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)

os.MkdirAll(tmpDir, os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "WORKSPACE"), []byte(""), 0600)
ioutil.WriteFile(filepath.Join(tmpDir, "BUILD"), []byte(""), 0600)

os.MkdirAll(filepath.Join(tmpDir, "tools"), os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "tools", "bazel"), []byte(""), 0700)

entrypoint := maybeDelegateToWrapperFromDir("bazel_real", tmpDir, true)
expected := filepath.Join(tmpDir, "tools", "bazel")

if entrypoint != expected {
t.Fatalf("Expected to delegate bazel to %q, but got %q", expected, entrypoint)
}
}

func TestMaybeDelegateToPowershellWrapper(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "TestMaybeDelegateToPowershellWrapper")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)

os.MkdirAll(tmpDir, os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "WORKSPACE"), []byte(""), 0600)
ioutil.WriteFile(filepath.Join(tmpDir, "BUILD"), []byte(""), 0600)

os.MkdirAll(filepath.Join(tmpDir, "tools"), os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "tools", "bazel.ps1"), []byte(""), 0700)

entrypoint := maybeDelegateToWrapperFromDir("bazel_real", tmpDir, true)
expected := filepath.Join(tmpDir, "tools", "bazel.ps1")

// Only windows platforms use powershell wrappers
if runtime.GOOS != "windows" {
expected = "bazel_real"
}

if entrypoint != expected {
t.Fatalf("Expected to delegate bazel to %q, but got %q", expected, entrypoint)
}
}

func TestMaybeDelegateToBatchWrapper(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "TestMaybeDelegateToBatchWrapper")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)

os.MkdirAll(tmpDir, os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "WORKSPACE"), []byte(""), 0600)
ioutil.WriteFile(filepath.Join(tmpDir, "BUILD"), []byte(""), 0600)

os.MkdirAll(filepath.Join(tmpDir, "tools"), os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "tools", "bazel.bat"), []byte(""), 0700)

entrypoint := maybeDelegateToWrapperFromDir("bazel_real", tmpDir, true)
expected := filepath.Join(tmpDir, "tools", "bazel.bat")

// Only windows platforms use batch wrappers
if runtime.GOOS != "windows" {
expected = "bazel_real"
}

if entrypoint != expected {
t.Fatalf("Expected to delegate bazel to %q, but got %q", expected, entrypoint)
}
}

func TestMaybeDelegateToPowershellOverBatchWrapper(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "TestMaybeDelegateToPowershellOverBatchWrapper")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)

os.MkdirAll(tmpDir, os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "WORKSPACE"), []byte(""), 0600)
ioutil.WriteFile(filepath.Join(tmpDir, "BUILD"), []byte(""), 0600)

os.MkdirAll(filepath.Join(tmpDir, "tools"), os.ModeDir | 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "tools", "bazel.ps1"), []byte(""), 0700)
ioutil.WriteFile(filepath.Join(tmpDir, "tools", "bazel.bat"), []byte(""), 0700)

entrypoint := maybeDelegateToWrapperFromDir("bazel_real", tmpDir, true)
expected := filepath.Join(tmpDir, "tools", "bazel.ps1")

// Only windows platforms use powershell or batch wrappers
if runtime.GOOS != "windows" {
expected = "bazel_real"
}

if entrypoint != expected {
t.Fatalf("Expected to delegate bazel to %q, but got %q", expected, entrypoint)
}
}

0 comments on commit 7275167

Please sign in to comment.