Skip to content

Commit

Permalink
add one more test case
Browse files Browse the repository at this point in the history
Signed-off-by: Soc Virnyl Estela <[email protected]>
  • Loading branch information
uncomfyhalomacro committed Nov 5, 2024
1 parent 1e3aec2 commit e0349e1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cargo/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 19 additions & 9 deletions cargo/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<PathBuf> = Vec::new();
for manifest in &registry.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 {
Expand Down Expand Up @@ -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,
)?;
Expand All @@ -87,18 +91,21 @@ 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,
"🔒 👀 Found an extra lockfile. Adding it to home registry for vendoring."
);
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."
Expand All @@ -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,
Expand Down
73 changes: 72 additions & 1 deletion cargo/tests/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(())
}

0 comments on commit e0349e1

Please sign in to comment.