use std::fs;
use std::process::ExitCode;
use tempfile::TempDir;
use callisto_cli::cli::{GlobalArgs, OutputFormat, SnapshotArgs, TagArgs, VersionArgs};
use callisto_cli::commands;
fn make_git_workspace(tmp: &TempDir) -> GlobalArgs {
let root = tmp.path();
drop(
std::process::Command::new("git")
.args(["init", "-q"])
.current_dir(root)
.output(),
);
drop(
std::process::Command::new("git")
.args(["config", "user.name", "Test"])
.current_dir(root)
.output(),
);
drop(
std::process::Command::new("git")
.args(["config", "user.email", "test@test.dev"])
.current_dir(root)
.output(),
);
fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/pkg-a\"]\nresolver = \"2\"\n",
)
.unwrap();
let pkg = root.join("crates/pkg-a");
fs::create_dir_all(&pkg).unwrap();
fs::write(
pkg.join("Cargo.toml"),
"[package]\nname = \"pkg-a\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
let global = GlobalArgs {
format: OutputFormat::Json,
cwd: root.to_path_buf(),
dry_run: true, };
commands::init::handle(callisto_cli::cli::InitArgs { yes: true }, &global).unwrap();
drop(
std::process::Command::new("git")
.args(["add", "."])
.current_dir(root)
.output(),
);
drop(
std::process::Command::new("git")
.args(["commit", "-m", "initial", "--allow-empty"])
.current_dir(root)
.output(),
);
global
}
#[test]
fn test_version_strict_no_changesets_exits_nonzero() {
let tmp = TempDir::new().unwrap();
let global = make_git_workspace(&tmp);
let args = VersionArgs {
strict: true,
strict_graph: false,
allow_empty_changesets: false,
refresh_lockfiles: false,
};
let result = commands::version::handle(args, &global);
assert!(
result.is_ok(),
"version --strict should return Ok(ExitCode); got Err: {result:?}"
);
let code = result.unwrap();
assert_ne!(
format!("{code:?}"),
format!("{:?}", ExitCode::SUCCESS),
"version --strict with no changesets must exit non-zero (EmptyChangeset escalated to Error)"
);
}
#[test]
fn test_version_no_strict_no_changesets_succeeds() {
let tmp = TempDir::new().unwrap();
let global = make_git_workspace(&tmp);
let args = VersionArgs {
strict: false,
strict_graph: false,
allow_empty_changesets: false,
refresh_lockfiles: false,
};
let result = commands::version::handle(args, &global);
assert!(
result.is_ok(),
"version without --strict should succeed; got: {result:?}"
);
let code = result.unwrap();
assert_eq!(
format!("{code:?}"),
format!("{:?}", ExitCode::SUCCESS),
"version without --strict on empty workspace should exit 0"
);
}
#[test]
fn test_snapshot_strict_clean_graph_succeeds() {
let tmp = TempDir::new().unwrap();
let global = make_git_workspace(&tmp);
let args = SnapshotArgs {
tag: "ci".to_string(),
strict: true,
};
let result = commands::snapshot::handle(args, &global);
assert!(
result.is_ok(),
"snapshot --strict on a clean graph should succeed; got: {result:?}"
);
let code = result.unwrap();
assert_eq!(
format!("{code:?}"),
format!("{:?}", ExitCode::SUCCESS),
"snapshot --strict on a clean graph should return exit code 0"
);
}
#[test]
fn test_snapshot_no_strict_clean_graph_succeeds() {
let tmp = TempDir::new().unwrap();
let global = make_git_workspace(&tmp);
let args = SnapshotArgs {
tag: "ci".to_string(),
strict: false,
};
let result = commands::snapshot::handle(args, &global);
assert!(
result.is_ok(),
"snapshot without --strict should succeed; got: {result:?}"
);
}
#[test]
fn test_tag_strict_clean_graph_succeeds() {
let tmp = TempDir::new().unwrap();
let global = make_git_workspace(&tmp);
let plan_json = serde_json::json!({
"schemaVersion": 1,
"rustCrates": [],
"npmPlatformPackages": [],
"npmMainPackages": [],
"releases": []
})
.to_string();
let args = TagArgs {
plan: plan_json,
floating_major: false,
strict: true,
};
let result = commands::tag::handle(args, &global);
assert!(
result.is_ok(),
"tag --strict on a clean graph should succeed; got: {result:?}"
);
let code = result.unwrap();
assert_eq!(
format!("{code:?}"),
format!("{:?}", ExitCode::SUCCESS),
"tag --strict on a clean graph should return exit code 0"
);
}
#[test]
fn test_tag_no_strict_clean_graph_succeeds() {
let tmp = TempDir::new().unwrap();
let global = make_git_workspace(&tmp);
let plan_json = serde_json::json!({
"schemaVersion": 1,
"rustCrates": [],
"npmPlatformPackages": [],
"npmMainPackages": [],
"releases": []
})
.to_string();
let args = TagArgs {
plan: plan_json,
floating_major: false,
strict: false,
};
let result = commands::tag::handle(args, &global);
assert!(result.is_ok(), "tag without --strict should succeed; got: {result:?}");
}