Skip to content

Commit

Permalink
fix compilation error + adding template test
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad committed Jan 12, 2025
1 parent 6ca79ed commit 32afb80
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
7 changes: 6 additions & 1 deletion loco-gen/src/templates/deployment/shuttle/shuttle.t
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ async fn main(
shuttle_runtime::Environment::Local => Environment::Development,
shuttle_runtime::Environment::Deployment => Environment::Production,
};
let boot_result = create_app::<App{% if with_db %}, Migrator{% endif %}>(StartMode::ServerOnly, &environment)

let config = environment
.load()
.expect("Failed to load configuration from the environment");

let boot_result = create_app::<App{% if with_db %}, Migrator{% endif %}>(StartMode::ServerOnly, &environment, config)
.await
.unwrap();

Expand Down
70 changes: 70 additions & 0 deletions loco-gen/tests/templates/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,73 @@ fn can_generate_nginx() {
.expect("nginx config missing")
);
}

#[test]
fn can_generate_shuttle() {
let mut settings = insta::Settings::clone_current();
settings.set_prepend_module_to_snapshot(false);
settings.set_snapshot_suffix("deployment");
let _guard = settings.bind_to_scope();

let component = Component::Deployment {
kind: DeploymentKind::Shuttle,
fallback_file: None,
asset_folder: None,
host: "localhost".to_string(),
port: 8080,
};

let tree_fs = tree_fs::TreeBuilder::default()
.drop(true)
.add(
".cargo/config.toml",
r#"[alias]
loco = "run --"
loco-tool = "run --"
playground = "run --example playground"
"#,
)
.add(
"Cargo.toml",
r"
[dependencies]
[dev-dependencies]
",
)
.create()
.unwrap();
let rrgen = RRgen::with_working_dir(&tree_fs.root);

let gen_result = generate(
&rrgen,
component,
&AppInfo {
app_name: "tester".to_string(),
},
)
.expect("Generation failed");

assert_eq!(
collect_messages(&gen_result),
r"* Shuttle.toml file created successfully
* Shuttle deployment ready do use
"
);
assert_snapshot!(
"generate[shuttle.rs]",
fs::read_to_string(tree_fs.root.join("src").join("bin").join("shuttle.rs"))
.expect("shuttle rs missing")
);
assert_snapshot!(
"inject[.config_toml]",
fs::read_to_string(tree_fs.root.join(".cargo").join("config.toml"))
.expect(".cargo/config.toml not exists")
);
assert_snapshot!(
"inject[cargo_toml]",
fs::read_to_string(tree_fs.root.join("Cargo.toml")).expect("cargo.toml not exists")
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: loco-gen/tests/templates/deployment.rs
expression: "fs::read_to_string(tree_fs.root.join(\"src\").join(\"bin\").join(\"shuttle.rs\")).expect(\"shuttle rs missing\")"
---
use loco_rs::boot::{create_app, StartMode};
use loco_rs::environment::Environment;
use tester::app::App;
use migration::Migrator;
use shuttle_runtime::DeploymentMetadata;

#[shuttle_runtime::main]
async fn main(
#[shuttle_shared_db::Postgres] conn_str: String,
#[shuttle_runtime::Metadata] meta: DeploymentMetadata,
) -> shuttle_axum::ShuttleAxum {
std::env::set_var("DATABASE_URL", conn_str);
let environment = match meta.env {
shuttle_runtime::Environment::Local => Environment::Development,
shuttle_runtime::Environment::Deployment => Environment::Production,
};

let config = environment
.load()
.expect("Failed to load configuration from the environment");

let boot_result = create_app::<App, Migrator>(StartMode::ServerOnly, &environment, config)
.await
.unwrap();

let router = boot_result.router.unwrap();
Ok(router.into())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: loco-gen/tests/templates/deployment.rs
expression: "fs::read_to_string(tree_fs.root.join(\".cargo\").join(\"config.toml\")).expect(\".cargo/config.toml not exists\")"
---
[alias]
loco = "run --bin tester-cli --"
loco-tool = "run --"

playground = "run --example playground"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: loco-gen/tests/templates/deployment.rs
expression: "fs::read_to_string(tree_fs.root.join(\"Cargo.toml\")).expect(\"cargo.toml not exists\")"
---
[dependencies]
shuttle-axum = "0.51.0"
shuttle-runtime = { version = "0.51.0", default-features = false }
shuttle-shared-db = { version = "0.51.0", features = ["postgres"] }


[[bin]]
name = "tester"
path = "src/bin/shuttle.rs"

[dev-dependencies]

0 comments on commit 32afb80

Please sign in to comment.