|
| 1 | +use std::fs; |
| 2 | +use std::io::Write; |
| 3 | + |
| 4 | +use tempfile::tempdir; |
| 5 | + |
| 6 | +const TEST_CONFIG: &str = r#"[urls] |
| 7 | +github = { url = "https://github.com", aliases = ["gh"] } |
| 8 | +dkdc-bookmarks = "https://github.com/lostmygithubaccount/bookmarks" |
| 9 | +
|
| 10 | +[groups] |
| 11 | +dev = ["gh", "dkdc-bookmarks"] |
| 12 | +"#; |
| 13 | + |
| 14 | +fn write_config(dir: &std::path::Path) -> std::path::PathBuf { |
| 15 | + let path = dir.join("bookmarks.toml"); |
| 16 | + let mut f = fs::File::create(&path).unwrap(); |
| 17 | + f.write_all(TEST_CONFIG.as_bytes()).unwrap(); |
| 18 | + path |
| 19 | +} |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn test_print_config() { |
| 23 | + let dir = tempdir().unwrap(); |
| 24 | + let path = write_config(dir.path()); |
| 25 | + let result = bookmarks::run_cli(["bookmarks", "-f", path.to_str().unwrap()]); |
| 26 | + assert!(result.is_ok()); |
| 27 | +} |
| 28 | + |
| 29 | +#[test] |
| 30 | +fn test_file_not_found() { |
| 31 | + let result = bookmarks::run_cli(["bookmarks", "-f", "/nonexistent/bookmarks.toml"]); |
| 32 | + assert!(result.is_err()); |
| 33 | + let err = result.unwrap_err().to_string(); |
| 34 | + assert!(err.contains("not found"), "unexpected error: {err}"); |
| 35 | +} |
| 36 | + |
| 37 | +#[test] |
| 38 | +fn test_unknown_bookmark() { |
| 39 | + let dir = tempdir().unwrap(); |
| 40 | + let path = write_config(dir.path()); |
| 41 | + let result = bookmarks::run_cli(["bookmarks", "-f", path.to_str().unwrap(), "nonexistent"]); |
| 42 | + assert!(result.is_err()); |
| 43 | +} |
| 44 | + |
| 45 | +#[test] |
| 46 | +fn test_local_creates_config() { |
| 47 | + let dir = tempdir().unwrap(); |
| 48 | + let config_path = dir.path().join("bookmarks.toml"); |
| 49 | + assert!(!config_path.exists()); |
| 50 | + |
| 51 | + // Set cwd to temp dir so --local creates bookmarks.toml there |
| 52 | + let original_dir = std::env::current_dir().unwrap(); |
| 53 | + std::env::set_current_dir(dir.path()).unwrap(); |
| 54 | + let result = bookmarks::run_cli(["bookmarks", "--local"]); |
| 55 | + std::env::set_current_dir(original_dir).unwrap(); |
| 56 | + |
| 57 | + assert!(result.is_ok()); |
| 58 | + assert!(config_path.exists()); |
| 59 | +} |
0 commit comments