2025-07-21 22:02:05 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2025-07-26 12:54:01 +02:00
|
|
|
use libips::repository::{
|
2025-07-27 15:22:49 +02:00
|
|
|
FileBackend, REPOSITORY_CONFIG_FILENAME, ReadableRepository, RepositoryVersion,
|
|
|
|
|
WritableRepository,
|
2025-07-26 12:54:01 +02:00
|
|
|
};
|
2025-07-22 10:21:16 +02:00
|
|
|
use std::fs;
|
2025-07-26 12:54:01 +02:00
|
|
|
use std::path::PathBuf;
|
2025-07-22 10:21:16 +02:00
|
|
|
|
|
|
|
|
// These tests interact with real repositories in a known location
|
|
|
|
|
// instead of using temporary directories. This allows for better
|
|
|
|
|
// debugging and inspection of the repositories during testing.
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// The base directory for all test repositories
|
|
|
|
|
const TEST_REPO_BASE_DIR: &str = "/tmp/pkg6repo_test";
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Helper function to create a unique test directory
|
|
|
|
|
fn create_test_dir(test_name: &str) -> PathBuf {
|
|
|
|
|
let test_dir = PathBuf::from(format!("{}/{}", TEST_REPO_BASE_DIR, test_name));
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Clean up any existing directory
|
|
|
|
|
if test_dir.exists() {
|
|
|
|
|
fs::remove_dir_all(&test_dir).unwrap();
|
|
|
|
|
}
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Create the directory
|
|
|
|
|
fs::create_dir_all(&test_dir).unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
test_dir
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper function to clean up test directory
|
|
|
|
|
fn cleanup_test_dir(test_dir: &PathBuf) {
|
|
|
|
|
if test_dir.exists() {
|
|
|
|
|
fs::remove_dir_all(test_dir).unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-21 22:02:05 +02:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_create_repository() {
|
2025-07-22 10:21:16 +02:00
|
|
|
// Create a real test directory
|
|
|
|
|
let test_dir = create_test_dir("create_repository");
|
|
|
|
|
let repo_path = test_dir.join("repo");
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Create a repository
|
|
|
|
|
let _ = FileBackend::create(&repo_path, RepositoryVersion::V4).unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Check that the repository was created
|
|
|
|
|
assert!(repo_path.exists());
|
|
|
|
|
assert!(repo_path.join("catalog").exists());
|
|
|
|
|
assert!(repo_path.join("file").exists());
|
|
|
|
|
assert!(repo_path.join("index").exists());
|
|
|
|
|
assert!(repo_path.join("pkg").exists());
|
|
|
|
|
assert!(repo_path.join("trans").exists());
|
|
|
|
|
assert!(repo_path.join(REPOSITORY_CONFIG_FILENAME).exists());
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Clean up
|
|
|
|
|
cleanup_test_dir(&test_dir);
|
2025-07-21 22:02:05 +02:00
|
|
|
}
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_add_publisher() {
|
2025-07-22 10:21:16 +02:00
|
|
|
// Create a real test directory
|
|
|
|
|
let test_dir = create_test_dir("add_publisher");
|
|
|
|
|
let repo_path = test_dir.join("repo");
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Create a repository
|
|
|
|
|
let mut repo = FileBackend::create(&repo_path, RepositoryVersion::V4).unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Add a publisher
|
|
|
|
|
repo.add_publisher("example.com").unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Check that the publisher was added
|
|
|
|
|
assert!(repo.config.publishers.contains(&"example.com".to_string()));
|
|
|
|
|
assert!(repo_path.join("catalog").join("example.com").exists());
|
|
|
|
|
assert!(repo_path.join("pkg").join("example.com").exists());
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Clean up
|
|
|
|
|
cleanup_test_dir(&test_dir);
|
2025-07-21 22:02:05 +02:00
|
|
|
}
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_remove_publisher() {
|
2025-07-22 10:21:16 +02:00
|
|
|
// Create a real test directory
|
|
|
|
|
let test_dir = create_test_dir("remove_publisher");
|
|
|
|
|
let repo_path = test_dir.join("repo");
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Create a repository
|
|
|
|
|
let mut repo = FileBackend::create(&repo_path, RepositoryVersion::V4).unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Add a publisher
|
|
|
|
|
repo.add_publisher("example.com").unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Check that the publisher was added
|
|
|
|
|
assert!(repo.config.publishers.contains(&"example.com".to_string()));
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 11:57:24 +02:00
|
|
|
// Check that the publisher directories were created
|
|
|
|
|
let catalog_dir = repo_path.join("catalog").join("example.com");
|
|
|
|
|
let pkg_dir = repo_path.join("pkg").join("example.com");
|
2025-07-26 12:54:01 +02:00
|
|
|
assert!(
|
|
|
|
|
catalog_dir.exists(),
|
|
|
|
|
"Catalog directory should exist after adding publisher"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
pkg_dir.exists(),
|
|
|
|
|
"Package directory should exist after adding publisher"
|
|
|
|
|
);
|
|
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Remove the publisher
|
|
|
|
|
repo.remove_publisher("example.com", false).unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 11:57:24 +02:00
|
|
|
// Check that the publisher was removed from the configuration
|
2025-07-21 22:02:05 +02:00
|
|
|
assert!(!repo.config.publishers.contains(&"example.com".to_string()));
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 11:57:24 +02:00
|
|
|
// Check that the publisher directories were removed
|
2025-07-26 12:54:01 +02:00
|
|
|
assert!(
|
|
|
|
|
!catalog_dir.exists(),
|
|
|
|
|
"Catalog directory should not exist after removing publisher"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
!pkg_dir.exists(),
|
|
|
|
|
"Package directory should not exist after removing publisher"
|
|
|
|
|
);
|
|
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Clean up
|
|
|
|
|
cleanup_test_dir(&test_dir);
|
2025-07-21 22:02:05 +02:00
|
|
|
}
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_set_property() {
|
2025-07-22 10:21:16 +02:00
|
|
|
// Create a real test directory
|
|
|
|
|
let test_dir = create_test_dir("set_property");
|
|
|
|
|
let repo_path = test_dir.join("repo");
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Create a repository
|
|
|
|
|
let mut repo = FileBackend::create(&repo_path, RepositoryVersion::V4).unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Set a property
|
2025-07-26 12:54:01 +02:00
|
|
|
repo.set_property("publisher/prefix", "example.com")
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2025-07-21 22:02:05 +02:00
|
|
|
// Check that the property was set
|
2025-07-26 12:54:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
|
repo.config.properties.get("publisher/prefix").unwrap(),
|
|
|
|
|
"example.com"
|
|
|
|
|
);
|
|
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Clean up
|
|
|
|
|
cleanup_test_dir(&test_dir);
|
|
|
|
|
}
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_get_info() {
|
|
|
|
|
// Create a real test directory
|
|
|
|
|
let test_dir = create_test_dir("get_info");
|
|
|
|
|
let repo_path = test_dir.join("repo");
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Create a repository
|
|
|
|
|
let mut repo = FileBackend::create(&repo_path, RepositoryVersion::V4).unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Add a publisher
|
|
|
|
|
repo.add_publisher("example.com").unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Get repository information
|
|
|
|
|
let repo_info = repo.get_info().unwrap();
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Check that the information is correct
|
|
|
|
|
assert_eq!(repo_info.publishers.len(), 1);
|
|
|
|
|
let publisher_info = &repo_info.publishers[0];
|
|
|
|
|
assert_eq!(publisher_info.name, "example.com");
|
|
|
|
|
assert_eq!(publisher_info.package_count, 0); // No packages yet
|
|
|
|
|
assert_eq!(publisher_info.status, "online");
|
2025-07-26 12:54:01 +02:00
|
|
|
|
2025-07-22 10:21:16 +02:00
|
|
|
// Clean up
|
|
|
|
|
cleanup_test_dir(&test_dir);
|
2025-07-21 22:02:05 +02:00
|
|
|
}
|
2025-07-26 12:54:01 +02:00
|
|
|
}
|