#[path = "../src/test_helpers.rs"]
mod test_helpers;
use std::io::prelude::*;
use std::path::PathBuf;
use std::process::Command;
use crate::test_helpers::bin_path;
use regex::Regex;
#[allow(non_snake_case)]
#[test]
#[cfg_attr(feature = "offline_tests", ignore)]
fn alternative_registry_works() {
let cargo_home_path = {
let mut path = PathBuf::from("target");
path.push("alt_reg_cloudsmith_CARGO_HOME");
path
};
std::fs::create_dir_all(&cargo_home_path)
.expect("Failed to create 'alt_reg_cloudsmith_CARGO_HOME' dir");
let cargo_config_file_path: PathBuf = {
let mut path = cargo_home_path.clone();
path.push("config");
path
};
println!(
"DEBUG: cargo config file path: '{:?}'",
cargo_config_file_path
);
let mut cfg_file_handle = std::fs::File::create(&cargo_config_file_path)
.expect("failed to create cargo home config file!");
let config_text = String::from(
r#"[registries]
cloudsmith = { index = "https://dl.cloudsmith.io/public/matthias-kruger/ccart/cargo/index.git" }
"#,
);
println!("DEBUG: config text:\n{}\n", config_text);
cfg_file_handle
.write_all(config_text.as_bytes())
.expect("failed to fill cargo home config file");
let project_path = {
let mut path = PathBuf::from("tests");
path.push("cloudsmith_registry_test");
path.push("use_cloudsmith_dep");
path
};
let cargo_home_path_absolute: PathBuf = {
let mut path: PathBuf = std::env::current_dir().unwrap();
path.push(cargo_home_path);
path
};
let fetch_cmd = Command::new("cargo")
.arg("fetch")
.current_dir(&project_path)
.env("CARGO_HOME", cargo_home_path_absolute.display().to_string())
.output()
.unwrap();
let status = fetch_cmd.status;
let stderr = String::from_utf8_lossy(&fetch_cmd.stderr).to_string();
let stdout = String::from_utf8_lossy(&fetch_cmd.stdout).to_string();
if !fetch_cmd.status.success() {
println!("error while cargo building test crate");
println!("stderr:\n{:?}", stderr);
println!("stdout:\n{:?}", stdout);
println!("status: {:?}", status);
panic!("error while building test crate");
}
println!("ERR {:?}", stderr);
println!("OUT {:?}", stdout);
let cargo_cache_cmd = Command::new(bin_path())
.env("CARGO_HOME", cargo_home_path_absolute.display().to_string())
.env("RUST_BACKTRACE", "1")
.output()
.unwrap();
if !cargo_cache_cmd.status.success() {
println!("error running cargo-cache on alt reg $CARGO_HOME");
println!("stderr:\n{:?}", stderr);
println!("stdout:\n{:?}", stdout);
println!("status: {:?}", status);
panic!("error while running cargo-home with alt regs");
}
let stdout = String::from_utf8_lossy(&cargo_cache_cmd.stdout).to_string();
println!("DEBUG: cargo-cache output:\n\n{}", stdout);
let mut desired_output =
String::from("Cargo cache .*target.*alt_reg_cloudsmith_CARGO_HOME.*\n\n");
desired_output.push_str(
"Total: .* MB
0 installed binaries: .* 0 B
Registry: .* MB
2 registry indices: .* MB
2 crate archives: .* KB
2 crate source checkouts: .* KB
Git db: .* 0 B
0 bare git repos: .* 0 B
0 git repo checkouts: .* 0 B",
);
let regex = Regex::new(&desired_output).unwrap();
assert!(
regex.is_match(&stdout),
"ERROR: regex did not match!\n\nregex:\n{:?}\n\ncc_output:\n{:?}",
regex,
stdout
);
let cargo_cache_registry_cmd = Command::new(bin_path())
.arg("registry")
.env("CARGO_HOME", cargo_home_path_absolute.display().to_string())
.output()
.unwrap();
if !cargo_cache_registry_cmd.status.success() {
println!("error running cargo-cache on alt reg $CARGO_HOME");
println!("stderr:\n{:?}", stderr);
println!("stdout:\n{:?}", stdout);
println!("status: {:?}", status);
panic!("error while running cargo-home with alt regs");
}
let stdout = String::from_utf8_lossy(&cargo_cache_registry_cmd.stdout).to_string();
println!("DEBUG: cargo-cache output:\n\n{}", stdout);
let mut desired_output =
String::from("Cargo cache .*target.*alt_reg_cloudsmith_CARGO_HOME.*\n\n");
desired_output.push_str(
"Total: .* MB
0 installed binaries: .* 0 B
Registry: dl.cloudsmith.io .* KB
Registry index: .* KB
1 crate archives: .* B
1 crate source checkouts: .* KB
Registry: github.com .* MB
Registry index: .* MB
1 crate archives: .* KB
1 crate source checkouts: .* KB
Git db: .* 0 B
0 bare git repos: .* 0 B
0 git repo checkouts: .* 0 B",
);
let regex = Regex::new(&desired_output).unwrap();
assert!(
regex.is_match(&stdout),
"ERROR: regex did not match!\n\nregex:\n{:?}\n\ncc_output:\n{:?}",
regex,
stdout
);
}