Skip to content

Commit b30cd30

Browse files
committed
wip: add debug info for mac
1 parent 1883daf commit b30cd30

File tree

1 file changed

+81
-26
lines changed

1 file changed

+81
-26
lines changed

src/uu/date/src/locale.rs

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ cfg_langinfo! {
4444
DEFAULT_FORMAT_CACHE.get_or_init(|| {
4545
// Try to get locale format string
4646
if let Some(format) = get_locale_format_string() {
47+
#[cfg(test)]
48+
eprintln!("DEBUG: get_locale_default_format: Using system format: '{}'", format);
4749
let format_with_tz = ensure_timezone_in_format(&format);
50+
#[cfg(test)]
51+
eprintln!("DEBUG: get_locale_default_format: After timezone adjustment: '{}'", format_with_tz);
4852
return Box::leak(format_with_tz.into_boxed_str());
4953
}
5054

55+
#[cfg(test)]
56+
eprintln!("DEBUG: get_locale_default_format: No system format, using fallback");
5157
// Fallback: use 24-hour format as safe default
5258
"%a %b %e %X %Z %Y"
5359
})
@@ -57,16 +63,32 @@ cfg_langinfo! {
5763
fn get_locale_format_string() -> Option<String> {
5864
unsafe {
5965
// Set locale from environment variables
60-
libc::setlocale(libc::LC_TIME, c"".as_ptr());
66+
let _locale_result = libc::setlocale(libc::LC_TIME, c"".as_ptr());
67+
#[cfg(test)]
68+
{
69+
let current_locale = if _locale_result.is_null() {
70+
"NULL".to_string()
71+
} else {
72+
CStr::from_ptr(_locale_result).to_string_lossy().into_owned()
73+
};
74+
eprintln!("DEBUG: get_locale_format_string: setlocale result: '{}'", current_locale);
75+
}
6176

6277
// Get the date/time format string
6378
let d_t_fmt_ptr = libc::nl_langinfo(libc::D_T_FMT);
6479
if d_t_fmt_ptr.is_null() {
80+
#[cfg(test)]
81+
eprintln!("DEBUG: get_locale_format_string: nl_langinfo returned null pointer");
6582
return None;
6683
}
6784

6885
let format = CStr::from_ptr(d_t_fmt_ptr).to_str().ok()?;
86+
#[cfg(test)]
87+
eprintln!("DEBUG: get_locale_format_string: raw format from nl_langinfo: '{}'", format);
88+
6989
if format.is_empty() {
90+
#[cfg(test)]
91+
eprintln!("DEBUG: get_locale_format_string: format string is empty");
7092
return None;
7193
}
7294

@@ -121,39 +143,72 @@ mod tests {
121143
#[test]
122144
fn test_default_format_contains_valid_codes() {
123145
let format = get_locale_default_format();
124-
assert!(format.contains("%a")); // abbreviated weekday
125-
assert!(format.contains("%b")); // abbreviated month
126-
assert!(format.contains("%Y") || format.contains("%y")); // year (4-digit or 2-digit)
127-
assert!(format.contains("%Z")); // timezone
146+
147+
// Print the actual format for debugging on macOS and other platforms
148+
eprintln!("DEBUG: Detected locale format: '{}'", format);
149+
eprintln!("DEBUG: Platform: {}", std::env::consts::OS);
150+
eprintln!("DEBUG: Arch: {}", std::env::consts::ARCH);
151+
152+
// Check for environment variables that might affect locale
153+
for var in ["LC_ALL", "LC_TIME", "LANG"] {
154+
if let Ok(val) = std::env::var(var) {
155+
eprintln!("DEBUG: {}={}", var, val);
156+
} else {
157+
eprintln!("DEBUG: {} is not set", var);
158+
}
159+
}
160+
161+
assert!(format.contains("%a"), "Format '{}' should contain abbreviated weekday (%a)", format);
162+
assert!(format.contains("%b"), "Format '{}' should contain abbreviated month (%b)", format);
163+
assert!(format.contains("%Y") || format.contains("%y"), "Format '{}' should contain year (%Y or %y)", format);
164+
assert!(format.contains("%Z"), "Format '{}' should contain timezone (%Z)", format);
128165
}
129166

130167
#[test]
131168
fn test_locale_format_structure() {
132169
// Verify we're using actual locale format strings, not hardcoded ones
133170
let format = get_locale_default_format();
134171

172+
// Print detailed debugging information
173+
eprintln!("DEBUG: Testing locale format structure");
174+
eprintln!("DEBUG: Format string: '{}'", format);
175+
eprintln!("DEBUG: Format length: {} characters", format.len());
176+
135177
// The format should not be empty
136-
assert!(!format.is_empty(), "Locale format should not be empty");
137-
138-
// Should contain date/time components
139-
let has_date_component = format.contains("%a")
140-
|| format.contains("%A")
141-
|| format.contains("%b")
142-
|| format.contains("%B")
143-
|| format.contains("%d")
144-
|| format.contains("%e");
145-
assert!(has_date_component, "Format should contain date components");
146-
147-
// Should contain time component (hour)
148-
let has_time_component = format.contains("%H")
149-
|| format.contains("%I")
150-
|| format.contains("%k")
151-
|| format.contains("%l")
152-
|| format.contains("%r")
153-
|| format.contains("%R")
154-
|| format.contains("%T")
155-
|| format.contains("%X");
156-
assert!(has_time_component, "Format should contain time components");
178+
assert!(!format.is_empty(), "Locale format should not be empty, got: '{}'", format);
179+
180+
// Check for date components with detailed output
181+
let date_components = ["%a", "%A", "%b", "%B", "%d", "%e"];
182+
let found_date_components: Vec<_> = date_components.iter()
183+
.filter(|&comp| format.contains(comp))
184+
.collect();
185+
eprintln!("DEBUG: Found date components: {:?}", found_date_components);
186+
187+
let has_date_component = !found_date_components.is_empty();
188+
assert!(has_date_component,
189+
"Format '{}' should contain date components. Checked: {:?}, Found: {:?}",
190+
format, date_components, found_date_components);
191+
192+
// Check for time components with detailed output
193+
let time_components = ["%H", "%I", "%k", "%l", "%r", "%R", "%T", "%X"];
194+
let found_time_components: Vec<_> = time_components.iter()
195+
.filter(|&comp| format.contains(comp))
196+
.collect();
197+
eprintln!("DEBUG: Found time components: {:?}", found_time_components);
198+
199+
let has_time_component = !found_time_components.is_empty();
200+
assert!(has_time_component,
201+
"Format '{}' should contain time components. Checked: {:?}, Found: {:?}",
202+
format, time_components, found_time_components);
203+
204+
// Additional debug: show raw locale format from system
205+
eprintln!("DEBUG: Checking raw system locale format...");
206+
if let Some(raw_format) = get_locale_format_string() {
207+
eprintln!("DEBUG: Raw system format: '{}'", raw_format);
208+
eprintln!("DEBUG: Raw format has timezone: {}", raw_format.contains("%Z"));
209+
} else {
210+
eprintln!("DEBUG: No raw system format available (using fallback)");
211+
}
157212
}
158213

159214
#[test]

0 commit comments

Comments
 (0)