Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test_type to TestDesc to compile on nightly rust #19

Merged
merged 10 commits into from
Nov 4, 2019
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = "1.0.84"
serde_yaml = "0.8.7"
yaml-rust = "0.4.2"
ctor = "0.1.10"
cfg-if = "0.1.10"
region = { version = "2.1.2", optional = true }

[dev-dependencies]
Expand Down
6 changes: 4 additions & 2 deletions azure-pipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ jobs:
- template: ci/job-rustfmt.yml
- template: ci/job-check.yml
parameters:
toolchain: nightly-2019-09-13
toolchain: nightly-2019-11-02
features: ["post_v139"]
- template: ci/job-test.yml
parameters:
name: test_stable
Expand All @@ -21,4 +22,5 @@ jobs:
- template: ci/job-test.yml
parameters:
name: test_nightly
toolchain: nightly-2019-09-13
toolchain: nightly-2019-11-02
features: ["post_v139"]
5 changes: 3 additions & 2 deletions ci/job-check.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
toolchain: stable
features: []
jobs:
- job: check
pool:
Expand All @@ -11,9 +12,9 @@ jobs:
components:
- clippy
- script: |
cargo check --all --all-targets
cargo check --all --all-targets --features "${{ join(' ', parameters.features) }}"
displayName: cargo check
- script: |
cargo clippy --all
cargo clippy --all --features "${{ join(' ', parameters.features) }}"
displayName: cargo clippy --all

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 @@ -629,6 +631,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
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,22 @@ fn read_to_end(path: &Path) -> Vec<u8> {
.unwrap_or_else(|e| panic!("cannot read test input at '{}': {}", path.display(), e));
input
}

#[cfg(feature = "post_v139")]
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.
#[cfg(feature = "post_v139")]
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
}
}
83 changes: 32 additions & 51 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::data::{DataTestDesc, DataTestFn};
use crate::files::{FilesTestDesc, FilesTestFn};
#[cfg(feature = "post_v139")]
use crate::rustc_test::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 All @@ -50,37 +49,6 @@ fn derive_test_name(root: &Path, path: &Path, test_name: &str) -> String {
test_name
}

#[cfg(feature = "post_v139")]
fn test_desc(
name: TestName,
ignore: bool,
should_panic: ShouldPanic,
allow_fail: bool,
) -> TestDesc {
TestDesc {
name,
ignore,
should_panic,
allow_fail,
test_type: TestType::Unknown,
}
}

#[cfg(not(feature = "post_v139"))]
fn test_desc(
name: TestName,
ignore: bool,
should_panic: ShouldPanic,
allow_fail: bool,
) -> TestDesc {
TestDesc {
name,
ignore,
should_panic,
allow_fail,
}
}

/// When compiling tests, Rust compiler collects all items marked with `#[test_case]` and passes
/// references to them to the test runner in a slice (like `&[&test_a, &test_b, &test_c]`). Since
/// we need a different descriptor for our data-driven tests than the standard one, we have two
Expand Down Expand Up @@ -201,13 +169,15 @@ fn render_files_test(desc: &FilesTestDesc, rendered: &mut Vec<TestDescAndFn>) {

// Generate a standard test descriptor
let desc = TestDescAndFn {
desc: test_desc(
TestName::DynTestName(test_name),
desc: TestDesc {
name: TestName::DynTestName(test_name),
ignore,
ShouldPanic::No,
should_panic: ShouldPanic::No,
// Cannot be used on stable: https://github.com/rust-lang/rust/issues/46488
false,
),
allow_fail: false,
#[cfg(feature = "post_v139")]
test_type: crate::test_type(desc.source_file),
},
testfn,
};

Expand Down Expand Up @@ -245,12 +215,14 @@ fn render_data_test(desc: &DataTestDesc, rendered: &mut Vec<TestDescAndFn>) {

// Generate a standard test descriptor
let desc = TestDescAndFn {
desc: test_desc(
TestName::DynTestName(case_name),
desc.ignore,
ShouldPanic::No,
false,
),
desc: TestDesc {
name: TestName::DynTestName(case_name),
ignore: desc.ignore,
should_panic: ShouldPanic::No,
allow_fail: false,
#[cfg(feature = "post_v139")]
test_type: crate::test_type(desc.source_file),
},
testfn,
};

Expand Down Expand Up @@ -351,7 +323,14 @@ pub fn register(new: &mut RegistrationNode) {
#[doc(hidden)]
pub fn runner(tests: &[&dyn TestDescriptor]) {
let args = std::env::args().collect::<Vec<_>>();
let mut opts = match crate::rustc_test::parse_opts(&args) {
cfg_if::cfg_if! {
if #[cfg(feature = "post_v139")] {
let parsed = crate::rustc_test::test::parse_opts(&args);
} else {
let parsed = crate::rustc_test::parse_opts(&args);
}
};
let mut opts = match parsed {
Some(Ok(o)) => o,
Some(Err(msg)) => panic!("{:?}", msg),
None => return,
Expand Down Expand Up @@ -403,13 +382,15 @@ fn render_test_descriptor(
}
DatatestTestDesc::RegularTest(desc) => {
rendered.push(TestDescAndFn {
desc: test_desc(
TestName::StaticTestName(real_name(desc.name)),
desc.ignore,
desc: TestDesc {
name: TestName::StaticTestName(real_name(desc.name)),
ignore: desc.ignore,
should_panic: desc.should_panic.into(),
// FIXME: should support!
desc.should_panic.into(),
false,
),
allow_fail: false,
#[cfg(feature = "post_v139")]
test_type: crate::test_type(desc.source_file),
},
testfn: TestFn::StaticTestFn(desc.testfn),
})
}
Expand Down