#![cfg(unix)] use std::{ env, fs, os::unix::fs::PermissionsExt, path::{Path, PathBuf}, process::{Command, Output}, sync::atomic::{AtomicUsize, Ordering}, }; const DEVELOPMENT_ORIGIN: &str = "git@git.dandaedre.com:Archive/rping.git"; const RELEASE_CONFIG: &str = "[[repository.fallbacks]]\n\ origin = \"git@git.dandaedre.com:Archive/rping.git\"\n\ fallback-url = \"https://proxy.dandaedre.com/gitea/Archive/rping\"\n"; static NEXT_TEMPORARY: AtomicUsize = AtomicUsize::new(0); struct TemporaryDirectory(PathBuf); impl TemporaryDirectory { fn new(label: &str) -> Self { let serial = NEXT_TEMPORARY.fetch_add(1, Ordering::Relaxed); let path = env::temp_dir().join(format!("rping-{label}-{}-{serial}", std::process::id())); fs::create_dir(&path).expect("temporary directory should be created"); Self(path) } fn path(&self) -> &Path { &self.0 } } impl Drop for TemporaryDirectory { fn drop(&mut self) { let _ = fs::remove_dir_all(&self.0); } } fn run(command: &mut Command) -> Output { command.output().expect("test command should start") } fn make_executable(path: &Path) { let mut permissions = fs::metadata(path) .expect("executable metadata should be readable") .permissions(); permissions.set_mode(0o755); fs::set_permissions(path, permissions).expect("executable mode should be set"); } fn prepare_repository(temporary: &TemporaryDirectory, version_line: &str) -> (PathBuf, PathBuf) { let repository = temporary.path().join("repository with spaces"); let fake_bin = temporary.path().join("fake bin"); fs::create_dir(&repository).expect("repository should be created"); fs::create_dir(&fake_bin).expect("fake binary directory should be created"); fs::copy( Path::new(env!("CARGO_MANIFEST_DIR")).join("release.sh"), repository.join("release.sh"), ) .expect("release wrapper should be copied"); make_executable(&repository.join("release.sh")); fs::copy( Path::new(env!("CARGO_MANIFEST_DIR")).join("rust-release.toml"), repository.join("rust-release.toml"), ) .expect("release configuration should be copied"); fs::write( repository.join("Cargo.toml"), format!("[package]\nname = \"rping\"\n{version_line}\n"), ) .expect("manifest should be written"); assert!(run(Command::new("git") .arg("-C") .arg(&repository) .args(["init", "--quiet"])) .status .success()); assert!(run(Command::new("git").arg("-C").arg(&repository).args([ "remote", "add", "origin", DEVELOPMENT_ORIGIN ])) .status .success()); (repository, fake_bin) } fn install_fake_publisher(fake_bin: &Path) { let publisher = fake_bin.join("rust_realease"); fs::write( &publisher, "#!/bin/sh\n\ [ \"$GITEA_TOKEN\" = test-token ] || exit 91\n\ printf '%s\\n' \"$GITEA_TOKEN\" >\"$OBSERVED_TOKEN\"\n\ printf '%s\\n' \"$@\" >\"$OBSERVED_ARGS\"\n\ git -C \"$1\" remote get-url origin >\"$OBSERVED_ORIGIN\"\n\ cat \"$1/rust-release.toml\" >\"$OBSERVED_CONFIG\"\n\ printf 'fake publisher ran\\n'\n\ exit \"$FAKE_RELEASE_STATUS\"\n", ) .expect("fake publisher should be written"); make_executable(&publisher); } fn wrapper_command( repository: &Path, fake_bin: &Path, token_file: &Path, temporary: &TemporaryDirectory, status: i32, ) -> Command { let mut command = Command::new(repository.join("release.sh")); command .arg(token_file) .env( "PATH", format!( "{}:{}", fake_bin.display(), env::var("PATH").expect("PATH should be available") ), ) .env("FAKE_RELEASE_STATUS", status.to_string()) .env("OBSERVED_TOKEN", temporary.path().join("observed token")) .env("OBSERVED_ARGS", temporary.path().join("observed args")) .env("OBSERVED_ORIGIN", temporary.path().join("observed origin")) .env("OBSERVED_CONFIG", temporary.path().join("observed config")); command } #[test] fn stable_release_forwards_exact_arguments_without_leaking_token() { let temporary = TemporaryDirectory::new("stable-release"); let (repository, fake_bin) = prepare_repository(&temporary, "version = \"0.1.0\""); install_fake_publisher(&fake_bin); let token_file = temporary.path().join("token file"); fs::write(&token_file, "test-token\nignored-second-line\n").expect("token should be written"); let output = run( wrapper_command(&repository, &fake_bin, &token_file, &temporary, 0).args([ "--dry-run", "--tag", "tag with spaces", ]), ); assert!(output.status.success()); assert!(!String::from_utf8_lossy(&output.stdout).contains("test-token")); assert!(!String::from_utf8_lossy(&output.stderr).contains("test-token")); let arguments = fs::read_to_string(temporary.path().join("observed args")) .expect("arguments should be recorded"); assert_eq!( arguments.lines().collect::>(), [ repository .to_str() .expect("repository path should be UTF-8"), "--package", "rping", "--artifact-format", "both", "--dry-run", "--tag", "tag with spaces", ] ); assert!(!arguments.contains("test-token")); assert_eq!( fs::read_to_string(temporary.path().join("observed token")) .expect("token should be recorded"), "test-token\n" ); assert_eq!( fs::read_to_string(temporary.path().join("observed config")) .expect("config should be recorded"), RELEASE_CONFIG ); assert_eq!( fs::read_to_string(temporary.path().join("observed origin")) .expect("origin should be recorded") .trim(), DEVELOPMENT_ORIGIN ); assert_eq!( String::from_utf8( run(Command::new("git") .arg("-C") .arg(&repository) .args(["remote", "get-url", "origin"])) .stdout ) .expect("origin should be UTF-8") .trim(), DEVELOPMENT_ORIGIN ); } #[test] fn prerelease_is_added_and_publisher_status_is_preserved() { let temporary = TemporaryDirectory::new("prerelease"); let (repository, fake_bin) = prepare_repository(&temporary, "version = \"0.2.0-rc.1\""); install_fake_publisher(&fake_bin); let token_file = temporary.path().join("token"); fs::write(&token_file, "test-token\n").expect("token should be written"); let output = run(&mut wrapper_command( &repository, &fake_bin, &token_file, &temporary, 17, )); assert_eq!(output.status.code(), Some(17)); let arguments = fs::read_to_string(temporary.path().join("observed args")) .expect("arguments should be recorded"); assert!(arguments.lines().any(|argument| argument == "--prerelease")); } #[test] fn invalid_inputs_fail_before_publishing() { let temporary = TemporaryDirectory::new("invalid-inputs"); let (repository, fake_bin) = prepare_repository(&temporary, "version = \"0.1.0\""); install_fake_publisher(&fake_bin); let wrapper = repository.join("release.sh"); let empty_path = temporary.path().join("empty path"); fs::create_dir(&empty_path).expect("empty PATH directory should be created"); assert!(!run(&mut Command::new(&wrapper)).status.success()); assert!( !run(Command::new(&wrapper).arg(temporary.path().join("missing token"))) .status .success() ); let empty_token = temporary.path().join("empty token"); fs::write(&empty_token, "").expect("empty token should be written"); assert!(!run(Command::new(&wrapper).arg(&empty_token)) .status .success()); let unreadable_token = temporary.path().join("unreadable token"); fs::write(&unreadable_token, "test-token\n").expect("token should be written"); let mut permissions = fs::metadata(&unreadable_token) .expect("token metadata should be readable") .permissions(); permissions.set_mode(0o000); fs::set_permissions(&unreadable_token, permissions).expect("token mode should be set"); assert!(!run(Command::new(&wrapper).arg(&unreadable_token)) .status .success()); let valid_token = temporary.path().join("valid token"); fs::write(&valid_token, "test-token\n").expect("token should be written"); let missing_publisher = run(Command::new(&wrapper) .arg(&valid_token) .env("PATH", &empty_path)); assert_eq!(missing_publisher.status.code(), Some(2)); assert!(String::from_utf8_lossy(&missing_publisher.stderr) .contains("rust_realease is not installed")); fs::write( repository.join("Cargo.toml"), "[package]\nname = \"rping\"\n", ) .expect("manifest without a version should be written"); let unresolved_version = run(&mut wrapper_command( &repository, &fake_bin, &valid_token, &temporary, 0, )); assert_eq!(unresolved_version.status.code(), Some(2)); assert!(String::from_utf8_lossy(&unresolved_version.stderr) .contains("could not determine the selected package version")); }