Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
riginding committed Oct 25, 2019
1 parent 671d72b commit 81fcb59
Showing 1 changed file with 68 additions and 8 deletions.
76 changes: 68 additions & 8 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,23 @@ pub fn create_new_project(name: &str) -> Result<()> {
.replace("%SEARCH%", &format!("{}", search))
.replace("%HIGHLIGHT%", &format!("{}", highlight));

populate(&path, compile_sass, &config)?;

println!();
console::success(&format!("Done! Your site was created in {:?}", canonicalize(path).unwrap()));
println!();
console::info(
"Get started by moving into the directory and using the built-in server: `zola serve`",
);
println!("Visit https://www.getzola.org for the full documentation.");
Ok(())
}

fn populate(path: &Path, compile_sass: bool, config: &str) -> Result<()> {
if !path.exists() {
create_dir(path)?;
}
create_file(&path.join("config.toml"), &config)?;

create_dir(path.join("content"))?;
create_dir(path.join("templates"))?;
create_dir(path.join("static"))?;
Expand All @@ -99,13 +111,6 @@ pub fn create_new_project(name: &str) -> Result<()> {
create_dir(path.join("sass"))?;
}

println!();
console::success(&format!("Done! Your site was created in {:?}", canonicalize(path).unwrap()));
println!();
console::info(
"Get started by moving into the directory and using the built-in server: `zola serve`",
);
println!("Visit https://www.getzola.org for the full documentation.");
Ok(())
}

Expand Down Expand Up @@ -164,4 +169,59 @@ mod tests {
remove_dir(&dir).unwrap();
assert_eq!(true, allowed);
}

#[test]
fn populate_existing_directory() {
let mut dir = temp_dir();
dir.push("test_existing_dir");
if dir.exists() {
remove_dir_all(&dir).expect("Could not free test directory");
}
create_dir(&dir).expect("Could not create test directory");
populate(&dir, true, "").expect("Could not populate zola directories");

assert_eq!(true, dir.join("config.toml").exists());
assert_eq!(true, dir.join("content").exists());
assert_eq!(true, dir.join("templates").exists());
assert_eq!(true, dir.join("static").exists());
assert_eq!(true, dir.join("themes").exists());
assert_eq!(true, dir.join("sass").exists());

remove_dir_all(&dir).unwrap();
}

#[test]
fn populate_non_existing_directory() {
let mut dir = temp_dir();
dir.push("test_non_existing_dir");
if dir.exists() {
remove_dir_all(&dir).expect("Could not free test directory");
}
populate(&dir, true, "").expect("Could not populate zola directories");

assert_eq!(true, dir.exists());
assert_eq!(true, dir.join("config.toml").exists());
assert_eq!(true, dir.join("content").exists());
assert_eq!(true, dir.join("templates").exists());
assert_eq!(true, dir.join("static").exists());
assert_eq!(true, dir.join("themes").exists());
assert_eq!(true, dir.join("sass").exists());

remove_dir_all(&dir).unwrap();
}

#[test]
fn populate_without_sass() {
let mut dir = temp_dir();
dir.push("test_wihout_sass_dir");
if dir.exists() {
remove_dir_all(&dir).expect("Could not free test directory");
}
create_dir(&dir).expect("Could not create test directory");
populate(&dir, false, "").expect("Could not populate zola directories");

assert_eq!(false, dir.join("sass").exists());

remove_dir_all(&dir).unwrap();
}
}

0 comments on commit 81fcb59

Please sign in to comment.