Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Nov 8, 2024
1 parent 199550d commit a7fded9
Show file tree
Hide file tree
Showing 24 changed files with 464 additions and 136 deletions.
91 changes: 9 additions & 82 deletions js/private/test/image/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//js:defs.bzl", "js_binary")
load(":asserts.bzl", "assert_tar_listing", "make_js_image_layer")
load(":asserts.bzl", "assert_checksum", "assert_js_image_layer_listings", "make_js_image_layer")

npm_link_all_packages(name = "node_modules")

Expand Down Expand Up @@ -33,31 +32,9 @@ make_js_image_layer(
root = "/app",
)

genrule(
name = "checksum_gen",
testonly = True,
srcs = [":cksum"],
outs = ["checksum_generated"],
cmd = """
COREUTILS_BIN=$$(realpath $(COREUTILS_BIN)) &&
cd $(BINDIR) && $$COREUTILS_BIN sha256sum $(rootpaths :cksum) > $(rootpaths checksum_generated)
""",
output_to_bindir = True,
toolchains = ["@coreutils_toolchains//:resolved_toolchain"],
)

write_source_file(
assert_checksum(
name = "checksum_test",
testonly = True,
in_file = ":checksum_gen",
out_file = "checksum.expected",
# Under bazel6 bzlmod name differs
tags = ["skip-on-bazel6"],
# Under bzlmod workspace name is a fixed string `_main` which differs from WORKSPACE
target_compatible_with = select({
"@aspect_bazel_lib//lib:bzlmod": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
image_layer = ":cksum",
)

# Case 1: Defaults
Expand All @@ -68,34 +45,9 @@ make_js_image_layer(
root = "/app",
)

assert_tar_listing(
name = "assert_default_node_layer",
actual = "default_node_layer",
expected = "default_node.listing",
)

assert_tar_listing(
name = "assert_default_package_store_3p_layer",
actual = "default_package_store_3p_layer",
expected = "default_package_store_3p.listing",
)

assert_tar_listing(
name = "assert_default_package_store_1p_layer",
actual = "default_package_store_1p_layer",
expected = "default_package_store_1p.listing",
)

assert_tar_listing(
name = "assert_default_node_modules_layer",
actual = "default_node_modules_layer",
expected = "default_node_modules.listing",
)

assert_tar_listing(
name = "assert_default_app_layer",
actual = "default_app_layer",
expected = "default_app.listing",
assert_js_image_layer_listings(
name = "default_test",
js_image_layer = ":default",
)

# Case 2: Change owner
Expand All @@ -107,32 +59,7 @@ make_js_image_layer(
root = "/app",
)

assert_tar_listing(
name = "assert_custom_owner_node_layer",
actual = "custom_owner_node_layer",
expected = "custom_owner_node.listing",
)

assert_tar_listing(
name = "assert_custom_owner_package_store_3p_layer",
actual = "custom_owner_package_store_3p_layer",
expected = "custom_owner_package_store_3p.listing",
)

assert_tar_listing(
name = "assert_custom_owner_package_store_1p_layer",
actual = "custom_owner_package_store_1p_layer",
expected = "custom_owner_package_store_1p.listing",
)

assert_tar_listing(
name = "assert_custom_owner_node_modules_layer",
actual = "custom_owner_node_modules_layer",
expected = "custom_owner_node_modules.listing",
)

assert_tar_listing(
name = "assert_custom_owner_app_layer",
actual = "custom_owner_app_layer",
expected = "custom_owner_app.listing",
assert_js_image_layer_listings(
name = "custom_owner_test",
js_image_layer = ":custom_owner",
)
120 changes: 69 additions & 51 deletions js/private/test/image/asserts.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
"Make shorter assertions"

load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
load("@aspect_bazel_lib//lib:utils.bzl", "is_bzlmod_enabled")
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file", "write_source_files")
load("//js:defs.bzl", "js_image_layer")

# buildifier: disable=function-docstring
def assert_tar_listing(name, actual, expected):
actual_listing = "_{}_listing".format(name)
native.genrule(
name = actual_listing,
srcs = [actual],
testonly = True,
outs = ["_{}.listing".format(name)],
cmd = 'TZ="UTC" LC_ALL="en_US.UTF-8" $(BSDTAR_BIN) -tvf $(execpath {}) >$@'.format(actual),
toolchains = ["@bsd_tar_toolchains//:resolved_toolchain"],
)

write_source_file(
name = name,
in_file = actual_listing,
out_file = expected,
testonly = True,
)

layers = [
"node",
"package_store_1p",
"package_store_3p",
"node_modules",
"app",
]

# buildifier: disable=function-docstring
def assert_js_image_layer_listings(name, js_image_layer):
for layer in layers:
assert_tar_listing(
name = "assert_{}_{}".format(name, layer),
actual = "{}_{}".format(js_image_layer, layer),
expected = "{}_{}{}.listing".format(name, layer, (".bzlmod" if is_bzlmod_enabled() else ".nobzlmod")),
)

write_source_files(
name = name + "_update_all",
additional_update_targets = [
"assert_{}_{}".format(name, layer)
for layer in layers
],
testonly = True,
)

# buildifier: disable=function-docstring
def make_js_image_layer(name, **kwargs):
js_image_layer(
Expand All @@ -16,61 +62,33 @@ def make_js_image_layer(name, **kwargs):
**kwargs
)

native.filegroup(
name = name + "_node_layer",
srcs = [name],
output_group = "node",
testonly = 1,
)

native.filegroup(
name = name + "_package_store_3p_layer",
srcs = [name],
output_group = "package_store_3p",
testonly = 1,
)

native.filegroup(
name = name + "_package_store_1p_layer",
srcs = [name],
output_group = "package_store_1p",
testonly = 1,
)

native.filegroup(
name = name + "_node_modules_layer",
srcs = [name],
output_group = "node_modules",
testonly = 1,
)

native.filegroup(
name = name + "_app_layer",
srcs = [name],
output_group = "app",
testonly = 1,
)
for layer in layers:
native.filegroup(
name = name + "_" + layer,
srcs = [name],
output_group = layer,
testonly = 1,
)

# buildifier: disable=function-docstring
def assert_tar_listing(name, actual, expected):
actual_listing = "_{}_listing".format(name)
def assert_checksum(name, image_layer):
native.genrule(
name = actual_listing,
srcs = [actual],
name = name,
testonly = True,
outs = ["_{}.listing".format(name)],
cmd = 'TZ="UTC" LC_ALL="en_US.UTF-8" $(BSDTAR_BIN) -tvf $(execpath {}) >$@'.format(actual),
toolchains = ["@bsd_tar_toolchains//:resolved_toolchain"],
srcs = [image_layer],
outs = [name + ".checksums"],
cmd = """
COREUTILS_BIN=$$(realpath $(COREUTILS_BIN)) &&
RESULT="$$($$COREUTILS_BIN sha256sum $(SRCS))"
BINDIR="$(BINDIR)/"
echo "$${RESULT//$$BINDIR/}" > $@
""",
output_to_bindir = True,
toolchains = ["@coreutils_toolchains//:resolved_toolchain"],
)

write_source_file(
name = name,
in_file = actual_listing,
out_file = expected,
name = name + "_test",
testonly = True,
# TODO: js_image_layer is broken with bzlmod https://github.com/aspect-build/rules_js/issues/1530
target_compatible_with = select({
"@aspect_bazel_lib//lib:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
in_file = name,
out_file = name + ("." if is_bzlmod_enabled() else ".no") + "bzlmod.expected",
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
33ae356a72851240aa60f080f90d5ddcd78942d6c30bdc84514788450d860e07 js/private/test/image/cksum_app.tar
c603eafdacc184f3df28ef966704cd580bf5f53c7d599f77132abaac45c110fc js/private/test/image/cksum_node.tar
fbd40000f29527fb494ba83172bf55ecb01e01b29a8ff4727ead46e4ebf745f1 js/private/test/image/cksum_node_modules.tar
842c83159aa2f59c53678ed4cc7d0e23d7e1ea9de0f80af272d1694300bd8012 js/private/test/image/cksum_package_store_3p.tar
9395e137de4767edfdd663bc6010a597844bdb68663f8b3ecb036175122bcb2f js/private/test/image/cksum_package_store_1p.tar
eec022801c54978ece38859f525f6f7114dcf743ea5e4564ef2a8a522d51aeab js/private/test/image/cksum_package_store_3p.tar
c7e5a122704601296eed4b106e5a323ab8fc267c36355784a9538aff9b7ed20b js/private/test/image/cksum_node_modules.tar
33ae356a72851240aa60f080f90d5ddcd78942d6c30bdc84514788450d860e07 js/private/test/image/cksum_app.tar
5 changes: 5 additions & 0 deletions js/private/test/image/checksum_test.nobzlmod.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
253421c42119a0ce9b5987673f4ba428907751cd2ebbebd0d45e937479105a07 js/private/test/image/cksum_node.tar
c8c99d4e558891fb1d752ffe597ef636242f5e5e8721c76a677ddd2dea33806c js/private/test/image/cksum_package_store_3p.tar
5fdcbbd175505f08c4c227b443ff4bdd6944561a53e2460375193a23d64d79d2 js/private/test/image/cksum_package_store_1p.tar
3940c0c8fdc9e723b2883b941f57e2c61e81b139a1dabb00cf594985efe5c38d js/private/test/image/cksum_node_modules.tar
d202df5e5951a96bf1cd106d82b6b03416540a05671c20a9f832a2765709c79a js/private/test/image/cksum_app.tar
24 changes: 24 additions & 0 deletions js/private/test/image/custom_owner_test_app.bzlmod.listing
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
drwxr-xr-x 0 100 0 0 Jan 1 1970 app
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image
-r-xr-xr-x 0 100 0 126 Jan 1 1970 app/js/private/test/image/bin
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d
-r-xr-xr-x 0 100 0 387 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/index.js
-r-xr-xr-x 0 100 0 164 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/package.json
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_
-r-xr-xr-x 0 100 0 23910 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/bin
-r-xr-xr-x 0 100 0 595 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/bin.repo_mapping
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin
-r-xr-xr-x 0 100 0 133 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/node
-r-xr-xr-x 0 100 0 20 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/main.js
17 changes: 17 additions & 0 deletions js/private/test/image/custom_owner_test_node.bzlmod.listing
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
drwxr-xr-x 0 100 0 0 Jan 1 1970 app
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/node-patches
-r-xr-xr-x 0 100 0 32555 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 100 0 1702 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/bin
-r-xr-xr-x 0 100 0 80316256 Jan 1 1970 app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/bin/node
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
drwxr-xr-x 0 100 0 0 Jan 1 1970 app
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/node_modules
lrwxrwxr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/node_modules/acorn -> ../../../../../node_modules/.aspect_rules_js/[email protected]/node_modules/acorn
lrwxrwxr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/node_modules/uuid -> ../../../../../node_modules/.aspect_rules_js/[email protected]/node_modules/uuid
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules/@mycorp
lrwxrwxr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules/@mycorp/pkg-d -> ../../../../../../node_modules/.aspect_rules_js/@[email protected]/node_modules/@mycorp/pkg-d
lrwxrwxr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules/acorn -> ../../../../../node_modules/.aspect_rules_js/[email protected]/node_modules/acorn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
drwxr-xr-x 0 100 0 0 Jan 1 1970 app
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@[email protected]
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@[email protected]/node_modules
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@[email protected]/node_modules/@mycorp
lrwxrwxr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@[email protected]/node_modules/@mycorp/pkg-d -> ../../../../../examples/npm_package/packages/pkg_d
Loading

0 comments on commit a7fded9

Please sign in to comment.