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

Expand trybuild-like for tests missing in cargo metadata #69

Open
Robbepop opened this issue Oct 8, 2019 · 1 comment
Open

Expand trybuild-like for tests missing in cargo metadata #69

Robbepop opened this issue Oct 8, 2019 · 1 comment

Comments

@Robbepop
Copy link

Robbepop commented Oct 8, 2019

Original idea: dtolnay/trybuild#35
Suggested approach: dtolnay/trybuild#35 (comment)

Solution Summary

cargo expand should be able to expand the code of a test file somename.rs in the tests directory of a given crate via --test somename even though somename.rs exists but "somename" is not part of the test targets listed for the given crate by cargo-metadata.

Technical Details

The expansion should be done in a similar way as the trybuild crate handles building and testing of its tests:

  • pull out the code under test (e.g. our somename.rs) into a temp. directory
  • run rustc against that
    • just like trybuild would
    • using --pretty=expanded instead of building or testing anything
@tamuratak
Copy link

I have figured out how to deal the issue. Let's take proc-macro-workshop/builder as an example:

  1. Make an examples directory and move test codes to the directory.
Cargo.toml
examples/
 01-parse.rs
 02-create-builder.rs
 03-call-setters.rs
 04-call-build.rs 
 05-method-chaining.rs
 06-optional-field.rs
 07-repeated-field.rs
 08-unrecognized-attribute.rs
 08-unrecognized-attribute.stderr
 09-redefined-prelude-types.rs
src/
 lib.rs
tests/
 progress.rs
  1. Edit tests/progress.rs as follows:
#[test]
fn tests() {
    let t = trybuild::TestCases::new();
    t.pass("examples/01-parse.rs");
    //t.pass("examples/02-create-builder.rs");
    //t.pass("examples/03-call-setters.rs");
    //t.pass("examples/04-call-build.rs");
    //t.pass("examples/05-method-chaining.rs");
    //t.pass("examples/06-optional-field.rs");
    //t.pass("examples/07-repeated-field.rs");
    //t.compile_fail("examples/08-unrecognized-attribute.rs");
    //t.pass("examples/09-redefined-prelude-types.rs");
}
  1. Execute cargo expand --example 01-parse. We can expand examples/01-parse.rs. We can also test the lib with cargo test --tests.
$  cargo expand --example 01-parse
    Checking derive_builder v0.0.0 (/Users/tamura/src/github/proc-macro-workshop/builder)
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use derive_builder::Builder;
pub struct Command {
    executable: String,
    args: Vec<String>,
    env: Vec<String>,
    current_dir: String,
}
fn main() {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants