Skip to content

Commit

Permalink
Add nativelink_test macro for tests (#888)
Browse files Browse the repository at this point in the history
This macro is to replace the tokio::test macro. It will allow us to
do some setup & cleanup on all tests without burdening the dev with
wrappers.
  • Loading branch information
allada authored Apr 26, 2024
1 parent 7f799d7 commit c0d7eaa
Show file tree
Hide file tree
Showing 46 changed files with 442 additions and 301 deletions.
62 changes: 38 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions nativelink-macro/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load(
"@rules_rust//rust:defs.bzl",
"rust_doc",
"rust_proc_macro",
)

rust_proc_macro(
name = "nativelink-macro",
srcs = [
"src/lib.rs",
],
visibility = ["//visibility:public"],
deps = [
"@crates//:proc-macro2",
"@crates//:quote",
"@crates//:syn",
],
)

rust_doc(
name = "docs",
crate = ":nativelink-macro",
)
14 changes: 14 additions & 0 deletions nativelink-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "nativelink-macro"
version = "0.3.0"
edition = "2021"

[lib]
crate_type = ["proc-macro"]

[dependencies]
# TODO(allada) We currently need to pin these to specific version.
# Some down-stream can't be upgraded just yet.
proc-macro2 = "=1.0.78"
quote = "=1.0.35"
syn = "=2.0.52"
41 changes: 41 additions & 0 deletions nativelink-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 The NativeLink Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn};

#[proc_macro_attribute]
pub fn nativelink_test(attr: TokenStream, item: TokenStream) -> TokenStream {
let attr = proc_macro2::TokenStream::from(attr);
let input_fn = parse_macro_input!(item as ItemFn);

let fn_name = &input_fn.sig.ident;
let fn_block = &input_fn.block;
let fn_inputs = &input_fn.sig.inputs;
let fn_output = &input_fn.sig.output;
let fn_attr = &input_fn.attrs;

let expanded = quote! {
#(#fn_attr)*
#[tokio::test(#attr)]
async fn #fn_name(#fn_inputs) #fn_output {
#fn_block
}
};

TokenStream::from(expanded)
}
1 change: 1 addition & 0 deletions nativelink-scheduler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ rust_test_suite(
"tests/utils/scheduler_utils.rs",
],
proc_macro_deps = [
"//nativelink-macro",
"@crates//:async-trait",
],
deps = [
Expand Down
2 changes: 2 additions & 0 deletions nativelink-scheduler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ tonic = { version = "0.11.0", features = ["gzip", "tls"] }
tracing = "0.1.40"

[dev-dependencies]
nativelink-macro = { path = "../nativelink-macro" }

pretty_assertions = "1.4.0"
9 changes: 5 additions & 4 deletions nativelink-scheduler/tests/action_messages_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::sync::Arc;
use std::time::{Duration, SystemTime};

use nativelink_error::Error;
use nativelink_macro::nativelink_test;
use nativelink_proto::build::bazel::remote::execution::v2::ExecuteResponse;
use nativelink_proto::google::longrunning::{operation, Operation};
use nativelink_proto::google::rpc::Status;
Expand All @@ -41,7 +42,7 @@ mod action_messages_tests {

use super::*; // Must be declared in every module.

#[tokio::test]
#[nativelink_test]
async fn action_state_any_url_test() -> Result<(), Error> {
let action_state = ActionState {
unique_qualifier: ActionInfoHashKey {
Expand All @@ -68,7 +69,7 @@ mod action_messages_tests {
Ok(())
}

#[tokio::test]
#[nativelink_test]
async fn execute_response_status_message_is_some_on_success_test() -> Result<(), Error> {
let execute_response: ExecuteResponse = ActionStage::Completed(ActionResult {
output_files: vec![],
Expand Down Expand Up @@ -102,7 +103,7 @@ mod action_messages_tests {
Ok(())
}

#[tokio::test]
#[nativelink_test]
async fn highest_priority_action_first() -> Result<(), Error> {
const INSTANCE_NAME: &str = "foobar_instance_name";

Expand Down Expand Up @@ -158,7 +159,7 @@ mod action_messages_tests {
Ok(())
}

#[tokio::test]
#[nativelink_test]
async fn equal_priority_earliest_first() -> Result<(), Error> {
const INSTANCE_NAME: &str = "foobar_instance_name";

Expand Down
Loading

0 comments on commit c0d7eaa

Please sign in to comment.