Skip to content

Commit 3385166

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

File tree

3 files changed

+173
-79
lines changed

3 files changed

+173
-79
lines changed
Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 78 deletions
This file was deleted.

tests/run-make/translation/rmake.rs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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. On `aarch64-apple-darwin`, somehow it *doesn't* ICE.
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+
}
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+
rfs::remove_file(dst_dir.join(dir_filename));
93+
rfs::create_dir_all(dst_dir.join(dir_filename));
94+
}
95+
96+
#[track_caller]
97+
fn setup_fakeroot_parents() -> PathBuf {
98+
let sysroot = sysroot();
99+
let fakeroot = cwd().join("fakeroot");
100+
rfs::create_dir_all(&fakeroot);
101+
shallow_symlink_dir_entries_materialize_single_dir(&sysroot, &fakeroot, "lib");
102+
shallow_symlink_dir_entries_materialize_single_dir(
103+
&sysroot.join("lib"),
104+
&fakeroot.join("lib"),
105+
"rustlib",
106+
);
107+
shallow_symlink_dir_entries_materialize_single_dir(
108+
&sysroot.join("lib").join("rustlib"),
109+
&fakeroot.join("lib").join("rustlib"),
110+
"src",
111+
);
112+
shallow_symlink_dir_entries(
113+
&sysroot.join("lib").join("rustlib").join("src"),
114+
&fakeroot.join("lib").join("rustlib").join("src"),
115+
);
116+
fakeroot
117+
}
118+
119+
/// Check that a locale can be loaded from the sysroot given a language identifier by making a local
120+
/// copy of the sysroot and adding the custom locale to it.
121+
fn locale_sysroot() {
122+
run_in_tmpdir(|| {
123+
let fakeroot = setup_fakeroot_parents();
124+
125+
// When download-rustc is enabled, real sysroot will have a share directory. Delete the link
126+
// to it.
127+
let _ = std::fs::remove_file(fakeroot.join("share"));
128+
129+
let fake_locale_path = fakeroot.join("share").join("locale").join("zh-CN");
130+
rfs::create_dir_all(&fake_locale_path);
131+
rfs::symlink_file(
132+
cwd().join("working.ftl"),
133+
fake_locale_path.join("basic-translation.ftl"),
134+
);
135+
136+
rustc()
137+
.env("RUSTC_ICE", "0")
138+
.input("test.rs")
139+
.sysroot(&fakeroot)
140+
.arg("-Ztranslate-lang=zh-CN")
141+
.run_fail()
142+
.assert_stderr_contains("this is a test message");
143+
});
144+
}
145+
146+
/// Check that the compiler errors out when the sysroot requested cannot be found. This test might
147+
/// start failing if there actually exists a Klingon translation of rustc's error messages.
148+
fn missing_sysroot() {
149+
run_in_tmpdir(|| {
150+
rustc()
151+
.input("test.rs")
152+
.arg("-Ztranslate-lang=tlh")
153+
.run_fail()
154+
.assert_stderr_contains("missing locale directory");
155+
});
156+
}
157+
158+
/// Check that the compiler errors out when the directory for the locale in the sysroot is actually
159+
/// a file.
160+
fn file_sysroot() {
161+
run_in_tmpdir(|| {
162+
let fakeroot = setup_fakeroot_parents();
163+
rfs::create_dir_all(fakeroot.join("share").join("locale"));
164+
rfs::write(fakeroot.join("share").join("locale").join("zh-CN"), b"not a dir");
165+
166+
rustc()
167+
.input("test.rs")
168+
.sysroot(&fakeroot)
169+
.arg("-Ztranslate-lang=zh-CN")
170+
.run_fail()
171+
.assert_stderr_contains("is not a directory");
172+
});
173+
}

0 commit comments

Comments
 (0)