Skip to content

Commit 388a52f

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

File tree

3 files changed

+174
-79
lines changed

3 files changed

+174
-79
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
run-make/split-debuginfo/Makefile
22
run-make/symbol-mangling-hashed/Makefile
3-
run-make/translation/Makefile

tests/run-make/translation/Makefile

-78
This file was deleted.

tests/run-make/translation/rmake.rs

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

0 commit comments

Comments
 (0)