Skip to content

Commit 139c5ae

Browse files
jieyouxuOneirical
andcommitted
tests: port translation to rmake.rs
Co-authored-by: Oneirical <manchot@videotron.ca>
1 parent 55fb857 commit 139c5ae

File tree

3 files changed

+179
-79
lines changed

3 files changed

+179
-79
lines changed

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ run-make/cat-and-grep-sanity-check/Makefile
22
run-make/jobserver-error/Makefile
33
run-make/split-debuginfo/Makefile
44
run-make/symbol-mangling-hashed/Makefile
5-
run-make/translation/Makefile

tests/run-make/translation/Makefile

-78
This file was deleted.

tests/run-make/translation/rmake.rs

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
//! Smoke test for the rustc diagnostics translation infrastructure.
2+
//!
3+
//! # References
4+
//!
5+
//! - Current tracking issue: <https://github.com/rust-lang/rust/issues/132181>.
6+
//! - Old tracking issue: <https://github.com/rust-lang/rust/issues/100717>
7+
//! - Initial translation infra implementation: <https://github.com/rust-lang/rust/pull/95512>.
8+
9+
// This test uses symbolic links to stub out a fake sysroot to save testing time.
10+
//@ needs-symlink
11+
12+
#![deny(warnings)]
13+
14+
use std::path::{Path, PathBuf};
15+
16+
use run_make_support::rustc::sysroot;
17+
use run_make_support::{cwd, rfs, run_in_tmpdir, rustc};
18+
19+
fn main() {
20+
builtin_fallback_bundle();
21+
additional_primary_bundle();
22+
missing_slug_prefers_fallback_bundle();
23+
broken_primary_bundle_prefers_fallback_bundle();
24+
locale_sysroot();
25+
missing_sysroot();
26+
file_sysroot();
27+
}
28+
29+
/// Check that the test works normally, using the built-in fallback bundle.
30+
fn builtin_fallback_bundle() {
31+
rustc().input("test.rs").run_fail().assert_stderr_contains("struct literal body without path");
32+
}
33+
34+
/// Check that a primary bundle can be loaded and will be preferentially used where possible.
35+
fn additional_primary_bundle() {
36+
rustc()
37+
.input("test.rs")
38+
.arg("-Ztranslate-additional-ftl=working.ftl")
39+
.run_fail()
40+
.assert_stderr_contains("this is a test message");
41+
}
42+
43+
/// Check that a primary bundle without the desired message will use the fallback bundle.
44+
fn missing_slug_prefers_fallback_bundle() {
45+
rustc()
46+
.input("test.rs")
47+
.arg("-Ztranslate-additional-ftl=missing.ftl")
48+
.run_fail()
49+
.assert_stderr_contains("struct literal body without path");
50+
}
51+
52+
/// Check that a primary bundle with a broken message (e.g. an interpolated variable is not
53+
/// provided) will use the fallback bundle.
54+
fn broken_primary_bundle_prefers_fallback_bundle() {
55+
// FIXME(#135817): as of the rmake.rs port, the compiler actually ICEs on the additional
56+
// `broken.ftl`, even though the original intention seems to be that it should gracefully
57+
// failover to the fallback bundle.
58+
59+
rustc()
60+
.env("RUSTC_ICE", "0") // disable ICE dump file, not needed
61+
.input("test.rs")
62+
.arg("-Ztranslate-additional-ftl=broken.ftl")
63+
.run_fail()
64+
.assert_exit_code(101);
65+
}
66+
67+
#[track_caller]
68+
fn shallow_symlink_dir_entries(src_dir: &Path, dst_dir: &Path) {
69+
for entry in rfs::read_dir(src_dir) {
70+
let entry = entry.unwrap();
71+
let src_entry_path = entry.path();
72+
let src_filename = src_entry_path.file_name().unwrap();
73+
let meta = rfs::symlink_metadata(&src_entry_path);
74+
75+
if meta.is_symlink() || meta.is_file() {
76+
rfs::symlink_file(&src_entry_path, dst_dir.join(src_filename));
77+
} else if meta.is_dir() {
78+
rfs::symlink_dir(&src_entry_path, dst_dir.join(src_filename));
79+
} else {
80+
unreachable!()
81+
}
82+
}
83+
}
84+
85+
#[track_caller]
86+
fn shallow_symlink_dir_entries_materialize_single_dir(
87+
src_dir: &Path,
88+
dst_dir: &Path,
89+
dir_filename: &str,
90+
) {
91+
shallow_symlink_dir_entries(src_dir, dst_dir);
92+
93+
// On Windows, this would be a symlink-to-dir, so we have to remove with `remove_dir` instead.
94+
#[cfg(windows)]
95+
rfs::remove_dir(dst_dir.join(dir_filename));
96+
#[cfg(not(windows))]
97+
rfs::remove_file(dst_dir.join(dir_filename));
98+
99+
rfs::create_dir_all(dst_dir.join(dir_filename));
100+
}
101+
102+
#[track_caller]
103+
fn setup_fakeroot_parents() -> PathBuf {
104+
let sysroot = sysroot();
105+
let fakeroot = cwd().join("fakeroot");
106+
rfs::create_dir_all(&fakeroot);
107+
shallow_symlink_dir_entries_materialize_single_dir(&sysroot, &fakeroot, "lib");
108+
shallow_symlink_dir_entries_materialize_single_dir(
109+
&sysroot.join("lib"),
110+
&fakeroot.join("lib"),
111+
"rustlib",
112+
);
113+
shallow_symlink_dir_entries_materialize_single_dir(
114+
&sysroot.join("lib").join("rustlib"),
115+
&fakeroot.join("lib").join("rustlib"),
116+
"src",
117+
);
118+
shallow_symlink_dir_entries(
119+
&sysroot.join("lib").join("rustlib").join("src"),
120+
&fakeroot.join("lib").join("rustlib").join("src"),
121+
);
122+
fakeroot
123+
}
124+
125+
/// Check that a locale can be loaded from the sysroot given a language identifier by making a local
126+
/// copy of the sysroot and adding the custom locale to it.
127+
fn locale_sysroot() {
128+
run_in_tmpdir(|| {
129+
let fakeroot = setup_fakeroot_parents();
130+
131+
// When download-rustc is enabled, real sysroot will have a share directory. Delete the link
132+
// to it.
133+
let _ = std::fs::remove_file(fakeroot.join("share"));
134+
135+
let fake_locale_path = fakeroot.join("share").join("locale").join("zh-CN");
136+
rfs::create_dir_all(&fake_locale_path);
137+
rfs::symlink_file(
138+
cwd().join("working.ftl"),
139+
fake_locale_path.join("basic-translation.ftl"),
140+
);
141+
142+
rustc()
143+
.env("RUSTC_ICE", "0")
144+
.input("test.rs")
145+
.sysroot(&fakeroot)
146+
.arg("-Ztranslate-lang=zh-CN")
147+
.run_fail()
148+
.assert_stderr_contains("this is a test message");
149+
});
150+
}
151+
152+
/// Check that the compiler errors out when the sysroot requested cannot be found. This test might
153+
/// start failing if there actually exists a Klingon translation of rustc's error messages.
154+
fn missing_sysroot() {
155+
run_in_tmpdir(|| {
156+
rustc()
157+
.input("test.rs")
158+
.arg("-Ztranslate-lang=tlh")
159+
.run_fail()
160+
.assert_stderr_contains("missing locale directory");
161+
});
162+
}
163+
164+
/// Check that the compiler errors out when the directory for the locale in the sysroot is actually
165+
/// a file.
166+
fn file_sysroot() {
167+
run_in_tmpdir(|| {
168+
let fakeroot = setup_fakeroot_parents();
169+
rfs::create_dir_all(fakeroot.join("share").join("locale"));
170+
rfs::write(fakeroot.join("share").join("locale").join("zh-CN"), b"not a dir");
171+
172+
rustc()
173+
.input("test.rs")
174+
.sysroot(&fakeroot)
175+
.arg("-Ztranslate-lang=zh-CN")
176+
.run_fail()
177+
.assert_stderr_contains("is not a directory");
178+
});
179+
}

0 commit comments

Comments
 (0)