mirror of
https://github.com/SeCherkasov/util-linux-cal.git
synced 2026-03-30 07:51:46 +03:00
Bug fixes: - Fix Zeller's formula for Julian calendar (separate branch without century correction) and use rem_euclid for negative modulo safety - Pass country code (cc=) to isdayoff.ru API in holiday plugin - Sync clap --version with Cargo.toml (was hardcoded "1.0.0") Tests: - Rename integration_tests.rs to unit_tests.rs (they are unit tests) - Fix race condition on env vars in plugin tests (Mutex + remove_var) - Fix incorrect assertions (color tty detection, locale fallback) - Add missing test cases: Julian/Gregorian Zeller, display date parsing, Sunday offset, week numbers, edge cases (87 tests total) - Remove brittle version-checking tests
81 lines
2.1 KiB
Rust
81 lines
2.1 KiB
Rust
//! Unit tests for holiday_highlighter plugin.
|
|
|
|
use std::sync::Mutex;
|
|
|
|
use holiday_highlighter::get_country_from_locale;
|
|
|
|
/// Mutex to serialize tests that modify environment variables.
|
|
/// `set_var` is not thread-safe, so locale tests must not run in parallel.
|
|
/// We use `lock().unwrap_or_else(|e| e.into_inner())` to recover from poison.
|
|
static ENV_LOCK: Mutex<()> = Mutex::new(());
|
|
|
|
fn lock_env() -> std::sync::MutexGuard<'static, ()> {
|
|
ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner())
|
|
}
|
|
|
|
/// Reset all locale env vars to a clean state, then set `LC_ALL` to the given value.
|
|
fn set_locale(lc_all: &str) {
|
|
unsafe {
|
|
std::env::set_var("LC_ALL", lc_all);
|
|
std::env::remove_var("LC_TIME");
|
|
std::env::remove_var("LANG");
|
|
}
|
|
}
|
|
|
|
/// Remove all locale env vars.
|
|
fn clear_locale() {
|
|
unsafe {
|
|
std::env::remove_var("LC_ALL");
|
|
std::env::remove_var("LC_TIME");
|
|
std::env::remove_var("LANG");
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Country detection from locale
|
|
// ---------------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn country_from_locale_ru() {
|
|
let _guard = lock_env();
|
|
set_locale("ru_RU.UTF-8");
|
|
assert_eq!(get_country_from_locale(), "RU");
|
|
}
|
|
|
|
#[test]
|
|
fn country_from_locale_us() {
|
|
let _guard = lock_env();
|
|
set_locale("en_US.UTF-8");
|
|
assert_eq!(get_country_from_locale(), "US");
|
|
}
|
|
|
|
#[test]
|
|
fn country_from_locale_by() {
|
|
let _guard = lock_env();
|
|
set_locale("be_BY.UTF-8");
|
|
assert_eq!(get_country_from_locale(), "BY");
|
|
}
|
|
|
|
#[test]
|
|
fn country_from_locale_kz() {
|
|
let _guard = lock_env();
|
|
set_locale("kk_KZ.UTF-8");
|
|
assert_eq!(get_country_from_locale(), "KZ");
|
|
}
|
|
|
|
#[test]
|
|
fn country_from_locale_fallback_to_us() {
|
|
let _guard = lock_env();
|
|
clear_locale();
|
|
// When no locale vars are set, the function defaults to "en_US.UTF-8" -> "US"
|
|
assert_eq!(get_country_from_locale(), "US");
|
|
}
|
|
|
|
#[test]
|
|
fn country_from_locale_lc_time_fallback() {
|
|
let _guard = lock_env();
|
|
clear_locale();
|
|
unsafe { std::env::set_var("LC_TIME", "tr_TR.UTF-8") };
|
|
assert_eq!(get_country_from_locale(), "TR");
|
|
}
|