Updating to use existing temp file API

This commit is contained in:
Kevin Bost
2025-02-28 01:11:38 -08:00
committed by Caelan
parent 1014067401
commit f443fbc087

View File

@@ -557,74 +557,54 @@ pub fn find_latest_full_package(packages_dir: &PathBuf) -> Option<(PathBuf, Mani
package
}
mod locator_tests
{
use super::*;
use std::fs;
#[test]
fn test_locator_staged_id_for_new_user() {
//Create new locator with paths to a test directory
let tmp_dir = tempfile::TempDir::new().unwrap();
let tmp_buf = tmp_dir.path().to_path_buf();
let test_dir = tmp_buf.join(format!("velopack_{}", util::random_string(8)));
struct TestCleanup {
path: PathBuf,
}
impl Drop for TestCleanup {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.path);
}
}
let mut paths = VelopackLocatorConfig::default();
paths.PackagesDir = test_dir;
//Esure the packages directory exists
assert!(std::fs::create_dir_all(&paths.PackagesDir).is_ok());
#[test]
fn test_locator_staged_id_for_new_user() {
//Create new locator with paths to a test directory
let test_dir = std::env::temp_dir().join(format!("velopack_{}", util::random_string(8)));
let locator = VelopackLocator::new(paths, Manifest::default());
let _cleanup = TestCleanup {
path: test_dir.clone(),
};
let staged_user_id = locator.get_staged_user_id();
let mut paths = VelopackLocatorConfig::default();
paths.PackagesDir = test_dir;
//Esure the packages directory exists
assert!(std::fs::create_dir_all(&paths.PackagesDir).is_ok());
let locator = VelopackLocator::new(paths, Manifest::default());
let staged_user_id = locator.get_staged_user_id();
assert_ne!(staged_user_id, "");
let packages_dir = locator.get_packages_dir();
let beta_id_path = packages_dir.join(".betaId");
assert!(beta_id_path.exists());
if let Ok(beta_id) = std::fs::read_to_string(&beta_id_path) {
assert_eq!(staged_user_id, beta_id);
} else {
assert!(false, "Couldn't read staging userId.");
}
}
#[test]
fn test_locator_staged_id_for_existing_user() {
let test_dir = std::env::temp_dir().join(format!("velopack_{}", util::random_string(8)));
assert_ne!(staged_user_id, "");
let packages_dir = locator.get_packages_dir();
let beta_id_path = packages_dir.join(".betaId");
assert!(beta_id_path.exists());
let _cleanup = TestCleanup {
path: test_dir.clone(),
};
let mut paths = VelopackLocatorConfig::default();
paths.PackagesDir = test_dir;
//Esure the packages directory exists
assert!(std::fs::create_dir_all(&paths.PackagesDir).is_ok());
let locator = VelopackLocator::new(paths, Manifest::default());
let packages_dir = locator.get_packages_dir();
let beta_id_path = packages_dir.join(".betaId");
let expected_user_id = "test user id";
std::fs::write(&beta_id_path, expected_user_id).unwrap();
let staged_user_id = locator.get_staged_user_id();
assert_eq!(expected_user_id, staged_user_id);
if let Ok(beta_id) = std::fs::read_to_string(&beta_id_path) {
assert_eq!(staged_user_id, beta_id);
} else {
assert!(false, "Couldn't read staging userId.");
}
}
#[test]
fn test_locator_staged_id_for_existing_user() {
let tmp_dir = tempfile::TempDir::new().unwrap();
let tmp_buf = tmp_dir.path().to_path_buf();
let test_dir = tmp_buf.join(format!("velopack_{}", util::random_string(8)));
let mut paths = VelopackLocatorConfig::default();
paths.PackagesDir = test_dir;
//Esure the packages directory exists
assert!(std::fs::create_dir_all(&paths.PackagesDir).is_ok());
let locator = VelopackLocator::new(paths, Manifest::default());
let packages_dir = locator.get_packages_dir();
let beta_id_path = packages_dir.join(".betaId");
let expected_user_id = "test user id";
std::fs::write(&beta_id_path, expected_user_id).unwrap();
let staged_user_id = locator.get_staged_user_id();
assert_eq!(expected_user_id, staged_user_id);
}