diff --git a/tools/src/main.rs b/tools/src/main.rs index 4df25fdd..79118229 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -3,6 +3,7 @@ use clap::Command; mod publish; mod release; +mod utils; #[derive(Debug)] pub enum Error { diff --git a/tools/src/publish.rs b/tools/src/publish.rs index 73a34d57..0f8935f7 100644 --- a/tools/src/publish.rs +++ b/tools/src/publish.rs @@ -1,12 +1,14 @@ use clap::{Arg, ArgAction, ArgMatches}; use std::collections::{HashMap, HashSet}; use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::Command; +use std::thread; use std::time::Duration; -use std::{env, thread}; use toml_edit::Document; +use crate::utils::*; + #[derive(Debug)] pub enum Error { FindRootDirectory, @@ -31,7 +33,7 @@ pub fn command() -> clap::Command { pub fn publish(matches: &ArgMatches) -> Result<(), Error> { let dry_run = matches.get_one::("Dry run").expect("defined by clap"); - let root_directory = find_agb_root_directory()?; + let root_directory = find_agb_root_directory().map_err(|_| Error::FindRootDirectory)?; let mut fully_published_crates: HashSet = HashSet::new(); let mut published_crates: HashSet = HashSet::new(); @@ -86,19 +88,6 @@ pub fn publish(matches: &ArgMatches) -> Result<(), Error> { Ok(()) } -fn find_agb_root_directory() -> Result { - let mut current_path = env::current_dir().map_err(|_| Error::FindRootDirectory)?; - - while !current_path.clone().join("justfile").exists() { - current_path = current_path - .parent() - .ok_or(Error::FindRootDirectory)? - .to_owned(); - } - - Ok(current_path) -} - fn check_if_released(crate_to_publish: &str, expected_version: &str) -> Result { let url_to_poll = &get_url_to_poll(crate_to_publish); @@ -220,16 +209,9 @@ mod test { } } - #[test] - fn should_find_root_directory() -> Result<(), Error> { - assert_ne!(find_agb_root_directory()?.to_string_lossy(), ""); - - Ok(()) - } - #[test] fn should_read_version() -> Result<(), Error> { - let root_directory = find_agb_root_directory()?; + let root_directory = crate::utils::find_agb_root_directory().unwrap(); let my_version = read_cargo_toml_version(&root_directory.join("tools"))?; assert_eq!(my_version, "0.1.0"); @@ -238,7 +220,7 @@ mod test { #[test] fn should_detect_dependencies() -> Result<(), Error> { - let root_directory = find_agb_root_directory()?; + let root_directory = crate::utils::find_agb_root_directory().unwrap(); let deps = get_agb_dependencies(&root_directory.join("agb"))?; assert_eq!( diff --git a/tools/src/utils.rs b/tools/src/utils.rs new file mode 100644 index 00000000..a9759165 --- /dev/null +++ b/tools/src/utils.rs @@ -0,0 +1,27 @@ +use std::{env, path::PathBuf}; + +#[derive(Debug)] +pub struct FindRootDirectoryError; + +pub fn find_agb_root_directory() -> Result { + let mut current_path = env::current_dir().map_err(|_| FindRootDirectoryError)?; + + while !current_path.clone().join("justfile").exists() { + current_path = current_path + .parent() + .ok_or(FindRootDirectoryError)? + .to_owned(); + } + + Ok(current_path) +} + +#[cfg(test)] +mod tests { + use super::find_agb_root_directory; + + #[test] + fn find_agb_root_directory_works() { + find_agb_root_directory().unwrap(); + } +}