aoc-toolkit 0.1.0-alpha.3

A toolkit for solving Advent of Code problems
Documentation
use super::*;
use std::fs;

use tempfile::tempdir;

fn options(destination: PathBuf) -> InitOptions {
    InitOptions {
        destination,
        package_name: "my-advent-of-code".to_owned(),
        language: Language::Rust,
    }
}

#[test]
fn initializes_new_project() {
    let directory = tempdir().unwrap();
    let destination = directory.path().join("aoc");

    let report = execute(options(destination.clone())).unwrap();

    assert_eq!(report.root, destination);
    assert_eq!(report.package_name, "my-advent-of-code");
    assert_eq!(report.created_files.len(), 10);

    for file in &report.created_files {
        assert!(file.is_file());
    }

    assert!(destination.join("Cargo.toml").is_file());
    assert!(destination.join("aoc.toml").is_file());
    assert!(destination.join("src/main.rs").is_file());
    assert!(destination.join("src/lib.rs").is_file());
}

#[test]
fn initializes_existing_directory() {
    let directory = tempdir().unwrap();

    let report = execute(options(directory.path().to_path_buf())).unwrap();

    assert_eq!(report.root, directory.path());
    assert_eq!(report.created_files.len(), 10);
}

#[test]
fn preserves_unrelated_files() {
    let directory = tempdir().unwrap();
    let existing = directory.path().join("notes.txt");

    fs::write(&existing, "My notes").unwrap();

    execute(options(directory.path().to_path_buf())).unwrap();

    assert_eq!(fs::read_to_string(existing).unwrap(), "My notes");
}

#[test]
fn rejects_invalid_package_name_without_creating_files() {
    let directory = tempdir().unwrap();
    let destination = directory.path().join("aoc");

    let mut options = options(destination.clone());
    options.package_name = "invalid name".to_owned();

    let result = execute(options);

    assert!(matches!(result, Err(InitError::InvalidPackageName { .. })));

    assert!(!destination.exists());
}

#[test]
fn rejects_destination_that_is_a_file() {
    let directory = tempdir().unwrap();
    let destination = directory.path().join("aoc");

    fs::write(&destination, "Existing content").unwrap();

    let result = execute(options(destination.clone()));

    assert!(matches!(
        result,
        Err(InitError::Scaffold(ScaffoldError::NotDirectory(_)))
    ));

    assert_eq!(fs::read_to_string(destination).unwrap(), "Existing content");
}

#[test]
fn rejects_conflicting_files_without_partial_scaffold() {
    let directory = tempdir().unwrap();
    let manifest = directory.path().join("Cargo.toml");

    fs::write(&manifest, "Existing manifest").unwrap();

    let result = execute(options(directory.path().to_path_buf()));

    assert!(matches!(
        result,
        Err(InitError::Scaffold(ScaffoldError::FileExists(_)))
    ));

    assert!(!directory.path().join("aoc.toml").exists());
    assert!(!directory.path().join("src").exists());

    assert_eq!(fs::read_to_string(manifest).unwrap(), "Existing manifest");
}

#[test]
fn rejects_reinitialization() {
    let directory = tempdir().unwrap();
    let destination = directory.path().join("aoc");

    execute(options(destination.clone())).unwrap();

    let manifest = destination.join("Cargo.toml");
    let original = fs::read_to_string(&manifest).unwrap();

    let result = execute(options(destination));

    assert!(matches!(
        result,
        Err(InitError::Scaffold(ScaffoldError::FileExists(_)))
    ));

    assert_eq!(fs::read_to_string(manifest).unwrap(), original);
}