Skip to content

Commit

Permalink
Split rust-tooling into separate crates (#1795)
Browse files Browse the repository at this point in the history
TL;DR: This makes CI faster.

The main advantage of splitting the tooling into several crates is that
the CI tests only need to compile the dependencies needed for them.
Notably, that excludes Tera, which is used for exercise generation. This
also paves the way for making the exercise generator more rich, using
potentially compile-time heavy crates (e.g. `clap`).
  • Loading branch information
senekor authored Nov 21, 2023
1 parent 63814ba commit b2f72ef
Show file tree
Hide file tree
Showing 23 changed files with 157 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:
toolchain: stable

- name: Run tests
run: cd rust-tooling && cargo test
run: cd rust-tooling/ci-tests && cargo test

rustformat:
name: Check Rust Formatting
Expand Down
86 changes: 56 additions & 30 deletions rust-tooling/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 3 additions & 17 deletions rust-tooling/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
[package]
name = "exercism_tooling"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
convert_case = "0.6.0"
glob = "0.3.1"
ignore = "0.4.20"
inquire = "0.6.2"
once_cell = "1.18.0"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = { version = "1.0.105", features = ["preserve_order"] }
tera = "1.19.1"
uuid = { version = "1.4.1", features = ["v4"] }
[workspace]
members = ["generator", "ci-tests", "utils", "models"]
resolver = "2"
16 changes: 16 additions & 0 deletions rust-tooling/ci-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "ci-tests"
version = "0.1.0"
edition = "2021"
description = "Tests to be run in CI to make sure the repo is in good shape"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

# This crate is built in CI to run the tests.
# Be considerate when adding dependencies to keep compile times reasonable.
[dependencies]
convert_case = "0.6.0"
ignore = "0.4.20"
models = { version = "0.1.0", path = "../models" }
serde_json = "1.0.108"
utils = { version = "0.1.0", path = "../utils" }
5 changes: 5 additions & 0 deletions rust-tooling/ci-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! dummy lib.rs
//!
//! This crate only exists for the tests.
//! The lib.rs may be used for shared code amoung the tests,
//! if that ever turns out to be useful.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::path::PathBuf;

use convert_case::{Case, Casing};
use exercism_tooling::fs_utils;

/// Runs a function for each bash script in the bin directory.
/// The function is passed the path of the script.
fn for_all_scripts(f: fn(&str)) {
fs_utils::cd_into_repo_root();
utils::fs::cd_into_repo_root();

for entry in std::fs::read_dir("bin").unwrap() {
let path = entry.unwrap().path();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use exercism_tooling::exercise_config::{
use models::exercise_config::{
get_all_concept_exercise_paths, get_all_practice_exercise_paths, PracticeExercise,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Make sure exercise difficulties are set correctly
use exercism_tooling::track_config::TRACK_CONFIG;
use models::track_config::TRACK_CONFIG;

#[test]
fn difficulties_are_valid() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use exercism_tooling::exercise_config::get_all_exercise_paths;
use models::exercise_config::get_all_exercise_paths;

/// The package manifest of each exercise should not contain an `authors` field.
/// The authors are already specified in the track configuration.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::path::Path;

use exercism_tooling::fs_utils;

fn contains_trailing_whitespace(p: &Path) -> bool {
let contents = std::fs::read_to_string(p).unwrap();
for line in contents.lines() {
Expand All @@ -14,7 +12,7 @@ fn contains_trailing_whitespace(p: &Path) -> bool {

#[test]
fn no_trailing_whitespace() {
fs_utils::cd_into_repo_root();
utils::fs::cd_into_repo_root();

for entry in ignore::Walk::new("./") {
let entry = entry.unwrap();
Expand Down
17 changes: 17 additions & 0 deletions rust-tooling/generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "generator"
version = "0.1.0"
edition = "2021"
description = "Generates exercise boilerplate, especially test cases"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
convert_case = "0.6.0"
glob = "0.3.1"
inquire = "0.6.2"
models = { version = "0.1.0", path = "../models" }
serde_json = { version = "1.0.105", features = ["preserve_order"] }
tera = "1.19.1"
utils = { version = "0.1.0", path = "../utils" }
uuid = { version = "1.4.1", features = ["v4"] }
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::path::PathBuf;

use convert_case::{Case, Casing};
use exercism_tooling::{
exercise_generation, fs_utils,
track_config::{self, TRACK_CONFIG},
};
use generator::exercise_generation;
use glob::glob;
use inquire::{validator::Validation, Select, Text};
use models::track_config::{self, TRACK_CONFIG};

enum Difficulty {
Easy,
Expand Down Expand Up @@ -39,7 +37,7 @@ impl std::fmt::Display for Difficulty {
}

fn main() {
fs_utils::cd_into_repo_root();
utils::fs::cd_into_repo_root();

let is_update = std::env::args().any(|arg| arg == "update");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::{
process::{Command, Stdio},
};

use tera::Context;
use tera::{Context, Tera};

use crate::{
exercise_config::{get_excluded_tests, get_test_emplate},
use models::{
exercise_config::get_excluded_tests,
problem_spec::{get_additional_test_cases, get_canonical_data, SingleTestCase, TestCase},
};

Expand Down Expand Up @@ -76,7 +76,7 @@ fn generate_example_rs(fn_name: &str) -> String {
)
}

static TEST_TEMPLATE: &str = include_str!("default_test_template.tera");
static TEST_TEMPLATE: &str = include_str!("../default_test_template.tera");

fn extend_single_cases(single_cases: &mut Vec<SingleTestCase>, cases: Vec<TestCase>) {
for case in cases {
Expand All @@ -101,7 +101,7 @@ fn generate_tests(slug: &str, fn_names: Vec<String>) -> String {
cases
};
let excluded_tests = get_excluded_tests(slug);
let mut template = get_test_emplate(slug).unwrap();
let mut template = get_test_template(slug).unwrap();
if template.get_template_names().next().is_none() {
template
.add_raw_template("test_template.tera", TEST_TEMPLATE)
Expand Down Expand Up @@ -143,3 +143,7 @@ fn generate_tests(slug: &str, fn_names: Vec<String>) -> String {
rendered.into()
}
}

pub fn get_test_template(slug: &str) -> Option<Tera> {
Some(Tera::new(format!("exercises/practice/{slug}/.meta/*.tera").as_str()).unwrap())
}
1 change: 1 addition & 0 deletions rust-tooling/generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod exercise_generation;
15 changes: 15 additions & 0 deletions rust-tooling/models/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "models"
version = "0.1.0"
edition = "2021"
description = "Data structures for exercism stuff"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ignore = "0.4.20"
once_cell = "1.18.0"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = { version = "1.0.105", features = ["preserve_order"] }
utils = { version = "0.1.0", path = "../utils" }
uuid = { version = "1.6.1", features = ["v4"] }
Loading

0 comments on commit b2f72ef

Please sign in to comment.