use super::*;
fn variables(pairs: &[(&str, &Path)]) -> impl Fn(&str) -> Option<OsString> {
let (pathext, extensions) = crate::test_os::PATHEXT;
let pairs: Vec<(String, OsString)> = pairs
.iter()
.map(|(name, value)| (name.to_string(), value.as_os_str().to_owned()))
.chain([(pathext.to_owned(), OsString::from(extensions))])
.collect();
move |name| {
pairs
.iter()
.find(|(key, _)| key == name)
.map(|(_, value)| value.clone())
}
}
#[test]
fn the_app_beside_the_running_command_is_found_first() {
let root = crate::test_os::tempdir();
let bin = root.path().join("bin");
std::fs::create_dir(&bin).unwrap();
let app = crate::test_os::executable_named(&bin, PROGRAM);
let elsewhere = root.path().join("elsewhere");
std::fs::write(&elsewhere, "").unwrap();
assert_eq!(
find(
Some(&bin.join("artifactize")),
Some(root.path()),
variables(&[]),
vec![elsewhere],
),
Some(app)
);
}
#[test]
fn the_app_on_path_is_found_when_none_is_beside_the_command() {
let root = crate::test_os::tempdir();
let path = root.path().join("path");
std::fs::create_dir(&path).unwrap();
let app = path.join(PROGRAM);
std::fs::write(&app, "").unwrap();
crate::test_os::make_executable(&app);
let found = find(
Some(&root.path().join("artifactize")),
Some(root.path()),
variables(&[("PATH", &path)]),
Vec::new(),
)
.expect("the app on PATH");
assert_eq!(found.parent(), Some(path.as_path()));
}
#[test]
fn an_existing_install_location_is_found_and_absent_ones_are_not() {
let root = crate::test_os::tempdir();
let missing = root.path().join("missing");
let installed = root.path().join("Artifactize.app");
assert_eq!(
find(None, None, variables(&[]), vec![missing.clone()]),
None
);
std::fs::create_dir(&installed).unwrap();
assert_eq!(
find(None, None, variables(&[]), vec![missing, installed.clone()]),
Some(installed)
);
}
#[cfg(target_os = "macos")]
#[test]
fn mac_locations_are_the_system_and_home_applications_folders() {
let root = crate::test_os::tempdir();
let home = root.path();
assert_eq!(
locations(variables(&[]), Some(home)),
[
PathBuf::from("/Applications/Artifactize.app/Contents/MacOS/artifactize-desktop"),
home.join("Applications/Artifactize.app/Contents/MacOS/artifactize-desktop"),
]
);
}
#[cfg(target_os = "linux")]
#[test]
fn linux_locations_include_the_unpacked_app_and_the_recorded_appimage() {
let root = crate::test_os::tempdir();
let data = root.path().join("data");
let artifactize = data.join("artifactize");
let read = || locations(variables(&[("XDG_DATA_HOME", &data)]), Some(root.path()));
assert_eq!(read(), [artifactize.join("desktop/AppRun")]);
let appimage = root.path().join("Downloads/Artifactize.AppImage");
record_appimage(&artifactize, &appimage).unwrap();
record_appimage(&artifactize, &appimage).unwrap();
assert_eq!(read(), [artifactize.join("desktop/AppRun"), appimage]);
assert_eq!(
locations(
variables(&[("XDG_DATA_HOME", Path::new("relative"))]),
Some(root.path())
),
[root.path().join(".local/share/artifactize/desktop/AppRun")]
);
}
#[cfg(windows)]
#[test]
fn windows_locations_are_the_install_folder_and_the_store_package() {
let root = crate::test_os::tempdir();
let local = root.path();
assert_eq!(
locations(variables(&[("LOCALAPPDATA", local)]), None),
[
local.join("Programs/artifactize/artifactize-desktop.exe"),
local
.join("Microsoft/WindowsApps")
.join(STORE_PACKAGE_FAMILY),
]
);
}