diff --git a/cargo/src/cli.rs b/cargo/src/cli.rs index 8e73d3f..ebe7fc4 100644 --- a/cargo/src/cli.rs +++ b/cargo/src/cli.rs @@ -200,7 +200,9 @@ impl Opts { if setup_workdir.exists() && setup_workdir.is_dir() { match &self.method { - Method::Registry => run_cargo_vendor_home_registry(&custom_root, self), + Method::Registry => { + run_cargo_vendor_home_registry(&setup_workdir, &custom_root, self) + } Method::Vendor => run_cargo_vendor(&setup_workdir, &custom_root, self), }?; } else { diff --git a/cargo/src/registry.rs b/cargo/src/registry.rs index f7fca94..04808c1 100644 --- a/cargo/src/registry.rs +++ b/cargo/src/registry.rs @@ -13,7 +13,11 @@ use crate::audit; use crate::cargo_commands::*; use crate::cli::Opts; -pub fn run_cargo_vendor_home_registry(setup_workdir: &Path, registry: &Opts) -> io::Result<()> { +pub fn run_cargo_vendor_home_registry( + setup_workdir: &Path, + custom_root: &Path, + registry: &Opts, +) -> io::Result<()> { debug!(?registry); info!("๐Ÿ›–๐Ÿƒ๐Ÿ“ฆ Starting Cargo Vendor Home Registry"); let tempdir_for_home_registry_binding = tempfile::Builder::new() @@ -26,19 +30,19 @@ pub fn run_cargo_vendor_home_registry(setup_workdir: &Path, registry: &Opts) -> debug!(?home_registry_dot_cargo); if !registry.no_root_manifest { if registry.update { - cargo_update(setup_workdir, "")?; + cargo_update(custom_root, "")?; } info!(?setup_workdir, "๐ŸŒณ Finished setting up workdir."); info!("๐Ÿ”“Attempting to regenerate lockfile..."); - cargo_generate_lockfile(setup_workdir, "", registry.update)?; + cargo_generate_lockfile(custom_root, "", registry.update)?; info!("๐Ÿ”’Regenerated lockfile."); info!("๐Ÿš Attempting to fetch dependencies."); - cargo_fetch(setup_workdir, "", registry.update)?; + cargo_fetch(custom_root, "", registry.update)?; info!("๐Ÿ’ผ Fetched dependencies."); } let mut lockfiles: Vec = Vec::new(); for manifest in ®istry.manifest_path { - let full_manifest_path = &setup_workdir.join(manifest); + let full_manifest_path = &custom_root.join(manifest); let full_manifest_path_parent = full_manifest_path.parent().unwrap_or(setup_workdir); if full_manifest_path.is_file() { if registry.update { @@ -73,7 +77,7 @@ pub fn run_cargo_vendor_home_registry(setup_workdir: &Path, registry: &Opts) -> "๐Ÿš Attempting to fetch dependencies at extra manifest path..." ); cargo_fetch( - setup_workdir, + custom_root, &full_manifest_path.to_string_lossy(), registry.update, )?; @@ -87,6 +91,9 @@ pub fn run_cargo_vendor_home_registry(setup_workdir: &Path, registry: &Opts) -> return Err(err); } let possible_lockfile = full_manifest_path_parent.join("Cargo.lock"); + let possible_lockfile = &possible_lockfile + .canonicalize() + .unwrap_or(possible_lockfile.to_path_buf()); if possible_lockfile.exists() { info!( ?possible_lockfile, @@ -94,11 +101,11 @@ pub fn run_cargo_vendor_home_registry(setup_workdir: &Path, registry: &Opts) -> ); let stripped_lockfile_path = possible_lockfile .strip_prefix(setup_workdir) - .unwrap_or(&possible_lockfile); + .unwrap_or(possible_lockfile); let new_lockfile_path = &home_registry.join(stripped_lockfile_path); let new_lockfile_parent = new_lockfile_path.parent().unwrap_or(home_registry); fs::create_dir_all(new_lockfile_parent)?; - fs::copy(&possible_lockfile, new_lockfile_path)?; + fs::copy(possible_lockfile, new_lockfile_path)?; info!( ?possible_lockfile, "๐Ÿ”’ ๐ŸŒŸ Successfully added extra lockfile." @@ -107,7 +114,10 @@ pub fn run_cargo_vendor_home_registry(setup_workdir: &Path, registry: &Opts) -> } } if !registry.no_root_manifest { - let possible_root_lockfile = &setup_workdir.join("Cargo.lock"); + let possible_root_lockfile = &custom_root.join("Cargo.lock"); + let possible_root_lockfile = &possible_root_lockfile + .canonicalize() + .unwrap_or(possible_root_lockfile.to_path_buf()); if possible_root_lockfile.exists() { info!( ?possible_root_lockfile, diff --git a/cargo/tests/behaviour.rs b/cargo/tests/behaviour.rs index a834d03..6973f00 100644 --- a/cargo/tests/behaviour.rs +++ b/cargo/tests/behaviour.rs @@ -261,7 +261,7 @@ async fn manifest_paths_with_vendor() -> io::Result<()> { } #[test(tokio::test)] -async fn custom_root_test() -> io::Result<()> { +async fn custom_root_test_1() -> io::Result<()> { let source = "https://github.com/influxdata/flux/archive/refs/tags/v0.194.4.tar.gz"; let mut rng = rand::thread_rng(); let random_tag: u8 = rng.gen(); @@ -328,3 +328,74 @@ async fn custom_root_test() -> io::Result<()> { assert!(cargo_lock_path.is_file()); Ok(()) } + +#[test(tokio::test)] +async fn custom_root_test_2() -> io::Result<()> { + let source = "https://github.com/influxdata/flux/archive/refs/tags/v0.194.4.tar.gz"; + let mut rng = rand::thread_rng(); + let random_tag: u8 = rng.gen(); + let random_tag = random_tag.to_string(); + let response = reqwest::get(source).await.unwrap(); + let fname = response + .url() + .path_segments() + .and_then(|segments| segments.last()) + .and_then(|name| if name.is_empty() { None } else { Some(name) }) + .unwrap_or("balls"); + info!("Source file: {}", &fname); + let outfile = format!("/{}/{}", "tmp", &fname); + info!("Downloaded to: '{:?}'", &outfile); + fs::File::create(&outfile).await.unwrap(); + let outfile = PathBuf::from(&outfile); + let data = response.bytes().await.unwrap(); + let data = data.to_vec(); + fs::write(&outfile, data).await.unwrap(); + let outdir = PathBuf::from("/tmp"); + let vendor_specific_args = VendorArgs { + filter: false, + versioned_dirs: true, + }; + let opt = cli::Opts { + no_root_manifest: false, + custom_root: Some("libflux".to_string()), + method: Method::Registry, + src: outfile.to_path_buf(), + compression: Compression::default(), + tag: Some(random_tag.clone()), + manifest_path: vec![], + update: true, + outdir: outdir.to_path_buf(), + color: clap::ColorChoice::Auto, + i_accept_the_risk: vec![], + vendor_specific_args, + }; + + let res = opt.run_vendor(); + assert!(res.is_ok()); + let vendor_tarball = match opt.method { + Method::Registry => format!("registry-{}.tar.zst", &random_tag), + Method::Vendor => format!("vendor-{}.tar.zst", &random_tag), + }; + + let vendor_tarball_path = &outdir.join(vendor_tarball); + assert!(vendor_tarball_path.is_file()); + + let raw_outdir = PathBuf::from("/tmp").join(random_tag).join("output"); + let raw_args = RawArgs { + target: vendor_tarball_path.to_path_buf(), + outdir: Some(raw_outdir.clone()), + }; + raw_opts(raw_args, false)?; + let vendor_path = raw_outdir.join("libflux").join("vendor"); + let cargo_config_path = raw_outdir + .join("libflux") + .join(".cargo") + .join("config.toml"); + let cargo_lock_path = raw_outdir.join("libflux").join("Cargo.lock"); + let cargo_registry_path = raw_outdir.join(".cargo").join("registry"); + assert!(!vendor_path.is_dir()); + assert!(!cargo_config_path.is_file()); + assert!(cargo_lock_path.is_file()); + assert!(cargo_registry_path.is_dir()); + Ok(()) +}