use std::fs; use std::path::{Path, PathBuf}; use std::process::{Command, Output}; use std::time::{SystemTime, UNIX_EPOCH}; struct TempDirectory(PathBuf); impl TempDirectory { fn new() -> Self { let stamp = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_nanos(); let path = std::env::temp_dir().join(format!("veins-rust-test-{}-{stamp}", std::process::id())); fs::create_dir_all(&path).unwrap(); Self(path) } fn path(&self) -> &Path { &self.0 } } impl Drop for TempDirectory { fn drop(&mut self) { let _ = fs::remove_dir_all(&self.0); } } fn vn(directory: &Path, arguments: &[&str]) -> Output { Command::new(env!("CARGO_BIN_EXE_vn")) .args(arguments) .current_dir(directory) .env("NO_COLOR", "1") .output() .unwrap() } fn ok(directory: &Path, arguments: &[&str]) -> String { let output = vn(directory, arguments); assert!( output.status.success(), "command {:?} failed: {}", arguments, String::from_utf8_lossy(&output.stderr) ); String::from_utf8(output.stdout).unwrap() } #[test] fn commit_log_checkout_and_bookmarks() { let temporary = TempDirectory::new(); let root = temporary.path(); ok(root, &["init", "."]); fs::write( root.join(".veins/config"), "user.name = Bench\nuser.email = bench@example.com\n", ) .unwrap(); fs::write(root.join("a.txt"), "hello\n").unwrap(); fs::create_dir(root.join("src")).unwrap(); fs::write(root.join("src/b.txt"), "world\n").unwrap(); ok(root, &["go", "-m", "first"]); fs::write(root.join("a.txt"), "hello again\n").unwrap(); ok(root, &["go", "-m", "second"]); let log = ok(root, &["log", "-no-col-titles"]); let first = log .lines() .find(|line| line.contains("first")) .and_then(|line| { line.split_whitespace() .find(|token| token.len() == 12 && token.bytes().all(|byte| byte.is_ascii_hexdigit())) }) .expect("first commit id"); ok(root, &["co", first]); assert_eq!(fs::read_to_string(root.join("a.txt")).unwrap(), "hello\n"); ok(root, &["status"]); ok(root, &["bmk", "set", "main"]); let bookmarks = ok(root, &["bmk", "ls"]); assert!(bookmarks.contains("main")); ok(root, &["self-test"]); }