Skip to content

Commit

Permalink
Fix issue with cargo version parsing (#9)
Browse files Browse the repository at this point in the history
Non-official cargo builds may have differing output for `cargo -V` that
the previous code wasn't accounting for, the output is now properly
trimmed to remove excess whitespace before attempting to parse it into a
semver.

Resolves: #8
  • Loading branch information
Jake-Shadle authored Aug 2, 2023
1 parent cc79d24 commit 46384ce
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- next-header -->
## [Unreleased] - ReleaseDate
### Fixed
- [PR#9](https://github.com/EmbarkStudios/tame-index/pull/9) resolved [#8](https://github.com/EmbarkStudios/tame-index/issues/8) by ensuring (valid) non-official cargo build version output can also be parsed.

## [0.2.4] - 2023-07-28
### Fixed
- [PR#7](https://github.com/EmbarkStudios/tame-index/pull/7) fixed an issue where `RemoteGitIndex::fetch` could fail in environments where the git committer was not configured.
Expand Down
7 changes: 3 additions & 4 deletions src/index/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ impl<'iu> IndexUrl<'iu> {
if let Some(si) = sparse_index {
si
} else {
let semver = match cargo_version {
Some(v) => std::borrow::Cow::Borrowed(v),
None => crate::utils::cargo_version(None)?.into(),
let vers = match cargo_version {
Some(v) => v.trim().parse()?,
None => crate::utils::cargo_version(None)?,
};

let vers: semver::Version = semver.parse()?;
vers >= semver::Version::new(1, 70, 0)
}
}
Expand Down
53 changes: 41 additions & 12 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,28 @@ pub fn get_index_details(url: &str, root: Option<PathBuf>) -> Result<(PathBuf, S
Ok((path, url_dir.canonical))
}

/// Retrieves the current version of cargo being used
pub fn cargo_version(working_dir: Option<&crate::Path>) -> Result<String, Error> {
use std::io;
use std::io;

/// Parses the output of `cargo -V` to get the semver
///
/// This handles the 2? cases that I am aware of
///
/// 1. Official cargo prints `cargo <semver>(?:-<channel>)? (<sha1[..7]> <date>)`
/// 2. Non-official builds may drop the additional metadata and just print `cargo <semver>`
#[inline]
fn parse_cargo_semver(s: &str) -> Result<semver::Version, Error> {
let semver = s.trim().split(' ').nth(1).ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
"cargo version information was in an invalid format",
)
})?;

Ok(semver.parse()?)
}

/// Retrieves the current version of cargo being used
pub fn cargo_version(working_dir: Option<&crate::Path>) -> Result<semver::Version, Error> {
let mut cargo = std::process::Command::new(
std::env::var_os("CARGO")
.as_deref()
Expand All @@ -244,14 +262,7 @@ pub fn cargo_version(working_dir: Option<&crate::Path>) -> Result<String, Error>
let stdout = String::from_utf8(output.stdout)
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;

let semver = stdout.split(' ').nth(1).ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
"cargo version information was in an invalid format",
)
})?;

Ok(semver.to_owned())
parse_cargo_semver(&stdout)
}

#[cfg(test)]
Expand Down Expand Up @@ -337,7 +348,25 @@ mod test {
#[test]
fn gets_cargo_version() {
const MINIMUM: semver::Version = semver::Version::new(1, 70, 0);
let version: semver::Version = super::cargo_version(None).unwrap().parse().unwrap();
let version = super::cargo_version(None).unwrap();
assert!(version >= MINIMUM);
}

#[test]
fn parses_cargo_semver() {
use super::parse_cargo_semver as pcs;

assert_eq!(
pcs("cargo 1.71.0 (cfd3bbd8f 2023-06-08)\n").unwrap(),
semver::Version::new(1, 71, 0)
);
assert_eq!(
pcs("cargo 1.73.0-nightly (7ac9416d8 2023-07-24)\n").unwrap(),
"1.73.0-nightly".parse().unwrap()
);
assert_eq!(
pcs("cargo 1.70.0\n").unwrap(),
semver::Version::new(1, 70, 0)
);
}
}

0 comments on commit 46384ce

Please sign in to comment.