Skip to content

Commit 3cff60d

Browse files
authored
fix: static folder local run clearing file contents, add missing tests in cargo-shuttle (#717)
* fix: static folder local run clearing file contents * fix: clippy * fix: cargo shuttle init tests not logging in * fix: bail if non-interactive and --new without api-key * fix: update codegen tests
1 parent 5ea3159 commit 3cff60d

File tree

6 files changed

+69
-22
lines changed

6 files changed

+69
-22
lines changed

cargo-shuttle/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ impl Shuttle {
136136
println!("First, let's log in to your Shuttle account.");
137137
self.login(args.login_args.clone()).await?;
138138
println!();
139-
} else if args.new && args.login_args.api_key.is_some() {
139+
} else if args.login_args.api_key.is_some() {
140140
self.login(args.login_args.clone()).await?;
141-
} else {
141+
} else if args.new {
142142
bail!("Tried to login to create a Shuttle environment, but no API key was set.")
143143
}
144144
}

cargo-shuttle/tests/integration/init.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ async fn non_interactive_basic_init() {
3333
assert!(cargo_toml.contains("shuttle-runtime = "));
3434
}
3535

36-
// TODO: unignore when shuttle-rocket is published
37-
#[ignore]
3836
#[tokio::test]
3937
async fn non_interactive_rocket_init() {
4038
let temp_dir = Builder::new().prefix("rocket-init").tempdir().unwrap();
@@ -57,8 +55,6 @@ async fn non_interactive_rocket_init() {
5755
assert_valid_rocket_project(temp_dir_path.as_path(), "rocket-init");
5856
}
5957

60-
// TODO: unignore when shuttle-rocket is published
61-
#[ignore]
6258
#[test]
6359
fn interactive_rocket_init() -> Result<(), Box<dyn std::error::Error>> {
6460
let temp_dir = Builder::new().prefix("rocket-init").tempdir().unwrap();
@@ -98,8 +94,6 @@ fn interactive_rocket_init() -> Result<(), Box<dyn std::error::Error>> {
9894
Ok(())
9995
}
10096

101-
// TODO: unignore when shuttle-rocket is published
102-
#[ignore]
10397
#[test]
10498
fn interactive_rocket_init_dont_prompt_framework() -> Result<(), Box<dyn std::error::Error>> {
10599
let temp_dir = Builder::new().prefix("rocket-init").tempdir().unwrap();
@@ -135,8 +129,6 @@ fn interactive_rocket_init_dont_prompt_framework() -> Result<(), Box<dyn std::er
135129
Ok(())
136130
}
137131

138-
// TODO: unignore when shuttle-rocket is published
139-
#[ignore]
140132
#[test]
141133
fn interactive_rocket_init_dont_prompt_name() -> Result<(), Box<dyn std::error::Error>> {
142134
let temp_dir = Builder::new().prefix("rocket-init").tempdir().unwrap();
@@ -176,11 +168,10 @@ fn interactive_rocket_init_dont_prompt_name() -> Result<(), Box<dyn std::error::
176168
fn assert_valid_rocket_project(path: &Path, name_prefix: &str) {
177169
let cargo_toml = read_to_string(path.join("Cargo.toml")).unwrap();
178170
assert!(cargo_toml.contains(&format!("name = \"{name_prefix}")));
179-
assert!(cargo_toml.contains("shuttle-service = { version = "));
180-
assert!(cargo_toml.contains("features = [\"web-rocket\"]"));
181-
assert!(cargo_toml.contains("rocket = "));
171+
assert!(cargo_toml.contains("shuttle-runtime = "));
172+
assert!(cargo_toml.contains("shuttle-rocket = "));
182173

183-
let lib_file = read_to_string(path.join("src").join("lib.rs")).unwrap();
174+
let main_file = read_to_string(path.join("src").join("main.rs")).unwrap();
184175
let expected = indoc! {r#"
185176
#[macro_use]
186177
extern crate rocket;
@@ -190,12 +181,12 @@ fn assert_valid_rocket_project(path: &Path, name_prefix: &str) {
190181
"Hello, world!"
191182
}
192183
193-
#[shuttle_service::main]
194-
async fn rocket() -> shuttle_service::ShuttleRocket {
184+
#[shuttle_runtime::main]
185+
async fn rocket() -> shuttle_rocket::ShuttleRocket {
195186
let rocket = rocket::build().mount("/hello", routes![index]);
196187
197-
Ok(rocket)
188+
Ok(rocket.into())
198189
}"#};
199190

200-
assert_eq!(lib_file, expected);
191+
assert_eq!(main_file, expected);
201192
}

cargo-shuttle/tests/integration/run.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,58 @@ async fn rocket_postgres() {
121121
assert_eq!(request_text, "{\"id\":1,\"note\":\"Deploy to shuttle\"}");
122122
}
123123

124+
#[tokio::test(flavor = "multi_thread")]
125+
async fn axum_static_files() {
126+
let url = cargo_shuttle_run("../examples/axum/static-files", false).await;
127+
let client = reqwest::Client::new();
128+
129+
let request_text = client
130+
.get(format!("{url}/hello"))
131+
.send()
132+
.await
133+
.unwrap()
134+
.text()
135+
.await
136+
.unwrap();
137+
138+
assert_eq!(request_text, "Hello, world!");
139+
140+
let request_text = client.get(url).send().await.unwrap().text().await.unwrap();
141+
142+
assert!(
143+
request_text.contains("This is an example of serving static files with axum and shuttle.")
144+
);
145+
}
146+
147+
#[tokio::test(flavor = "multi_thread")]
148+
async fn shuttle_next() {
149+
let url = cargo_shuttle_run("../examples/next/hello-world", false).await;
150+
let client = reqwest::Client::new();
151+
152+
let request_text = client
153+
.get(format!("{url}/hello"))
154+
.send()
155+
.await
156+
.unwrap()
157+
.text()
158+
.await
159+
.unwrap();
160+
161+
assert_eq!(request_text, "Hello, World!");
162+
163+
let post_text = client
164+
.post(format!("{url}/uppercase"))
165+
.body("uppercase this")
166+
.send()
167+
.await
168+
.unwrap()
169+
.text()
170+
.await
171+
.unwrap();
172+
173+
assert_eq!(post_text, "UPPERCASE THIS");
174+
}
175+
124176
#[tokio::test(flavor = "multi_thread")]
125177
#[ignore]
126178
async fn rocket_authentication() {

codegen/src/shuttle_main/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl ToTokens for Loader {
232232
None
233233
} else {
234234
Some(parse_quote!(
235-
use shuttle_runtime::ResourceBuilder;
235+
use shuttle_runtime::{Factory, ResourceBuilder};
236236
))
237237
};
238238

@@ -391,7 +391,7 @@ mod tests {
391391
) -> ShuttleComplex {
392392
use shuttle_runtime::Context;
393393
use shuttle_runtime::tracing_subscriber::prelude::*;
394-
use shuttle_runtime::ResourceBuilder;
394+
use shuttle_runtime::{Factory, ResourceBuilder};
395395

396396
let filter_layer =
397397
shuttle_runtime::tracing_subscriber::EnvFilter::try_from_default_env()
@@ -506,7 +506,7 @@ mod tests {
506506
) -> ShuttleComplex {
507507
use shuttle_runtime::Context;
508508
use shuttle_runtime::tracing_subscriber::prelude::*;
509-
use shuttle_runtime::ResourceBuilder;
509+
use shuttle_runtime::{Factory, ResourceBuilder};
510510

511511
let filter_layer =
512512
shuttle_runtime::tracing_subscriber::EnvFilter::try_from_default_env()

resources/static-folder/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ impl<'a> ResourceBuilder<PathBuf> for StaticFolder<'a> {
6565

6666
trace!(output_directory = ?output_dir, "got output directory");
6767

68+
if output_dir.join(self.folder) == input_dir {
69+
return Ok(output_dir.join(self.folder));
70+
}
71+
6872
let copy_options = CopyOptions::new().overwrite(true);
6973
match copy(&input_dir, &output_dir, &copy_options) {
7074
Ok(_) => Ok(output_dir.join(self.folder)),

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub use logger::Logger;
227227
pub use next::{AxumWasm, NextArgs};
228228
pub use provisioner_factory::ProvisionerFactory;
229229
pub use shuttle_common::storage_manager::StorageManager;
230-
pub use shuttle_service::{main, CustomError, Error, ResourceBuilder, Service};
230+
pub use shuttle_service::{main, CustomError, Error, Factory, ResourceBuilder, Service};
231231

232232
// Dependencies required by the codegen
233233
pub use anyhow::Context;

0 commit comments

Comments
 (0)