Skip to content

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 5 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/scripts/run_rustc_tests.sh
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
21 changes: 21 additions & 0 deletions .github/workflows/nightly.yml
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"
12 changes: 12 additions & 0 deletions Cargo.toml
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",
]
3 changes: 3 additions & 0 deletions rust-toolchain.toml
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"]
14 changes: 14 additions & 0 deletions tools/compiletest/Cargo.toml
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" ] }
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
16 changes: 16 additions & 0 deletions tools/compiletest/build.rs
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());
}
40 changes: 40 additions & 0 deletions tools/compiletest/src/args.rs
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
}
}
12 changes: 12 additions & 0 deletions tools/compiletest/src/main.rs
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);
}
12 changes: 12 additions & 0 deletions tools/test-drive/Cargo.toml
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
15 changes: 15 additions & 0 deletions tools/test-drive/build.rs
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()
);
}
88 changes: 88 additions & 0 deletions tools/test-drive/src/main.rs
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;
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
}
Loading