From 86db5c08fc1a8a776ab467a03784dc6270ab34c8 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 1 Aug 2016 19:31:52 +0200 Subject: [PATCH] drying tests --- ipc/codegen/src/lib.rs | 37 +++++++++++++++++++++ ipc/tests/binary.rs | 2 +- ipc/tests/build.rs | 73 +++-------------------------------------- ipc/tests/nested.rs | 2 +- ipc/tests/service.rs | 2 +- ipc/tests/with_attrs.rs | 2 +- 6 files changed, 45 insertions(+), 73 deletions(-) diff --git a/ipc/codegen/src/lib.rs b/ipc/codegen/src/lib.rs index afa7979d0..ff8abf515 100644 --- a/ipc/codegen/src/lib.rs +++ b/ipc/codegen/src/lib.rs @@ -95,3 +95,40 @@ pub fn register(reg: &mut rustc_plugin::Registry) { reg.register_attribute("ipc".to_owned(), AttributeType::Normal); } + +#[derive(Debug)] +pub enum Error { InvalidFileName, ExpandFailure } + +pub fn derive_ipc(src_path: &str) -> Result<(), Error> { + use std::env; + use std::path::{Path, PathBuf}; + + let out_dir = env::var_os("OUT_DIR").unwrap(); + let file_name = try!(PathBuf::from(src_path).file_name().ok_or(Error::InvalidFileName).map(|val| val.to_str().unwrap().to_owned())); + + let mut intermediate_file_name = file_name.clone(); + intermediate_file_name.push_str("rpc.in"); + + let intermediate_path = Path::new(&out_dir).join(&intermediate_file_name); + let final_path = Path::new(&out_dir).join(&file_name); + + { + let mut registry = syntex::Registry::new(); + register(&mut registry); + if let Err(_) = registry.expand("", &Path::new(src_path), &intermediate_path) { + // will be reported by compiler + return Err(Error::ExpandFailure) + } + } + + { + let mut registry = syntex::Registry::new(); + register(&mut registry); + if let Err(_) = registry.expand("", &intermediate_path, &final_path) { + // will be reported by compiler + return Err(Error::ExpandFailure) + } + } + + Ok(()) +} diff --git a/ipc/tests/binary.rs b/ipc/tests/binary.rs index ab4adee39..c8d100c6e 100644 --- a/ipc/tests/binary.rs +++ b/ipc/tests/binary.rs @@ -16,4 +16,4 @@ #![allow(dead_code, unused_assignments, unused_variables)] // codegen issues -include!(concat!(env!("OUT_DIR"), "/binary.rs")); +include!(concat!(env!("OUT_DIR"), "/binary.rs.in")); diff --git a/ipc/tests/build.rs b/ipc/tests/build.rs index 6538f56e1..64476a482 100644 --- a/ipc/tests/build.rs +++ b/ipc/tests/build.rs @@ -14,76 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -extern crate syntex; extern crate ethcore_ipc_codegen as codegen; -use std::env; -use std::path::Path; -use std::process::exit; - pub fn main() { - let out_dir = env::var_os("OUT_DIR").unwrap(); - - // rpc pass - if { - let src = Path::new("nested.rs.in"); - let dst = Path::new(&out_dir).join("nested_ipc.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &dst).is_ok() - } - // serialization pass - { - let src = Path::new(&out_dir).join("nested_ipc.rs"); - let dst = Path::new(&out_dir).join("nested_cg.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &dst).unwrap(); - } - - // rpc pass - if { - let src = Path::new("service.rs.in"); - let dst = Path::new(&out_dir).join("service_ipc.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &dst).is_ok() - } - // serialization pass - { - let src = Path::new(&out_dir).join("service_ipc.rs"); - let dst = Path::new(&out_dir).join("service_cg.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &dst).unwrap(); - } - - // rpc pass - if { - let src = Path::new("with_attrs.rs.in"); - let dst = Path::new(&out_dir).join("with_attrs_ipc.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &dst).is_ok() - } - // serialization pass - { - let src = Path::new(&out_dir).join("with_attrs_ipc.rs"); - let dst = Path::new(&out_dir).join("with_attrs_cg.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &dst).unwrap(); - } - - // rpc pass - { - let src = Path::new("binary.rs.in"); - let dst = Path::new(&out_dir).join("binary.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - if let Err(err_msg) = registry.expand("", &src, &dst) { - println!("error: {}", err_msg); - exit(1); - } - } + codegen::derive_ipc("nested.rs.in").unwrap(); + codegen::derive_ipc("service.rs.in").unwrap(); + codegen::derive_ipc("with_attrs.rs.in").unwrap(); + codegen::derive_ipc("binary.rs.in").unwrap(); } diff --git a/ipc/tests/nested.rs b/ipc/tests/nested.rs index 5c7504ad8..6e6e3942f 100644 --- a/ipc/tests/nested.rs +++ b/ipc/tests/nested.rs @@ -15,4 +15,4 @@ // along with Parity. If not, see . #![allow(dead_code, unused_assignments, unused_variables)] // codegen issues -include!(concat!(env!("OUT_DIR"), "/nested_cg.rs")); +include!(concat!(env!("OUT_DIR"), "/nested.rs.in")); diff --git a/ipc/tests/service.rs b/ipc/tests/service.rs index 7e778c615..3d5159a9b 100644 --- a/ipc/tests/service.rs +++ b/ipc/tests/service.rs @@ -15,4 +15,4 @@ // along with Parity. If not, see . #![allow(dead_code, unused_assignments, unused_variables)] // codegen issues -include!(concat!(env!("OUT_DIR"), "/service_cg.rs")); +include!(concat!(env!("OUT_DIR"), "/service.rs.in")); diff --git a/ipc/tests/with_attrs.rs b/ipc/tests/with_attrs.rs index e9b3e0d76..bda734368 100644 --- a/ipc/tests/with_attrs.rs +++ b/ipc/tests/with_attrs.rs @@ -15,4 +15,4 @@ // along with Parity. If not, see . #![allow(dead_code, unused_assignments, unused_variables)] // codegen issues -include!(concat!(env!("OUT_DIR"), "/with_attrs_cg.rs")); +include!(concat!(env!("OUT_DIR"), "/with_attrs.rs.in"));