Skip to content

Commit f800298

Browse files
committed
[fs] take clone_fn to sync to make it more testable
Signed-off-by: Colton J. McCurdy <mccurdyc22@gmail.com>
1 parent 3a9fc3a commit f800298

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

src/fs.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ use walkdir::WalkDir;
99

1010
const GITRS_ROOT_DEFAULT: &str = "src";
1111

12-
pub fn sync(root: PathBuf, repos: &HashMap<String, repo::Repo>, _clean_only: &bool) -> Result<()> {
12+
pub fn sync(root: PathBuf, repos: &HashMap<String, repo::Repo>, clean_only: &bool) -> Result<()> {
13+
sync_with_fn(root, repos, clean_only, clone_ssh)
14+
}
15+
16+
fn sync_with_fn(
17+
root: PathBuf,
18+
repos: &HashMap<String, repo::Repo>,
19+
_clean_only: &bool,
20+
clone_fn: fn(&str, &Path) -> Result<()>,
21+
) -> Result<()> {
1322
for entry in WalkDir::new(root.as_path())
1423
.min_depth(3) // forces it to look at full paths only
1524
.max_depth(3)
@@ -38,7 +47,7 @@ pub fn sync(root: PathBuf, repos: &HashMap<String, repo::Repo>, _clean_only: &bo
3847
debug!("On repository: {:?}", r.get_name());
3948

4049
if !root.join(r.get_name()).exists() {
41-
clone_ssh(r.get_url(), root.join(r.get_name()).as_path())?;
50+
clone_fn(r.get_url(), root.join(r.get_name()).as_path())?;
4251
}
4352
}
4453

@@ -116,6 +125,7 @@ fn root(p: Option<PathBuf>) -> PathBuf {
116125
#[cfg(test)]
117126
mod tests {
118127
use super::*;
128+
use repo;
119129
use tempfile::{tempdir, TempDir};
120130
extern crate log;
121131
use env_logger;
@@ -171,4 +181,49 @@ mod tests {
171181
env::set_var("HOME", old_home);
172182
cleanup(root);
173183
}
184+
185+
#[test]
186+
fn test_sync_add_repo_dir_doesnt_exists() {
187+
let root = setup();
188+
189+
let got = sync(
190+
root.path().to_path_buf(),
191+
&HashMap::from([(
192+
"github.com/a/a".to_string(),
193+
repo::Repo::new()
194+
.name("github.com/a/a".to_string())
195+
.expect("sync name failed")
196+
.url("github.com/a/a".to_string())
197+
.expect("sync url failed")
198+
.pin(false)
199+
.sha("".to_string())
200+
.to_owned(),
201+
)]),
202+
&false,
203+
);
204+
assert_eq!(got.is_err(), false);
205+
206+
cleanup(root);
207+
}
208+
209+
#[test]
210+
fn test_sync_add_repo_dir_exists() {
211+
let root = setup();
212+
cleanup(root);
213+
unimplemented!("test_sync");
214+
}
215+
216+
#[test]
217+
fn test_sync_remove_repo_dir_exists() {
218+
let root = setup();
219+
cleanup(root);
220+
unimplemented!("test_sync");
221+
}
222+
223+
#[test]
224+
fn test_sync_remove_repo_dir_doesnt_exists() {
225+
let root = setup();
226+
cleanup(root);
227+
unimplemented!("test_sync");
228+
}
174229
}

0 commit comments

Comments
 (0)