Skip to content

Commit

Permalink
test: introduce an e2e test asserting that bzlmod works
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Apr 27, 2022
1 parent c2d4481 commit 1cceab8
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 4 deletions.
18 changes: 18 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"aspect-build/bazel-lib"

module(
name = "aspect_bazel_lib",
version = "0.0.0",
compatibility_level = 1,
toolchains_to_register = [
"@jq_toolchains//:all",
"@yq_toolchains//:all",
],
)

bazel_dep(name = "bazel_skylib", version = "1.1.1")
bazel_dep(name = "platforms", version = "0.0.4")

ext = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "lib")
use_repo(ext, "jq_toolchains")
use_repo(ext, "yq_toolchains")
6 changes: 6 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ local_repository(
path = "./lib/tests/external_test_repo",
)

# Avoid descending into the e2e workspaces
local_repository(
name = "e2e_bzlmod",
path = "./e2e/bzlmod",
)

############################################
# Gazelle, for generating bzl_library targets
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
Expand Down
2 changes: 2 additions & 0 deletions e2e/bzlmod/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
common --experimental_enable_bzlmod
common --registry=https://raw.githubusercontent.com/aspect-build/bazel-central-registry/main/
8 changes: 8 additions & 0 deletions e2e/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@aspect_bazel_lib//lib:jq.bzl", "jq")

# No sources produces null (equivalent to --null-input)
jq(
name = "case_no_sources",
srcs = [],
filter = ".",
)
19 changes: 19 additions & 0 deletions e2e/bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module(
name = "e2e_bzlmod",
version = "0.0.0",
compatibility_level = 1,
toolchains_to_register = [
"@jq_toolchains//:all",
],
)

bazel_dep(name = "aspect_bazel_lib", version = "99.99.99")

local_path_override(
module_name = "aspect_bazel_lib",
path = "../..",
)

ext = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "lib")
use_repo(ext, "jq_toolchains")
use_repo(ext, "yq_toolchains")
Empty file added e2e/bzlmod/WORKSPACE
Empty file.
17 changes: 17 additions & 0 deletions lib/extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"Module extensions for use with bzlmod"

load(
"@aspect_bazel_lib//lib:repositories.bzl",
"DEFAULT_JQ_VERSION",
"DEFAULT_YQ_VERSION",
"register_jq_toolchains",
"register_yq_toolchains",
)

def _toolchain_extension(_):
register_yq_toolchains(version = DEFAULT_YQ_VERSION, register = False)
register_jq_toolchains(version = DEFAULT_JQ_VERSION, register = False)

lib = module_extension(
implementation = _toolchain_extension,
)
14 changes: 10 additions & 4 deletions lib/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ def aspect_bazel_lib_dependencies():
DEFAULT_JQ_VERSION = _DEFAULT_JQ_VERSION
DEFAULT_YQ_VERSION = _DEFAULT_YQ_VERSION

def register_jq_toolchains(name = "jq", version = DEFAULT_JQ_VERSION):
def register_jq_toolchains(name = "jq", version = DEFAULT_JQ_VERSION, register = True):
"""Registers jq toolchain and repositories
Args:
name: override the prefix for the generated toolchain repositories
version: the version of jq to execute (see https://github.com/stedolan/jq/releases)
register: whether to call through to native.register_toolchains.
Should be True for WORKSPACE users, but false when used under bzlmod extension
"""
for [platform, meta] in JQ_PLATFORMS.items():
jq_platform_repo(
name = "%s_%s" % (name, platform),
platform = platform,
version = version,
)
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))
if register:
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))

jq_host_alias_repo(
name = "%s_host" % name,
Expand All @@ -46,20 +49,23 @@ def register_jq_toolchains(name = "jq", version = DEFAULT_JQ_VERSION):
user_repository_name = name,
)

def register_yq_toolchains(name = "yq", version = DEFAULT_YQ_VERSION):
def register_yq_toolchains(name = "yq", version = DEFAULT_YQ_VERSION, register = True):
"""Registers yq toolchain and repositories
Args:
name: override the prefix for the generated toolchain repositories
version: the version of yq to execute (see https://github.com/mikefarah/yq/releases)
register: whether to call through to native.register_toolchains.
Should be True for WORKSPACE users, but false when used under bzlmod extension
"""
for [platform, meta] in YQ_PLATFORMS.items():
yq_platform_repo(
name = "%s_%s" % (name, platform),
platform = platform,
version = version,
)
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))
if register:
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))

yq_host_alias_repo(
name = "%s_host" % name,
Expand Down

0 comments on commit 1cceab8

Please sign in to comment.