2021-11-09 23:13:30 +00:00
|
|
|
"""unit tests for paths"""
|
|
|
|
|
2022-08-20 19:53:12 +00:00
|
|
|
load("@bazel_skylib//lib:partial.bzl", "partial")
|
2021-11-09 23:13:30 +00:00
|
|
|
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
|
|
|
|
load("//lib/private:paths.bzl", "paths")
|
|
|
|
|
|
|
|
def _relative_file_test_impl(ctx):
|
|
|
|
env = unittest.begin(ctx)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"lib/requirements.in",
|
|
|
|
paths.relative_file(
|
|
|
|
"bazel/python/internal/pip/test/lib/requirements.in",
|
|
|
|
"bazel/python/internal/pip/test/requirements.out",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"pip/test/lib/requirements.in",
|
|
|
|
paths.relative_file(
|
|
|
|
"bazel/python/internal/pip/test/lib/requirements.in",
|
|
|
|
"bazel/python/internal/requirements.out",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"bazel/python/internal/pip/test/lib/requirements.in",
|
|
|
|
paths.relative_file(
|
|
|
|
"/bazel/python/internal/pip/test/lib/requirements.in",
|
|
|
|
"/requirements.out",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../requirements.in",
|
|
|
|
paths.relative_file(
|
|
|
|
"bazel/python/internal/pip/test/requirements.in",
|
|
|
|
"bazel/python/internal/pip/test/lib/requirements.in",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../requirements.out",
|
|
|
|
paths.relative_file(
|
|
|
|
"bazel/python/internal/pip/test/requirements.out",
|
|
|
|
"bazel/python/internal/pip/test/lib/requirements.in",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"requirements.out",
|
|
|
|
paths.relative_file(
|
|
|
|
"bazel/python/internal/pip/example/service/requirements.out",
|
|
|
|
"bazel/python/internal/pip/example/service/requirements.in",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../service/requirements.out",
|
|
|
|
paths.relative_file(
|
|
|
|
"bazel/python/internal/pip/example/service/requirements.out",
|
|
|
|
"bazel/python/internal/pip/example/lib/requirements.in",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../lib/bar/requirements.in",
|
|
|
|
paths.relative_file(
|
|
|
|
"bazel/python/internal/pip/example/lib/bar/requirements.in",
|
|
|
|
"bazel/python/internal/pip/example/service/requirements.out",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../../../example/lib/bar/requirements.in",
|
|
|
|
paths.relative_file(
|
|
|
|
"bazel/python/internal/pip/example/lib/bar/requirements.in",
|
|
|
|
"bazel/python/internal/pip/lib/example/service/requirements.out",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"requirements.in",
|
|
|
|
paths.relative_file(
|
|
|
|
"requirements.in",
|
|
|
|
"requirements.out",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"requirements.in",
|
|
|
|
paths.relative_file(
|
|
|
|
"/bazel/requirements.in",
|
|
|
|
"/bazel/requirements.out",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../requirements.in",
|
|
|
|
paths.relative_file(
|
|
|
|
"/requirements.in",
|
|
|
|
"/bazel/requirements.out",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-06-20 21:21:49 +00:00
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../../../../../repo/some/external/repo/short/path.txt",
|
|
|
|
paths.relative_file(
|
|
|
|
"../repo/some/external/repo/short/path.txt",
|
|
|
|
"some/main/repo/short/path.txt",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../../../../../../../../repo/some/external/repo/short/path.txt",
|
|
|
|
paths.relative_file(
|
|
|
|
"../../../../repo/some/external/repo/short/path.txt",
|
|
|
|
"some/main/repo/short/path.txt",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../../../../repo/some/external/repo/short/path.txt",
|
|
|
|
paths.relative_file(
|
|
|
|
"repo/some/external/repo/short/path.txt",
|
|
|
|
"../some/main/repo/short/path.txt",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../../../../repo/some/external/repo/short/path.txt",
|
|
|
|
paths.relative_file(
|
|
|
|
"repo/some/external/repo/short/path.txt",
|
|
|
|
"../../../../some/main/repo/short/path.txt",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../../../../../repo/some/external/repo/short/path.txt",
|
|
|
|
paths.relative_file(
|
|
|
|
"../../repo/some/external/repo/short/path.txt",
|
|
|
|
"../some/main/repo/short/path.txt",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../../../../../../../repo/some/external/repo/short/path.txt",
|
|
|
|
paths.relative_file(
|
|
|
|
"../../../../repo/some/external/repo/short/path.txt",
|
|
|
|
"../some/main/repo/short/path.txt",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../../../../repo/some/external/repo/short/path.txt",
|
|
|
|
paths.relative_file(
|
|
|
|
"../repo/some/external/repo/short/path.txt",
|
|
|
|
"../some/main/repo/short/path.txt",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
asserts.equals(
|
|
|
|
env,
|
|
|
|
"../../../../repo/some/external/repo/short/path.txt",
|
|
|
|
paths.relative_file(
|
|
|
|
"../../../repo/some/external/repo/short/path.txt",
|
|
|
|
"../../../some/main/repo/short/path.txt",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2021-11-09 23:13:30 +00:00
|
|
|
return unittest.end(env)
|
|
|
|
|
2023-02-11 20:36:49 +00:00
|
|
|
def _rlocation_path_test_impl(ctx):
|
2021-11-10 14:30:07 +00:00
|
|
|
env = unittest.begin(ctx)
|
2023-02-11 20:36:49 +00:00
|
|
|
asserts.equals(env, "bazel_skylib/LICENSE", paths.to_rlocation_path(ctx, ctx.file.f1))
|
|
|
|
asserts.equals(env, "aspect_bazel_lib/lib/paths.bzl", paths.to_rlocation_path(ctx, ctx.file.f2))
|
2021-11-10 14:30:07 +00:00
|
|
|
return unittest.end(env)
|
|
|
|
|
2023-02-11 20:36:49 +00:00
|
|
|
def _repository_relative_path_test_impl(ctx):
|
2021-11-17 05:30:32 +00:00
|
|
|
env = unittest.begin(ctx)
|
2023-02-11 20:36:49 +00:00
|
|
|
asserts.equals(env, "LICENSE", paths.to_repository_relative_path(ctx.file.f1))
|
|
|
|
asserts.equals(env, "lib/paths.bzl", paths.to_repository_relative_path(ctx.file.f2))
|
2021-11-17 05:30:32 +00:00
|
|
|
return unittest.end(env)
|
|
|
|
|
2022-06-13 18:53:04 +00:00
|
|
|
def _output_relative_path_test_impl(ctx):
|
|
|
|
env = unittest.begin(ctx)
|
|
|
|
asserts.equals(env, "../../../external/bazel_skylib/LICENSE", paths.to_output_relative_path(ctx.file.f1))
|
|
|
|
asserts.equals(env, "../../../lib/paths.bzl", paths.to_output_relative_path(ctx.file.f2))
|
|
|
|
asserts.equals(env, "external/external_test_repo/test_a", paths.to_output_relative_path(ctx.file.f3))
|
|
|
|
asserts.equals(env, "lib/tests/template.txt", paths.to_output_relative_path(ctx.file.f4))
|
|
|
|
return unittest.end(env)
|
|
|
|
|
2021-11-17 05:30:32 +00:00
|
|
|
_ATTRS = {
|
2022-06-13 18:53:04 +00:00
|
|
|
# source file in external repo
|
2021-11-10 14:30:07 +00:00
|
|
|
"f1": attr.label(allow_single_file = True, default = "@bazel_skylib//:LICENSE"),
|
2022-06-13 18:53:04 +00:00
|
|
|
# source file in current repo
|
2021-11-10 14:30:07 +00:00
|
|
|
"f2": attr.label(allow_single_file = True, default = "//lib:paths.bzl"),
|
2022-06-13 18:53:04 +00:00
|
|
|
# output file in external repo
|
|
|
|
"f3": attr.label(allow_single_file = True, default = "@external_test_repo//:test_a"),
|
|
|
|
# output file in current repo
|
|
|
|
"f4": attr.label(allow_single_file = True, default = "//lib/tests:gen_template"),
|
2021-11-17 05:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
relative_file_test = unittest.make(_relative_file_test_impl)
|
2023-02-11 20:36:49 +00:00
|
|
|
rlocation_path_test = unittest.make(_rlocation_path_test_impl, attrs = _ATTRS)
|
2022-06-13 18:53:04 +00:00
|
|
|
output_relative_path_test = unittest.make(_output_relative_path_test_impl, attrs = _ATTRS)
|
2023-02-11 20:36:49 +00:00
|
|
|
repository_relative_path_test = unittest.make(_repository_relative_path_test_impl, attrs = _ATTRS)
|
2021-11-09 23:13:30 +00:00
|
|
|
|
|
|
|
def paths_test_suite():
|
2021-11-10 14:30:07 +00:00
|
|
|
unittest.suite(
|
|
|
|
"paths_tests",
|
2022-08-20 19:53:12 +00:00
|
|
|
partial.make(relative_file_test, timeout = "short"),
|
2023-11-15 23:07:03 +00:00
|
|
|
partial.make(
|
|
|
|
rlocation_path_test,
|
|
|
|
timeout = "short",
|
|
|
|
# TODO: rlocation_path tests don't work under bzlmod
|
|
|
|
target_compatible_with = select({
|
|
|
|
"@aspect_bazel_lib//lib:bzlmod": ["@platforms//:incompatible"],
|
|
|
|
"//conditions:default": [],
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
partial.make(
|
|
|
|
output_relative_path_test,
|
|
|
|
timeout = "short",
|
|
|
|
# TODO: output_relative_path tests don't work under bzlmod
|
|
|
|
target_compatible_with = select({
|
|
|
|
"@aspect_bazel_lib//lib:bzlmod": ["@platforms//:incompatible"],
|
|
|
|
"//conditions:default": [],
|
|
|
|
}),
|
|
|
|
),
|
2023-02-11 20:36:49 +00:00
|
|
|
partial.make(repository_relative_path_test, timeout = "short"),
|
2021-11-17 05:30:32 +00:00
|
|
|
)
|