xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/bzl_version/bzl_version_test.rs (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1 //! A test to ensure the rules_rust bzlmod versions match the standard versions.
2 
3 use runfiles::Runfiles;
4 
parse_module_bazel_version(text: &str) -> String5 fn parse_module_bazel_version(text: &str) -> String {
6     let mut found_module = false;
7     for line in text.split('\n') {
8         if found_module {
9             assert!(!line.ends_with(')'), "Failed to parse version");
10             if let Some((param, value)) = line.rsplit_once(" = ") {
11                 if param.trim() == "version" {
12                     return value.trim().trim_matches(',').trim_matches('"').to_owned();
13                 }
14             }
15         } else if line.starts_with("module(") {
16             found_module = true;
17             continue;
18         }
19     }
20     panic!("Failed to find MODULE.bazel version");
21 }
22 
23 /// If this test fails it means `//:version.bzl` and `//:MODULE.bazel` need to
24 /// be synced up. `//:version.bzl` should contain the source of truth.
25 #[test]
module_bzl_has_correct_version()26 fn module_bzl_has_correct_version() {
27     let version = std::env::var("VERSION").unwrap();
28     let module_bazel_text = {
29         let r = Runfiles::create().unwrap();
30         let path = runfiles::rlocation!(r, std::env::var("MODULE_BAZEL").unwrap());
31         std::fs::read_to_string(path).unwrap()
32     };
33 
34     let module_bazel_version = parse_module_bazel_version(&module_bazel_text);
35 
36     assert_eq!(
37         version, module_bazel_version,
38         "//:version.bzl and //:MODULE.bazel versions are out of sync"
39     );
40 }
41