Added some tests

Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com>
This commit is contained in:
alexis-opolka 2025-06-01 21:23:01 +02:00
parent 41e6987319
commit dfcb2e2173
3 changed files with 72 additions and 12 deletions

View file

@ -9,6 +9,7 @@ use crate::prelude::*;
use std::fs;
use std::path;
use std::path::{MAIN_SEPARATOR, MAIN_SEPARATOR_STR};
use tracing_subscriber::fmt::format;
fn ask_if_should_import_all(finder: &FinderChoice) -> Result<bool> {
let opts = FinderOpts {
@ -122,21 +123,25 @@ pub fn main(uri: String, yes_flag: bool, branch: &Option<String>) -> Result<()>
// We are copying the .git folder along with the cheat files
// For more details, see: (https://github.com/denisidoro/navi/issues/733)
for file in git_files.split('\n') {
let filename = format!("{}{}", &tmp_path_str, &file);
let file_path = format!("{}{}", &tmp_path_str, &file);
let from = {
let mut p = tmp_pathbuf.clone();
p.push(&filename);
p
};
let to = {
let mut p = to_folder.clone();
p.push(file);
p.push(&file_path);
p
};
eprintln!("{file_path}");
let path_str = &PathBuf::clone(&to).to_string();
let path_str = format!("{}{}{}", to_folder.to_string(), path::MAIN_SEPARATOR, &file_path);
let local_collection = &path_str.split(MAIN_SEPARATOR).collect::<Vec<&str>>();
let collection_str = local_collection[0..&local_collection.len() - 1].join(MAIN_SEPARATOR_STR);
let collection_str = if cfg!(windows) {
eprintln!("{:?}", local_collection);
local_collection[1..&local_collection.len() - 1].join(MAIN_SEPARATOR_STR)
} else {
local_collection[0..&local_collection.len() - 1].join(MAIN_SEPARATOR_STR)
};
// This should be able to fix an issue with the clone on windows where both
// to_folder and collection_str are equal
@ -156,12 +161,19 @@ pub fn main(uri: String, yes_flag: bool, branch: &Option<String>) -> Result<()>
format!(
"{}{}",
&to_folder.to_string(),
&to.to_string()
&collection_str
)
} else {
to_folder.to_string()
};
eprintln!("=> (&to_folder.to_string() == &collection_str) = {}", &to_folder.to_string() == &collection_str);
eprintln!("=> To_folder: {}", &to_folder.to_string());
eprintln!("=> Collection: {}", &collection_str);
eprintln!("=> local_to_folder: {}", &local_to_folder);
eprintln!("=> complete_local_path: {}", &complete_local_path);
debug!("=> {}", &complete_local_path);
fs::create_dir_all(&local_to_folder).unwrap_or(());

View file

@ -52,9 +52,9 @@ fn synchronize(cheat_repo: String) -> Result<()> {
// Now that the folder has been cleaned, we need to fetch the latest HEAD available.
git::fetch_origin(&cheat_repo)?;
// git::pull(&cheat_repo)?;
let status = git::diff(&cheat_repo)?;
let r = git::remote_update(&cheat_repo)?;
eprintln!("Status: {:?}", status.code());
eprintln!("Status: {:?}", r.code());
return Ok(());
// We delete them since they are now out of tree

View file

@ -112,6 +112,54 @@ pub fn diff(uri: &str) -> Result<ExitStatus> {
.wait()?)
}
pub fn remote_update(uri: &str) -> Result<ExitStatus> {
Ok(Command::new("git")
.current_dir(&uri)
.args(["remote", "update"])
.spawn()?
.wait()?
)
}
/// Gets the local state of a git repository against its remote
///
/// Return: an i8 integer with the following values:
/// - "-2" => There has been an error within this function
/// - "-1" => This repository is behind the remote
/// - "0" => This repository is the same as the remote
/// - "1" => This repository is ahead the remote
pub fn get_local_state(uri: &str) -> i8 {
// We're updating the remote
let remote_update = Command::new("git")
.current_dir(&uri)
.args(["remote", "update"])
.spawn().unwrap()
.wait().unwrap();
// If we couldn't update the remotes, we can't continue and compare them,
// we need to send an error and quit.
if ! remote_update.success() {
error!("Unable to update the remote (i.e. git remote update)");
return -2;
}
let local_hash = Command::new("git")
.current_dir(&uri)
.args(["rev-parse", "@"])
.spawn().unwrap().stdout.take().unwrap();
let remote_hash = Command::new("git")
.current_dir(&uri)
.args(["rev-parse", "\"@{u}\""])
.spawn().unwrap().stdout.take().unwrap();
eprintln!("{:?} => {:?}", local_hash, remote_hash);
-1
}
#[cfg(test)]
mod tests {