aboutsummaryrefslogtreecommitdiff
path: root/tests/core/runfiles
diff options
context:
space:
mode:
Diffstat (limited to 'tests/core/runfiles')
-rw-r--r--tests/core/runfiles/BUILD.bazel48
-rw-r--r--tests/core/runfiles/README.rst23
-rw-r--r--tests/core/runfiles/bin.go17
-rw-r--r--tests/core/runfiles/check_runfiles.go121
-rw-r--r--tests/core/runfiles/empty_bin.go3
-rw-r--r--tests/core/runfiles/local_file.txt0
-rw-r--r--tests/core/runfiles/local_group.txt0
-rw-r--r--tests/core/runfiles/runfiles_cmd.go28
-rw-r--r--tests/core/runfiles/runfiles_remote_test/BUILD.bazel27
-rw-r--r--tests/core/runfiles/runfiles_remote_test/WORKSPACE1
-rw-r--r--tests/core/runfiles/runfiles_remote_test/remote_file.txt0
-rw-r--r--tests/core/runfiles/runfiles_remote_test/remote_group.txt0
-rw-r--r--tests/core/runfiles/runfiles_test.go27
13 files changed, 295 insertions, 0 deletions
diff --git a/tests/core/runfiles/BUILD.bazel b/tests/core/runfiles/BUILD.bazel
new file mode 100644
index 00000000..c7db6d3d
--- /dev/null
+++ b/tests/core/runfiles/BUILD.bazel
@@ -0,0 +1,48 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
+
+package(default_visibility = ["//visibility:public"])
+
+test_suite(
+ name = "runfiles_tests",
+ tests = [
+ ":local_test",
+ "@runfiles_remote_test//:remote_test",
+ ],
+)
+
+go_test(
+ name = "local_test",
+ srcs = ["runfiles_test.go"],
+ deps = [":check_runfiles"],
+)
+
+go_binary(
+ name = "local_cmd",
+ srcs = ["runfiles_cmd.go"],
+ deps = [":check_runfiles"],
+)
+
+go_binary(
+ name = "local_bin",
+ srcs = ["empty_bin.go"],
+)
+
+go_library(
+ name = "check_runfiles",
+ srcs = ["check_runfiles.go"],
+ data = [
+ "local_file.txt",
+ ":local_bin",
+ ":local_group",
+ "@runfiles_remote_test//:remote_bin",
+ "@runfiles_remote_test//:remote_file.txt",
+ "@runfiles_remote_test//:remote_group",
+ ],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/runfiles/check",
+ deps = ["//go/tools/bazel:go_default_library"],
+)
+
+filegroup(
+ name = "local_group",
+ srcs = ["local_group.txt"],
+)
diff --git a/tests/core/runfiles/README.rst b/tests/core/runfiles/README.rst
new file mode 100644
index 00000000..ec357826
--- /dev/null
+++ b/tests/core/runfiles/README.rst
@@ -0,0 +1,23 @@
+Runfiles functionality
+=====================
+
+runfiles_tests
+--------------
+
+Checks that functions in ``//go/tools/bazel:go_default_library`` that
+provide access to runfiles behave correctly. In particular, this checks:
+
+* ``Runfile`` works for regular files.
+* ``FindBinary`` works for binaries.
+* ``ListRunfiles`` lists all expected files.
+* These functions work for runfiles in the local workspace and for files in
+ external repositories (``@runfiles_remote_test`` is a ``local_repository``
+ that points to a subdirectory here).
+* These functions work in tests invoked with ``bazel test`` and
+ binaries invoked with ``bazel run``.
+* These functions work on Windows and other platforms. Bazel doesn't
+ create a symlink tree for runfiles on Windows since symbolic links
+ can't be created without administrative privilege by default.
+
+TODO: Verify binary behavior in CI. The ``local_bin`` and ``remote_bin``
+targets verify behavior for binaries, but they are not tests.
diff --git a/tests/core/runfiles/bin.go b/tests/core/runfiles/bin.go
new file mode 100644
index 00000000..0ced7a9e
--- /dev/null
+++ b/tests/core/runfiles/bin.go
@@ -0,0 +1,17 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+func main() {}
diff --git a/tests/core/runfiles/check_runfiles.go b/tests/core/runfiles/check_runfiles.go
new file mode 100644
index 00000000..8858a573
--- /dev/null
+++ b/tests/core/runfiles/check_runfiles.go
@@ -0,0 +1,121 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package check
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "runtime"
+ "sort"
+ "strings"
+
+ "github.com/bazelbuild/rules_go/go/tools/bazel"
+)
+
+type TestFile struct {
+ Workspace, ShortPath, Path string
+ Binary bool
+}
+
+var DefaultTestFiles = []TestFile{
+ {Workspace: "io_bazel_rules_go", Path: "tests/core/runfiles/local_file.txt"},
+ {Workspace: "io_bazel_rules_go", Path: "tests/core/runfiles/local_group.txt"},
+ {Workspace: "io_bazel_rules_go", Path: "tests/core/runfiles/local_bin", Binary: true},
+ {Workspace: "runfiles_remote_test", Path: "remote_file.txt"},
+ {Workspace: "runfiles_remote_test", Path: "remote_group.txt"},
+ {Workspace: "runfiles_remote_test", Path: "remote_bin", Binary: true},
+}
+
+func CheckRunfiles(files []TestFile) error {
+ // Check that the runfiles directory matches the current workspace.
+ // There is no runfiles directory on Windows.
+ if runtime.GOOS != "windows" {
+ dir, err := bazel.RunfilesPath()
+ if err != nil {
+ return err
+ }
+ root, base := filepath.Dir(dir), filepath.Base(dir)
+ if !strings.HasSuffix(root, ".runfiles") {
+ return fmt.Errorf("RunfilesPath: %q is not a .runfiles directory", dir)
+ }
+ workspace := os.Getenv("TEST_WORKSPACE")
+ if workspace != "" && workspace != base {
+ return fmt.Errorf("RunfilesPath: %q does not match test workspace %s", dir, workspace)
+ }
+ if srcDir := os.Getenv("TEST_SRCDIR"); srcDir != "" && filepath.Join(srcDir, workspace) != dir {
+ return fmt.Errorf("RunfilesPath: %q does not match TEST_SRCDIR %q", dir, srcDir)
+ }
+ }
+
+ // Check that files can be found with Runfile or FindBinary.
+ // Make sure the paths returned are absolute paths to actual files.
+ seen := make(map[string]string)
+ for _, f := range files {
+ var got string
+ var err error
+ if !f.Binary {
+ if got, err = bazel.Runfile(f.Path); err != nil {
+ return err
+ }
+ if !filepath.IsAbs(got) {
+ return fmt.Errorf("Runfile %s: got a relative path %q; want absolute", f.Path, got)
+ }
+ seen[f.Path] = got
+ } else {
+ var pkg, name string
+ if i := strings.LastIndex(f.Path, "/"); i < 0 {
+ name = f.Path
+ } else {
+ pkg = f.Path[:i]
+ name = f.Path[i+1:]
+ }
+ var ok bool
+ if got, ok = bazel.FindBinary(pkg, name); !ok {
+ return fmt.Errorf("FindBinary %s %s: could not find binary", pkg, name)
+ }
+ if !filepath.IsAbs(got) {
+ return fmt.Errorf("FindBinary %s %s: got a relative path %q; want absolute", pkg, name, got)
+ }
+ }
+
+ if _, err := os.Stat(got); err != nil {
+ return fmt.Errorf("%s: could not stat: %v", f.Path, err)
+ }
+ }
+
+ // Check that the files can be listed.
+ entries, err := bazel.ListRunfiles()
+ if err != nil {
+ return err
+ }
+ for _, e := range entries {
+ if want, ok := seen[e.ShortPath]; ok && want != e.Path {
+ return err
+ }
+ delete(seen, e.ShortPath)
+ }
+
+ if len(seen) > 0 {
+ unseen := make([]string, 0, len(seen))
+ for short := range seen {
+ unseen = append(unseen, short)
+ }
+ sort.Strings(unseen)
+ return fmt.Errorf("ListRunfiles did not include files:\n\t%s", strings.Join(unseen, "\n\t"))
+ }
+
+ return nil
+}
diff --git a/tests/core/runfiles/empty_bin.go b/tests/core/runfiles/empty_bin.go
new file mode 100644
index 00000000..38dd16da
--- /dev/null
+++ b/tests/core/runfiles/empty_bin.go
@@ -0,0 +1,3 @@
+package main
+
+func main() {}
diff --git a/tests/core/runfiles/local_file.txt b/tests/core/runfiles/local_file.txt
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/core/runfiles/local_file.txt
diff --git a/tests/core/runfiles/local_group.txt b/tests/core/runfiles/local_group.txt
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/core/runfiles/local_group.txt
diff --git a/tests/core/runfiles/runfiles_cmd.go b/tests/core/runfiles/runfiles_cmd.go
new file mode 100644
index 00000000..0ab124b5
--- /dev/null
+++ b/tests/core/runfiles/runfiles_cmd.go
@@ -0,0 +1,28 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/bazelbuild/rules_go/tests/core/runfiles/check"
+)
+
+func main() {
+ if err := check.CheckRunfiles(check.DefaultTestFiles); err != nil {
+ fmt.Fprintln(os.Stderr, err.Error())
+ }
+}
diff --git a/tests/core/runfiles/runfiles_remote_test/BUILD.bazel b/tests/core/runfiles/runfiles_remote_test/BUILD.bazel
new file mode 100644
index 00000000..112dcd35
--- /dev/null
+++ b/tests/core/runfiles/runfiles_remote_test/BUILD.bazel
@@ -0,0 +1,27 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test")
+
+package(default_visibility = ["//visibility:public"])
+
+go_test(
+ name = "remote_test",
+ srcs = ["@io_bazel_rules_go//tests/core/runfiles:runfiles_test.go"],
+ deps = ["@io_bazel_rules_go//tests/core/runfiles:check_runfiles"],
+)
+
+go_binary(
+ name = "remote_cmd",
+ srcs = ["@io_bazel_rules_go//tests/core/runfiles:runfiles_cmd.go"],
+ deps = ["@io_bazel_rules_go//tests/core/runfiles:check_runfiles"],
+)
+
+go_binary(
+ name = "remote_bin",
+ srcs = ["@io_bazel_rules_go//tests/core/runfiles:empty_bin.go"],
+)
+
+filegroup(
+ name = "remote_group",
+ srcs = ["remote_group.txt"],
+)
+
+exports_files(["remote_file.txt"])
diff --git a/tests/core/runfiles/runfiles_remote_test/WORKSPACE b/tests/core/runfiles/runfiles_remote_test/WORKSPACE
new file mode 100644
index 00000000..c9af3f85
--- /dev/null
+++ b/tests/core/runfiles/runfiles_remote_test/WORKSPACE
@@ -0,0 +1 @@
+workspace(name = "runfiles_remote_test")
diff --git a/tests/core/runfiles/runfiles_remote_test/remote_file.txt b/tests/core/runfiles/runfiles_remote_test/remote_file.txt
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/core/runfiles/runfiles_remote_test/remote_file.txt
diff --git a/tests/core/runfiles/runfiles_remote_test/remote_group.txt b/tests/core/runfiles/runfiles_remote_test/remote_group.txt
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/core/runfiles/runfiles_remote_test/remote_group.txt
diff --git a/tests/core/runfiles/runfiles_test.go b/tests/core/runfiles/runfiles_test.go
new file mode 100644
index 00000000..62aab3af
--- /dev/null
+++ b/tests/core/runfiles/runfiles_test.go
@@ -0,0 +1,27 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "testing"
+
+ "github.com/bazelbuild/rules_go/tests/core/runfiles/check"
+)
+
+func Test(t *testing.T) {
+ if err := check.CheckRunfiles(check.DefaultTestFiles); err != nil {
+ t.Fatal(err)
+ }
+}