Skip to content

Commit

Permalink
generator: only add template upon request
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor committed Aug 9, 2024
1 parent 2a66d7d commit 4911a2b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
7 changes: 7 additions & 0 deletions rust-tooling/generate/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,10 @@ pub fn prompt_for_difficulty() -> Result<Difficulty> {
.prompt()
.context("failed to select difficulty")
}

pub fn prompt_for_template_generation() -> bool {
inquire::Confirm::new("Would you like to add a template for test generation?")
.with_default(false)
.prompt()
.unwrap_or_default()
}
21 changes: 13 additions & 8 deletions rust-tooling/generate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use anyhow::{Context, Result};
use clap::Parser;
use cli::{AddArgs, FullAddArgs, UpdateArgs};
use cli::{prompt_for_template_generation, AddArgs, FullAddArgs, UpdateArgs};
use models::track_config::{self, TRACK_CONFIG};

mod cli;
Expand Down Expand Up @@ -98,14 +98,19 @@ fn generate_exercise_files(slug: &str, is_update: bool) -> Result<()> {
}

let template_path = exercise_path.join(".meta/test_template.tera");
if std::fs::metadata(&template_path).is_err() {
std::fs::write(template_path, exercise.test_template)?;
if !template_path.exists() && (!is_update || prompt_for_template_generation()) {
// Some exercises have existing test cases that are difficult to migrate
// to the test generator. That's why we only generate a template for
// existing exercises if the users requests it.
std::fs::write(&template_path, exercise.test_template)?;
}

std::fs::create_dir(exercise_path.join("tests")).ok();
std::fs::write(
exercise_path.join(format!("tests/{slug}.rs")),
exercise.tests,
)
.map_err(Into::into)
if template_path.exists() {
std::fs::write(
exercise_path.join(format!("tests/{slug}.rs")),
exercise.tests,
)?;
}
Ok(())
}

0 comments on commit 4911a2b

Please sign in to comment.