Add apply Rust test

This commit is contained in:
Caelan Sayler
2024-01-03 22:20:32 +00:00
parent 07fca1a5f5
commit fdfcfe1160
5 changed files with 44 additions and 11 deletions

View File

@@ -8,14 +8,20 @@ use glob::glob;
use runas::Command as RunAsCommand;
use std::path::PathBuf;
pub fn apply<'a>(restart: bool, wait_for_parent: bool, package: Option<&PathBuf>, exe_args: Option<Vec<&str>>, noelevate: bool) -> Result<()> {
pub fn apply<'a>(
root_path: &PathBuf,
app: &Manifest,
restart: bool,
wait_for_parent: bool,
package: Option<&PathBuf>,
exe_args: Option<Vec<&str>>,
noelevate: bool,
) -> Result<()> {
if wait_for_parent {
let _ = shared::wait_for_parent_to_exit(60_000); // 1 minute
}
let (root_path, app) = shared::detect_current_manifest()?;
if let Err(e) = apply_package(package, &app, &root_path, noelevate, restart, exe_args.clone()) {
if let Err(e) = apply_package(&root_path, &app, restart, package, exe_args.clone(), noelevate) {
error!("Error applying package: {}", e);
if !restart {
return Err(e);
@@ -30,7 +36,14 @@ pub fn apply<'a>(restart: bool, wait_for_parent: bool, package: Option<&PathBuf>
Ok(())
}
fn apply_package<'a>(package: Option<&PathBuf>, app: &Manifest, root_path: &PathBuf, noelevate: bool, restart: bool, exe_args: Option<Vec<&str>>) -> Result<()> {
fn apply_package<'a>(
root_path: &PathBuf,
app: &Manifest,
restart: bool,
package: Option<&PathBuf>,
exe_args: Option<Vec<&str>>,
noelevate: bool,
) -> Result<()> {
let mut package_manifest: Option<Manifest> = None;
let mut package_bundle: Option<BundleInfo<'a>> = None;

View File

@@ -135,7 +135,8 @@ fn apply(matches: &ArgMatches) -> Result<()> {
info!(" Exe Args: {:?}", exe_args);
info!(" No Elevate: {}", noelevate);
commands::apply(restart, wait_for_parent, package, exe_args, noelevate)
let (root_path, app) = shared::detect_current_manifest()?;
commands::apply(&root_path, &app, restart, wait_for_parent, package, exe_args, noelevate)
}
#[cfg(target_os = "windows")]

View File

@@ -34,14 +34,26 @@ pub fn run_hook(app: &shared::bundle::Manifest, root_path: &PathBuf, hook_name:
let _ = shared::force_stop_package(&root_path);
}
pub fn create_global_mutex(app: &shared::bundle::Manifest) -> Result<Foundation::HANDLE> {
pub struct MutexDropGuard {
mutex: Foundation::HANDLE,
}
impl Drop for MutexDropGuard {
fn drop(&mut self) {
unsafe {
Foundation::CloseHandle(self.mutex).ok();
}
}
}
pub fn create_global_mutex(app: &shared::bundle::Manifest) -> Result<MutexDropGuard> {
let mutex_name = format!("velopack-{}", &app.id);
info!("Attempting to open global system mutex: '{}'", &mutex_name);
let encoded = mutex_name.encode_utf16().chain([0u16]).collect::<Vec<u16>>();
let pw = PCWSTR(encoded.as_ptr());
let mutex = unsafe { CreateMutexW(None, true, pw) }?;
match unsafe { GetLastError() } {
Ok(_) => Ok(mutex),
Ok(_) => Ok(MutexDropGuard { mutex }),
Err(err) => {
if err == Foundation::ERROR_ALREADY_EXISTS.into() {
bail!("Another installer or updater for this application is running, quit that process and try again.");
@@ -387,4 +399,4 @@ fn test_authenticode() {
assert!(verify_authenticode_against_powershell(r"C:\Users\Caelan\AppData\Local\Programs\Microsoft VS Code\Code.exe"));
assert!(!verify_authenticode_against_powershell(r"C:\Users\Caelan\AppData\Local\Clowd\Update.exe"));
assert!(!verify_authenticode_against_powershell(r"C:\Users\Caelan\.cargo\bin\cargo.exe"));
}
}

View File

@@ -9,8 +9,7 @@ use winsafe::{self as w, co};
#[cfg(target_os = "windows")]
#[test]
pub fn test_install_uninstall() {
logging::trace_logger();
pub fn test_install_apply_uninstall() {
dialogs::set_silent(true);
let fixtures = find_fixtures();
@@ -36,6 +35,14 @@ pub fn test_install_uninstall() {
let (root_dir, app) = shared::detect_manifest_from_update_path(&tmp_buf.join("Update.exe")).unwrap();
assert_eq!(app_id, app.id);
assert!(semver::Version::parse("1.0.11").unwrap() == app.version);
let pkg_name_apply = "AvaloniaCrossPlat-1.0.15-win-full.nupkg";
let nupkg_apply = fixtures.join(pkg_name_apply);
commands::apply(&root_dir, &app, false, false, Some(&nupkg_apply), None, true).unwrap();
let (root_dir, app) = shared::detect_manifest_from_update_path(&tmp_buf.join("Update.exe")).unwrap();
assert!(semver::Version::parse("1.0.15").unwrap() == app.version);
commands::uninstall(&root_dir, &app, false).unwrap();
assert!(!tmp_buf.join("current").exists());

Binary file not shown.