Skip to content

Commit

Permalink
If target is None, use current directory as default
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-jianliang committed Nov 13, 2023
1 parent 5e26f4d commit 985ec52
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ malachite-q = "=0.3.2"
malachite-base = "=0.3.2"
rustpython = "0.3.0"
colored = "2.0.4"
walkdir = "2.4.0"
tempfile = "3.8.1"

[dependencies.async-std]
version = "1.6"
Expand Down
41 changes: 20 additions & 21 deletions src/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,26 @@ async fn do_sync(
});
}
}
(None, Some(dir)) => {
abs_root_dir = env::current_dir()?.join(dir);
(Some(u), None) => {
let repo_name = git_utils::get_repo_name(&u).ok_or(Error {
message: format!("Failed to get repo name from url {}", u),
})?;
abs_root_dir = env::current_dir()?.join(repo_name);
url_str = u.clone();
target_branch = target_branch.or(git_utils::get_remote_default_branch(
&url.unwrap(),
Some(remote_name),
));
}
(None, _) => {
match root_dir {
Some(dir) => {
abs_root_dir = env::current_dir()?.join(dir);
}
None => {
abs_root_dir = env::current_dir()?;
}
}
let repo = Repository::open(&abs_root_dir)?;
url_str = repo
.find_remote("origin")?
Expand All @@ -62,8 +80,6 @@ async fn do_sync(

// If the target_branch is None, we try to find it from the repository in root_dir
target_branch = target_branch.or_else(|| {
let dir = root_dir.clone().unwrap();
let abs_root_dir = env::current_dir().ok()?.join(dir);
let repo = Repository::open(&abs_root_dir).ok()?;
let head = repo.head().ok()?;
let b = head.shorthand().map(|b| b.to_string());
Expand All @@ -73,23 +89,6 @@ async fn do_sync(
b
});
}
(Some(u), None) => {
let repo_name = git_utils::get_repo_name(&u).ok_or(Error {
message: format!("Failed to get repo name from url {}", u),
})?;
abs_root_dir = env::current_dir()?.join(repo_name);
url_str = u.clone();
target_branch = target_branch.or(git_utils::get_remote_default_branch(
&url.unwrap(),
Some(remote_name),
));
}
(None, None) => {
return Err(Error {
message: "Ether the url or an exsit repository directory should be provided"
.to_string(),
});
}
}

println!("Sync solution to {}", abs_root_dir.display());
Expand Down
58 changes: 58 additions & 0 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::fs;
use std::path::Path;
use walkdir::WalkDir;

pub fn copy_dir_to(src: &Path, dst: &Path) -> std::io::Result<()> {
if !dst.is_dir() {
fs::create_dir_all(dst)?;
}

for entry in WalkDir::new(src) {
let entry = entry?;
let src_path = entry.path();
let relative_path = src_path
.strip_prefix(src)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
let dst_path = dst.join(relative_path);

if src_path.is_dir() {
fs::create_dir_all(&dst_path)?;
} else {
fs::copy(&src_path, &dst_path)?;
}
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs::File;
use std::io::prelude::*;
use tempfile::tempdir;

#[test]
fn test_copy_dir_to() {
// Create a temporary directory.
let src_dir = tempdir().unwrap();
let dst_dir = tempdir().unwrap();

// Create a file in the source directory.
let file_path = src_dir.path().join("test.txt");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "Hello, world!").unwrap();

// Copy the source directory to the destination directory.
copy_dir_to(src_dir.path(), dst_dir.path()).unwrap();

// Check if the file exists in the destination directory.
let copied_file_path = dst_dir.path().join("test.txt");
assert!(copied_file_path.exists());

// Check the content of the copied file.
let mut copied_file = File::open(copied_file_path).unwrap();
let mut contents = String::new();
copied_file.read_to_string(&mut contents).unwrap();
assert_eq!(contents, "Hello, world!\n");
}
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod cache;
pub mod encode;
pub mod fs;
pub mod git_utils;
pub mod parser;
pub mod process;
Expand Down
63 changes: 62 additions & 1 deletion tests/sync_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
mod tests {
use assert_cmd::prelude::*;
use assert_fs::{prelude::*, TempDir};
use git2::Repository;
use predicates::prelude::*;
use std::{path::PathBuf, process::Command};
use test_log::test;

use crane::utils::test_utils;
use crane::utils::{fs::copy_dir_to, test_utils};

#[test(tokio::test)]
async fn test_sync_simple_with_url_and_without_dir() -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -180,6 +181,66 @@ mod tests {
Ok(())
}

#[test(tokio::test)]
async fn test_sync_simple_without_url_and_without_dir() -> Result<(), Box<dyn std::error::Error>>
{
// Create a source repository with 1 commit
let source_repo_dir = TempDir::new()?;
let _ = test_utils::create_git_repo_in_dir(
&source_repo_dir.path(),
&PathBuf::from("README.md"),
"test",
)
.unwrap();

let workdir = &TempDir::new()?;
let target_dir = "test_sync_simple_without_url_and_without_dir";

// Copy source repo to target_dir
copy_dir_to(&source_repo_dir.path(), &workdir.join(target_dir))?;

// Set remote url
let repo = Repository::open(&workdir.join(target_dir))?;
repo.remote(
"origin",
format!("file://{}/.git", source_repo_dir.path().display()).as_str(),
)?;

// Add another commit to source repository
test_utils::modify_file_in_repo(
&source_repo_dir,
&PathBuf::from("README.2.md"),
"test",
true,
true,
true,
)?;

// Sync again without a url
let mut cmd = Command::cargo_bin("crane")?;
cmd.arg("sync");

cmd.current_dir(&workdir.join(target_dir))
.assert()
.success()
.stdout(predicate::str::contains(format!(
"Sync solution to {}",
workdir.path().join(target_dir).display()
)));

workdir.child(target_dir).assert(predicate::path::exists());
workdir
.child(target_dir)
.child("README.2.md")
.assert(predicate::path::exists());
workdir
.child(target_dir)
.child(".git")
.assert(predicate::path::exists());

Ok(())
}

#[test]
fn test_sync_nested_repositories() {
// main
Expand Down

0 comments on commit 985ec52

Please sign in to comment.