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

Return resource's short_path when fail to find relative #1010

Merged
merged 8 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 9 additions & 3 deletions scala/private/resources.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def paths(resources, resource_strip_prefix):
return [(_target_path(resource, resource_strip_prefix), resource.path) for resource in resources]

def _target_path(resource, resource_strip_prefix):
path = _target_path_by_strip_prefix(resource, resource_strip_prefix) if resource_strip_prefix else _target_path_by_default_prefixes(resource.path)
path = _target_path_by_strip_prefix(resource, resource_strip_prefix) if resource_strip_prefix else _target_path_by_default_prefixes(resource)
return _strip_prefix(path, "/")

def _target_path_by_strip_prefix(resource, resource_strip_prefix):
Expand All @@ -31,7 +31,9 @@ def _target_path_by_strip_prefix(resource, resource_strip_prefix):
fail("Resource file %s is not under the specified prefix %s to strip" % (path, prefix))
return path[len(prefix):]

def _target_path_by_default_prefixes(path):
def _target_path_by_default_prefixes(resource):
path = resource.path

# Here we are looking to find out the offset of this resource inside
# any resources folder. We want to return the root to the resources folder
# and then the sub path inside it
Expand All @@ -44,7 +46,11 @@ def _target_path_by_default_prefixes(path):
if rel_path:
return rel_path

return path
# Both short_path and path have quirks we wish to avoid, in short_path there are times where
# it is prefixed by `../` instead of `external/`. And in .path it will instead return the entire
# bazel-out/... path, which is also wanting to be avoided. So instead, we return the short-path if
# path starts with bazel-out and the entire path if it does not.
return resource.short_path if path.startswith("bazel-out") else path

def _strip_prefix(path, prefix):
return path[len(prefix):] if path.startswith(prefix) else path
12 changes: 12 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,15 @@ scala_junit_test(
tests_from = [":JunitMixedSeparateTarget"],
runtime_deps = [":JunitMixedSeparateTarget"],
)

py_binary(
name = "py_resource_binary",
srcs = ["py_resource.py"],
main = "py_resource.py",
)

scala_test(
name = "ScalaTestResourcesFromLocalTargetTest",
srcs = ["ScalaTestResourcesFromLocalTargetTest.scala"],
resources = [":py_resource_binary"],
)
8 changes: 8 additions & 0 deletions test/ScalaTestResourcesFromLocalTargetTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package scalarules.test

class ScalaTestResourcesFromLocalTargetTest extends org.scalatest.FlatSpec {
"scala_test's resources when referencing local target" should "assert that local target is not placed in bazel-out, but rather next to the generated code" in {
ittaiz marked this conversation as resolved.
Show resolved Hide resolved
assert(getClass.getResource("/bazel-out/darwin-fastbuild/bin/test/py_resource_binary") == null)
ittaiz marked this conversation as resolved.
Show resolved Hide resolved
assert(getClass.getResource("/test/py_resource_binary") != null)
}
}
1 change: 1 addition & 0 deletions test/py_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty file just need to be used for reference