Skip to content

Commit e142d0c

Browse files
jieyouxuOneirical
andcommitted
tests: port translation to rmake.rs
Co-authored-by: Oneirical <[email protected]>
1 parent f7db496 commit e142d0c

File tree

3 files changed

+175
-79
lines changed

3 files changed

+175
-79
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
run-make/jobserver-error/Makefile
22
run-make/split-debuginfo/Makefile
33
run-make/symbol-mangling-hashed/Makefile
4-
run-make/translation/Makefile

tests/run-make/translation/Makefile

-78
This file was deleted.

tests/run-make/translation/rmake.rs

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

0 commit comments

Comments
 (0)