use std::fs; use std::path::Path; use std::process; use ignore::Walk; fn copy_tests_files(work_path: &Path) { for result in Walk::new("tests") { let entry = result.unwrap(); if let Some(file_type) = entry.file_type() { if file_type.is_file() { let src = entry.path(); let name = entry.file_name(); let dest = work_path.join(name); fs::copy(src, dest).unwrap(); } } } } fn run_git(working_path: &Path, args: &[&str]) { assert!(working_path.exists()); let process = process::Command::new("git") .args(args) .current_dir(working_path) .status() .unwrap(); assert!(process.success()); } fn expect_git_fail(working_path: &Path, args: &[&str]) { assert!(working_path.exists()); let process = process::Command::new("git") .args(args) .current_dir(working_path) .status() .unwrap(); assert!(!process.success()); } fn get_command(work_path: &Path) -> tbump::Command { tbump::Command { new_version: Some("1.43.0".to_string()), dry_run: false, non_interactive: true, patch_only: false, cwd: Some(work_path.to_path_buf()), sub_cmd: None, } } fn setup_project(tmp_path: &Path) -> tbump::Command { let remote_path = tmp_path.join("repo.git"); fs::create_dir(&remote_path).unwrap(); run_git(&remote_path, &["init", "--bare"]); let work_path = &tmp_path.join("work"); fs::create_dir(&work_path).unwrap(); run_git(&work_path, &["init"]); copy_tests_files(&work_path); run_git(&work_path, &["add", "."]); run_git(&work_path, &["commit", "-m", "initial commit"]); run_git( &work_path, &["remote", "add", "origin", remote_path.to_str().unwrap()], ); run_git(&work_path, &["push", "-u", "origin", "master"]); get_command(work_path) } #[test] fn test_default_workflow() { let tempdir = tempfile::tempdir().unwrap(); let tmp_path = tempdir.path(); let command = setup_project(&tmp_path); tbump::run(&command).unwrap(); } #[test] fn test_should_abort_if_dirty() { let tempdir = tempfile::tempdir().unwrap(); let tmp_path = tempdir.path(); let command = setup_project(&tmp_path); let setup_cfg = &tmp_path.join("work/setup.cfg"); fs::write(&setup_cfg, "untracked changed").unwrap(); let error = tbump::run(&command).unwrap_err().to_string(); assert!( error.contains("dirty"), format!("'dirty' not found in:\n{}", error) ); } #[test] fn test_only_patch_and_dry_run_should_not_bump_files() { let tempdir = tempfile::tempdir().unwrap(); let tmp_path = tempdir.path(); let mut command = setup_project(&tmp_path); command.dry_run = true; command.patch_only = true; tbump::run(&command).unwrap(); let setup_cfg = &tmp_path.join("work/setup.cfg"); let contents = fs::read_to_string(&setup_cfg).unwrap(); assert!( contents.contains("version = 1.42.5"), "setup.cfg should not have been bumped" ); } #[test] fn test_only_patch_should_bump_files_but_not_create_a_tag() { let tempdir = tempfile::tempdir().unwrap(); let tmp_path = tempdir.path(); let mut command = setup_project(&tmp_path); command.patch_only = true; tbump::run(&command).unwrap(); let setup_cfg = &tmp_path.join("work/setup.cfg"); let contents = fs::read_to_string(&setup_cfg).unwrap(); assert!( contents.contains("version = 1.43.0"), "setup.cfg should have been bumped" ); let work_path = &tmp_path.join("work"); expect_git_fail(&work_path, &["show", "v1.43.0"]) }