Giter Site home page Giter Site logo

sergiobenitez / compiletest-rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from manishearth/compiletest-rs

0.0 2.0 0.0 335 KB

An extraction of the compiletest utility from the Rust compiler

License: Apache License 2.0

Rust 99.87% Pascal 0.13%

compiletest-rs's Introduction

compiletest-rs

This project is an attempt at extracting the compiletest utility from the Rust compiler.

The compiletest utility is useful for library and plugin developers, who want to include test programs that should fail to compile, issue warnings or otherwise produce compile-time output.

To use in your project

To use compiletest-rs in your application, add the following to Cargo.toml

[dev-dependencies]
compiletest_rs = "*"

Create a tests folder in the root folder of your project. Create a test file with something like the following:

extern crate compiletest_rs as compiletest;

use std::path::PathBuf;

fn run_mode(mode: &'static str) {
    let mut config = compiletest::Config::default();

    config.mode = mode.parse().expect("Invalid mode");
    config.src_base = PathBuf::from(format!("tests/{}", mode));
    config.link_deps(); // Populate config.target_rustcflags with dependencies on the path
    config.clean_rmeta(); // If your tests import the parent crate, this helps with E0464

    compiletest::run_tests(&config);
}

#[test]
fn compile_test() {
    run_mode("compile-fail");
    run_mode("run-pass");
}

Each mode corresponds to a folder with the same name in the tests folder. That is for the compile-fail mode the test runner looks for the tests/compile-fail folder.

Adding flags to the Rust compiler is a matter of assigning the correct field in the config. The most common flag to populate is the target_rustcflags to include the link dependencies on the path.

// NOTE! This is the manual way of adding flags
config.target_rustcflags = Some("-L target/debug".to_string());

This is useful (and necessary) for library development. Note that other secondary library dependencies may have their build artifacts placed in different (non-obvious) locations and these locations must also be added.

For convenience, Config provides a link_deps() method that populates target_rustcflags with all the dependencies found in the PATH variable (which is OS specific). For most cases, it should be sufficient to do:

let mut config = compiletest::Config::default();
config.link_deps();

Note that link_deps() should not be used if any of the added paths contain spaces, as these are currently not handled correctly.

Example

See the test-project folder for a complete working example using the compiletest-rs utility. Simply cd test-project and cargo test to see the tests run. The annotation syntax is documented in rust's test-suite.

TODO

  • The run-pass mode is strictly not necessary since it's baked right into Cargo, but I haven't bothered to take it out

Contributing

Thank you for your interest in improving this utility! Please consider submitting your patch to the upstream source instead, as it will be incorporated into this repo in due time. Still, there are some supporting files that are specific to this repo, for example:

  • src/lib.rs
  • src/uidiff.rs
  • test-project/

If you are unsure, open a pull request anyway and we would be glad to help!

compiletest-rs's People

Contributors

denzp avatar dtolnay avatar dwrensha avatar eijebong avatar erickt avatar fpoli avatar hdhoang avatar kennytm avatar llogiq avatar malbarbo avatar manishearth avatar mcarton avatar messense avatar munksgaard avatar pczarn avatar ryman avatar sanxiyn avatar sergiobenitez avatar simonsapin avatar valff avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.