Skip to content

Commit

Permalink
Merge pull request rust-lang#276 from NobodyXu/fix
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodyXu committed Aug 4, 2022
2 parents 346bb8e + 7156638 commit d2ab613
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main.rs
@@ -1,5 +1,6 @@
use std::{
ffi::OsString,
fs,
mem::take,
path::{Path, PathBuf},
process::{ExitCode, Termination},
Expand Down Expand Up @@ -295,6 +296,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
error!("No viable install path found of specified, try `--install-path`");
miette!("No install path found or specified")
})?;
fs::create_dir_all(&install_path).map_err(BinstallError::Io)?;
debug!("Using install path: {}", install_path.display());

// Create a temporary directory for downloads etc.
Expand Down Expand Up @@ -393,6 +395,10 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {

block_in_place(|| {
if !custom_install_path {
// If using standardised install path,
// then create_dir_all(&install_path) would also
// create .cargo.

debug!("Writing .crates.toml");
metafiles::v1::CratesToml::append(metadata_vec.iter())?;

Expand Down
35 changes: 34 additions & 1 deletion src/metafiles/v1.rs
Expand Up @@ -74,7 +74,11 @@ impl CratesToml {
Iter: IntoIterator<Item = &'a MetaData>,
{
let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
let mut c1 = Self::load_from_reader(&mut *file)?;
let mut c1 = if file.metadata()?.len() != 0 {
Self::load_from_reader(&mut *file)?
} else {
Self::default()
};

for metadata in iter {
c1.insert(&CrateVersionSource::from(metadata), metadata.bins.clone());
Expand Down Expand Up @@ -108,3 +112,32 @@ pub enum CratesTomlParseError {
#[error(transparent)]
CvsParse(#[from] super::CvsParseError),
}

#[cfg(test)]
mod tests {
use super::{super::binstall_v1, *};
use crate::target::TARGET;

use semver::Version;
use tempfile::TempDir;

#[test]
fn test_empty() {
let tempdir = TempDir::new().unwrap();
let path = tempdir.path().join("crates-v1.toml");

CratesToml::append_to_path(
&path,
&[MetaData {
name: "cargo-binstall".into(),
version_req: "*".into(),
current_version: Version::new(0, 11, 1),
source: binstall_v1::Source::cratesio_registry(),
target: TARGET.into(),
bins: vec!["cargo-binstall".into()],
other: Default::default(),
}],
)
.unwrap();
}
}

0 comments on commit d2ab613

Please sign in to comment.