Skip to content

Commit 803c259

Browse files
committed
test: write unit tests for add and delete commands
1 parent 86f8bdc commit 803c259

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

src/commands.rs

+163
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,167 @@ mod tests {
319319
assert_eq!(env::var("TEST_OVERWRITE").unwrap(), "new_value");
320320
env::remove_var("TEST_OVERWRITE");
321321
}
322+
323+
#[test]
324+
fn test_add_to_nonexistent_variable() {
325+
let args = AddArgs {
326+
key: "TEST_ADD_NEW".to_string(),
327+
value: "new_value".to_string(),
328+
global: false,
329+
process: None,
330+
};
331+
332+
let result = add(&args);
333+
assert!(result.is_ok());
334+
assert_eq!(env::var("TEST_ADD_NEW").unwrap(), "new_value");
335+
env::remove_var("TEST_ADD_NEW");
336+
}
337+
338+
#[test]
339+
fn test_add_to_existing_variable() {
340+
env::set_var("TEST_ADD_EXISTING", "existing_");
341+
342+
let args = AddArgs {
343+
key: "TEST_ADD_EXISTING".to_string(),
344+
value: "appended".to_string(),
345+
global: false,
346+
process: None,
347+
};
348+
349+
let result = add(&args);
350+
assert!(result.is_ok());
351+
assert_eq!(env::var("TEST_ADD_EXISTING").unwrap(), "existing_appended");
352+
env::remove_var("TEST_ADD_EXISTING");
353+
}
354+
355+
#[test]
356+
fn test_add_with_invalid_name() {
357+
let args = AddArgs {
358+
key: "INVALID NAME".to_string(),
359+
value: "test_value".to_string(),
360+
global: false,
361+
process: None,
362+
};
363+
364+
let result = add(&args);
365+
assert!(result.is_err());
366+
match result.unwrap_err() {
367+
ErrorKind::NameValidationError(err) => {
368+
assert!(err.contains("cannot contain spaces"));
369+
},
370+
_ => panic!("Unexpected error type"),
371+
}
372+
}
373+
374+
#[test]
375+
fn test_add_empty_value() {
376+
env::set_var("TEST_ADD_EMPTY", "existing");
377+
378+
let args = AddArgs {
379+
key: "TEST_ADD_EMPTY".to_string(),
380+
value: "".to_string(),
381+
global: false,
382+
process: None,
383+
};
384+
385+
let result = add(&args);
386+
assert!(result.is_ok());
387+
assert_eq!(env::var("TEST_ADD_EMPTY").unwrap(), "existing");
388+
env::remove_var("TEST_ADD_EMPTY");
389+
}
390+
391+
#[test]
392+
fn test_add_with_process() {
393+
let args = AddArgs {
394+
key: "TEST_ADD_PROCESS".to_string(),
395+
value: "_value".to_string(),
396+
global: false,
397+
process: Some("echo test".to_string()),
398+
};
399+
400+
env::set_var("TEST_ADD_PROCESS", "initial");
401+
let result = add(&args);
402+
assert!(result.is_ok());
403+
assert_eq!(env::var("TEST_ADD_PROCESS").unwrap(), "initial_value");
404+
env::remove_var("TEST_ADD_PROCESS");
405+
}
406+
407+
#[test]
408+
fn test_delete_existing_variable() {
409+
env::set_var("TEST_DELETE_VAR", "test_value");
410+
411+
let args = DeleteArgs {
412+
key: "TEST_DELETE_VAR".to_string(),
413+
global: false,
414+
process: None,
415+
};
416+
417+
let result = delete(&args);
418+
assert!(result.is_ok());
419+
assert!(env::var("TEST_DELETE_VAR").is_err());
420+
}
421+
422+
#[test]
423+
fn test_delete_nonexistent_variable() {
424+
let args = DeleteArgs {
425+
key: "NONEXISTENT_VAR".to_string(),
426+
global: false,
427+
process: None,
428+
};
429+
430+
let result = delete(&args);
431+
// Should succeed even if variable doesn't exist
432+
assert!(result.is_ok());
433+
}
434+
435+
#[test]
436+
fn test_delete_with_invalid_name() {
437+
let args = DeleteArgs {
438+
key: "INVALID NAME".to_string(),
439+
global: false,
440+
process: None,
441+
};
442+
443+
let result = delete(&args);
444+
assert!(result.is_err());
445+
match result.unwrap_err() {
446+
ErrorKind::NameValidationError(err) => {
447+
assert!(err.contains("cannot contain spaces"));
448+
},
449+
_ => panic!("Unexpected error type"),
450+
}
451+
}
452+
453+
#[test]
454+
fn test_delete_with_process() {
455+
env::set_var("TEST_DELETE_PROCESS", "test_value");
456+
457+
let args = DeleteArgs {
458+
key: "TEST_DELETE_PROCESS".to_string(),
459+
global: false,
460+
process: Some("echo test".to_string()),
461+
};
462+
463+
let result = delete(&args);
464+
assert!(result.is_ok());
465+
assert!(env::var("TEST_DELETE_PROCESS").is_err());
466+
}
467+
468+
#[test]
469+
fn test_delete_with_empty_name() {
470+
let args = DeleteArgs {
471+
key: "".to_string(),
472+
global: false,
473+
process: None,
474+
};
475+
476+
let result = delete(&args);
477+
assert!(result.is_err());
478+
match result.unwrap_err() {
479+
ErrorKind::NameValidationError(err) => {
480+
assert!(err.contains("empty"));
481+
},
482+
_ => panic!("Unexpected error type"),
483+
}
484+
}
322485
}

0 commit comments

Comments
 (0)