-
Notifications
You must be signed in to change notification settings - Fork 15
Create tooling for end-to-end testing #25
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
set -u | ||
|
||
# Location of a rust repository. Clone one if path doesn't exist. | ||
RUST_REPO="${RUST_REPO:?Missing path to rust repository. Set RUST_REPO}" | ||
# Where we will store the SMIR tools (Optional). | ||
TOOLS_BIN="${TOOLS_BIN:-"/tmp/smir/bin"}" | ||
# Assume we are inside SMIR repository | ||
SMIR_PATH=$(git rev-parse --show-toplevel) | ||
export RUST_BACKTRACE=1 | ||
|
||
pushd "${SMIR_PATH}" | ||
cargo build -Z unstable-options --out-dir "${TOOLS_BIN}" | ||
export PATH="${TOOLS_BIN}":"${PATH}" | ||
|
||
if [[ ! -e "${RUST_REPO}" ]]; then | ||
mkdir -p "$(dirname ${RUST_REPO})" | ||
git clone --depth 1 https://github.com/rust-lang/rust.git "${RUST_REPO}" | ||
fi | ||
|
||
pushd "${RUST_REPO}" | ||
SUITES=( | ||
# Match https://github.com/rust-lang/rust/blob/master/src/bootstrap/test.rs for now | ||
"tests/ui/cfg ui" | ||
) | ||
for suite_cfg in "${SUITES[@]}"; do | ||
# Hack to work on older bash like the ones on MacOS. | ||
suite_pair=($suite_cfg) | ||
suite=${suite_pair[0]} | ||
mode=${suite_pair[1]} | ||
echo "${suite_cfg} pair: $suite_pair mode: $mode" | ||
compiletest --driver-path="${TOOLS_BIN}/test-drive" --mode=${mode} --src-base="${suite}" --output-path "${RUST_REPO}/build" | ||
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: Run compiler tests | ||
|
||
on: | ||
schedule: | ||
- cron: "0 6 * * *" # Run daily at 06:00 UTC | ||
workflow_dispatch: # Allow manual dispatching | ||
pull_request: | ||
|
||
jobs: | ||
compile-test: | ||
name: Rust Compiler Tests | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Test | ||
run: ./.github/scripts/run_rustc_tests.sh | ||
env: | ||
RUST_REPO: "/tmp/rustc" | ||
TOOLS_BIN: "/tmp/smir/bin" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Cargo workspace for utility tools used to check stable-mir in CI | ||
[workspace] | ||
resolver = "2" | ||
members = [ | ||
"tools/compiletest", | ||
"tools/test-drive", | ||
] | ||
|
||
exclude = [ | ||
"build", | ||
"target", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[toolchain] | ||
channel = "nightly" | ||
components = ["llvm-tools-preview", "rustc-dev", "rust-src", "rustfmt"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "compiletest" | ||
description = "Run tests using compiletest-rs" | ||
version = "0.0.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
compiletest_rs = { version = "0.10.0", features = [ "rustc" ] } | ||
celinval marked this conversation as resolved.
Show resolved
Hide resolved
|
||
clap = { version = "4.1.3", features = ["derive"] } | ||
|
||
[package.metadata.rust-analyzer] | ||
# This crate uses #[feature(rustc_private)]. | ||
# See https://github.com/rust-analyzer/rust-analyzer/pull/7891 | ||
rustc_private = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
pub fn main() { | ||
// Add rustup to the rpath in order to properly link with the correct rustc version. | ||
let rustup_home = env::var("RUSTUP_HOME").unwrap(); | ||
let toolchain = env::var("RUSTUP_TOOLCHAIN").unwrap(); | ||
let rustc_lib: PathBuf = [&rustup_home, "toolchains", &toolchain, "lib"] | ||
.iter() | ||
.collect(); | ||
println!( | ||
"cargo:rustc-link-arg-bin=compiletest=-Wl,-rpath,{}", | ||
rustc_lib.display() | ||
); | ||
println!("cargo:rustc-env=RUSTC_LIB_PATH={}", rustc_lib.display()); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use compiletest_rs::Config; | ||
use std::fmt::Debug; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug, clap::Parser)] | ||
#[command(version, name = "compiletest")] | ||
pub struct Args { | ||
/// The path where all tests are | ||
#[arg(long)] | ||
src_base: PathBuf, | ||
|
||
/// The mode according to compiletest modes. | ||
#[arg(long)] | ||
mode: String, | ||
|
||
/// Path for the stable-mir driver. | ||
#[arg(long)] | ||
driver_path: PathBuf, | ||
|
||
/// Path for where the output should be stored. | ||
#[arg(long)] | ||
output_path: PathBuf, | ||
|
||
#[arg(long)] | ||
verbose: bool, | ||
} | ||
|
||
impl From<Args> for Config { | ||
fn from(args: Args) -> Config { | ||
let mut config = Config::default(); | ||
config.mode = args.mode.parse().expect("Invalid mode"); | ||
config.src_base = args.src_base; | ||
config.rustc_path = args.driver_path; | ||
config.build_base = args.output_path; | ||
config.verbose = args.verbose; | ||
config.run_lib_path = PathBuf::from(env!("RUSTC_LIB_PATH")); | ||
config.link_deps(); | ||
config | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//! Run compiletest on a given folder. | ||
|
||
mod args; | ||
use clap::Parser; | ||
use compiletest_rs::Config; | ||
|
||
fn main() { | ||
let args = args::Args::parse(); | ||
println!("args: ${args:?}"); | ||
let cfg = Config::from(args); | ||
compiletest_rs::run_tests(&cfg); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "test-drive" | ||
description = "A rustc wrapper that can be used to test stable-mir on a crate" | ||
version = "0.0.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
||
[package.metadata.rust-analyzer] | ||
# This crate uses #[feature(rustc_private)]. | ||
# See https://github.com/rust-analyzer/rust-analyzer/pull/7891 | ||
rustc_private = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
pub fn main() { | ||
// Add rustup to the rpath in order to properly link with the correct rustc version. | ||
let rustup_home = env::var("RUSTUP_HOME").unwrap(); | ||
let toolchain = env::var("RUSTUP_TOOLCHAIN").unwrap(); | ||
let rustc_lib: PathBuf = [&rustup_home, "toolchains", &toolchain, "lib"] | ||
.iter() | ||
.collect(); | ||
println!( | ||
"cargo:rustc-link-arg-bin=test-drive=-Wl,-rpath,{}", | ||
rustc_lib.display() | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! Test that users are able to inspec the MIR body of functions and types | ||
|
||
#![feature(rustc_private)] | ||
#![feature(assert_matches)] | ||
#![feature(result_option_inspect)] | ||
|
||
mod sanity_checks; | ||
|
||
extern crate rustc_middle; | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
extern crate rustc_smir; | ||
|
||
use rustc_middle::ty::TyCtxt; | ||
use rustc_smir::{rustc_internal, stable_mir}; | ||
use std::panic::{catch_unwind, AssertUnwindSafe}; | ||
use std::process::ExitCode; | ||
|
||
const CHECK_ARG: &str = "--check-smir"; | ||
|
||
type TestResult = Result<(), String>; | ||
|
||
/// This is a wrapper that can be used to replace rustc. | ||
/// | ||
/// Besides all supported rustc arguments, use `--check-smir` to run all the stable-mir checks. | ||
/// This allows us to use this tool in cargo projects to analyze the target crate only by running | ||
/// `cargo rustc --check-smir`. | ||
fn main() -> ExitCode { | ||
let mut check_smir = false; | ||
let args: Vec<_> = std::env::args() | ||
.filter(|arg| { | ||
let is_check_arg = arg == CHECK_ARG; | ||
check_smir |= is_check_arg; | ||
!is_check_arg | ||
}) | ||
.collect(); | ||
|
||
|
||
let callback = if check_smir { test_stable_mir } else { |_: TyCtxt| ExitCode::SUCCESS }; | ||
let result = rustc_internal::StableMir::new(args, callback).continue_compilation().run(); | ||
if let Ok(test_result) = result { | ||
test_result | ||
} else { | ||
ExitCode::FAILURE | ||
} | ||
} | ||
|
||
macro_rules! run_tests { | ||
($( $test:path ),+) => { | ||
[$({ | ||
run_test(stringify!($test), || { $test() }) | ||
},)+] | ||
}; | ||
} | ||
|
||
/// This function invoke other tests and process their results. | ||
/// Tests should avoid panic, | ||
fn test_stable_mir(tcx: TyCtxt<'_>) -> ExitCode { | ||
let results = run_tests![ | ||
sanity_checks::test_entry_fn, | ||
sanity_checks::test_all_fns, | ||
sanity_checks::test_traits, | ||
sanity_checks::test_crates | ||
]; | ||
let (success, failure): (Vec<_>, Vec<_>) = results.iter().partition(|r| r.is_ok()); | ||
println!( | ||
"Ran {} tests. {} succeeded. {} failed", | ||
results.len(), | ||
success.len(), | ||
failure.len() | ||
); | ||
if failure.is_empty() { | ||
ExitCode::SUCCESS | ||
} else { | ||
ExitCode::FAILURE | ||
} | ||
} | ||
|
||
fn run_test<F: FnOnce() -> TestResult>(name: &str, f: F) -> TestResult { | ||
let result = match catch_unwind(AssertUnwindSafe(f)) { | ||
Err(_) => Err("Panic: {}".to_string()), | ||
Ok(result) => result, | ||
}; | ||
println!( | ||
"Test {}: {}", | ||
name, | ||
result.as_ref().err().unwrap_or(&"Ok".to_string()) | ||
); | ||
result | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.