serial_test_derive-0.5.1/.cargo_vcs_info.json0000644000000001121375755443700147670ustar { "git": { "sha1": "910aef4106ba891271f0f0d22ac356fc6cc1204f" } } serial_test_derive-0.5.1/Cargo.toml0000644000000020301375755443700127660ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "serial_test_derive" version = "0.5.1" authors = ["Tom Parker-Shemilt "] description = "Helper crate for serial_test" readme = "README.md" categories = ["development-tools::testing"] license = "MIT" repository = "https://github.com/palfrey/serial_test/" [lib] proc-macro = true [dependencies.proc-macro2] version = "1.0" [dependencies.quote] version = "1.0" [dependencies.syn] version = "1.0" features = ["full"] [dev-dependencies.env_logger] version = ">= 0.7, <0.9" serial_test_derive-0.5.1/Cargo.toml.orig010064400017500001750000000007271375755403400164640ustar 00000000000000[package] name = "serial_test_derive" description = "Helper crate for serial_test" license = "MIT" version = "0.5.1" authors = ["Tom Parker-Shemilt "] edition = "2018" readme = "README.md" repository = "https://github.com/palfrey/serial_test/" categories = ["development-tools::testing"] [lib] proc-macro = true [dependencies] quote = "1.0" syn = { version="1.0", features=["full"] } proc-macro2 = "1.0" [dev-dependencies] env_logger = ">= 0.7, <0.9"serial_test_derive-0.5.1/LICENSE010064400017500001750000000020451371232757400145730ustar 00000000000000Copyright (c) 2018 Tom Parker-Shemilt Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.serial_test_derive-0.5.1/README.md010064400017500001750000000031131375755365100150500ustar 00000000000000# serial_test [![Version](https://img.shields.io/crates/v/serial_test.svg)](https://crates.io/crates/serial_test) [![Downloads](https://img.shields.io/crates/d/serial_test)](https://crates.io/crates/serial_test) [![Docs](https://docs.rs/serial_test/badge.svg)](https://docs.rs/serial_test/) [![MIT license](https://img.shields.io/crates/l/serial_test.svg)](./LICENSE) [![Build Status](https://travis-ci.com/palfrey/serial_test.svg?branch=main)](https://travis-ci.com/palfrey/serial_test) [![MSRV: 1.39.0](https://flat.badgen.net/badge/MSRV/1.39.0/purple)](https://blog.rust-lang.org/2019/11/07/Rust-1.39.0.html) [![dependency status](https://deps.rs/repo/github/palfrey/serial_test/status.svg)](https://deps.rs/repo/github/palfrey/serial_test) `serial_test` allows for the creation of serialised Rust tests using the `serial` attribute e.g. ```rust #[test] #[serial] fn test_serial_one() { // Do things } #[test] #[serial] fn test_serial_another() { // Do things } #[tokio::test] #[serial] async fn test_serial_another() { // Do things asynchronously } ``` Multiple tests with the `serial` attribute are guaranteed to be executed in serial. Ordering of the tests is not guaranteed however. ## Usage We require at least Rust 1.39 for [async/await](https://blog.rust-lang.org/2019/11/07/Async-await-stable.html) support Add to your Cargo.toml ```toml [dev-dependencies] serial_test = "*" ``` plus `use serial_test::serial;` (for Rust 2018) or ```rust #[macro_use] extern crate serial_test; ``` for earlier versions. You can then either add `#[serial]` or `#[serial(some_text)]` to tests as required. serial_test_derive-0.5.1/src/lib.rs010064400017500001750000000142661372272737600155060ustar 00000000000000//! # serial_test_derive //! Helper crate for [serial_test](../serial_test/index.html) extern crate proc_macro; use proc_macro::TokenStream; use proc_macro2::TokenTree; use quote::quote; use std::ops::Deref; use syn; /// Allows for the creation of serialised Rust tests /// ```` /// #[test] /// #[serial] /// fn test_serial_one() { /// // Do things /// } /// /// #[test] /// #[serial] /// fn test_serial_another() { /// // Do things /// } /// ```` /// Multiple tests with the [serial](attr.serial.html) attribute are guaranteed to be executed in serial. Ordering /// of the tests is not guaranteed however. If you want different subsets of tests to be serialised with each /// other, but not depend on other subsets, you can add an argument to [serial](attr.serial.html), and all calls /// with identical arguments will be called in serial. e.g. /// ```` /// #[test] /// #[serial(something)] /// fn test_serial_one() { /// // Do things /// } /// /// #[test] /// #[serial(something)] /// fn test_serial_another() { /// // Do things /// } /// /// #[test] /// #[serial(other)] /// fn test_serial_third() { /// // Do things /// } /// /// #[test] /// #[serial(other)] /// fn test_serial_fourth() { /// // Do things /// } /// ```` /// `test_serial_one` and `test_serial_another` will be executed in serial, as will `test_serial_third` and `test_serial_fourth` /// but neither sequence will be blocked by the other #[proc_macro_attribute] pub fn serial(attr: TokenStream, input: TokenStream) -> TokenStream { return serial_core(attr.into(), input.into()).into(); } fn serial_core( attr: proc_macro2::TokenStream, input: proc_macro2::TokenStream, ) -> proc_macro2::TokenStream { let attrs = attr.into_iter().collect::>(); let key = match attrs.len() { 0 => "".to_string(), 1 => { if let TokenTree::Ident(id) = &attrs[0] { id.to_string() } else { panic!("Expected a single name as argument, got {:?}", attrs); } } n => { panic!("Expected either 0 or 1 arguments, got {}: {:?}", n, attrs); } }; let ast: syn::ItemFn = syn::parse2(input).unwrap(); let asyncness = ast.sig.asyncness; let name = ast.sig.ident; let return_type = match ast.sig.output { syn::ReturnType::Default => None, syn::ReturnType::Type(_rarrow, ref box_type) => Some(box_type.deref()), }; let block = ast.block; let attrs: Vec = ast .attrs .into_iter() .filter(|at| { if let Ok(m) = at.parse_meta() { let path = m.path(); if path.is_ident("ignore") || path.is_ident("should_panic") { // we skip ignore/should_panic because the test framework already deals with it false } else { true } } else { true } }) .collect(); let gen = if let Some(ret) = return_type { match asyncness { Some(_) => quote! { #(#attrs) * async fn #name () -> #ret { serial_test::async_serial_core_with_return(#key, || async { #block }).await; } }, None => quote! { #(#attrs) * fn #name () -> #ret { serial_test::serial_core_with_return(#key, || { #block }) } }, } } else { match asyncness { Some(_) => quote! { #(#attrs) * async fn #name () { serial_test::async_serial_core(#key, || async { #block }).await; } }, None => quote! { #(#attrs) * fn #name () { serial_test::serial_core(#key, || { #block }); } }, } }; return gen.into(); } #[test] fn test_serial() { let attrs = proc_macro2::TokenStream::new(); let input = quote! { #[test] fn foo() {} }; let stream = serial_core(attrs.into(), input); let compare = quote! { #[test] fn foo () { serial_test::serial_core("", || { {} }); } }; assert_eq!(format!("{}", compare), format!("{}", stream)); } #[test] fn test_stripped_attributes() { let _ = env_logger::builder().is_test(true).try_init(); let attrs = proc_macro2::TokenStream::new(); let input = quote! { #[test] #[ignore] #[should_panic(expected = "Testing panic")] #[something_else] fn foo() {} }; let stream = serial_core(attrs.into(), input); let compare = quote! { #[test] #[something_else] fn foo () { serial_test::serial_core("", || { {} }); } }; assert_eq!(format!("{}", compare), format!("{}", stream)); } #[test] fn test_serial_async() { let attrs = proc_macro2::TokenStream::new(); let input = quote! { #[tokio::test] async fn foo() {} }; let stream = serial_core(attrs.into(), input); let compare = quote! { #[tokio::test] async fn foo () { serial_test::async_serial_core("", || async { {} }).await; } }; assert_eq!(format!("{}", compare), format!("{}", stream)); } #[test] fn test_serial_async_return() { let attrs = proc_macro2::TokenStream::new(); let input = quote! { #[tokio::test] async fn foo() -> Result<(), ()> { Ok(()) } }; let stream = serial_core(attrs.into(), input); let compare = quote! { #[tokio::test] async fn foo () -> Result<(), ()> { serial_test::async_serial_core_with_return("", || async { { Ok(()) } }).await; } }; assert_eq!(format!("{}", compare), format!("{}", stream)); }