#![allow(clippy::unnecessary_wraps)]
use assert_cmd::Command;
use color_eyre::eyre;
use predicates::prelude::*;
use std::fs;
use std::path::Path;
fn git_init(dir: &Path) -> eyre::Result<()> {
let output = std::process::Command::new("git")
.arg("init")
.current_dir(dir)
.output()?;
eyre::ensure!(output.status.success(), "failed to init git repo");
Ok(())
}
#[test]
fn test_show_help() -> eyre::Result<()> {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_bumpversion"));
cmd.arg("show").arg("--help");
cmd.assert()
.success()
.stdout(predicate::str::contains("Usage: bumpversion show"));
Ok(())
}
#[test]
fn test_show_bump_help() -> eyre::Result<()> {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_bumpversion"));
cmd.arg("show-bump").arg("--help");
cmd.assert()
.success()
.stdout(predicate::str::contains("Usage: bumpversion show-bump"));
Ok(())
}
#[test]
fn test_show_current_version() -> eyre::Result<()> {
let temp = tempfile::tempdir()?;
let config_path = temp.path().join("pyproject.toml");
fs::write(
&config_path,
r#"
[tool.bumpversion]
current_version = "1.2.3"
"#,
)?;
git_init(temp.path())?;
let mut cmd = Command::new(env!("CARGO_BIN_EXE_bumpversion"));
cmd.current_dir(temp.path())
.arg("show")
.arg("current_version");
cmd.assert()
.success()
.stdout(predicate::str::contains("1.2.3"));
Ok(())
}
#[test]
fn test_show_bump_major() -> eyre::Result<()> {
let temp = tempfile::tempdir()?;
let config_path = temp.path().join(".bumpversion.toml");
fs::write(
&config_path,
r#"
[tool.bumpversion]
current_version = "1.2.3"
"#,
)?;
git_init(temp.path())?;
let mut cmd = Command::new(env!("CARGO_BIN_EXE_bumpversion"));
cmd.current_dir(temp.path())
.arg("show-bump")
.arg("major");
cmd.assert()
.success()
.stdout(predicate::str::contains("old_version=1.2.3"))
.stdout(predicate::str::contains("new_version=2.0.0"));
Ok(())
}
#[test]
fn test_values_bump_scenario() -> eyre::Result<()> {
let temp = tempfile::tempdir()?;
let config_path = temp.path().join("pyproject.toml");
fs::write(
&config_path,
r#"
[tool.bumpversion]
current_version = "1.0.0"
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(?:-(?P<release>[a-z]+))?"
serialize = ["{major}.{minor}.{patch}-{release}", "{major}.{minor}.{patch}"]
[tool.bumpversion.parts.release]
values = ["alpha", "beta", "rc", "final"]
optional_value = "final"
"#,
)?;
git_init(temp.path())?;
fs::write(
&config_path,
r#"
[tool.bumpversion]
current_version = "1.0.0-alpha"
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(?:-(?P<release>[a-z]+))?"
serialize = ["{major}.{minor}.{patch}-{release}", "{major}.{minor}.{patch}"]
[tool.bumpversion.parts.release]
values = ["alpha", "beta", "rc", "final"]
optional_value = "final"
"#,
)?;
let mut cmd = Command::new(env!("CARGO_BIN_EXE_bumpversion"));
cmd.current_dir(temp.path())
.arg("show-bump")
.arg("release");
cmd.assert()
.success()
.stdout(predicate::str::contains("old_version=1.0.0-alpha"))
.stdout(predicate::str::contains("new_version=1.0.0-beta"));
Ok(())
}
#[test]
fn test_bump_modifies_file() -> eyre::Result<()> {
let temp = tempfile::tempdir()?;
let config_path = temp.path().join(".bumpversion.toml");
let source_path = temp.path().join("VERSION");
fs::write(
&config_path,
r#"
[tool.bumpversion]
current_version = "1.2.3"
[[tool.bumpversion.files]]
filename = "VERSION"
"#,
)?;
fs::write(&source_path, "1.2.3")?;
git_init(temp.path())?;
let mut cmd = Command::new(env!("CARGO_BIN_EXE_bumpversion"));
cmd.current_dir(temp.path())
.arg("bump")
.arg("patch")
.arg("--allow-dirty")
.arg("--no-commit")
.arg("--no-tag");
cmd.assert()
.success();
let content = fs::read_to_string(&source_path)?;
assert_eq!(content, "1.2.4");
let config_content = fs::read_to_string(&config_path)?;
assert!(config_content.contains(r#"current_version = "1.2.4""#));
Ok(())
}
#[test]
fn test_bump_custom_search_replace() -> eyre::Result<()> {
let temp = tempfile::tempdir()?;
let config_path = temp.path().join(".bumpversion.toml");
let source_path = temp.path().join("VERSION");
fs::write(
&config_path,
r#"
[tool.bumpversion]
current_version = "1.2.3"
search = "my-version: {current_version}"
replace = "my-version: {new_version}"
[[tool.bumpversion.files]]
filename = "VERSION"
"#,
)?;
fs::write(&source_path, "my-version: 1.2.3")?;
git_init(temp.path())?;
let mut cmd = Command::new(env!("CARGO_BIN_EXE_bumpversion"));
cmd.current_dir(temp.path())
.arg("bump")
.arg("patch")
.arg("--allow-dirty")
.arg("--no-commit")
.arg("--no-tag");
cmd.assert()
.success();
let content = fs::read_to_string(&source_path)?;
assert_eq!(content, "my-version: 1.2.4");
Ok(())
}
#[test]
fn test_bump_dry_run() -> eyre::Result<()> {
let temp = tempfile::tempdir()?;
let config_path = temp.path().join(".bumpversion.toml");
let source_path = temp.path().join("VERSION");
fs::write(
&config_path,
r#"
[tool.bumpversion]
current_version = "1.2.3"
[[tool.bumpversion.files]]
filename = "VERSION"
"#,
)?;
let initial_content = "1.2.3";
fs::write(&source_path, initial_content)?;
git_init(temp.path())?;
let mut cmd = Command::new(env!("CARGO_BIN_EXE_bumpversion"));
cmd.current_dir(temp.path())
.arg("bump")
.arg("patch")
.arg("--dry-run")
.arg("--allow-dirty")
.arg("--no-commit")
.arg("--no-tag");
cmd.assert()
.success();
let content = fs::read_to_string(&source_path)?;
assert_eq!(content, initial_content, "File should not change in dry-run");
let config_content = fs::read_to_string(&config_path)?;
assert!(config_content.contains(r#"current_version = "1.2.3""#), "Config should not change in dry-run");
Ok(())
}