Extract utils

This commit is contained in:
Gwilym Kuiper 2022-10-01 20:36:42 +01:00
parent d8ca41ec3d
commit f2f0f771fb
3 changed files with 35 additions and 25 deletions

View file

@ -3,6 +3,7 @@ use clap::Command;
mod publish;
mod release;
mod utils;
#[derive(Debug)]
pub enum Error {

View file

@ -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::<bool>("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<String> = HashSet::new();
let mut published_crates: HashSet<String> = HashSet::new();
@ -86,19 +88,6 @@ pub fn publish(matches: &ArgMatches) -> Result<(), Error> {
Ok(())
}
fn find_agb_root_directory() -> Result<PathBuf, Error> {
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<bool, Error> {
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!(

27
tools/src/utils.rs Normal file
View file

@ -0,0 +1,27 @@
use std::{env, path::PathBuf};
#[derive(Debug)]
pub struct FindRootDirectoryError;
pub fn find_agb_root_directory() -> Result<PathBuf, FindRootDirectoryError> {
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();
}
}