Skip to content

Commit

Permalink
Auto merge of #33588 - nikomatsakis:compiletest-ui, r=acrichto
Browse files Browse the repository at this point in the history
add UI testing framework

This adds a framework for capturing and tracking the precise output of rustc, which allows us to check all manner of minor details with the output. It's pretty strict right now -- the output must match almost exactly -- and hence maybe a bit too strict. But I figure we can add wildcards or whatever later. There is also a script intended to make updating the references easy, though the script could make things a *bit* easier (in particular, it'd be nice if it would find the build directory for you automatically).

One thing I was wondering about is the best way to test colors. Since windows doesn't embed those in the output stream, this test framework can't test colors on windows -- so I figure we can just write tests that are ignored on windows and which pass `--color=always` or whatever to rustc.

cc @jonathandturner
r? @alexcrichton
  • Loading branch information
bors committed May 16, 2016
2 parents 4fdf2c4 + 24cfa1e commit cd6a400
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 3 deletions.
15 changes: 13 additions & 2 deletions mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ check-stage$(1)-T-$(2)-H-$(3)-exec: \
check-stage$(1)-T-$(2)-H-$(3)-debuginfo-gdb-exec \
check-stage$(1)-T-$(2)-H-$(3)-debuginfo-lldb-exec \
check-stage$(1)-T-$(2)-H-$(3)-incremental-exec \
check-stage$(1)-T-$(2)-H-$(3)-ui-exec \
check-stage$(1)-T-$(2)-H-$(3)-doc-exec \
check-stage$(1)-T-$(2)-H-$(3)-pretty-exec

Expand Down Expand Up @@ -452,6 +453,9 @@ CODEGEN_CC := $(call rwildcard,$(S)src/test/codegen/,*.cc)
CODEGEN_UNITS_RS := $(call rwildcard,$(S)src/test/codegen-units/,*.rs)
INCREMENTAL_RS := $(call rwildcard,$(S)src/test/incremental/,*.rs)
RMAKE_RS := $(wildcard $(S)src/test/run-make/*/Makefile)
UI_RS := $(call rwildcard,$(S)src/test/ui/,*.rs) \
$(call rwildcard,$(S)src/test/ui/,*.stdout) \
$(call rwildcard,$(S)src/test/ui/,*.stderr)
RUSTDOCCK_RS := $(call rwildcard,$(S)src/test/rustdoc/,*.rs)

RPASS_TESTS := $(RPASS_RS)
Expand All @@ -469,6 +473,7 @@ CODEGEN_TESTS := $(CODEGEN_RS) $(CODEGEN_CC)
CODEGEN_UNITS_TESTS := $(CODEGEN_UNITS_RS)
INCREMENTAL_TESTS := $(INCREMENTAL_RS)
RMAKE_TESTS := $(RMAKE_RS)
UI_TESTS := $(UI_RS)
RUSTDOCCK_TESTS := $(RUSTDOCCK_RS)

CTEST_SRC_BASE_rpass = run-pass
Expand Down Expand Up @@ -541,6 +546,11 @@ CTEST_BUILD_BASE_rmake = run-make
CTEST_MODE_rmake = run-make
CTEST_RUNTOOL_rmake = $(CTEST_RUNTOOL)

CTEST_SRC_BASE_ui = ui
CTEST_BUILD_BASE_ui = ui
CTEST_MODE_ui = ui
CTEST_RUNTOOL_ui = $(CTEST_RUNTOOL)

CTEST_SRC_BASE_rustdocck = rustdoc
CTEST_BUILD_BASE_rustdocck = rustdoc
CTEST_MODE_rustdocck = rustdoc
Expand Down Expand Up @@ -672,7 +682,7 @@ CTEST_DEPS_codegen-units_$(1)-T-$(2)-H-$(3) = $$(CODEGEN_UNITS_TESTS)
CTEST_DEPS_incremental_$(1)-T-$(2)-H-$(3) = $$(INCREMENTAL_TESTS)
CTEST_DEPS_rmake_$(1)-T-$(2)-H-$(3) = $$(RMAKE_TESTS) \
$$(CSREQ$(1)_T_$(3)_H_$(3)) $$(SREQ$(1)_T_$(2)_H_$(3))

CTEST_DEPS_ui_$(1)-T-$(2)-H-$(3) = $$(UI_TESTS)
CTEST_DEPS_rustdocck_$(1)-T-$(2)-H-$(3) = $$(RUSTDOCCK_TESTS) \
$$(HBIN$(1)_H_$(3))/rustdoc$$(X_$(3)) \
$(S)src/etc/htmldocck.py
Expand Down Expand Up @@ -744,7 +754,7 @@ endef

CTEST_NAMES = rpass rpass-valgrind rpass-full rfail-full cfail-full rfail cfail pfail \
debuginfo-gdb debuginfo-lldb codegen codegen-units rustdocck incremental \
rmake
rmake ui

$(foreach host,$(CFG_HOST), \
$(eval $(foreach target,$(CFG_TARGET), \
Expand Down Expand Up @@ -943,6 +953,7 @@ TEST_GROUPS = \
codegen \
codegen-units \
incremental \
ui \
doc \
$(foreach docname,$(DOC_NAMES),doc-$(docname)) \
pretty \
Expand Down
8 changes: 8 additions & 0 deletions src/bootstrap/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ impl Build {
check::compiletest(self, &compiler, target.target,
"codegen-units", "codegen-units");
}
CheckIncremental { compiler } => {
check::compiletest(self, &compiler, target.target,
"incremental", "incremental");
}
CheckUi { compiler } => {
check::compiletest(self, &compiler, target.target,
"ui", "ui");
}
CheckDebuginfo { compiler } => {
if target.target.contains("msvc") ||
target.target.contains("android") {
Expand Down
6 changes: 6 additions & 0 deletions src/bootstrap/build/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ macro_rules! targets {
(check_pfail, CheckPFail { compiler: Compiler<'a> }),
(check_codegen, CheckCodegen { compiler: Compiler<'a> }),
(check_codegen_units, CheckCodegenUnits { compiler: Compiler<'a> }),
(check_incremental, CheckIncremental { compiler: Compiler<'a> }),
(check_ui, CheckUi { compiler: Compiler<'a> }),
(check_debuginfo, CheckDebuginfo { compiler: Compiler<'a> }),
(check_rustdoc, CheckRustdoc { compiler: Compiler<'a> }),
(check_pretty, CheckPretty { compiler: Compiler<'a> }),
Expand Down Expand Up @@ -379,6 +381,8 @@ impl<'a> Step<'a> {
self.check_cfail(compiler),
self.check_rfail(compiler),
self.check_pfail(compiler),
self.check_incremental(compiler),
self.check_ui(compiler),
self.check_crate_std(compiler),
self.check_crate_test(compiler),
self.check_crate_rustc(compiler),
Expand Down Expand Up @@ -412,6 +416,8 @@ impl<'a> Step<'a> {
Source::CheckPFail { compiler } |
Source::CheckCodegen { compiler } |
Source::CheckCodegenUnits { compiler } |
Source::CheckIncremental { compiler } |
Source::CheckUi { compiler } |
Source::CheckRustdoc { compiler } |
Source::CheckPretty { compiler } |
Source::CheckCFail { compiler } |
Expand Down
31 changes: 31 additions & 0 deletions src/test/ui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Guide to the UI Tests

The UI tests are intended to capture the compiler's complete output,
so that we can test all aspects of the presentation. They work by
compiling a file (e.g., `hello_world/main.rs`), capturing the output,
and then applying some normalization (see below). This normalized
result is then compared against reference files named
`hello_world/main.stderr` and `hello_world/main.stdout`. If either of
those files doesn't exist, the output must be empty. If the test run
fails, we will print out the current output, but it is also saved in
`build/<target-triple>/test/ui/hello_world/main.stdout` (this path is
printed as part of the test failure mesage), so you can run `diff` and
so forth.

# Editing and updating the reference files

If you have changed the compiler's output intentionally, or you are
making a new test, you can use the script `update-references.sh` to
update the references. When you run the test framework, it will report
various errors: in those errors is a command you can use to run the
`update-references.sh` script, which will then copy over the files
from the build directory and use them as the new reference. You can
also just run `update-all-references.sh`. In both cases, you can run
the script with `--help` to get a help message.

# Normalization

The normalization applied is aimed at filenames:

- the test directory is replaced with `$DIR`
- all backslashes (\) are converted to forward slashes (/) (for windows)
15 changes: 15 additions & 0 deletions src/test/ui/hello_world/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test that compiling hello world succeeds with no output of any kind.

fn main() {
println!("Hello, world!");
}
17 changes: 17 additions & 0 deletions src/test/ui/mismatched_types/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// rustc-env:RUST_NEW_ERROR_FORMAT

fn main() {
let x: u32 = (
);
}

8 changes: 8 additions & 0 deletions src/test/ui/mismatched_types/main.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: mismatched types [--explain E0308]
--> $DIR/main.rs:14:18
14 |> let x: u32 = (
|> ^ expected u32, found ()
note: expected type `u32`
note: found type `()`

error: aborting due to previous error
31 changes: 31 additions & 0 deletions src/test/ui/update-all-references.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
#
# Copyright 2015 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

# A script to update the references for all tests. The idea is that
# you do a run, which will generate files in the build directory
# containing the (normalized) actual output of the compiler. You then
# run this script, which will copy those files over. If you find
# yourself manually editing a foo.stderr file, you're doing it wrong.
#
# See all `update-references.sh`, if you just want to update a single test.

if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "" || "$2" != "" ]]; then
echo "usage: $0 <build-directory>"
echo ""
echo "For example:"
echo " $0 ../../../build/x86_64-apple-darwin/test/ui"
fi

BUILD_DIR=$PWD/$1
MY_DIR=$(dirname $0)
cd $MY_DIR
find . -name '*.rs' | xargs ./update-references.sh $BUILD_DIR
50 changes: 50 additions & 0 deletions src/test/ui/update-references.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
#
# Copyright 2015 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

# A script to update the references for particular tests. The idea is
# that you do a run, which will generate files in the build directory
# containing the (normalized) actual output of the compiler. This
# script will then copy that output and replace the "expected output"
# files. You can then commit the changes.
#
# If you find yourself manually editing a foo.stderr file, you're
# doing it wrong.

if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "" || "$2" == "" ]]; then
echo "usage: $0 <build-directory> <relative-path-to-rs-files>"
echo ""
echo "For example:"
echo " $0 ../../../build/x86_64-apple-darwin/test/ui *.rs */*.rs"
fi

MYDIR=$(dirname $0)

BUILD_DIR="$1"
shift

while [[ "$1" != "" ]]; do
STDERR_NAME="${1/%.rs/.stderr}"
STDOUT_NAME="${1/%.rs/.stdout}"
shift
if [ -f $BUILD_DIR/$STDOUT_NAME ] && \
! (diff $BUILD_DIR/$STDOUT_NAME $MYDIR/$STDOUT_NAME > /dev/null); then
echo updating $MYDIR/$STDOUT_NAME
cp $BUILD_DIR/$STDOUT_NAME $MYDIR/$STDOUT_NAME
fi
if [ -f $BUILD_DIR/$STDERR_NAME ] && \
! (diff $BUILD_DIR/$STDERR_NAME $MYDIR/$STDERR_NAME > /dev/null); then
echo updating $MYDIR/$STDERR_NAME
cp $BUILD_DIR/$STDERR_NAME $MYDIR/$STDERR_NAME
fi
done


3 changes: 3 additions & 0 deletions src/tools/compiletest/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum Mode {
CodegenUnits,
Incremental,
RunMake,
Ui,
}

impl FromStr for Mode {
Expand All @@ -47,6 +48,7 @@ impl FromStr for Mode {
"codegen-units" => Ok(CodegenUnits),
"incremental" => Ok(Incremental),
"run-make" => Ok(RunMake),
"ui" => Ok(Ui),
_ => Err(()),
}
}
Expand All @@ -68,6 +70,7 @@ impl fmt::Display for Mode {
CodegenUnits => "codegen-units",
Incremental => "incremental",
RunMake => "run-make",
Ui => "ui",
}, f)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/compiletest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub mod runtest;
pub mod common;
pub mod errors;
mod raise_fd_limit;
mod uidiff;

fn main() {
#[cfg(cargobuild)]
Expand Down
Loading

0 comments on commit cd6a400

Please sign in to comment.