|
| 1 | +use super::*; |
| 2 | +use std::fs; |
| 3 | +use std::io::Write; |
1 | 4 |
|
| 5 | +#[tokio::test] |
| 6 | +#[serial] |
| 7 | +/// Tests the basic functionality of add command by adding a single file |
| 8 | +async fn test_add_single_file() { |
| 9 | + let test_dir = tempdir().unwrap(); |
| 10 | + test::setup_with_new_libra_in(test_dir.path()).await; |
| 11 | + let _guard = test::ChangeDirGuard::new(test_dir.path()); |
| 12 | + |
| 13 | + // Create a new file |
| 14 | + let file_content = "Hello, World!"; |
| 15 | + let file_path = "test_file.txt"; |
| 16 | + let mut file = fs::File::create(file_path).unwrap(); |
| 17 | + file.write_all(file_content.as_bytes()).unwrap(); |
| 18 | + |
| 19 | + // Execute add command |
| 20 | + add::execute(AddArgs { |
| 21 | + pathspec: vec![String::from(file_path)], |
| 22 | + all: false, |
| 23 | + update: false, |
| 24 | + refresh: false, |
| 25 | + verbose: false, |
| 26 | + dry_run: false, |
| 27 | + ignore_errors: false, |
| 28 | + }) |
| 29 | + .await; |
| 30 | + |
| 31 | + // Verify the file was added to index. |
| 32 | + let changes = changes_to_be_committed().await; |
| 33 | + |
| 34 | + assert!(changes.new.iter().any(|x| x.to_str().unwrap() == file_path)); |
| 35 | +} |
| 36 | + |
| 37 | +#[tokio::test] |
| 38 | +#[serial] |
| 39 | +/// Tests adding multiple files at once |
| 40 | +async fn test_add_multiple_files() { |
| 41 | + let test_dir = tempdir().unwrap(); |
| 42 | + test::setup_with_new_libra_in(test_dir.path()).await; |
| 43 | + let _guard = test::ChangeDirGuard::new(test_dir.path()); |
| 44 | + |
| 45 | + // Create multiple files |
| 46 | + for i in 1..=3 { |
| 47 | + let file_content = format!("File content {i}"); |
| 48 | + let file_path = format!("test_file_{i}.txt"); |
| 49 | + let mut file = fs::File::create(&file_path).unwrap(); |
| 50 | + file.write_all(file_content.as_bytes()).unwrap(); |
| 51 | + } |
| 52 | + |
| 53 | + // Execute add command |
| 54 | + add::execute(AddArgs { |
| 55 | + pathspec: vec![ |
| 56 | + String::from("test_file_1.txt"), |
| 57 | + String::from("test_file_2.txt"), |
| 58 | + String::from("test_file_3.txt"), |
| 59 | + ], |
| 60 | + all: false, |
| 61 | + update: false, |
| 62 | + refresh: false, |
| 63 | + verbose: false, |
| 64 | + dry_run: false, |
| 65 | + ignore_errors: false, |
| 66 | + }) |
| 67 | + .await; |
| 68 | + |
| 69 | + // Verify all files were added to index |
| 70 | + let changes = changes_to_be_committed().await; |
| 71 | + assert!(changes.new.iter().any(|x| x.to_str().unwrap() == "test_file_1.txt")); |
| 72 | + assert!(changes.new.iter().any(|x| x.to_str().unwrap() == "test_file_2.txt")); |
| 73 | + assert!(changes.new.iter().any(|x| x.to_str().unwrap() == "test_file_3.txt")); |
| 74 | +} |
| 75 | + |
| 76 | +#[tokio::test] |
| 77 | +#[serial] |
| 78 | +/// Tests the --all flag which adds all files in the working tree |
| 79 | +async fn test_add_all_flag() { |
| 80 | + let test_dir = tempdir().unwrap(); |
| 81 | + test::setup_with_new_libra_in(test_dir.path()).await; |
| 82 | + let _guard = test::ChangeDirGuard::new(test_dir.path()); |
| 83 | + |
| 84 | + // Create multiple files |
| 85 | + for i in 1..=3 { |
| 86 | + let file_content = format!("File content {i}"); |
| 87 | + let file_path = format!("test_file_{i}.txt"); |
| 88 | + let mut file = fs::File::create(&file_path).unwrap(); |
| 89 | + file.write_all(file_content.as_bytes()).unwrap(); |
| 90 | + } |
| 91 | + |
| 92 | + // Execute add command with --all flag |
| 93 | + add::execute(AddArgs { |
| 94 | + pathspec: vec![], |
| 95 | + all: true, |
| 96 | + update: false, |
| 97 | + refresh: false, |
| 98 | + verbose: false, |
| 99 | + dry_run: false, |
| 100 | + ignore_errors: false, |
| 101 | + }) |
| 102 | + .await; |
| 103 | + |
| 104 | + // Verify all files were added to index |
| 105 | + let changes = changes_to_be_committed().await; |
| 106 | + assert!(changes.new.iter().any(|x| x.to_str().unwrap() == "test_file_1.txt")); |
| 107 | + assert!(changes.new.iter().any(|x| x.to_str().unwrap() == "test_file_2.txt")); |
| 108 | + assert!(changes.new.iter().any(|x| x.to_str().unwrap() == "test_file_3.txt")); |
| 109 | +} |
| 110 | + |
| 111 | +#[tokio::test] |
| 112 | +#[serial] |
| 113 | +/// Tests the --update flag which only updates files already in the index |
| 114 | +async fn test_add_update_flag() { |
| 115 | + let test_dir = tempdir().unwrap(); |
| 116 | + test::setup_with_new_libra_in(test_dir.path()).await; |
| 117 | + let _guard = test::ChangeDirGuard::new(test_dir.path()); |
| 118 | + |
| 119 | + // Create files and add one to the index |
| 120 | + let tracked_file = "tracked_file.txt"; |
| 121 | + let untracked_file = "untracked_file.txt"; |
| 122 | + |
| 123 | + // Create and write initial content |
| 124 | + let mut file1 = fs::File::create(tracked_file).unwrap(); |
| 125 | + file1.write_all(b"Initial content").unwrap(); |
| 126 | + |
| 127 | + let mut file2 = fs::File::create(untracked_file).unwrap(); |
| 128 | + file2.write_all(b"Initial content").unwrap(); |
| 129 | + |
| 130 | + // Add only one file to the index |
| 131 | + add::execute(AddArgs { |
| 132 | + pathspec: vec![String::from(tracked_file)], |
| 133 | + all: false, |
| 134 | + update: false, |
| 135 | + refresh: false, |
| 136 | + verbose: false, |
| 137 | + dry_run: false, |
| 138 | + ignore_errors: false, |
| 139 | + }) |
| 140 | + .await; |
| 141 | + |
| 142 | + // Modify both files |
| 143 | + let mut file1 = fs::OpenOptions::new().write(true).truncate(true).open(tracked_file).unwrap(); |
| 144 | + file1.write_all(b" - Modified").unwrap(); |
| 145 | + |
| 146 | + let mut file2 = fs::OpenOptions::new().write(true).truncate(true).open(untracked_file).unwrap(); |
| 147 | + file2.write_all(b" - Modified").unwrap(); |
| 148 | + |
| 149 | + // Execute add command with --update flag |
| 150 | + add::execute(AddArgs { |
| 151 | + pathspec: vec![String::from(".")], |
| 152 | + all: false, |
| 153 | + update: true, |
| 154 | + refresh: false, |
| 155 | + verbose: false, |
| 156 | + dry_run: false, |
| 157 | + ignore_errors: false, |
| 158 | + }) |
| 159 | + .await; |
| 160 | + |
| 161 | + // Verify only tracked file was updated |
| 162 | + let changes = changes_to_be_staged(); |
| 163 | + // Tracked file should not appear in changes (because it was updated in index) |
| 164 | + assert!(!changes.modified.iter().any(|x| x.to_str().unwrap() == tracked_file)); |
| 165 | + // Untracked file should still be untracked and show as new |
| 166 | + assert!(changes.new.iter().any(|x| x.to_str().unwrap() == untracked_file)); |
| 167 | +} |
| 168 | + |
| 169 | +#[tokio::test] |
| 170 | +#[serial] |
| 171 | +/// Tests adding files with respect to ignore patterns in .libraignore |
| 172 | +async fn test_add_with_ignore_patterns() { |
| 173 | + let test_dir = tempdir().unwrap(); |
| 174 | + test::setup_with_new_libra_in(test_dir.path()).await; |
| 175 | + let _guard = test::ChangeDirGuard::new(test_dir.path()); |
| 176 | + |
| 177 | + // Create .libraignore file |
| 178 | + let mut ignore_file = fs::File::create(".libraignore").unwrap(); |
| 179 | + ignore_file.write_all(b"ignored_*.txt\nignore_dir/**").unwrap(); |
| 180 | + |
| 181 | + // Create files that should be ignored and not ignored |
| 182 | + let ignored_file = "ignored_file.txt"; |
| 183 | + let tracked_file = "tracked_file.txt"; |
| 184 | + |
| 185 | + // Create directory that should be ignored |
| 186 | + fs::create_dir("ignore_dir").unwrap(); |
| 187 | + let ignored_dir_file = "ignore_dir/file.txt"; |
| 188 | + |
| 189 | + // Create and write content |
| 190 | + let mut file1 = fs::File::create(ignored_file).unwrap(); |
| 191 | + file1.write_all(b"Should be ignored").unwrap(); |
| 192 | + |
| 193 | + let mut file2 = fs::File::create(tracked_file).unwrap(); |
| 194 | + file2.write_all(b"Should be tracked").unwrap(); |
| 195 | + |
| 196 | + let mut file3 = fs::File::create(ignored_dir_file).unwrap(); |
| 197 | + file3.write_all(b"Should be ignored").unwrap(); |
| 198 | + |
| 199 | + // Execute add command with all files |
| 200 | + add::execute(AddArgs { |
| 201 | + pathspec: vec![String::from(".")], |
| 202 | + all: true, |
| 203 | + update: false, |
| 204 | + refresh: false, |
| 205 | + verbose: false, |
| 206 | + dry_run: false, |
| 207 | + ignore_errors: false, |
| 208 | + }) |
| 209 | + .await; |
| 210 | + |
| 211 | + // Verify only non-ignored files were added |
| 212 | + let changes_staged = changes_to_be_staged(); |
| 213 | + let changes_committed = changes_to_be_committed().await; |
| 214 | + |
| 215 | + // Ignored files should not appear in any status (they are ignored) |
| 216 | + assert!(!changes_staged.new.iter().any(|x| x.to_str().unwrap() == ignored_file)); |
| 217 | + assert!(!changes_staged.new.iter().any(|x| x.to_str().unwrap() == ignored_dir_file)); |
| 218 | + assert!(!changes_committed.new.iter().any(|x| x.to_str().unwrap() == ignored_file)); |
| 219 | + assert!(!changes_committed.new.iter().any(|x| x.to_str().unwrap() == ignored_dir_file)); |
| 220 | + |
| 221 | + // Non-ignored file should not show as new in staged (was added) but should show in committed |
| 222 | + assert!(!changes_staged.new.iter().any(|x| x.to_str().unwrap() == tracked_file)); |
| 223 | + assert!(changes_committed.new.iter().any(|x| x.to_str().unwrap() == tracked_file)); |
| 224 | +} |
| 225 | + |
| 226 | +#[tokio::test] |
| 227 | +#[serial] |
| 228 | +/// Tests the dry-run flag which should not actually add files |
| 229 | +async fn test_add_dry_run() { |
| 230 | + let test_dir = tempdir().unwrap(); |
| 231 | + test::setup_with_new_libra_in(test_dir.path()).await; |
| 232 | + let _guard = test::ChangeDirGuard::new(test_dir.path()); |
| 233 | + |
| 234 | + // Create a file. |
| 235 | + let file_path = "test_file.txt"; |
| 236 | + let mut file = fs::File::create(file_path).unwrap(); |
| 237 | + file.write_all(b"Test content").unwrap(); |
| 238 | + |
| 239 | + // Execute add command with dry-run |
| 240 | + add::execute(AddArgs { |
| 241 | + pathspec: vec![String::from(file_path)], |
| 242 | + all: false, |
| 243 | + update: false, |
| 244 | + refresh: false, |
| 245 | + verbose: false, |
| 246 | + dry_run: true, |
| 247 | + ignore_errors: false, |
| 248 | + }) |
| 249 | + .await; |
| 250 | + |
| 251 | + // Verify the file was not actually added to index |
| 252 | + let changes = changes_to_be_staged(); |
| 253 | + assert!(changes.new.iter().any(|x| x.to_str().unwrap() == file_path)); |
| 254 | +} |
0 commit comments