diff options
Diffstat (limited to 'third_party/bazel/rules_haskell/tests/unit-tests')
-rw-r--r-- | third_party/bazel/rules_haskell/tests/unit-tests/BUILD.bazel | 125 | ||||
-rw-r--r-- | third_party/bazel/rules_haskell/tests/unit-tests/tests.bzl | 83 |
2 files changed, 208 insertions, 0 deletions
diff --git a/third_party/bazel/rules_haskell/tests/unit-tests/BUILD.bazel b/third_party/bazel/rules_haskell/tests/unit-tests/BUILD.bazel new file mode 100644 index 000000000000..990035af685f --- /dev/null +++ b/third_party/bazel/rules_haskell/tests/unit-tests/BUILD.bazel @@ -0,0 +1,125 @@ +load( + ":tests.bzl", + "create_rpath_entry_test", + "dedup_on_test", + "parent_dir_path_test", +) + +parent_dir_path_test( + name = "parent_dir_just_file", + filename = "foo", + output = ["."], +) + +parent_dir_path_test( + name = "parent_dir", + filename = "foo/", + output = ["foo"], +) + +parent_dir_path_test( + name = "parent_dir_file", + filename = "foo/bar", + output = ["foo"], +) + +parent_dir_path_test( + name = "parent_dir_file_dots", + filename = "foo/../bar", + output = [ + "foo", + "..", + ], +) + +parent_dir_path_test( + name = "parent_dir_rooted", + filename = "/foo/bar", + output = [ + "", + "foo", + ], +) + +create_rpath_entry_test( + name = "rpath_entry_simple", + binary_short_path = "foo/a.so", + dependency_short_path = "bar/b.so", + output = "../bar", +) + +# checks that a binary in //:bin works properly +create_rpath_entry_test( + name = "rpath_entry_binary_root", + binary_short_path = "bin", + dependency_short_path = "xyz/b.so", + output = "xyz", +) + +# same for dependency +create_rpath_entry_test( + name = "rpath_entry_dep_root", + binary_short_path = "lib/bin", + dependency_short_path = "b.so", + output = "..", +) + +create_rpath_entry_test( + name = "rpath_entry_simple_filename", + binary_short_path = "foo/a.so", + dependency_short_path = "bar/b.so", + keep_filename = True, + output = "../bar/b.so", +) + +create_rpath_entry_test( + name = "rpath_entry_prefix", + binary_short_path = "foo/a.so", + dependency_short_path = "bar/b.so", + output = "$ORIGIN/../bar", + prefix = "$ORIGIN", +) + +# if the short-paths have leading dots, they are in `external` + +create_rpath_entry_test( + name = "rpath_entry_binary_leading_dots_dep", + # non-external + binary_short_path = "foo/a.so", + # external dep + dependency_short_path = "../bar/b.so", + output = "../external/bar", +) + +create_rpath_entry_test( + name = "rpath_entry_binary_leading_dots_bin", + # external dep + binary_short_path = "../foo/a.so", + # non-external + dependency_short_path = "bar/b.so", + # back through `external` + output = "../../bar", +) + +create_rpath_entry_test( + name = "rpath_entry_binary_leading_dots_both", + # external dep + binary_short_path = "../foo/a.so", + # external dep + dependency_short_path = "../bar/b.so", + # stay in `external` + output = "../bar", +) + +# we have no idea how to handle internal dots, should they arise +# create_rpath_entry_test( +# name = "rpath_entry_binary_internal_dots", +# binary_short_path = "foo/../../a.so", +# dependency_short_path = "../bar/../b.so", +# # but that doesn’t change anything for the runpath +# output = "../bar", +# ) + +dedup_on_test( + name = "dedup_on_test", +) diff --git a/third_party/bazel/rules_haskell/tests/unit-tests/tests.bzl b/third_party/bazel/rules_haskell/tests/unit-tests/tests.bzl new file mode 100644 index 000000000000..58a3f400adbe --- /dev/null +++ b/third_party/bazel/rules_haskell/tests/unit-tests/tests.bzl @@ -0,0 +1,83 @@ +load( + "@bazel_skylib//lib:unittest.bzl", + "asserts", + unit = "unittest", +) +load("//haskell:private/actions/link.bzl", "create_rpath_entry", "parent_dir_path") +load("//haskell:private/list.bzl", "list") + +def parent_dir_path_test_impl(ctx): + env = unit.begin(ctx) + asserts.equals( + env, + expected = ctx.attr.output, + actual = parent_dir_path(ctx.attr.filename), + ) + unit.end(env) + +parent_dir_path_test = unit.make( + parent_dir_path_test_impl, + attrs = { + "filename": attr.string(), + "output": attr.string_list(), + }, +) + +def create_rpath_entry_test_impl(ctx): + env = unit.begin(ctx) + asserts.equals( + env, + expected = ctx.attr.output, + actual = create_rpath_entry( + struct( + short_path = ctx.attr.binary_short_path, + ), + struct( + short_path = ctx.attr.dependency_short_path, + ), + keep_filename = ctx.attr.keep_filename, + prefix = ctx.attr.prefix, + ), + ) + unit.end(env) + +create_rpath_entry_test = unit.make( + create_rpath_entry_test_impl, + attrs = { + "binary_short_path": attr.string(), + "dependency_short_path": attr.string(), + "keep_filename": attr.bool(default = False, mandatory = False), + "prefix": attr.string(default = "", mandatory = False), + "output": attr.string(), + }, +) + +def compare_x(el): + return el.x + +def dedup_on_test_impl(ctx): + env = unit.begin(ctx) + asserts.equals( + env, + expected = [], + actual = list.dedup_on(compare_x, []), + ) + asserts.equals( + env, + expected = [struct(x = 3)], + actual = list.dedup_on( + compare_x, + [struct(x = 3), struct(x = 3), struct(x = 3)], + ), + ) + asserts.equals( + env, + expected = [struct(x = 3), struct(x = 4), struct(x = 5)], + actual = list.dedup_on( + compare_x, + [struct(x = 3), struct(x = 4), struct(x = 3), struct(x = 5), struct(x = 3)], + ), + ) + unit.end(env) + +dedup_on_test = unit.make(dedup_on_test_impl) |