Rename `tdb-derive` to `tdb-macros`

The proc_macro can now be applied on modules only
All functions within the module will be considered to be a test
This has the advantage of not having to flag every test function
next
Sayan Nandan 4 years ago
parent 16304cc4f7
commit 147f13a020
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

66
Cargo.lock generated

@ -127,9 +127,9 @@ checksum = "6035b7b9244bf9637cd7ef80b5e1c54404bef92cccd34738c85c45f04ae8b244"
[[package]]
name = "env_logger"
version = "0.7.1"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
checksum = "54532e3223c5af90a6a757c90b5c5521564b07e5e7a958681bcd2afad421cdcd"
dependencies = [
"atty",
"humantime",
@ -194,12 +194,9 @@ dependencies = [
[[package]]
name = "humantime"
version = "1.3.0"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"
[[package]]
name = "instant"
@ -436,19 +433,13 @@ checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
[[package]]
name = "proc-macro2"
version = "1.0.18"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa"
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quote"
version = "1.0.7"
@ -507,9 +498,9 @@ checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "regex"
version = "1.3.9"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6"
checksum = "8963b85b8ce3074fecffde43b4b0dded83ce2f367dc8d363afc56679f3ee820b"
dependencies = [
"aho-corasick",
"memchr",
@ -519,9 +510,9 @@ dependencies = [
[[package]]
name = "regex-syntax"
version = "0.6.18"
version = "0.6.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
checksum = "8cab7a364d15cde1e505267766a2d3c4e22a843e1a601f0fa7564c0f82ced11c"
[[package]]
name = "scopeguard"
@ -531,18 +522,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.116"
version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96fe57af81d28386a513cbc6858332abc6117cfdb5999647c6444b8f43a370a5"
checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.116"
version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f630a6370fd8e457873b4bd2ffdae75408bc291ba72be773772a4c2a065d9ae8"
checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e"
dependencies = [
"proc-macro2",
"quote",
@ -591,9 +582,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "syn"
version = "1.0.40"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "963f7d3cc59b59b9325165add223142bbf1df27655d07789f109896d353d8350"
checksum = "ea9c5432ff16d6152371f808fb5a871cd67368171b09bb21b43df8e4a47a3556"
dependencies = [
"proc-macro2",
"quote",
@ -617,7 +608,8 @@ dependencies = [
"regex",
"serde",
"serde_derive",
"tokio",
"tokio 0.2.22",
"tokio 0.3.0",
"toml",
]
@ -631,10 +623,12 @@ dependencies = [
]
[[package]]
name = "tdb-derive"
name = "tdb_macros"
version = "0.1.0"
dependencies = [
"proc-macro2",
"quote",
"rand",
"syn",
]
@ -651,7 +645,7 @@ dependencies = [
name = "testsuite"
version = "0.1.0"
dependencies = [
"tdb-derive",
"tdb_macros",
]
[[package]]
@ -707,6 +701,16 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "tokio"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7137dbb0abee577362ccdc7df21605cfcbb949243aeab47dac9ea6ef7d830e21"
dependencies = [
"bytes",
"pin-project-lite",
]
[[package]]
name = "tokio-macros"
version = "0.2.5"
@ -720,9 +724,9 @@ dependencies = [
[[package]]
name = "toml"
version = "0.5.6"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a"
checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645"
dependencies = [
"serde",
]
@ -735,7 +739,7 @@ dependencies = [
"lazy_static",
"libtdb",
"regex",
"tokio",
"tokio 0.2.22",
]
[[package]]

@ -4,7 +4,7 @@ members = [
"server",
"libtdb",
"tdb-bench",
"tdb-derive",
"tdb-macros",
"testsuite"
]

@ -13,17 +13,17 @@ libtdb = {path ="../libtdb"}
bincode = "1.3.1"
parking_lot = "0.11.0"
lazy_static = "1.4.0"
serde_derive = "1.0.116"
serde = {version = "1.0.116", features= ["derive"]}
toml = "0.5.6"
serde_derive = "1.0.117"
serde = {version = "1.0.117", features= ["derive"]}
toml = "0.5.7"
clap = {version = "2.33.3", features=["yaml"]}
env_logger = "0.7.1"
env_logger = "0.8.1"
log = "0.4.11"
chrono = "0.4.19"
regex = "1.3.9"
regex = "1.4.1"
[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemallocator = "0.3.2"
[dev-dependencies]
tokio = { version = "0.2", features = ["test-util"] }
tokio = { version = "0.3.0", features = ["test-util"] }

@ -1,75 +0,0 @@
/*
* Created on Sun Sep 13 2020
*
* This file is a part of TerrabaseDB
* Copyright (c) 2020, Sayan Nandan <ohsayan at outlook dot com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
//! A library containing a collection of custom derives used by TerrabaseDB
use proc_macro::TokenStream;
use quote::quote;
use syn;
fn parse_toks(mut input: syn::ItemFn) -> Result<TokenStream, syn::Error> {
let sig = &mut input.sig;
let body = &input.block;
let attrs = &input.attrs;
let vis = input.vis;
let header = quote! {
#[::core::prelude::v1::test]
};
let result = quote! {
#header
#(#attrs)*
#vis #sig {
let runtime = testsuite::service::BackGroundTask::new();
runtime.execute(|| {#body});
drop(runtime);
}
};
Ok(result.into())
}
fn test(_args: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::ItemFn);
for attr in &input.attrs {
if attr.path.is_ident("test") {
let msg = "second test attribute is supplied";
return syn::Error::new_spanned(&attr, msg)
.to_compile_error()
.into();
}
}
if !input.sig.inputs.is_empty() {
let msg = "the test function cannot accept arguments";
return syn::Error::new_spanned(&input.sig.inputs, msg)
.to_compile_error()
.into();
}
parse_toks(input).unwrap_or_else(|e| e.to_compile_error().into())
}
#[proc_macro_attribute]
/// Execute the function as a test
/// This function starts the server in the background and terminates it when
/// the test is over. Do note that, at the moment, this expects a `service` module
/// to have a `BackgroundTask` object which should start the background the server
pub fn dbtest(args: TokenStream, item: TokenStream) -> TokenStream {
test(args, item)
}

@ -1,5 +1,5 @@
[package]
name = "tdb-derive"
name = "tdb_macros"
version = "0.1.0"
authors = ["Sayan Nandan <ohsayan@outlook.com>"]
edition = "2018"
@ -10,5 +10,7 @@ edition = "2018"
proc-macro = true
[dependencies]
syn = {version = "1.0.45", features = ["full"]}
quote = "1.0.7"
syn = {version="1.0.40", features=["full"]}
rand = "0.7.3"
proc-macro2 = "1.0.24"

@ -0,0 +1,138 @@
/*
* Created on Sun Sep 13 2020
*
* This file is a part of TerrabaseDB
* Copyright (c) 2020, Sayan Nandan <ohsayan at outlook dot com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
//! A library containing a collection of custom derives used by TerrabaseDB
use proc_macro::TokenStream;
use quote::quote;
use rand::*;
use std::collections::HashSet;
use syn;
// TODO(@ohsayan): Write docs and also make this use the tokio runtime
fn parse_dbtest(mut input: syn::ItemFn, rand: u16) -> Result<TokenStream, syn::Error> {
let sig = &mut input.sig;
let fname = sig.ident.to_string();
let body = &input.block;
let attrs = &input.attrs;
let vis = input.vis;
let header = quote! {
#[::core::prelude::v1::test]
};
if sig.asyncness.is_none() {
let msg = "`dbtest` functions need to be async";
return Err(syn::Error::new_spanned(sig.fn_token, msg));
}
let body = quote! {
let mut socket = tokio::net::TcpStream::connect(#rand).await;
#body
socket.shutdown(std::net::Shutdown::Both).unwrap();
};
let result = quote! {
#header
#(#attrs)*
#vis #sig {
let runtime = Builder::new_multi_thread()
.worker_threads(4)
.thread_name(#fname)
.thread_stack_size(3 * 1024 * 1024)
.build()
.unwrap()
.block_on(async { #body });
}
};
Ok(result.into())
}
fn parse_test_sig(input: syn::ItemFn, rand: u16) -> TokenStream {
for attr in &input.attrs {
if attr.path.is_ident("test") {
let msg = "second test attribute is supplied";
return syn::Error::new_spanned(&attr, msg)
.to_compile_error()
.into();
}
}
if !input.sig.inputs.is_empty() {
let msg = "the test function cannot accept arguments";
return syn::Error::new_spanned(&input.sig.inputs, msg)
.to_compile_error()
.into();
}
parse_dbtest(input, rand).unwrap_or_else(|e| e.to_compile_error().into())
}
fn parse_test_module(_args: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::ItemMod);
let (_, content) = input.content.unwrap();
let attrs = input.attrs;
let vis = input.vis;
let mod_token = input.mod_token;
let modname = input.ident;
let mut rng = thread_rng();
let mut in_set = HashSet::<u16>::new();
in_set.insert(80);
in_set.insert(443);
let mut result = quote! {};
for item in content {
let mut rand: u16 = rng.gen_range(0, 65535);
while in_set.contains(&rand) {
rand = rng.gen_range(0, 65535);
}
match item {
// We just care about functions, so parse functions and ignore everything
// else
syn::Item::Fn(function) => {
let inp = parse_test_sig(function, rand);
let __tok: syn::ItemFn = syn::parse_macro_input!(inp as syn::ItemFn);
let tok = quote! {
#__tok
};
result = quote! {
#result
#tok
};
}
_ => continue,
}
}
let result = quote! {
#result
};
let header = quote! {
#[::core::prelude::v1::test]
};
let finalres = quote! {
#header
#(#attrs)*
#mod_token #vis #modname {
#result
}
};
finalres.into()
}
#[proc_macro_attribute]
pub fn dbtest(args: TokenStream, item: TokenStream) -> TokenStream {
parse_test_module(args, item)
}

@ -7,4 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tdb-derive = {path="../tdb-derive"}
tdb_macros = {path="../tdb-macros"}

@ -24,4 +24,4 @@
//! This crate contains modules which are used for testing TerrabaseDB
pub mod service;
pub use tdb_derive;
pub use tdb_macros;

Loading…
Cancel
Save