Skip to content

Commit

Permalink
[fs] take clone_fn to sync to make it more testable
Browse files Browse the repository at this point in the history
Signed-off-by: Colton J. McCurdy <[email protected]>
  • Loading branch information
mccurdyc committed Jun 13, 2023
1 parent 3a9fc3a commit f800298
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ use walkdir::WalkDir;

const GITRS_ROOT_DEFAULT: &str = "src";

pub fn sync(root: PathBuf, repos: &HashMap<String, repo::Repo>, _clean_only: &bool) -> Result<()> {
pub fn sync(root: PathBuf, repos: &HashMap<String, repo::Repo>, clean_only: &bool) -> Result<()> {
sync_with_fn(root, repos, clean_only, clone_ssh)
}

fn sync_with_fn(
root: PathBuf,
repos: &HashMap<String, repo::Repo>,
_clean_only: &bool,
clone_fn: fn(&str, &Path) -> Result<()>,
) -> Result<()> {
for entry in WalkDir::new(root.as_path())
.min_depth(3) // forces it to look at full paths only
.max_depth(3)
Expand Down Expand Up @@ -38,7 +47,7 @@ pub fn sync(root: PathBuf, repos: &HashMap<String, repo::Repo>, _clean_only: &bo
debug!("On repository: {:?}", r.get_name());

if !root.join(r.get_name()).exists() {
clone_ssh(r.get_url(), root.join(r.get_name()).as_path())?;
clone_fn(r.get_url(), root.join(r.get_name()).as_path())?;
}
}

Expand Down Expand Up @@ -116,6 +125,7 @@ fn root(p: Option<PathBuf>) -> PathBuf {
#[cfg(test)]
mod tests {
use super::*;
use repo;
use tempfile::{tempdir, TempDir};
extern crate log;
use env_logger;
Expand Down Expand Up @@ -171,4 +181,49 @@ mod tests {
env::set_var("HOME", old_home);
cleanup(root);
}

#[test]
fn test_sync_add_repo_dir_doesnt_exists() {
let root = setup();

let got = sync(
root.path().to_path_buf(),
&HashMap::from([(
"github.com/a/a".to_string(),
repo::Repo::new()
.name("github.com/a/a".to_string())
.expect("sync name failed")
.url("github.com/a/a".to_string())
.expect("sync url failed")
.pin(false)
.sha("".to_string())
.to_owned(),
)]),
&false,
);
assert_eq!(got.is_err(), false);

cleanup(root);
}

#[test]
fn test_sync_add_repo_dir_exists() {
let root = setup();
cleanup(root);
unimplemented!("test_sync");
}

#[test]
fn test_sync_remove_repo_dir_exists() {
let root = setup();
cleanup(root);
unimplemented!("test_sync");
}

#[test]
fn test_sync_remove_repo_dir_doesnt_exists() {
let root = setup();
cleanup(root);
unimplemented!("test_sync");
}
}

0 comments on commit f800298

Please sign in to comment.