diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index 7d2e57e05..70b4fbb6d 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -41,6 +41,9 @@ The tests are generated using the template engine [Tera]. The input of the template is the canonical data from [`problem-specifications`]. if you want to exclude certain tests from being generated, you have to set `include = false` in `.meta/tests.toml`. +Please include a comment about the reason for excluding it. +If you want to add additional track-specific tests, you can do so in a file `.meta/additional-tests.json`. +Only do this with a good reason for not upstreaming these tests. Find some tips about writing tera templates [here](#tera-templates). diff --git a/rust-tooling/src/exercise_generation.rs b/rust-tooling/src/exercise_generation.rs index 9c2e91a57..3421abbf7 100644 --- a/rust-tooling/src/exercise_generation.rs +++ b/rust-tooling/src/exercise_generation.rs @@ -8,7 +8,7 @@ use tera::Context; use crate::{ exercise_config::{get_excluded_tests, get_test_emplate}, - problem_spec::{get_canonical_data, SingleTestCase, TestCase}, + problem_spec::{get_additional_test_cases, get_canonical_data, SingleTestCase, TestCase}, }; pub struct GeneratedExercise { @@ -95,7 +95,11 @@ fn to_hex(value: &tera::Value, _args: &HashMap) -> tera::Re } fn generate_tests(slug: &str, fn_names: Vec) -> String { - let cases = get_canonical_data(slug).cases; + let cases = { + let mut cases = get_canonical_data(slug).cases; + cases.extend_from_slice(&get_additional_test_cases(slug)); + cases + }; let excluded_tests = get_excluded_tests(slug); let mut template = get_test_emplate(slug).unwrap(); if template.get_template_names().next().is_none() { diff --git a/rust-tooling/src/problem_spec.rs b/rust-tooling/src/problem_spec.rs index b32bef808..56a5c4fe7 100644 --- a/rust-tooling/src/problem_spec.rs +++ b/rust-tooling/src/problem_spec.rs @@ -51,6 +51,25 @@ pub fn get_canonical_data(slug: &str) -> CanonicalData { }) } +/// The Rust track may define additional test cases in +/// `.meta/additional-tests.json`, which are not appropriate +/// to upstream for all other languages to implement. +pub fn get_additional_test_cases(slug: &str) -> Vec { + let path = std::path::PathBuf::from("exercises/practice") + .join(slug) + .join(".meta/additional-tests.json"); + if !path.exists() { + return Vec::new(); + } + let contents = std::fs::read_to_string(&path).unwrap(); + serde_json::from_str(contents.as_str()).unwrap_or_else(|e| { + panic!( + "should deserialize additional tests for {}: {e}", + path.display() + ) + }) +} + #[test] fn deserialize_canonical_data() { crate::fs_utils::cd_into_repo_root();