Skip to content

Commit

Permalink
Generators testing toolkit (#887)
Browse files Browse the repository at this point in the history
* refactor: move gen into a crate - loco-gen + better seperation
* implement a generators testing kit similar to Rails' testing rig
* separate gen CI from loco CI
  • Loading branch information
jondot authored Oct 22, 2024
1 parent 092b813 commit 6d59f68
Show file tree
Hide file tree
Showing 56 changed files with 622 additions and 110 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/ci-loco-gen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: CI/loco-gen

on:
push:
branches:
- master
pull_request:

env:
RUST_TOOLCHAIN: stable
TOOLCHAIN_PROFILE: minimal

defaults:
run:
working-directory: ./loco-gen

jobs:
rustfmt:
name: Check Style
runs-on: ubuntu-latest

permissions:
contents: read

steps:
- name: Checkout the code
uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ env.RUST_TOOLCHAIN }}
components: rustfmt
- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Run Clippy
runs-on: ubuntu-latest

permissions:
contents: read

steps:
- name: Checkout the code
uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ env.RUST_TOOLCHAIN }}
- name: Setup Rust cache
uses: Swatinem/rust-cache@v2
- name: Run cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-features -- -D warnings -W clippy::pedantic -W clippy::nursery -W rust-2018-idioms

test:
name: Run Tests
needs: [rustfmt, clippy]
runs-on: ubuntu-latest

permissions:
contents: read

steps:
- name: Checkout the code
uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ env.RUST_TOOLCHAIN }}
- name: Setup Rust cache
uses: Swatinem/rust-cache@v2

- run: |
cargo install --path ../loco-cli
- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features --all
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ jobs:
toolchain: ${{ env.RUST_TOOLCHAIN }}
- name: Setup Rust cache
uses: Swatinem/rust-cache@v2

- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features --all
args: --all-features --workspace --exclude loco-gen
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# local dev
todo.txt
todo.md
examples/demo2
*.sqlite
*.sqlite-wal
Expand Down
38 changes: 24 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["xtask", "loco-extras"]
members = ["xtask", "loco-extras", "loco-gen"]
exclude = ["starters"]

[workspace.package]
Expand All @@ -26,7 +26,7 @@ default = ["auth_jwt", "cli", "with-db", "cache_inmem", "bg_redis", "bg_pg"]
auth_jwt = ["dep:jsonwebtoken"]
cli = ["dep:clap"]
testing = ["dep:axum-test"]
with-db = ["dep:sea-orm", "dep:sea-orm-migration"]
with-db = ["dep:sea-orm", "dep:sea-orm-migration", "loco-gen/with-db"]
channels = ["dep:socketioxide"]
# Storage features
all_storage = ["storage_aws_s3", "storage_azure", "storage_gcp"]
Expand All @@ -39,6 +39,7 @@ bg_redis = ["dep:rusty-sidekiq", "dep:bb8"]
bg_pg = ["dep:sqlx", "dep:ulid"]

[dependencies]
loco-gen = { path = "./loco-gen" }
backtrace_printer = { version = "1.3.0" }

# cli
Expand All @@ -56,8 +57,8 @@ sea-orm = { version = "1.0.0", features = [
tokio = { version = "1.33.0", default-features = false }
# the rest

serde = "1"
serde_json = "1"
serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = "0.9"
serde_variant = "0.1.2"

Expand All @@ -66,8 +67,8 @@ async-trait = { workspace = true }

axum = { workspace = true }
axum-extra = { version = "0.9", features = ["cookie"] }
regex = "1"
lazy_static = "1.4.0"
regex = { workspace = true }
lazy_static = { workspace = true }
fs-err = "2.11.0"
# mailer
tera = "1.19.1"
Expand All @@ -79,8 +80,8 @@ lettre = { version = "0.11.4", default-features = false, features = [
"tokio1-rustls-tls",
] }
include_dir = "0.7.3"
thiserror = "1"
tracing = "0.1.40"
thiserror = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
tracing-appender = "0.2.3"

Expand All @@ -103,12 +104,7 @@ ipnetwork = "0.20.0"

axum-test = { version = "16.1.0", optional = true }

# gen
rrgen = "0.5.3"
chrono = "0.4.31"
cargo_metadata = "0.18.1"
dialoguer = "0.11.0"

chrono = { workspace = true }
cfg-if = "1"

uuid = { version = "1.10.0", features = ["v4", "fast-rng"] }
Expand Down Expand Up @@ -138,6 +134,14 @@ rusty-sidekiq = { version = "0.11.0", default-features = false, optional = true
bb8 = { version = "0.8.1", optional = true }

[workspace.dependencies]

chrono = { version = "0.4", features = ["serde"] }
tracing = "0.1.40"
regex = "1"
thiserror = "1"
serde = "1"
serde_json = "1"
lazy_static = "1.4.0"
async-trait = { version = "0.1.74" }
axum = { version = "0.7.5", features = ["macros"] }
tower = "0.4"
Expand Down Expand Up @@ -168,10 +172,16 @@ features = [
features = ["testing"]

[dev-dependencies]
cargo_metadata = "0.18.1"
loco-rs = { path = ".", features = ["testing"] }
rstest = "0.21.0"
insta = { version = "1.34.0", features = ["redactions", "yaml", "filters"] }
tree-fs = { version = "0.1.0" }
reqwest = { version = "0.12.7" }
serial_test = "3.1.1"
tower = { workspace = true, features = ["util"] }

# generator tests
tempfile = "3"
duct_sh = { version = "0.13.7" }
syn = { version = "2", features = ["full"] }
9 changes: 0 additions & 9 deletions examples/demo/Cargo.lock

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

32 changes: 32 additions & 0 deletions loco-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "loco-gen"
version = "0.11.0"
description = "Loco generators"
license.workspace = true
edition.workspace = true
rust-version.workspace = true

[features]
with-db = []

[lib]
path = "src/lib.rs"

[dependencies]

lazy_static = { workspace = true }
rrgen = "0.5.3"
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
regex = { workspace = true }
tracing = { workspace = true }
chrono = { workspace = true }

clap = { version = "4.4.7", features = ["derive"] }
dialoguer = "0.11"
duct = "0.13"

[dev-dependencies]
tempfile = "3"
syn = { version = "2", features = ["full"] }
14 changes: 7 additions & 7 deletions src/gen/controller.rs → loco-gen/src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rrgen::RRgen;
use serde_json::json;

use crate::{app::Hooks, gen};
use crate as gen;

const API_CONTROLLER_CONTROLLER_T: &str = include_str!("templates/controller/api/controller.t");
const API_CONTROLLER_TEST_T: &str = include_str!("templates/controller/api/test.t");
Expand All @@ -12,16 +12,16 @@ const HTMX_VIEW_T: &str = include_str!("templates/controller/htmx/view.t");
const HTML_CONTROLLER_CONTROLLER_T: &str = include_str!("templates/controller/html/controller.t");
const HTML_VIEW_T: &str = include_str!("templates/controller/html/view.t");

use super::collect_messages;
use crate::Result;
use super::{collect_messages, AppInfo, Result};

pub fn generate<H: Hooks>(
pub fn generate(
rrgen: &RRgen,
name: &str,
actions: &[String],
kind: &gen::ScaffoldKind,
appinfo: &AppInfo,
) -> Result<String> {
let vars = json!({"name": name, "actions": actions, "pkg_name": H::app_name()});
let vars = json!({"name": name, "actions": actions, "pkg_name": appinfo.app_name});
match kind {
gen::ScaffoldKind::Api => {
let res1 = rrgen.generate(API_CONTROLLER_CONTROLLER_T, &vars)?;
Expand All @@ -34,7 +34,7 @@ pub fn generate<H: Hooks>(
let res = rrgen.generate(HTML_CONTROLLER_CONTROLLER_T, &vars)?;
messages.push(res);
for action in actions {
let vars = json!({"name": name, "action": action, "pkg_name": H::app_name()});
let vars = json!({"name": name, "action": action, "pkg_name": appinfo.app_name});
messages.push(rrgen.generate(HTML_VIEW_T, &vars)?);
}
Ok(collect_messages(messages))
Expand All @@ -44,7 +44,7 @@ pub fn generate<H: Hooks>(
let res = rrgen.generate(HTMX_CONTROLLER_CONTROLLER_T, &vars)?;
messages.push(res);
for action in actions {
let vars = json!({"name": name, "action": action, "pkg_name": H::app_name()});
let vars = json!({"name": name, "action": action, "pkg_name": appinfo.app_name});
messages.push(rrgen.generate(HTMX_VIEW_T, &vars)?);
}
Ok(collect_messages(messages))
Expand Down
Loading

0 comments on commit 6d59f68

Please sign in to comment.