Skip to content

Commit 398b3d9

Browse files
oech3cakebaker
authored andcommitted
uucore/build.rs: remove unsafe
1 parent 6f102bf commit 398b3d9

1 file changed

Lines changed: 14 additions & 77 deletions

File tree

src/uucore/build.rs

Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
3131
// Try to detect if we're building for a specific utility by checking build configuration
3232
// This attempts to identify individual utility builds vs multicall binary builds
3333
let target_utility = detect_target_utility();
34-
let locales_to_embed = get_locales_to_embed();
34+
let locales_to_embed = get_locales_to_embed(env::var("LANG").ok());
3535

3636
match target_utility {
3737
Some(util_name) => {
@@ -273,8 +273,8 @@ fn embed_static_utility_locales(
273273
///
274274
/// It always includes "en-US" to ensure that a fallback is available if the
275275
/// system locale's translation file is missing or if `LANG` is not set.
276-
fn get_locales_to_embed() -> (String, Option<String>) {
277-
let system_locale = env::var("LANG").ok().and_then(|lang| {
276+
fn get_locales_to_embed(env: Option<String>) -> (String, Option<String>) {
277+
let system_locale = env.and_then(|lang| {
278278
let locale = lang.split('.').next()?.replace('_', "-");
279279
if locale != "en-US" && !locale.is_empty() {
280280
Some(locale)
@@ -417,123 +417,60 @@ mod tests {
417417

418418
#[test]
419419
fn get_locales_to_embed_no_lang() {
420-
unsafe {
421-
env::remove_var("LANG");
422-
}
423-
let (en_locale, system_locale) = get_locales_to_embed();
420+
let (en_locale, system_locale) = get_locales_to_embed(None);
424421
assert_eq!(en_locale, "en-US");
425422
assert_eq!(system_locale, None);
426423

427-
unsafe {
428-
env::set_var("LANG", "");
429-
}
430-
let (en_locale, system_locale) = get_locales_to_embed();
424+
let (en_locale, system_locale) = get_locales_to_embed(Some(String::new().into()));
431425
assert_eq!(en_locale, "en-US");
432426
assert_eq!(system_locale, None);
433-
unsafe {
434-
env::remove_var("LANG");
435-
}
436427

437-
unsafe {
438-
env::set_var("LANG", "en_US.UTF-8");
439-
}
440-
let (en_locale, system_locale) = get_locales_to_embed();
428+
let (en_locale, system_locale) = get_locales_to_embed(Some("en_US.UTF-8".to_string()));
441429
assert_eq!(en_locale, "en-US");
442430
assert_eq!(system_locale, None);
443-
unsafe {
444-
env::remove_var("LANG");
445-
}
446431
}
447432

448433
#[test]
449434
fn get_locales_to_embed_with_lang() {
450-
unsafe {
451-
env::set_var("LANG", "fr_FR.UTF-8");
452-
}
453-
let (en_locale, system_locale) = get_locales_to_embed();
435+
let (en_locale, system_locale) = get_locales_to_embed(Some("fr_FR.UTF-8".to_string()));
454436
assert_eq!(en_locale, "en-US");
455437
assert_eq!(system_locale, Some("fr-FR".to_string()));
456-
unsafe {
457-
env::remove_var("LANG");
458-
}
459438

460-
unsafe {
461-
env::set_var("LANG", "zh_CN.UTF-8");
462-
}
463-
let (en_locale, system_locale) = get_locales_to_embed();
439+
let (en_locale, system_locale) = get_locales_to_embed(Some("zh_CN.UTF-8".to_string()));
464440
assert_eq!(en_locale, "en-US");
465441
assert_eq!(system_locale, Some("zh-CN".to_string()));
466-
unsafe {
467-
env::remove_var("LANG");
468-
}
469442

470-
unsafe {
471-
env::set_var("LANG", "de");
472-
}
473-
let (en_locale, system_locale) = get_locales_to_embed();
443+
let (en_locale, system_locale) = get_locales_to_embed(Some("de".to_string()));
474444
assert_eq!(en_locale, "en-US");
475445
assert_eq!(system_locale, Some("de".to_string()));
476-
unsafe {
477-
env::remove_var("LANG");
478-
}
479446
}
480447

481448
#[test]
482449
fn get_locales_to_embed_invalid_lang() {
483450
// invalid locale format
484-
unsafe {
485-
env::set_var("LANG", "invalid");
486-
}
487-
let (en_locale, system_locale) = get_locales_to_embed();
451+
let (en_locale, system_locale) = get_locales_to_embed(Some("invalid".to_string()));
488452
assert_eq!(en_locale, "en-US");
489453
assert_eq!(system_locale, Some("invalid".to_string()));
490-
unsafe {
491-
env::remove_var("LANG");
492-
}
493454

494455
// numeric values
495-
unsafe {
496-
env::set_var("LANG", "123");
497-
}
498-
let (en_locale, system_locale) = get_locales_to_embed();
456+
let (en_locale, system_locale) = get_locales_to_embed(Some("123".to_string()));
499457
assert_eq!(en_locale, "en-US");
500458
assert_eq!(system_locale, Some("123".to_string()));
501-
unsafe {
502-
env::remove_var("LANG");
503-
}
504459

505460
// special characters
506-
unsafe {
507-
env::set_var("LANG", "@@@@");
508-
}
509-
let (en_locale, system_locale) = get_locales_to_embed();
461+
let (en_locale, system_locale) = get_locales_to_embed(Some("@@@@".to_string()));
510462
assert_eq!(en_locale, "en-US");
511463
assert_eq!(system_locale, Some("@@@@".to_string()));
512-
unsafe {
513-
env::remove_var("LANG");
514-
}
515464

516465
// malformed locale (no country code but with encoding)
517-
unsafe {
518-
env::set_var("LANG", "en.UTF-8");
519-
}
520-
let (en_locale, system_locale) = get_locales_to_embed();
466+
let (en_locale, system_locale) = get_locales_to_embed(Some("en.UTF-8".to_string()));
521467
assert_eq!(en_locale, "en-US");
522468
assert_eq!(system_locale, Some("en".to_string()));
523-
unsafe {
524-
env::remove_var("LANG");
525-
}
526469

527470
// valid format but unusual locale
528-
unsafe {
529-
env::set_var("LANG", "XX_YY.UTF-8");
530-
}
531-
let (en_locale, system_locale) = get_locales_to_embed();
471+
let (en_locale, system_locale) = get_locales_to_embed(Some("XX_YY.UTF-8".to_string()));
532472
assert_eq!(en_locale, "en-US");
533473
assert_eq!(system_locale, Some("XX-YY".to_string()));
534-
unsafe {
535-
env::remove_var("LANG");
536-
}
537474
}
538475

539476
#[test]

0 commit comments

Comments
 (0)