-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test to ensure extension rules pin the right rules_rust version
- Loading branch information
1 parent
b6ee4f9
commit 8a60df7
Showing
15 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
load("@rules_rust//test/bzl_version:bzl_ext_version_test.bzl", "bzl_ext_version_test") | ||
|
||
bzl_ext_version_test( | ||
name = "bzl_ext_version_test", | ||
module_bazel = "//:MODULE.bazel", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
load("@rules_rust//test/bzl_version:bzl_ext_version_test.bzl", "bzl_ext_version_test") | ||
|
||
bzl_ext_version_test( | ||
name = "bzl_ext_version_test", | ||
module_bazel = "//:MODULE.bazel", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ toolchain_type( | |
|
||
exports_files([ | ||
"defs.bzl", | ||
"MODULE.bazel", | ||
]) | ||
|
||
rust_binary( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
load("@rules_rust//test/bzl_version:bzl_ext_version_test.bzl", "bzl_ext_version_test") | ||
|
||
bzl_ext_version_test( | ||
name = "bzl_ext_version_test", | ||
module_bazel = "//:MODULE.bazel", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
load("@rules_rust//test/bzl_version:bzl_ext_version_test.bzl", "bzl_ext_version_test") | ||
|
||
bzl_ext_version_test( | ||
name = "bzl_ext_version_test", | ||
module_bazel = "//:MODULE.bazel", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//! 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." | ||
); | ||
} |