Skip to content

Commit

Permalink
Added test to ensure extension rules pin the right rules_rust version
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Dec 6, 2024
1 parent b6ee4f9 commit 2bd562d
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 9 deletions.
1 change: 1 addition & 0 deletions extensions/bindgen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package(default_visibility = ["//visibility:public"])

exports_files([
"defs.bzl",
"MODULE.bazel",
])

toolchain_type(
Expand Down
4 changes: 2 additions & 2 deletions extensions/bindgen/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

module(
name = "rules_rust_bindgen",
version = "0.54.1",
version = "0.55.0",
)

bazel_dep(
name = "rules_rust",
version = "0.54.1",
version = "0.55.0",
)
local_path_override(
module_name = "rules_rust",
Expand Down
12 changes: 12 additions & 0 deletions extensions/bindgen/test/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_rust//test/bzl_version:bzl_ext_version_test.bzl", "bzl_ext_version_test")
load("@rules_rust//test/bzl_version:bzl_version_test.bzl", "bzl_version_test")

bzl_ext_version_test(
name = "bzl_ext_version_test",
module_bazel = "//:MODULE.bazel",
)

bzl_version_test(
name = "bzl_version_test",
module_bazel = "//:MODULE.bazel",
)
1 change: 1 addition & 0 deletions extensions/prost/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package(default_visibility = ["//visibility:public"])

exports_files([
"defs.bzl",
"MODULE.bazel",
])

toolchain_type(
Expand Down
4 changes: 2 additions & 2 deletions extensions/prost/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

module(
name = "rules_rust_prost",
version = "0.54.1",
version = "0.55.0",
)

bazel_dep(
name = "rules_rust",
version = "0.54.1",
version = "0.55.0",
)
local_path_override(
module_name = "rules_rust",
Expand Down
12 changes: 12 additions & 0 deletions extensions/prost/private/tests/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_rust//test/bzl_version:bzl_ext_version_test.bzl", "bzl_ext_version_test")
load("@rules_rust//test/bzl_version:bzl_version_test.bzl", "bzl_version_test")

bzl_ext_version_test(
name = "bzl_ext_version_test",
module_bazel = "//:MODULE.bazel",
)

bzl_version_test(
name = "bzl_version_test",
module_bazel = "//:MODULE.bazel",
)
1 change: 1 addition & 0 deletions extensions/protobuf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ toolchain_type(

exports_files([
"defs.bzl",
"MODULE.bazel",
])

rust_binary(
Expand Down
4 changes: 2 additions & 2 deletions extensions/protobuf/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

module(
name = "rules_rust_protobuf",
version = "0.54.1",
version = "0.55.0",
)

bazel_dep(
name = "rules_rust",
version = "0.54.1",
version = "0.55.0",
)
local_path_override(
module_name = "rules_rust",
Expand Down
12 changes: 12 additions & 0 deletions extensions/protobuf/test/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_rust//test/bzl_version:bzl_ext_version_test.bzl", "bzl_ext_version_test")
load("@rules_rust//test/bzl_version:bzl_version_test.bzl", "bzl_version_test")

bzl_ext_version_test(
name = "bzl_ext_version_test",
module_bazel = "//:MODULE.bazel",
)

bzl_version_test(
name = "bzl_version_test",
module_bazel = "//:MODULE.bazel",
)
1 change: 1 addition & 0 deletions extensions/wasm_bindgen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package(default_visibility = ["//visibility:public"])

exports_files([
"defs.bzl",
"MODULE.bazel",
])

toolchain_type(
Expand Down
4 changes: 2 additions & 2 deletions extensions/wasm_bindgen/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

module(
name = "rules_rust_wasm_bindgen",
version = "0.54.1",
version = "0.55.0",
)

bazel_dep(
name = "rules_rust",
version = "0.54.1",
version = "0.55.0",
)
local_path_override(
module_name = "rules_rust",
Expand Down
12 changes: 12 additions & 0 deletions extensions/wasm_bindgen/test/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_rust//test/bzl_version:bzl_ext_version_test.bzl", "bzl_ext_version_test")
load("@rules_rust//test/bzl_version:bzl_version_test.bzl", "bzl_version_test")

bzl_ext_version_test(
name = "bzl_ext_version_test",
module_bazel = "//:MODULE.bazel",
)

bzl_version_test(
name = "bzl_version_test",
module_bazel = "//:MODULE.bazel",
)
1 change: 1 addition & 0 deletions test/bzl_version/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load(":bzl_version_test.bzl", "bzl_version_test")

exports_files([
"bzl_version_test.rs",
"bzl_ext_version_test.rs",
])

bzl_version_test(
Expand Down
30 changes: 30 additions & 0 deletions test/bzl_version/bzl_ext_version_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Test utiltiies for ensuring bzlmod and workspace versions match"""

load("//rust:defs.bzl", "rust_test")

def bzl_ext_version_test(name, module_bazel, **kwargs):
"""A test for ensuring a rules_rust extension bzlmod repo is in sync with the core rules_rust version.
Args:
name (str): The name of the test
module_bazel (label): The label of a `MODULE.bazel` file.
**kwargs (dict): Additional keyword arguments.
"""

rust_test(
name = name,
srcs = [Label("//test/bzl_version:bzl_ext_version_test.rs")],
data = [
module_bazel,
Label("@rules_rust//:MODULE.bazel"),
],
edition = "2021",
env = {
"CORE_MODULE_BAZEL": "$(rlocationpath @rules_rust//:MODULE.bazel)",
"MODULE_BAZEL": "$(rlocationpath {})".format(module_bazel),
},
deps = [
Label("//tools/runfiles"),
],
**kwargs
)
82 changes: 82 additions & 0 deletions test/bzl_version/bzl_ext_version_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//! A test to ensure the rules_rust extension bzlmod core version is accurate.
use runfiles::Runfiles;

fn parse_rules_rust_version(text: &str) -> String {
let mut in_module = false;
let mut found_dep = false;
for line in text.split('\n') {
if in_module {
if line.ends_with(')') {
in_module = false;
continue;
}

if found_dep {
if let Some((param, value)) = line.rsplit_once(" = ") {
if param.trim() == "version" {
return value.trim().trim_matches(',').trim_matches('"').to_owned();
}
}
}

if line.trim().starts_with("name = \"rules_rust\"") {
found_dep = true;
continue;
}

continue;
}

if line.starts_with("bazel_dep(") {
assert!(
!found_dep,
"Found `rules_rust` dep but couldn't determine version."
);
in_module = true;
continue;
}
}
panic!("Failed to find rules_rust version in:\n```\n{}\n```", text);
}

fn parse_module_bazel_version(text: &str) -> String {
let mut found_module = false;
for line in text.split('\n') {
if found_module {
assert!(!line.ends_with(')'), "Failed to parse version");
if let Some((param, value)) = line.rsplit_once(" = ") {
if param.trim() == "version" {
return value.trim().trim_matches(',').trim_matches('"').to_owned();
}
}
} else if line.starts_with("module(") {
found_module = true;
continue;
}
}
panic!("Failed to find MODULE.bazel version");
}

/// If this test fails it means `//:version.bzl` and `//:MODULE.bazel` need to
/// be synced up. `//:version.bzl` should contain the source of truth.
#[test]
fn module_bzl_has_correct_version() {
let r = Runfiles::create().unwrap();

let core_version = {
let path = runfiles::rlocation!(r, std::env::var("CORE_MODULE_BAZEL").unwrap()).unwrap();
let text = std::fs::read_to_string(path).unwrap();
parse_module_bazel_version(&text)
};
let requested_version = {
let path = runfiles::rlocation!(r, std::env::var("MODULE_BAZEL").unwrap()).unwrap();
let text = std::fs::read_to_string(path).unwrap();
parse_rules_rust_version(&text)
};

assert_eq!(
core_version, requested_version,
"Core rules_rust and the dependency for the current module are out of sync."
);
}
2 changes: 1 addition & 1 deletion test/bzl_version/bzl_version_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ fn module_bzl_has_correct_version() {

assert_eq!(
version, module_bazel_version,
"//:version.bzl and //:MODULE.bazel versions are out of sync"
"@rules_rust//:version.bzl and //:MODULE.bazel versions are out of sync"
);
}

0 comments on commit 2bd562d

Please sign in to comment.