Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More predictable local git repo test #7

Merged
merged 1 commit into from Jun 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 31 additions & 13 deletions src/main.rs
Expand Up @@ -60,13 +60,13 @@ fn extract_matter(path: &Path) -> Result<ParsedEntity, io::Error> {
fn add(output: &std::path::Path, blog: &str) -> Result<std::path::PathBuf, io::Error> {
let cache = output.join(".blogs");
fs::create_dir_all(&cache)?;
let path = Path::new(&cache).join(blog.replace("/", "-"));
let path = Path::new(&cache).join(blog.replace('/', "-"));

if blog.ends_with(".git") {
if let Err(e) = clone_or_pull(&blog, &path) {
if let Err(e) = clone_or_pull(blog, &path) {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("failed to clone or pull: {}", e),
format!("failed to clone or pull {}: {}", blog, e),
));
}
let posts = path.join("content/post");
Expand Down Expand Up @@ -209,7 +209,7 @@ fn rfc3339(date: &str) -> Result<String, io::Error> {
}
},
};
return Ok(parsed);
Ok(parsed)
}

fn post(
Expand All @@ -221,7 +221,7 @@ fn post(
date: &str,
orig: &str,
) -> Result<String, io::Error> {
let path = output.join(format!("{}-{}.md", date, title).replace("/", "-"));
let path = output.join(format!("{}-{}.md", date, title).replace('/', "-"));
let mut file = File::create(&path).expect(format!("invalid path: {:?}", path).as_str());
write!(
file,
Expand All @@ -232,7 +232,7 @@ author: {}
original_link: {}
---
{}",
title.replace("'", "''"),
title.replace('\'', "''"),
date,
author,
orig,
Expand All @@ -245,6 +245,7 @@ original_link: {}
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
use git2::Signature;
use tempfile::tempdir;

#[test]
Expand Down Expand Up @@ -291,17 +292,34 @@ mod tests {

#[test]
fn test_add() -> Result<(), io::Error> {
let project_path = tempdir()?;
let output = project_path.path().join("content/post");
let parent_folder = tempdir()?;
let project_path = parent_folder.path().join("blog.git");
let output = project_path.join("content/post");
fs::create_dir_all(&output).unwrap();

let repo = Repository::init(&project_path).unwrap();
let example = &output.join("2020-02-02-example.md");
let mut file = File::create(&example)?;
write!(
file,
"---\ntitle: Example\ndate: 2020-02-02\n---\nLorem ipsum"
)?;
let mut index = repo.index().unwrap();
index
.add_path(Path::new("content/post/2020-02-02-example.md"))
.unwrap();
let sig = Signature::now("Example", "email@example.com").unwrap();
let tree = repo.find_tree(index.write_tree().unwrap()).unwrap();
repo.commit(Some("HEAD"), &sig, &sig, "example", &tree, &[])
.unwrap();

let blog = "https://gitlab.com/kalikiana/kalikiana.gitlab.io.git";
let blog = project_path.to_str().unwrap().to_string();
let path = project_path
.path()
.join("content/post/.blogs")
.join(blog.replace("/", "-"));
assert_eq!(add(&output, &blog)?.file_name(), path.file_name());
.join(blog.replace('/', "-"));
assert_eq!(add(&output, &blog).unwrap().file_name(), path.file_name());
// Repeat, this should be fine on an existing folder
assert_eq!(add(&output, &blog)?.file_name(), path.file_name());
assert_eq!(add(&output, &blog).unwrap().file_name(), path.file_name());
Ok(())
}

Expand Down