#![allow(missing_docs)]
#![allow(clippy ::std_instead_of_core)]
#[ cfg(feature = "enabled") ]
use crates_tools ::CrateArchive;
#[ cfg(feature = "enabled") ]
#[ test ]
fn test_error_handling_network_failures()
{
let result = CrateArchive ::download_crates_io("this_crate_definitely_does_not_exist_xyz123", "0.1.0");
assert!(result.is_err(), "Download of non-existent crate should return Err, not panic");
let err_msg = format!("{}", result.unwrap_err());
assert!(!err_msg.is_empty(), "Error message should not be empty");
}
#[ cfg(feature = "enabled") ]
#[ test ]
fn test_binary_content_handling()
{
let binary_data: Vec< u8 > = vec![ 0xFF, 0xFE, 0xFD, 0x00, 0x80, 0x81 ];
assert!(core ::str ::from_utf8(&binary_data).is_err(), "Test data must be invalid UTF-8");
use flate2 ::write ::GzEncoder;
use flate2 ::Compression;
use tar ::Builder;
use std ::io ::Write;
let mut tar_data = Vec ::new();
{
let mut builder = Builder ::new(&mut tar_data);
let mut header = tar ::Header ::new_gnu();
header.set_path("test_binary.bin").unwrap();
header.set_size(binary_data.len() as u64);
header.set_cksum();
builder.append(&header, &binary_data[..]).unwrap();
builder.finish().unwrap();
}
let mut encoder = GzEncoder ::new(Vec ::new(), Compression ::default());
encoder.write_all(&tar_data).unwrap();
let compressed = encoder.finish().unwrap();
let archive = CrateArchive ::decode(compressed)
.expect("Archive decode should succeed even with binary content");
let files = archive.list();
assert_eq!(files.len(), 1, "Archive should contain 1 file");
let content = archive.content_bytes("test_binary.bin")
.expect("Binary file should exist in archive");
assert_eq!(content, &binary_data[..], "Binary content should match");
assert!(core ::str ::from_utf8(content).is_err(), "Binary content should fail UTF-8 validation");
if let Ok(_text) = core ::str ::from_utf8(content)
{
panic!("Should not be UTF-8!")
}
else
{
let size = content.len();
assert_eq!(size, 6, "Binary size should be correct");
}
}
#[ cfg(feature = "enabled") ]
#[ test ]
fn test_result_based_api_usage()
{
fn download_and_process() -> Result< usize, Box< dyn std ::error ::Error > >
{
let archive = CrateArchive ::download_crates_io("test_experimental_c", "0.1.0")?;
let file_count = archive.list().len();
Ok(file_count)
}
let result = download_and_process();
assert!(result.is_ok(), "Valid download should succeed");
assert!(result.unwrap() > 0, "Archive should contain files");
fn download_invalid() -> Result< usize, Box< dyn std ::error ::Error > >
{
let archive = CrateArchive ::download_crates_io("invalid_crate_name_xyz", "0.1.0")?;
Ok(archive.list().len())
}
let result = download_invalid();
assert!(result.is_err(), "Invalid download should return Err");
}
#[ cfg(feature = "enabled") ]
#[ test ]
fn test_example_handles_all_edge_cases()
{
let err_result = CrateArchive ::download_crates_io("", "0.0.0");
assert!(err_result.is_err(), "Empty crate name should error, not panic");
let ok_result = CrateArchive ::download_crates_io("test_experimental_c", "0.1.0");
assert!(ok_result.is_ok(), "Valid crate should download successfully");
let archive = ok_result.unwrap();
for path in archive.list()
{
if let Some(bytes) = archive.content_bytes(path)
{
match core ::str ::from_utf8(bytes)
{
Ok(_text) =>
{
}
Err(_) =>
{
assert!(!bytes.is_empty(), "Binary files should have content");
}
}
}
}
}