Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support XDG_CONFIG_HOME for receipts #171

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 103 additions & 9 deletions axoupdater-cli/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ fn install_receipt(version: &str, cargo_dist_version: &str, prefix: &Utf8PathBuf
fn write_receipt(
version: &str,
cargo_dist_version: &str,
prefix: &Utf8PathBuf,
receipt_prefix: &Utf8PathBuf,
install_prefix: &Utf8PathBuf,
) -> std::io::Result<()> {
let contents = install_receipt(version, cargo_dist_version, prefix);
let receipt_name = prefix.join("axolotlsay-receipt.json");
// Create the prefix in case it doesn't exist
LocalAsset::create_dir_all(receipt_prefix).unwrap();

let contents = install_receipt(version, cargo_dist_version, install_prefix);
let receipt_name = receipt_prefix.join("axolotlsay-receipt.json");
LocalAsset::write_new(&contents, receipt_name).unwrap();

Ok(())
Expand Down Expand Up @@ -94,7 +98,12 @@ fn test_upgrade() -> std::io::Result<()> {
.unwrap();

// Write the receipt for the updater to use
write_receipt(base_version, "0.11.1", &bindir.to_path_buf())?;
write_receipt(
base_version,
"0.11.1",
&bindir.to_path_buf(),
&bindir.to_path_buf(),
)?;

LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

Expand Down Expand Up @@ -133,6 +142,71 @@ fn test_upgrade() -> std::io::Result<()> {
Ok(())
}

#[test]
fn test_upgrade_xdg_config_home() -> std::io::Result<()> {
let tempdir = TempDir::new()?;
let bindir_path = &tempdir.path().join("bin");
let bindir = Utf8Path::from_path(bindir_path).unwrap();
std::fs::create_dir_all(bindir)?;
let xdg_config_home = tempdir.path().join("config");
let xdg_config_home = Utf8Path::from_path(&xdg_config_home).unwrap();

let base_version = "0.2.115";

let url = axolotlsay_tarball_path(base_version);
let compressed_path =
Utf8PathBuf::from_path_buf(tempdir.path().join("axolotlsay.tar.gz")).unwrap();

let client = axoasset::AxoClient::with_reqwest(axoasset::reqwest::Client::new());
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(client.load_and_write_to_file(&url, &compressed_path))
.unwrap();

// Write the receipt for the updater to use
write_receipt(
base_version,
"0.11.1",
&xdg_config_home.join("axolotlsay"),
&bindir.to_path_buf(),
)?;

LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

// Now install our copy of the updater instead of the one axolotlsay came with
let updater_path = bindir.join(format!("axolotlsay-update{EXE_SUFFIX}"));
std::fs::copy(BIN, &updater_path)?;

let mut updater = Cmd::new(&updater_path, "run updater");
updater.env("XDG_CONFIG_HOME", xdg_config_home);
// If we're not running in CI, try to avoid ruining the user's PATH.
if std::env::var("CI").is_err() {
updater.env("INSTALLER_NO_MODIFY_PATH", "1");
updater.env("AXOLOTLSAY_NO_MODIFY_PATH", "1");
}
// We'll do that manually
updater.check(false);
let res = updater.output().unwrap();
let output_stdout = String::from_utf8(res.stdout).unwrap();
let output_stderr = String::from_utf8(res.stderr).unwrap();

// Now let's check the version we just updated to
let new_axolotlsay_path = &bindir.join(format!("axolotlsay{EXE_SUFFIX}"));
assert!(
new_axolotlsay_path.exists(),
"update result was\nstdout\n{}\nstderr\n{}",
output_stdout,
output_stderr
);
let mut new_axolotlsay = Cmd::new(new_axolotlsay_path, "version test");
new_axolotlsay.arg("--version");
let output = new_axolotlsay.output().unwrap();
let stderr_string = String::from_utf8(output.stdout).unwrap();
assert!(stderr_string.starts_with("axolotlsay "));
assert_ne!(stderr_string, format!("axolotlsay {}\n", base_version));

Ok(())
}

#[test]
fn test_upgrade_allow_prerelease() -> std::io::Result<()> {
let tempdir = TempDir::new()?;
Expand All @@ -152,7 +226,12 @@ fn test_upgrade_allow_prerelease() -> std::io::Result<()> {
.unwrap();

// Write the receipt for the updater to use
write_receipt(base_version, "0.11.1", &bindir.to_path_buf())?;
write_receipt(
base_version,
"0.11.1",
&bindir.to_path_buf(),
&bindir.to_path_buf(),
)?;

LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

Expand Down Expand Up @@ -214,7 +293,12 @@ fn test_upgrade_to_specific_version() -> std::io::Result<()> {
.unwrap();

// Write the receipt for the updater to use
write_receipt(base_version, "0.11.1", &bindir.to_path_buf())?;
write_receipt(
base_version,
"0.11.1",
&bindir.to_path_buf(),
&bindir.to_path_buf(),
)?;

LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

Expand Down Expand Up @@ -268,7 +352,12 @@ fn test_downgrade_to_specific_version() -> std::io::Result<()> {
.unwrap();

// Write the receipt for the updater to use
write_receipt(base_version, "0.11.1", &bindir.to_path_buf())?;
write_receipt(
base_version,
"0.11.1",
&bindir.to_path_buf(),
&bindir.to_path_buf(),
)?;

LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

Expand Down Expand Up @@ -335,7 +424,12 @@ fn test_downgrade_to_specific_old_version() -> std::io::Result<()> {
.unwrap();

// Write the receipt for the updater to use
write_receipt(base_version, "0.11.1", &bindir.to_path_buf())?;
write_receipt(
base_version,
"0.11.1",
&bindir.to_path_buf(),
&bindir.to_path_buf(),
)?;

LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

Expand Down Expand Up @@ -396,7 +490,7 @@ fn test_upgrade_from_prefix_with_no_bin() -> std::io::Result<()> {
// Write the receipt for the updater to use
// 0.15.0 is the first cargo-dist that published fixed installers for the
// /bin bug mentioned above
write_receipt(base_version, "0.15.0", &prefix)?;
write_receipt(base_version, "0.15.0", &prefix, &prefix)?;

LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

Expand Down
10 changes: 10 additions & 0 deletions axoupdater/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ pub enum AxoupdateError {
app_name: String,
},

/// Not a generic receipt load failure, but the receipt itself doesn't exist.
#[error("Unable to load receipt for app {app_name}")]
#[diagnostic(help(
"This may indicate that this installation of {app_name} was installed via a method that's not eligible for upgrades."
))]
NoReceipt {
/// This app's name
app_name: String,
},

/// Indicates that this app's name couldn't be determined when trying
/// to autodetect it.
#[error("Unable to determine the name of the app to update")]
Expand Down
55 changes: 43 additions & 12 deletions axoupdater/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,38 +116,69 @@ impl AxoUpdater {
}
}

pub(crate) fn get_config_path(app_name: &str) -> AxoupdateResult<Utf8PathBuf> {
/// Returns a Vec of possible receipt locations, beginning with
/// `XDG_CONFIG_HOME` (if set).
pub(crate) fn get_config_paths(app_name: &str) -> AxoupdateResult<Vec<Utf8PathBuf>> {
let mut potential_homes = vec![];

if env::var("AXOUPDATER_CONFIG_WORKING_DIR").is_ok() {
Ok(Utf8PathBuf::try_from(current_dir()?)?)
Ok(vec![Utf8PathBuf::try_from(current_dir()?)?])
} else if let Ok(path) = env::var("AXOUPDATER_CONFIG_PATH") {
Ok(Utf8PathBuf::from(path))
Ok(vec![Utf8PathBuf::from(path)])
} else {
let xdg_home = env::var("XDG_CONFIG_HOME")
.ok()
.map(Utf8PathBuf::from)
.map(|h| h.join(app_name));
if let Some(home) = &xdg_home {
if home.exists() {
potential_homes.push(home.to_owned());
}
}
let home = if cfg!(windows) {
env::var("LOCALAPPDATA").map(PathBuf::from).ok()
env::var("LOCALAPPDATA")
.map(PathBuf::from)
.map(|h| h.join(app_name))
.ok()
} else {
homedir::my_home()?.map(|path| path.join(".config"))
homedir::my_home()?.map(|path| path.join(".config").join(app_name))
};
let Some(home) = home else {
if let Some(home) = home {
potential_homes.push(Utf8PathBuf::try_from(home)?);
}

if potential_homes.is_empty() {
return Err(AxoupdateError::NoHome {});
};
}

Ok(Utf8PathBuf::try_from(home)?.join(app_name))
Ok(potential_homes)
}
}

/// Iterates through the list of possible receipt locations from
/// `get_config_paths` and returns the first that contains a valid receipt.
pub(crate) fn get_receipt_path(app_name: &str) -> AxoupdateResult<Option<Utf8PathBuf>> {
for receipt_prefix in get_config_paths(app_name)? {
let install_receipt_path = receipt_prefix.join(format!("{app_name}-receipt.json"));
if install_receipt_path.exists() {
return Ok(Some(install_receipt_path));
}
}

Ok(None)
}

fn load_receipt_from_path(install_receipt_path: &Utf8PathBuf) -> AxoupdateResult<InstallReceipt> {
Ok(SourceFile::load_local(install_receipt_path)?.deserialize_json()?)
}

fn load_receipt_for(app_name: &str) -> AxoupdateResult<InstallReceipt> {
let Ok(receipt_prefix) = get_config_path(app_name) else {
return Err(AxoupdateError::ConfigFetchFailed {
let Some(install_receipt_path) = get_receipt_path(app_name)? else {
return Err(AxoupdateError::NoReceipt {
app_name: app_name.to_owned(),
});
};

let install_receipt_path = receipt_prefix.join(format!("{app_name}-receipt.json"));

load_receipt_from_path(&install_receipt_path).map_err(|_| AxoupdateError::ReceiptLoadFailed {
app_name: app_name.to_owned(),
})
Expand Down
7 changes: 5 additions & 2 deletions axoupdater/src/test/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
process::{Command, Stdio},
};

use crate::{receipt::get_config_path, ReleaseSourceType};
use crate::{receipt::get_receipt_path, ReleaseSourceType};

static RECEIPT_TEMPLATE: &str = r#"{"binaries":[BINARIES],"install_prefix":"INSTALL_PREFIX","provider":{"source":"cargo-dist","version":"0.10.0-prerelease.1"},"source":{"app_name":"APP_NAME","name":"PACKAGE","owner":"OWNER","release_type":"RELEASE_TYPE"},"version":"VERSION"}"#;

Expand Down Expand Up @@ -104,7 +104,10 @@ pub fn perform_runtest(runtest_args: &RuntestArgs) -> PathBuf {
let app_home = &home.join(".cargo").join("bin");
let app_path = &app_home.join(basename);

let config_path = get_config_path(app_name).unwrap().into_std_path_buf();
let config_path = get_receipt_path(app_name)
.unwrap()
.unwrap()
.into_std_path_buf();

// Ensure we delete any previous copy that may exist
// at this path before we copy in our version.
Expand Down
Loading