Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tools/bazel.ps1 and tools/bazel.bat on Windows #417

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" {
powershellWrapper := filepath.Join(root, wrapperPath + ".ps1")
if stat, err := os.Stat(powershellWrapper); err == nil && !stat.Mode().IsDir() {
return powershellWrapper
}

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

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
169 changes: 169 additions & 0 deletions core/core_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
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) {
// It's not guaranteed that `tools/bazel` is executable on the
// Windows host running this test. Thus the test is skipped on
// this platform to guarantee consistent results.
if runtime.GOOS == "windows" {
return
}

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) {
// It's not guaranteed that `tools/bazel` is executable on the
// Windows host running this test. Thus the test is skipped on
// this platform to guarantee consistent results.
if runtime.GOOS == "windows" {
return
}

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)
}
}