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

generator: only add template upon request #1953

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}