Skip to content

Commit

Permalink
guess TestType from where the test macros were expanded
Browse files Browse the repository at this point in the history
  • Loading branch information
cormacrelf committed Oct 19, 2019
1 parent 468f001 commit e8db020
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions datatest-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ fn files_internal(
pattern: #pattern_idx,
ignorefn: #ignore_func_ref,
testfn: ::datatest::__internal::FilesTestFn::#kind(#trampoline_func_ident),
source_file: file!(),
};

#[automatically_derived]
Expand Down Expand Up @@ -523,6 +524,7 @@ fn data_internal(
name: concat!(module_path!(), "::", #func_name_str),
ignore: #ignore,
describefn: #describe_func_ident,
source_file: file!(),
};

#[automatically_derived]
Expand Down Expand Up @@ -631,6 +633,7 @@ pub fn test_ctor_registration(
::datatest::__internal::assert_test_result(result);
},
should_panic: #should_panic,
source_file: file!(),
};

#func_item
Expand Down
1 change: 1 addition & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct DataTestDesc {
pub name: &'static str,
pub ignore: bool,
pub describefn: fn() -> Vec<DataTestCaseDesc<DataTestFn>>,
pub source_file: &'static str,
}

/// Used internally for `#[datatest::data(..)]` tests.
Expand Down
1 change: 1 addition & 0 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct FilesTestDesc {
pub pattern: usize,
pub ignorefn: Option<fn(&Path) -> bool>,
pub testfn: FilesTestFn,
pub source_file: &'static str,
}

/// Trait defining conversion into a function argument. We use it to convert discovered paths
Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,20 @@ fn read_to_end(path: &Path) -> Vec<u8> {
.unwrap_or_else(|e| panic!("cannot read test input at '{}': {}", path.display(), e));
input
}

use crate::rustc_test::TestType;

/// Helper function used internally, to mirror how rustc_test chooses a TestType.
/// Must be called with the result of `file!()` (called in macro output) to be meaningful.
pub fn test_type(path: &'static str) -> TestType {
if path.starts_with("src") {
// `/src` folder contains unit-tests.
TestType::UnitTest
} else if path.starts_with("tests") {
// `/tests` folder contains integration tests.
TestType::IntegrationTest
} else {
// Crate layout doesn't match expected one, test type is unknown.
TestType::Unknown
}
}
11 changes: 5 additions & 6 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::data::{DataTestDesc, DataTestFn};
use crate::files::{FilesTestDesc, FilesTestFn};
use crate::rustc_test::{
Bencher, ShouldPanic, TestDesc, TestDescAndFn, TestFn, TestName, TestType,
};
use crate::rustc_test::{Bencher, ShouldPanic, TestDesc, TestDescAndFn, TestFn, TestName};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};

Expand Down Expand Up @@ -34,6 +32,7 @@ pub struct RegularTestDesc {
pub ignore: bool,
pub testfn: fn(),
pub should_panic: RegularShouldPanic,
pub source_file: &'static str,
}

fn derive_test_name(root: &Path, path: &Path, test_name: &str) -> String {
Expand Down Expand Up @@ -176,7 +175,7 @@ fn render_files_test(desc: &FilesTestDesc, rendered: &mut Vec<TestDescAndFn>) {
should_panic: ShouldPanic::No,
// Cannot be used on stable: https://github.com/rust-lang/rust/issues/46488
allow_fail: false,
test_type: TestType::IntegrationTest,
test_type: crate::test_type(desc.source_file),
},
testfn,
};
Expand Down Expand Up @@ -220,7 +219,7 @@ fn render_data_test(desc: &DataTestDesc, rendered: &mut Vec<TestDescAndFn>) {
ignore: desc.ignore,
should_panic: ShouldPanic::No,
allow_fail: false,
test_type: TestType::IntegrationTest,
test_type: crate::test_type(desc.source_file),
},
testfn,
};
Expand Down Expand Up @@ -380,7 +379,7 @@ fn render_test_descriptor(
// FIXME: should support!
should_panic: desc.should_panic.into(),
allow_fail: false,
test_type: TestType::IntegrationTest,
test_type: crate::test_type(desc.source_file),
},
testfn: TestFn::StaticTestFn(desc.testfn),
})
Expand Down

0 comments on commit e8db020

Please sign in to comment.