Skip to content

Commit cd6a400

Browse files
committed
Auto merge of #33588 - nikomatsakis:compiletest-ui, r=acrichto
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
2 parents 4fdf2c4 + 24cfa1e commit cd6a400

File tree

13 files changed

+354
-3
lines changed

13 files changed

+354
-3
lines changed

mk/tests.mk

+13-2
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ check-stage$(1)-T-$(2)-H-$(3)-exec: \
274274
check-stage$(1)-T-$(2)-H-$(3)-debuginfo-gdb-exec \
275275
check-stage$(1)-T-$(2)-H-$(3)-debuginfo-lldb-exec \
276276
check-stage$(1)-T-$(2)-H-$(3)-incremental-exec \
277+
check-stage$(1)-T-$(2)-H-$(3)-ui-exec \
277278
check-stage$(1)-T-$(2)-H-$(3)-doc-exec \
278279
check-stage$(1)-T-$(2)-H-$(3)-pretty-exec
279280

@@ -452,6 +453,9 @@ CODEGEN_CC := $(call rwildcard,$(S)src/test/codegen/,*.cc)
452453
CODEGEN_UNITS_RS := $(call rwildcard,$(S)src/test/codegen-units/,*.rs)
453454
INCREMENTAL_RS := $(call rwildcard,$(S)src/test/incremental/,*.rs)
454455
RMAKE_RS := $(wildcard $(S)src/test/run-make/*/Makefile)
456+
UI_RS := $(call rwildcard,$(S)src/test/ui/,*.rs) \
457+
$(call rwildcard,$(S)src/test/ui/,*.stdout) \
458+
$(call rwildcard,$(S)src/test/ui/,*.stderr)
455459
RUSTDOCCK_RS := $(call rwildcard,$(S)src/test/rustdoc/,*.rs)
456460

457461
RPASS_TESTS := $(RPASS_RS)
@@ -469,6 +473,7 @@ CODEGEN_TESTS := $(CODEGEN_RS) $(CODEGEN_CC)
469473
CODEGEN_UNITS_TESTS := $(CODEGEN_UNITS_RS)
470474
INCREMENTAL_TESTS := $(INCREMENTAL_RS)
471475
RMAKE_TESTS := $(RMAKE_RS)
476+
UI_TESTS := $(UI_RS)
472477
RUSTDOCCK_TESTS := $(RUSTDOCCK_RS)
473478

474479
CTEST_SRC_BASE_rpass = run-pass
@@ -541,6 +546,11 @@ CTEST_BUILD_BASE_rmake = run-make
541546
CTEST_MODE_rmake = run-make
542547
CTEST_RUNTOOL_rmake = $(CTEST_RUNTOOL)
543548

549+
CTEST_SRC_BASE_ui = ui
550+
CTEST_BUILD_BASE_ui = ui
551+
CTEST_MODE_ui = ui
552+
CTEST_RUNTOOL_ui = $(CTEST_RUNTOOL)
553+
544554
CTEST_SRC_BASE_rustdocck = rustdoc
545555
CTEST_BUILD_BASE_rustdocck = rustdoc
546556
CTEST_MODE_rustdocck = rustdoc
@@ -672,7 +682,7 @@ CTEST_DEPS_codegen-units_$(1)-T-$(2)-H-$(3) = $$(CODEGEN_UNITS_TESTS)
672682
CTEST_DEPS_incremental_$(1)-T-$(2)-H-$(3) = $$(INCREMENTAL_TESTS)
673683
CTEST_DEPS_rmake_$(1)-T-$(2)-H-$(3) = $$(RMAKE_TESTS) \
674684
$$(CSREQ$(1)_T_$(3)_H_$(3)) $$(SREQ$(1)_T_$(2)_H_$(3))
675-
685+
CTEST_DEPS_ui_$(1)-T-$(2)-H-$(3) = $$(UI_TESTS)
676686
CTEST_DEPS_rustdocck_$(1)-T-$(2)-H-$(3) = $$(RUSTDOCCK_TESTS) \
677687
$$(HBIN$(1)_H_$(3))/rustdoc$$(X_$(3)) \
678688
$(S)src/etc/htmldocck.py
@@ -744,7 +754,7 @@ endef
744754

745755
CTEST_NAMES = rpass rpass-valgrind rpass-full rfail-full cfail-full rfail cfail pfail \
746756
debuginfo-gdb debuginfo-lldb codegen codegen-units rustdocck incremental \
747-
rmake
757+
rmake ui
748758

749759
$(foreach host,$(CFG_HOST), \
750760
$(eval $(foreach target,$(CFG_TARGET), \
@@ -943,6 +953,7 @@ TEST_GROUPS = \
943953
codegen \
944954
codegen-units \
945955
incremental \
956+
ui \
946957
doc \
947958
$(foreach docname,$(DOC_NAMES),doc-$(docname)) \
948959
pretty \

src/bootstrap/build/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,14 @@ impl Build {
342342
check::compiletest(self, &compiler, target.target,
343343
"codegen-units", "codegen-units");
344344
}
345+
CheckIncremental { compiler } => {
346+
check::compiletest(self, &compiler, target.target,
347+
"incremental", "incremental");
348+
}
349+
CheckUi { compiler } => {
350+
check::compiletest(self, &compiler, target.target,
351+
"ui", "ui");
352+
}
345353
CheckDebuginfo { compiler } => {
346354
if target.target.contains("msvc") ||
347355
target.target.contains("android") {

src/bootstrap/build/step.rs

+6
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ macro_rules! targets {
111111
(check_pfail, CheckPFail { compiler: Compiler<'a> }),
112112
(check_codegen, CheckCodegen { compiler: Compiler<'a> }),
113113
(check_codegen_units, CheckCodegenUnits { compiler: Compiler<'a> }),
114+
(check_incremental, CheckIncremental { compiler: Compiler<'a> }),
115+
(check_ui, CheckUi { compiler: Compiler<'a> }),
114116
(check_debuginfo, CheckDebuginfo { compiler: Compiler<'a> }),
115117
(check_rustdoc, CheckRustdoc { compiler: Compiler<'a> }),
116118
(check_pretty, CheckPretty { compiler: Compiler<'a> }),
@@ -379,6 +381,8 @@ impl<'a> Step<'a> {
379381
self.check_cfail(compiler),
380382
self.check_rfail(compiler),
381383
self.check_pfail(compiler),
384+
self.check_incremental(compiler),
385+
self.check_ui(compiler),
382386
self.check_crate_std(compiler),
383387
self.check_crate_test(compiler),
384388
self.check_crate_rustc(compiler),
@@ -412,6 +416,8 @@ impl<'a> Step<'a> {
412416
Source::CheckPFail { compiler } |
413417
Source::CheckCodegen { compiler } |
414418
Source::CheckCodegenUnits { compiler } |
419+
Source::CheckIncremental { compiler } |
420+
Source::CheckUi { compiler } |
415421
Source::CheckRustdoc { compiler } |
416422
Source::CheckPretty { compiler } |
417423
Source::CheckCFail { compiler } |

src/test/ui/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Guide to the UI Tests
2+
3+
The UI tests are intended to capture the compiler's complete output,
4+
so that we can test all aspects of the presentation. They work by
5+
compiling a file (e.g., `hello_world/main.rs`), capturing the output,
6+
and then applying some normalization (see below). This normalized
7+
result is then compared against reference files named
8+
`hello_world/main.stderr` and `hello_world/main.stdout`. If either of
9+
those files doesn't exist, the output must be empty. If the test run
10+
fails, we will print out the current output, but it is also saved in
11+
`build/<target-triple>/test/ui/hello_world/main.stdout` (this path is
12+
printed as part of the test failure mesage), so you can run `diff` and
13+
so forth.
14+
15+
# Editing and updating the reference files
16+
17+
If you have changed the compiler's output intentionally, or you are
18+
making a new test, you can use the script `update-references.sh` to
19+
update the references. When you run the test framework, it will report
20+
various errors: in those errors is a command you can use to run the
21+
`update-references.sh` script, which will then copy over the files
22+
from the build directory and use them as the new reference. You can
23+
also just run `update-all-references.sh`. In both cases, you can run
24+
the script with `--help` to get a help message.
25+
26+
# Normalization
27+
28+
The normalization applied is aimed at filenames:
29+
30+
- the test directory is replaced with `$DIR`
31+
- all backslashes (\) are converted to forward slashes (/) (for windows)

src/test/ui/hello_world/main.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that compiling hello world succeeds with no output of any kind.
12+
13+
fn main() {
14+
println!("Hello, world!");
15+
}

src/test/ui/mismatched_types/main.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// rustc-env:RUST_NEW_ERROR_FORMAT
12+
13+
fn main() {
14+
let x: u32 = (
15+
);
16+
}
17+
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: mismatched types [--explain E0308]
2+
--> $DIR/main.rs:14:18
3+
14 |> let x: u32 = (
4+
|> ^ expected u32, found ()
5+
note: expected type `u32`
6+
note: found type `()`
7+
8+
error: aborting due to previous error

src/test/ui/update-all-references.sh

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2015 The Rust Project Developers. See the COPYRIGHT
4+
# file at the top-level directory of this distribution and at
5+
# http://rust-lang.org/COPYRIGHT.
6+
#
7+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8+
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9+
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10+
# option. This file may not be copied, modified, or distributed
11+
# except according to those terms.
12+
13+
# A script to update the references for all tests. The idea is that
14+
# you do a run, which will generate files in the build directory
15+
# containing the (normalized) actual output of the compiler. You then
16+
# run this script, which will copy those files over. If you find
17+
# yourself manually editing a foo.stderr file, you're doing it wrong.
18+
#
19+
# See all `update-references.sh`, if you just want to update a single test.
20+
21+
if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "" || "$2" != "" ]]; then
22+
echo "usage: $0 <build-directory>"
23+
echo ""
24+
echo "For example:"
25+
echo " $0 ../../../build/x86_64-apple-darwin/test/ui"
26+
fi
27+
28+
BUILD_DIR=$PWD/$1
29+
MY_DIR=$(dirname $0)
30+
cd $MY_DIR
31+
find . -name '*.rs' | xargs ./update-references.sh $BUILD_DIR

src/test/ui/update-references.sh

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2015 The Rust Project Developers. See the COPYRIGHT
4+
# file at the top-level directory of this distribution and at
5+
# http://rust-lang.org/COPYRIGHT.
6+
#
7+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8+
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9+
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10+
# option. This file may not be copied, modified, or distributed
11+
# except according to those terms.
12+
13+
# A script to update the references for particular tests. The idea is
14+
# that you do a run, which will generate files in the build directory
15+
# containing the (normalized) actual output of the compiler. This
16+
# script will then copy that output and replace the "expected output"
17+
# files. You can then commit the changes.
18+
#
19+
# If you find yourself manually editing a foo.stderr file, you're
20+
# doing it wrong.
21+
22+
if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "" || "$2" == "" ]]; then
23+
echo "usage: $0 <build-directory> <relative-path-to-rs-files>"
24+
echo ""
25+
echo "For example:"
26+
echo " $0 ../../../build/x86_64-apple-darwin/test/ui *.rs */*.rs"
27+
fi
28+
29+
MYDIR=$(dirname $0)
30+
31+
BUILD_DIR="$1"
32+
shift
33+
34+
while [[ "$1" != "" ]]; do
35+
STDERR_NAME="${1/%.rs/.stderr}"
36+
STDOUT_NAME="${1/%.rs/.stdout}"
37+
shift
38+
if [ -f $BUILD_DIR/$STDOUT_NAME ] && \
39+
! (diff $BUILD_DIR/$STDOUT_NAME $MYDIR/$STDOUT_NAME > /dev/null); then
40+
echo updating $MYDIR/$STDOUT_NAME
41+
cp $BUILD_DIR/$STDOUT_NAME $MYDIR/$STDOUT_NAME
42+
fi
43+
if [ -f $BUILD_DIR/$STDERR_NAME ] && \
44+
! (diff $BUILD_DIR/$STDERR_NAME $MYDIR/$STDERR_NAME > /dev/null); then
45+
echo updating $MYDIR/$STDERR_NAME
46+
cp $BUILD_DIR/$STDERR_NAME $MYDIR/$STDERR_NAME
47+
fi
48+
done
49+
50+

src/tools/compiletest/src/common.rs

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub enum Mode {
2828
CodegenUnits,
2929
Incremental,
3030
RunMake,
31+
Ui,
3132
}
3233

3334
impl FromStr for Mode {
@@ -47,6 +48,7 @@ impl FromStr for Mode {
4748
"codegen-units" => Ok(CodegenUnits),
4849
"incremental" => Ok(Incremental),
4950
"run-make" => Ok(RunMake),
51+
"ui" => Ok(Ui),
5052
_ => Err(()),
5153
}
5254
}
@@ -68,6 +70,7 @@ impl fmt::Display for Mode {
6870
CodegenUnits => "codegen-units",
6971
Incremental => "incremental",
7072
RunMake => "run-make",
73+
Ui => "ui",
7174
}, f)
7275
}
7376
}

src/tools/compiletest/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub mod runtest;
5050
pub mod common;
5151
pub mod errors;
5252
mod raise_fd_limit;
53+
mod uidiff;
5354

5455
fn main() {
5556
#[cfg(cargobuild)]

0 commit comments

Comments
 (0)