• Stars
    star
    216
  • Rank 183,179 (Top 4 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An extraction of the compiletest utility from the Rust compiler

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 = "0.10.0"

By default, compiletest-rs should be able to run on both stable, beta and nightly channels of rust. We use the tester fork of Rust's builtin test crate, so that we don't have require nightly. If you are running nightly and want to use Rust's test crate directly, you need to have the rustc development libraries install (which you can get by running rustup component add rustc-dev --toolchain nightly). Once you have the rustc development libraries installed, you can use the rustc feature to make compiletest use them instead of the tester crate.

[dev-dependencies]
compiletest_rs = { version = "0.10.0", features = [ "rustc" ] }

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 the rustc-guide.

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!

More Repositories

1

rust-gc

Simple tracing (mark and sweep) garbage collector for Rust
Rust
862
star
2

elsa

Append-only collections for Rust where borrows to entries can outlive insertions
Rust
171
star
3

triomphe

Fork of std::sync::Arc with lots of utilities useful for FFI
Rust
154
star
4

rust-adorn

Python-style function decorators for Rust
Rust
153
star
5

absolution

"Freedom from syn": Proc macro tools for operating on token trees
Rust
107
star
6

mitosis

Spawn processes with arbitrary closures in rust
Rust
105
star
7

oreutils

Installer for various Rust reimaginations of coreutils. These are not drop-in replacements.
Rust
93
star
8

array-init

Safe wrapper for initializing arrays
Rust
67
star
9

ChatExchange

A Python API for talking to Stack Exchange chat
Python
65
star
10

namespacing-rfc

RFC for Packages as Optional Namespaces
46
star
11

rust-tenacious

Lint to disallow the moving of marked types in Rust
Rust
34
star
12

pathdiff

Rust
28
star
13

cs733

CS 733 assignments
Go
25
star
14

rust-internals-docs

Deprecated, please use the official docs at https://doc.rust-lang.org/nightly/nightly-rustc
HTML
24
star
15

IIT-Timetable

Timetable generator for IIT Bombay
HTML
14
star
16

humpty_dumpty

Implicit Drop/move protection for Rust (linear types)
Rust
12
star
17

AnnoTabe

Annotations for tabs in Chrome
JavaScript
11
star
18

dash

Distributed Shell: A way to replicate system administration commands across machines using Raft
Go
10
star
19

handlebars-fluent

Rust
10
star
20

webgl-to-webvr

Example of turning a WebGL application into WebVR
JavaScript
8
star
21

Manish-Codes

JavaScript
8
star
22

manishearth.github.io

HTML
5
star
23

webgl-to-webxr

Webxr version of https://github.com/Manishearth/webgl-to-webvr
JavaScript
5
star
24

error-index-localization

Rust error index localization
Rust
4
star
25

trashmap

A HashMap and HashSet that operate directly on hashes instead of keys, avoiding rehashing
Rust
4
star
26

rust-extensible

Extensible enums for Rust
Rust
4
star
27

hashglobe

A stable fork of std::HashMap with fallible APIs. Untested.
Rust
3
star
28

reiterate

Iterator adaptor with caching that allows reiterating over the same iterator through the cache
Rust
3
star
29

sudoku-zkp

JavaScript
2
star
30

media-capture

playing around with gstreamer media capture (temporary repo)
Rust
2
star
31

typing-cheatsheets

Cheatsheets for various keyboards
HTML
2
star
32

HostelNoticeboard

Electronic noticeboard for IIT Bombay hostels. Meant to run on an Raspberry Pi.
PHP
2
star
33

sudoku

sudoku annotation/solving tool
JavaScript
2
star
34

webxr-three

three.js fork for webxr testing stuff.
JavaScript
2
star
35

Kapi

Math notebook for Windows 8 Metro
JavaScript
2
star
36

freestring

CString, but with malloc and free
Rust
2
star
37

MathToTeX

JavaScript
2
star
38

css-properties-list

HTML
2
star
39

testcrate

Rust
1
star
40

gstreamer-presentation

JavaScript
1
star
41

rustconf-2017

Website for RustConf 2017
CSS
1
star
42

ChatExchange-Scripts

Scripts that use ChatExchange
Python
1
star
43

ElectionPortal

The worst PHP code I have ever written. Hey, I was in a hurry.
CSS
1
star
44

stylo-flat

Copy of http://hg.mozilla.org/incubator/stylo with history removed for easy cloning and contribution
1
star
45

servo-gecko-try

Service for forwarding servo PRs to gecko's try infra
Python
1
star
46

iterm-git-add-hunk

lol
Shell
1
star
47

StackExchange-ChatBot

Generic Stack Exchange chatbot, using ChatExchange
Python
1
star
48

Presentations

Slides and presentations
JavaScript
1
star
49

rustfest-slides

Slides for my RustFest 2017 (Kiev) keynote
JavaScript
1
star
50

starcon-presentation

JavaScript
1
star
51

bell-inequality-paper

Term paper on Bell's inequality
TeX
1
star