Add some tests

This commit is contained in:
Marc Plano-Lesay 2024-11-06 14:36:46 +11:00
parent bc46df634d
commit c525f63660
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
6 changed files with 889 additions and 27 deletions

View file

@ -1,3 +1,4 @@
use openapiv3::OpenAPI;
use std::{
env,
fs::{self, File},
@ -6,17 +7,34 @@ use std::{
fn main() {
let src = "./src/immich-openapi-specs.json";
println!("cargo:rerun-if-changed={}", src);
let out_dir = Path::new(&env::var("OUT_DIR").unwrap()).to_path_buf();
let file = File::open(src).unwrap();
let spec = serde_json::from_reader(file).unwrap();
let mut generator = progenitor::Generator::default();
let tokens = generator.generate_tokens(&spec).unwrap();
let ast = syn::parse2(tokens).unwrap();
let content = prettyplease::unparse(&ast);
let client = generate_client(&spec);
let client_dst = out_dir.join("client.rs");
fs::write(&client_dst, client).unwrap();
let mut out_file = Path::new(&env::var("OUT_DIR").unwrap()).to_path_buf();
out_file.push("codegen.rs");
let mock = generate_mock(&spec);
let mock_dst = out_dir.join("mock.rs");
fs::write(&mock_dst, mock).unwrap();
fs::write(out_file, content).unwrap();
println!("cargo:rerun-if-changed={}", src);
println!("cargo:rerun-if-changed={}", client_dst.display());
println!("cargo:rerun-if-changed={}", mock_dst.display());
}
fn generate_client(spec: &OpenAPI) -> String {
let mut generator = progenitor::Generator::default();
let tokens = generator.generate_tokens(spec).unwrap();
let ast = syn::parse2(tokens).unwrap();
prettyplease::unparse(&ast)
}
fn generate_mock(spec: &OpenAPI) -> String {
let mut generator = progenitor::Generator::default();
let tokens = generator.httpmock(spec, "crate").unwrap();
let ast = syn::parse2(tokens).unwrap();
prettyplease::unparse(&ast)
}