Skip to content

Commit 8fa3bef

Browse files
echobtfactorydroid
andauthored
fix(cortex-cli): update tests for Rust 2024 edition safety requirements (#334)
- Wrap std::env::set_var and std::env::remove_var calls in unsafe blocks (required by Rust 2024 edition) - Add proper imports to doc tests for print_success, print_error, print_warning, print_info, print_dim, and styled_label functions This fixes test compilation failures due to Rust 2024's new safety requirements for environment variable manipulation functions. Co-authored-by: Droid Agent <droid@factory.ai>
1 parent 7ce93dc commit 8fa3bef

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

cortex-cli/src/styled_output.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ fn print_styled_internal(msg_type: MessageType, message: &str, to_stderr: bool,
201201
///
202202
/// # Example
203203
/// ```
204+
/// use cortex_cli::styled_output::print_success;
204205
/// print_success("Operation completed successfully");
205206
/// // Output: ✓ Operation completed successfully (in green)
206207
/// ```
@@ -215,6 +216,7 @@ pub fn print_success(message: &str) {
215216
///
216217
/// # Example
217218
/// ```
219+
/// use cortex_cli::styled_output::print_error;
218220
/// print_error("Failed to connect to server");
219221
/// // Output: ✗ Failed to connect to server (in red)
220222
/// ```
@@ -229,6 +231,7 @@ pub fn print_error(message: &str) {
229231
///
230232
/// # Example
231233
/// ```
234+
/// use cortex_cli::styled_output::print_warning;
232235
/// print_warning("Configuration file not found, using defaults");
233236
/// // Output: [WARN] Configuration file not found, using defaults (in amber)
234237
/// ```
@@ -243,6 +246,7 @@ pub fn print_warning(message: &str) {
243246
///
244247
/// # Example
245248
/// ```
249+
/// use cortex_cli::styled_output::print_info;
246250
/// print_info("Processing 42 files...");
247251
/// // Output: [INFO] Processing 42 files... (in blue)
248252
/// ```
@@ -257,6 +261,7 @@ pub fn print_info(message: &str) {
257261
///
258262
/// # Example
259263
/// ```
264+
/// use cortex_cli::styled_output::print_dim;
260265
/// print_dim("Note: Using default configuration");
261266
/// // Output: · Note: Using default configuration (in gray)
262267
/// ```
@@ -344,6 +349,7 @@ fn format_styled(msg_type: MessageType, message: &str) -> String {
344349
///
345350
/// # Example
346351
/// ```
352+
/// use cortex_cli::styled_output::{styled_label, MessageType};
347353
/// println!("Status: {}", styled_label(MessageType::Success, "PASSED"));
348354
/// // Output: Status: ✓ PASSED (with PASSED in green)
349355
/// ```
@@ -389,33 +395,35 @@ mod tests {
389395
#[test]
390396
fn test_format_styled_no_color() {
391397
// When colors are disabled, should just return icon + message
392-
std::env::set_var("NO_COLOR", "1");
398+
// SAFETY: These tests run serially and we restore env vars immediately
399+
unsafe { std::env::set_var("NO_COLOR", "1") };
393400
let result = format_styled(MessageType::Success, "test message");
394401
assert!(result.contains("[OK]"));
395402
assert!(result.contains("test message"));
396-
std::env::remove_var("NO_COLOR");
403+
unsafe { std::env::remove_var("NO_COLOR") };
397404
}
398405

399406
#[test]
400407
fn test_colors_disabled() {
401-
std::env::set_var("NO_COLOR", "1");
408+
// SAFETY: These tests run serially and we restore env vars immediately
409+
unsafe { std::env::set_var("NO_COLOR", "1") };
402410
assert!(colors_disabled());
403-
std::env::remove_var("NO_COLOR");
411+
unsafe { std::env::remove_var("NO_COLOR") };
404412

405-
std::env::set_var("NO_COLOR", "true");
413+
unsafe { std::env::set_var("NO_COLOR", "true") };
406414
assert!(colors_disabled());
407-
std::env::remove_var("NO_COLOR");
415+
unsafe { std::env::remove_var("NO_COLOR") };
408416

409-
std::env::set_var("NO_COLOR", "0");
417+
unsafe { std::env::set_var("NO_COLOR", "0") };
410418
assert!(!colors_disabled());
411-
std::env::remove_var("NO_COLOR");
419+
unsafe { std::env::remove_var("NO_COLOR") };
412420

413-
std::env::set_var("NO_COLOR", "false");
421+
unsafe { std::env::set_var("NO_COLOR", "false") };
414422
assert!(!colors_disabled());
415-
std::env::remove_var("NO_COLOR");
423+
unsafe { std::env::remove_var("NO_COLOR") };
416424

417-
std::env::set_var("NO_COLOR", "");
425+
unsafe { std::env::set_var("NO_COLOR", "") };
418426
assert!(!colors_disabled());
419-
std::env::remove_var("NO_COLOR");
427+
unsafe { std::env::remove_var("NO_COLOR") };
420428
}
421429
}

0 commit comments

Comments
 (0)