|
| 1 | +#![cfg(not(target_family = "wasm"))] |
| 2 | + |
| 3 | +use crate::utils::setup; |
| 4 | +use assert_cmd::cargo::cargo_bin_cmd; |
| 5 | +use predicates::prelude::predicate; |
| 6 | +use std::fs; |
| 7 | + |
| 8 | +/// Precondition: A file exists in the filesystem. |
| 9 | +/// Action: Create archive using stdio with --mac-metadata --unstable flags. |
| 10 | +/// Expectation: Command succeeds (option is recognized and accepted). |
| 11 | +#[test] |
| 12 | +fn stdio_mac_metadata_option_accepted() { |
| 13 | + setup(); |
| 14 | + let file = "stdio_mac_metadata_option_accepted.txt"; |
| 15 | + fs::write(file, "test content").unwrap(); |
| 16 | + |
| 17 | + let mut cmd = cargo_bin_cmd!("pna"); |
| 18 | + cmd.arg("experimental") |
| 19 | + .arg("stdio") |
| 20 | + .arg("-c") |
| 21 | + .arg("--unstable") |
| 22 | + .arg("--mac-metadata") |
| 23 | + .arg(file) |
| 24 | + .assert() |
| 25 | + .success(); |
| 26 | +} |
| 27 | + |
| 28 | +/// Precondition: A file exists in the filesystem. |
| 29 | +/// Action: Create archive using stdio with --no-mac-metadata --unstable flags. |
| 30 | +/// Expectation: Command succeeds (option is recognized and accepted). |
| 31 | +#[test] |
| 32 | +fn stdio_no_mac_metadata_option_accepted() { |
| 33 | + setup(); |
| 34 | + let file = "stdio_no_mac_metadata_option_accepted.txt"; |
| 35 | + fs::write(file, "test content").unwrap(); |
| 36 | + |
| 37 | + let mut cmd = cargo_bin_cmd!("pna"); |
| 38 | + cmd.arg("experimental") |
| 39 | + .arg("stdio") |
| 40 | + .arg("-c") |
| 41 | + .arg("--unstable") |
| 42 | + .arg("--no-mac-metadata") |
| 43 | + .arg(file) |
| 44 | + .assert() |
| 45 | + .success(); |
| 46 | +} |
| 47 | + |
| 48 | +/// Precondition: A file exists in the filesystem. |
| 49 | +/// Action: Attempt to use --mac-metadata without --unstable flag. |
| 50 | +/// Expectation: Command fails because --mac-metadata requires --unstable. |
| 51 | +#[test] |
| 52 | +fn stdio_mac_metadata_requires_unstable() { |
| 53 | + setup(); |
| 54 | + let file = "stdio_mac_metadata_requires_unstable.txt"; |
| 55 | + fs::write(file, "test content").unwrap(); |
| 56 | + |
| 57 | + let mut cmd = cargo_bin_cmd!("pna"); |
| 58 | + cmd.arg("experimental") |
| 59 | + .arg("stdio") |
| 60 | + .arg("-c") |
| 61 | + .arg("--mac-metadata") |
| 62 | + .arg(file) |
| 63 | + .assert() |
| 64 | + .failure() |
| 65 | + .stderr(predicate::str::contains("--unstable")); |
| 66 | +} |
| 67 | + |
| 68 | +/// Precondition: A file exists in the filesystem. |
| 69 | +/// Action: Attempt to use both --mac-metadata and --no-mac-metadata together. |
| 70 | +/// Expectation: Command fails because the options are mutually exclusive. |
| 71 | +#[test] |
| 72 | +fn stdio_mac_metadata_and_no_mac_metadata_mutually_exclusive() { |
| 73 | + setup(); |
| 74 | + let file = "stdio_mac_metadata_mutually_exclusive.txt"; |
| 75 | + fs::write(file, "test content").unwrap(); |
| 76 | + |
| 77 | + let mut cmd = cargo_bin_cmd!("pna"); |
| 78 | + cmd.arg("experimental") |
| 79 | + .arg("stdio") |
| 80 | + .arg("-c") |
| 81 | + .arg("--unstable") |
| 82 | + .arg("--mac-metadata") |
| 83 | + .arg("--no-mac-metadata") |
| 84 | + .arg(file) |
| 85 | + .assert() |
| 86 | + .failure() |
| 87 | + .stderr(predicate::str::contains("cannot be used with")); |
| 88 | +} |
| 89 | + |
| 90 | +/// Precondition: A file exists in the filesystem. |
| 91 | +/// Action: Extract archive using stdio with --mac-metadata --unstable flags. |
| 92 | +/// Expectation: Command succeeds (option is recognized for extract mode). |
| 93 | +#[test] |
| 94 | +fn stdio_extract_mac_metadata_option_accepted() { |
| 95 | + setup(); |
| 96 | + fs::create_dir_all("stdio_extract_mac_metadata_dir").unwrap(); |
| 97 | + fs::write("stdio_extract_mac_metadata_dir/test.txt", "test content").unwrap(); |
| 98 | + fs::create_dir_all("stdio_extract_mac_metadata_dir/out").unwrap(); |
| 99 | + |
| 100 | + // Create an archive first |
| 101 | + cargo_bin_cmd!("pna") |
| 102 | + .args([ |
| 103 | + "create", |
| 104 | + "stdio_extract_mac_metadata_dir/test.pna", |
| 105 | + "--overwrite", |
| 106 | + "stdio_extract_mac_metadata_dir/test.txt", |
| 107 | + ]) |
| 108 | + .assert() |
| 109 | + .success(); |
| 110 | + |
| 111 | + // Extract with --mac-metadata |
| 112 | + cargo_bin_cmd!("pna") |
| 113 | + .args([ |
| 114 | + "experimental", |
| 115 | + "stdio", |
| 116 | + "-x", |
| 117 | + "--unstable", |
| 118 | + "--mac-metadata", |
| 119 | + "-f", |
| 120 | + "stdio_extract_mac_metadata_dir/test.pna", |
| 121 | + "--out-dir", |
| 122 | + "stdio_extract_mac_metadata_dir/out", |
| 123 | + ]) |
| 124 | + .assert() |
| 125 | + .success(); |
| 126 | +} |
| 127 | + |
| 128 | +/// Precondition: A file exists in the filesystem. |
| 129 | +/// Action: Use stdio append mode with --mac-metadata --unstable flags. |
| 130 | +/// Expectation: Command succeeds (option is recognized for append mode). |
| 131 | +#[test] |
| 132 | +fn stdio_append_mac_metadata_option_accepted() { |
| 133 | + setup(); |
| 134 | + fs::create_dir_all("stdio_append_mac_metadata_dir").unwrap(); |
| 135 | + fs::write("stdio_append_mac_metadata_dir/file1.txt", "test content 1").unwrap(); |
| 136 | + fs::write("stdio_append_mac_metadata_dir/file2.txt", "test content 2").unwrap(); |
| 137 | + |
| 138 | + // Create an archive first |
| 139 | + cargo_bin_cmd!("pna") |
| 140 | + .args([ |
| 141 | + "create", |
| 142 | + "stdio_append_mac_metadata_dir/test.pna", |
| 143 | + "--overwrite", |
| 144 | + "stdio_append_mac_metadata_dir/file1.txt", |
| 145 | + ]) |
| 146 | + .assert() |
| 147 | + .success(); |
| 148 | + |
| 149 | + // Append with --mac-metadata |
| 150 | + cargo_bin_cmd!("pna") |
| 151 | + .args([ |
| 152 | + "experimental", |
| 153 | + "stdio", |
| 154 | + "-r", |
| 155 | + "--unstable", |
| 156 | + "--mac-metadata", |
| 157 | + "-f", |
| 158 | + "stdio_append_mac_metadata_dir/test.pna", |
| 159 | + "stdio_append_mac_metadata_dir/file2.txt", |
| 160 | + ]) |
| 161 | + .assert() |
| 162 | + .success(); |
| 163 | +} |
| 164 | + |
| 165 | +/// Precondition: A file exists in the filesystem. |
| 166 | +/// Action: Use stdio update mode with --mac-metadata --unstable flags. |
| 167 | +/// Expectation: Command succeeds (option is recognized for update mode). |
| 168 | +#[test] |
| 169 | +fn stdio_update_mac_metadata_option_accepted() { |
| 170 | + setup(); |
| 171 | + fs::create_dir_all("stdio_update_mac_metadata_dir").unwrap(); |
| 172 | + fs::write("stdio_update_mac_metadata_dir/test.txt", "test content").unwrap(); |
| 173 | + |
| 174 | + // Create an archive first |
| 175 | + cargo_bin_cmd!("pna") |
| 176 | + .args([ |
| 177 | + "create", |
| 178 | + "stdio_update_mac_metadata_dir/test.pna", |
| 179 | + "--overwrite", |
| 180 | + "stdio_update_mac_metadata_dir/test.txt", |
| 181 | + ]) |
| 182 | + .assert() |
| 183 | + .success(); |
| 184 | + |
| 185 | + // Update the file |
| 186 | + fs::write("stdio_update_mac_metadata_dir/test.txt", "updated content").unwrap(); |
| 187 | + |
| 188 | + // Update with --mac-metadata |
| 189 | + cargo_bin_cmd!("pna") |
| 190 | + .args([ |
| 191 | + "experimental", |
| 192 | + "stdio", |
| 193 | + "-u", |
| 194 | + "--unstable", |
| 195 | + "--mac-metadata", |
| 196 | + "-f", |
| 197 | + "stdio_update_mac_metadata_dir/test.pna", |
| 198 | + "stdio_update_mac_metadata_dir/test.txt", |
| 199 | + ]) |
| 200 | + .assert() |
| 201 | + .success(); |
| 202 | +} |
| 203 | + |
| 204 | +// macOS-specific tests that verify xattr preservation |
| 205 | +#[cfg(target_os = "macos")] |
| 206 | +mod macos_tests { |
| 207 | + use super::*; |
| 208 | + use std::process::Command; |
| 209 | + |
| 210 | + /// Precondition: A file with extended attributes exists on macOS. |
| 211 | + /// Action: Create archive with --mac-metadata and extract it. |
| 212 | + /// Expectation: Extended attributes are preserved in the archive and restored on extraction. |
| 213 | + #[test] |
| 214 | + fn stdio_mac_metadata_preserves_xattrs() { |
| 215 | + setup(); |
| 216 | + fs::create_dir_all("stdio_mac_metadata_xattr_dir").unwrap(); |
| 217 | + fs::write("stdio_mac_metadata_xattr_dir/test.txt", "test content").unwrap(); |
| 218 | + fs::create_dir_all("stdio_mac_metadata_xattr_dir/out").unwrap(); |
| 219 | + |
| 220 | + // Set xattr |
| 221 | + Command::new("xattr") |
| 222 | + .args([ |
| 223 | + "-w", |
| 224 | + "com.example.test", |
| 225 | + "test_value", |
| 226 | + "stdio_mac_metadata_xattr_dir/test.txt", |
| 227 | + ]) |
| 228 | + .status() |
| 229 | + .expect("Failed to set xattr"); |
| 230 | + |
| 231 | + // Create archive with --mac-metadata |
| 232 | + cargo_bin_cmd!("pna") |
| 233 | + .args([ |
| 234 | + "experimental", |
| 235 | + "stdio", |
| 236 | + "-c", |
| 237 | + "--unstable", |
| 238 | + "--mac-metadata", |
| 239 | + "-f", |
| 240 | + "stdio_mac_metadata_xattr_dir/test.pna", |
| 241 | + "stdio_mac_metadata_xattr_dir/test.txt", |
| 242 | + ]) |
| 243 | + .assert() |
| 244 | + .success(); |
| 245 | + |
| 246 | + // Extract with --mac-metadata |
| 247 | + cargo_bin_cmd!("pna") |
| 248 | + .args([ |
| 249 | + "experimental", |
| 250 | + "stdio", |
| 251 | + "-x", |
| 252 | + "--unstable", |
| 253 | + "--mac-metadata", |
| 254 | + "-f", |
| 255 | + "stdio_mac_metadata_xattr_dir/test.pna", |
| 256 | + "--out-dir", |
| 257 | + "stdio_mac_metadata_xattr_dir/out", |
| 258 | + "--overwrite", |
| 259 | + ]) |
| 260 | + .assert() |
| 261 | + .success(); |
| 262 | + |
| 263 | + // Verify xattr is preserved |
| 264 | + let output = Command::new("xattr") |
| 265 | + .args([ |
| 266 | + "-p", |
| 267 | + "com.example.test", |
| 268 | + "stdio_mac_metadata_xattr_dir/out/stdio_mac_metadata_xattr_dir/test.txt", |
| 269 | + ]) |
| 270 | + .output() |
| 271 | + .expect("Failed to read xattr"); |
| 272 | + |
| 273 | + assert!(output.status.success()); |
| 274 | + let extracted_value = String::from_utf8_lossy(&output.stdout); |
| 275 | + assert!(extracted_value.trim() == "test_value"); |
| 276 | + } |
| 277 | + |
| 278 | + /// Precondition: A file with extended attributes exists on macOS. |
| 279 | + /// Action: Create archive with --no-mac-metadata and extract it. |
| 280 | + /// Expectation: Extended attributes are NOT preserved. |
| 281 | + #[test] |
| 282 | + fn stdio_no_mac_metadata_excludes_xattrs() { |
| 283 | + setup(); |
| 284 | + fs::create_dir_all("stdio_no_mac_metadata_xattr_dir").unwrap(); |
| 285 | + fs::write("stdio_no_mac_metadata_xattr_dir/test.txt", "test content").unwrap(); |
| 286 | + fs::create_dir_all("stdio_no_mac_metadata_xattr_dir/out").unwrap(); |
| 287 | + |
| 288 | + // Set xattr |
| 289 | + Command::new("xattr") |
| 290 | + .args([ |
| 291 | + "-w", |
| 292 | + "com.example.test", |
| 293 | + "test_value", |
| 294 | + "stdio_no_mac_metadata_xattr_dir/test.txt", |
| 295 | + ]) |
| 296 | + .status() |
| 297 | + .expect("Failed to set xattr"); |
| 298 | + |
| 299 | + // Create archive with --no-mac-metadata |
| 300 | + cargo_bin_cmd!("pna") |
| 301 | + .args([ |
| 302 | + "experimental", |
| 303 | + "stdio", |
| 304 | + "-c", |
| 305 | + "--unstable", |
| 306 | + "--no-mac-metadata", |
| 307 | + "-f", |
| 308 | + "stdio_no_mac_metadata_xattr_dir/test.pna", |
| 309 | + "stdio_no_mac_metadata_xattr_dir/test.txt", |
| 310 | + ]) |
| 311 | + .assert() |
| 312 | + .success(); |
| 313 | + |
| 314 | + // Extract with --mac-metadata (even if we try to restore, nothing should be there) |
| 315 | + cargo_bin_cmd!("pna") |
| 316 | + .args([ |
| 317 | + "experimental", |
| 318 | + "stdio", |
| 319 | + "-x", |
| 320 | + "--unstable", |
| 321 | + "--mac-metadata", |
| 322 | + "-f", |
| 323 | + "stdio_no_mac_metadata_xattr_dir/test.pna", |
| 324 | + "--out-dir", |
| 325 | + "stdio_no_mac_metadata_xattr_dir/out", |
| 326 | + "--overwrite", |
| 327 | + ]) |
| 328 | + .assert() |
| 329 | + .success(); |
| 330 | + |
| 331 | + // Verify xattr is NOT preserved |
| 332 | + let output = Command::new("xattr") |
| 333 | + .args([ |
| 334 | + "-p", |
| 335 | + "com.example.test", |
| 336 | + "stdio_no_mac_metadata_xattr_dir/out/stdio_no_mac_metadata_xattr_dir/test.txt", |
| 337 | + ]) |
| 338 | + .output() |
| 339 | + .expect("Failed to check xattr"); |
| 340 | + |
| 341 | + // xattr command should fail because the attribute doesn't exist |
| 342 | + assert!(!output.status.success()); |
| 343 | + } |
| 344 | +} |
0 commit comments