1 //! A small test binary for ensuring the version of the rules matches the binary version 2 3 use std::fs::File; 4 use std::io::{BufRead, BufReader}; 5 use std::path::PathBuf; 6 7 #[test] test_cargo_and_bazel_versions()8fn test_cargo_and_bazel_versions() { 9 // Parse the version field from the `cargo-bazel` Cargo.toml file 10 let cargo_version = { 11 let cargo_path = PathBuf::from(env!("CARGO_TOML")); 12 let file = File::open(cargo_path).expect("Failed to load Cargo.toml file"); 13 BufReader::new(file) 14 .lines() 15 .map_while(Result::ok) 16 .find(|line| line.contains("version = ")) 17 .map(|line| { 18 line.trim() 19 .replace("version = ", "") 20 .trim_matches('\"') 21 .to_owned() 22 }) 23 .expect("The version.bzl file should have a line with `version = `") 24 }; 25 26 // Parse the version global from the Bazel module 27 let bazel_version = { 28 let bazel_path = PathBuf::from(env!("VERSION_BZL")); 29 let file = File::open(bazel_path).expect("Failed to load versions.bzl file"); 30 BufReader::new(file) 31 .lines() 32 .map_while(Result::ok) 33 .find(|line| line.contains("VERSION = ")) 34 .map(|line| { 35 line.trim() 36 .replace("VERSION = ", "") 37 .trim_matches('\"') 38 .to_owned() 39 }) 40 .expect("The version.bzl file should have a line with `VERSION = `") 41 }; 42 43 assert_eq!(cargo_version, bazel_version, "make sure `//crate_universe:version.bzl` and `//crate_universe:Cargo.toml` have matching versions."); 44 } 45