Skip to content

Commit

Permalink
Check claimed Nickel versions in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
vkleen committed Sep 28, 2023
1 parent 0b2b6ce commit 01d61ea
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 12 deletions.
21 changes: 10 additions & 11 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ nickel = use_extension(
"nickel",
dev_dependency = True,
)
nickel.toolchain(
name = "nickel_1_0_0",
nickel_version = "1.0.0",
)
nickel.toolchain(
name = "nickel_1_1_1",
nickel_version = "1.1.1",
)
use_repo(nickel, "nickel_1_0_0_toolchains")
use_repo(nickel, "nickel_1_1_1_toolchains")
nickel.toolchain(name = "nickel")
use_repo(nickel, "nickel_toolchains")

register_toolchains(
"@nickel_1_1_1_toolchains//:all",
"@nickel_toolchains//:all",
dev_dependency = True,
)

version_test = use_extension(
"//nickel/tests:version_test.bzl",
"version_test",
dev_dependency = True,
)
use_repo(version_test, "all_nickel_versions")
4 changes: 3 additions & 1 deletion nickel/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ effectively overriding the default named toolchain due to toolchain resolution p
"""

load(":repositories.bzl", "nickel_register_toolchains")
load("//nickel/private:versions.bzl", "TOOL_VERSIONS")

_DEFAULT_NAME = "nickel"
_DEFAULT_VERSION = TOOL_VERSIONS.keys()[0]

nickel_toolchain = tag_class(attrs = {
"name": attr.string(doc = """\
Base name for generated repositories, allowing more than one nickel toolchain to be registered.
Overriding the default is only permitted in the root module.
""", default = _DEFAULT_NAME),
"nickel_version": attr.string(doc = "Explicit version of Nickel.", mandatory = True),
"nickel_version": attr.string(doc = "Explicit version of Nickel.", default = _DEFAULT_VERSION),
})

def _toolchain_extension(module_ctx):
Expand Down
1 change: 1 addition & 0 deletions nickel/private/versions.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

"""DO NOT EDIT
Generated from https://github.com/tweag/nickel/releases using `bazel run //nickel/private:versions.update`
Expand Down
13 changes: 13 additions & 0 deletions nickel/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load(":version_test.bzl", "version_test_rules")
load("//nickel/private:toolchains_repo.bzl", "PLATFORMS")
load("@bazel_skylib//lib:selects.bzl", "selects")

[selects.config_setting_group(
name = "version_test_{}".format(platform),
match_all = meta.compatible_with,
) for [
platform,
meta,
] in PLATFORMS.items()]

version_test_rules(name = "nickel")
64 changes: 64 additions & 0 deletions nickel/tests/version_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Testing that Nickel versions are what they claim to be"""

load("//nickel/private:versions.bzl", "TOOL_VERSIONS")
load("//nickel/private:toolchains_repo.bzl", "PLATFORMS")

def _version_pattern(version):
if version == "1.0.0":
return "nickel-lang 1.0.0"
if version == "1.1.1":
return "unexpected argument"
return "nickel-lang-cli nickel {0}".format(version)

def _version_test_extension(_ctx):
all_versions_repository(name = "all_nickel_versions")

version_test = module_extension(
implementation = _version_test_extension,
)

def _all_versions_repository(repository_ctx):
for [version, artifacts] in TOOL_VERSIONS.items():
files_list = []

for [name, artifact] in artifacts.items():
repository_ctx.download(
url = artifact["url"],
output = "{dir}/{name}".format(dir = version, name = name),
executable = True,
integrity = artifact["hash"],
)
files_list.append("\"{}\"".format(name))

build_content = """#Generated by nickel/tets/version_test.bzl
exports_files([{}])
""".format(", ".join(files_list))
repository_ctx.file("{}/BUILD.bazel".format(version), build_content)

all_versions_repository = repository_rule(
_all_versions_repository,
doc = "Fetch all Nickel artifacts for testing",
)

def version_test_rules(name):
"""Assemble testing rules to check Nickel versions are what they claim to be
Args:
name: base name for all the tests
"""
for version in TOOL_VERSIONS.keys():
tools = {}
args = {}

for platform in PLATFORMS.keys():
nickel_target = "@all_nickel_versions//{0}:nickel-{1}".format(version, platform)
tools[":version_test_{}".format(platform)] = [nickel_target]
args[":version_test_{}".format(platform)] = ["$(location {})".format(nickel_target), _version_pattern(version)]

version_underscore = version.replace(".", "_")
native.sh_test(
name = "{}_{}_version_test".format(name, version_underscore),
data = select(tools),
args = select(args),
srcs = ["version_test.sh"],
)
4 changes: 4 additions & 0 deletions nickel/tests/version_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
_nickel=$1
_version_pattern=$2

("${_nickel}" --version 2>&1 || true) | grep "${_version_pattern}"

0 comments on commit 01d61ea

Please sign in to comment.