diff options
author | Ted Xie <tedx@google.com> | 2023-03-14 08:21:07 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-03-14 08:21:39 -0700 |
commit | 0d84923b7fc9701d261ae353b6a2b37bf5dc93c3 (patch) | |
tree | 0178c9b884243f7cc1e3a27a944f9fe9461e8a80 | |
parent | 263b164a0879021682e7341381c687789fd532b6 (diff) | |
download | bazelbuild-rules_android-0d84923b7fc9701d261ae353b6a2b37bf5dc93c3.tar.gz |
Introduce new rlocation helper function for AK
PiperOrigin-RevId: 516530149
Change-Id: Iad2eaac6ffeb1b86c955cd70ec345c6c22253228
-rw-r--r-- | prereqs.bzl | 6 | ||||
-rw-r--r-- | src/common/golang/BUILD | 20 | ||||
-rw-r--r-- | src/common/golang/runfilelocation.go | 40 | ||||
-rw-r--r-- | src/common/golang/runfilelocation_test.go | 51 |
4 files changed, 114 insertions, 3 deletions
diff --git a/prereqs.bzl b/prereqs.bzl index d106a88..c2dc015 100644 --- a/prereqs.bzl +++ b/prereqs.bzl @@ -59,10 +59,10 @@ def rules_android_prereqs(): maybe( http_archive, name = "io_bazel_rules_go", - sha256 = "16e9fca53ed6bd4ff4ad76facc9b7b651a89db1689a2877d6fd7b82aa824e366", + sha256 = "dd926a88a564a9246713a9c00b35315f54cbd46b31a26d5d8fb264c07045f05d", urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip", - "https://github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip", + "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.38.1/rules_go-v0.38.1.zip", + "https://github.com/bazelbuild/rules_go/releases/download/v0.38.1/rules_go-v0.38.1.zip", ], ) diff --git a/src/common/golang/BUILD b/src/common/golang/BUILD index 23d2761..4c1bf0f 100644 --- a/src/common/golang/BUILD +++ b/src/common/golang/BUILD @@ -94,3 +94,23 @@ go_library( srcs = ["flagfile.go"], importpath = "src/common/golang/flagfile", ) + +genrule( + name = "a_txt", + outs = ["a.txt"], + cmd = "echo hello world > $@", +) + +go_library( + name = "runfilelocation", + srcs = ["runfilelocation.go"], + importpath = "src/common/golang/runfilelocation", + deps = ["@io_bazel_rules_go//go/runfiles"], +) + +go_test( + name = "runfilelocation_test", + srcs = ["runfilelocation_test.go"], + data = [":a_txt"], + embed = [":runfilelocation"], +) diff --git a/src/common/golang/runfilelocation.go b/src/common/golang/runfilelocation.go new file mode 100644 index 0000000..b2eb5d6 --- /dev/null +++ b/src/common/golang/runfilelocation.go @@ -0,0 +1,40 @@ +// Copyright 2023 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 runfilelocation provides utility functions to deal with runfiles + +package runfilelocation + +import ( + "os" + "path" + + "github.com/bazelbuild/rules_go/go/runfiles" +) + +// Find determines the absolute path to a given runfile +func Find(runfilePath string) (string, error) { + runfileLocation, err := runfiles.Rlocation(path.Join(os.Getenv("TEST_WORKSPACE"), runfilePath)) + + if err != nil { + return "", err + } + + // Check if file exists + if _, err := os.Stat(runfileLocation); err != nil { + return "", err + } + + return runfileLocation, err +} diff --git a/src/common/golang/runfilelocation_test.go b/src/common/golang/runfilelocation_test.go new file mode 100644 index 0000000..b2f3fb4 --- /dev/null +++ b/src/common/golang/runfilelocation_test.go @@ -0,0 +1,51 @@ +// Copyright 2023 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 runfilelocation + +import ( + "io/ioutil" + "testing" +) + +func TestValidRunfileLocation(t *testing.T) { + // Check that Find() returns a valid path to a runfile + runfilePath := "src/common/golang/a.txt" + + absRunFilePath, err := Find(runfilePath) + if err != nil { + t.Errorf("Runfile path through Runfilelocation() failed: %v", err) + } + + // Check that the path actually exists + contents, err := ioutil.ReadFile(absRunFilePath) + text := string(contents) + if err != nil { + t.Errorf("Could not read file: %v", err) + } + + if text != "hello world\n" { + t.Errorf("Expected 'hello world' in file, got %v instead.", text) + } +} + +func TestInvalidRunfileLocation(t *testing.T) { + invalidRunfilePath := "src/common/golang/b.txt" + + runfileLocationShouldNotExist, err := Find(invalidRunfilePath) + if err == nil { + // If no error, that means the library is not properly detecting invalid runfile paths + t.Errorf("Expected error: %v should not be a valid path. Returned %v", invalidRunfilePath, runfileLocationShouldNotExist) + } +} |