use std::fs;
use std::path::Path;
use std::time::SystemTime;
fn timestamp(f: impl AsRef<Path>) -> SystemTime {
fs::metadata(f).unwrap().modified().unwrap()
}
const GRPC_OUTPUT_FILE: &str = "src/serverless/grpc.rs";
const PROTO_FILE: &str = "proto/serverless_collections.proto";
#[test]
#[ignore = "run explicitly to regenerate serverless gRPC stubs"]
fn regenerate_serverless_protos() {
tonic_prost_build::configure()
.build_server(false)
.build_client(true)
.out_dir("src/serverless/")
.compile_protos(&[PROTO_FILE], &["proto"])
.expect("failed to compile serverless protos");
let candidates = [
"src/serverless/qdrant.serverless.rs",
"src/serverless/serverless.rs",
"src/serverless/qdrant.rs",
];
let generated = candidates
.into_iter()
.find(|p| Path::new(p).exists())
.expect("expected generated serverless stub file");
if generated != GRPC_OUTPUT_FILE {
fs::rename(generated, GRPC_OUTPUT_FILE).expect("rename generated file");
}
let contents = fs::read_to_string(GRPC_OUTPUT_FILE).unwrap();
let banner = "// @generated by tonic-prost-build. DO NOT EDIT.\n// Regenerate: cargo test --test serverless_protos -- --ignored --nocapture\n";
if !contents.starts_with("// @generated") {
fs::write(GRPC_OUTPUT_FILE, format!("{banner}{contents}")).unwrap();
}
eprintln!("Wrote {GRPC_OUTPUT_FILE}");
}
#[test]
fn serverless_protos_are_fresh() {
if !Path::new(GRPC_OUTPUT_FILE).exists() {
panic!("{GRPC_OUTPUT_FILE} missing; run: cargo test --test serverless_protos -- --ignored");
}
let out_time = timestamp(GRPC_OUTPUT_FILE);
let proto_time = timestamp(PROTO_FILE);
assert!(
proto_time <= out_time,
"{PROTO_FILE} is newer than {GRPC_OUTPUT_FILE}; regenerate with: cargo test --test serverless_protos -- --ignored"
);
}