Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: static folder local run clearing file contents, add missing tests in cargo-shuttle #717

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ impl Shuttle {
println!("First, let's log in to your Shuttle account.");
self.login(args.login_args.clone()).await?;
println!();
} else if args.new && args.login_args.api_key.is_some() {
} else if args.login_args.api_key.is_some() {
self.login(args.login_args.clone()).await?;
} else {
} else if args.new {
bail!("Tried to login to create a Shuttle environment, but no API key was set.")
}
}
Expand Down
23 changes: 7 additions & 16 deletions cargo-shuttle/tests/integration/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ async fn non_interactive_basic_init() {
assert!(cargo_toml.contains("shuttle-runtime = "));
}

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

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

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

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

let lib_file = read_to_string(path.join("src").join("lib.rs")).unwrap();
let main_file = read_to_string(path.join("src").join("main.rs")).unwrap();
let expected = indoc! {r#"
#[macro_use]
extern crate rocket;
Expand All @@ -190,12 +181,12 @@ fn assert_valid_rocket_project(path: &Path, name_prefix: &str) {
"Hello, world!"
}
#[shuttle_service::main]
async fn rocket() -> shuttle_service::ShuttleRocket {
#[shuttle_runtime::main]
async fn rocket() -> shuttle_rocket::ShuttleRocket {
let rocket = rocket::build().mount("/hello", routes![index]);
Ok(rocket)
Ok(rocket.into())
}"#};

assert_eq!(lib_file, expected);
assert_eq!(main_file, expected);
}
52 changes: 52 additions & 0 deletions cargo-shuttle/tests/integration/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,58 @@ async fn rocket_postgres() {
assert_eq!(request_text, "{\"id\":1,\"note\":\"Deploy to shuttle\"}");
}

#[tokio::test(flavor = "multi_thread")]
async fn axum_static_files() {
let url = cargo_shuttle_run("../examples/axum/static-files", false).await;
let client = reqwest::Client::new();

let request_text = client
.get(format!("{url}/hello"))
.send()
.await
.unwrap()
.text()
.await
.unwrap();

assert_eq!(request_text, "Hello, world!");

let request_text = client.get(url).send().await.unwrap().text().await.unwrap();

assert!(
request_text.contains("This is an example of serving static files with axum and shuttle.")
);
}

#[tokio::test(flavor = "multi_thread")]
async fn shuttle_next() {
let url = cargo_shuttle_run("../examples/next/hello-world", false).await;
let client = reqwest::Client::new();

let request_text = client
.get(format!("{url}/hello"))
.send()
.await
.unwrap()
.text()
.await
.unwrap();

assert_eq!(request_text, "Hello, World!");

let post_text = client
.post(format!("{url}/uppercase"))
.body("uppercase this")
.send()
.await
.unwrap()
.text()
.await
.unwrap();

assert_eq!(post_text, "UPPERCASE THIS");
}

#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn rocket_authentication() {
Expand Down
6 changes: 3 additions & 3 deletions codegen/src/shuttle_main/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl ToTokens for Loader {
None
} else {
Some(parse_quote!(
use shuttle_runtime::ResourceBuilder;
use shuttle_runtime::{Factory, ResourceBuilder};
))
};

Expand Down Expand Up @@ -391,7 +391,7 @@ mod tests {
) -> ShuttleComplex {
use shuttle_runtime::Context;
use shuttle_runtime::tracing_subscriber::prelude::*;
use shuttle_runtime::ResourceBuilder;
use shuttle_runtime::{Factory, ResourceBuilder};

let filter_layer =
shuttle_runtime::tracing_subscriber::EnvFilter::try_from_default_env()
Expand Down Expand Up @@ -506,7 +506,7 @@ mod tests {
) -> ShuttleComplex {
use shuttle_runtime::Context;
use shuttle_runtime::tracing_subscriber::prelude::*;
use shuttle_runtime::ResourceBuilder;
use shuttle_runtime::{Factory, ResourceBuilder};

let filter_layer =
shuttle_runtime::tracing_subscriber::EnvFilter::try_from_default_env()
Expand Down
4 changes: 4 additions & 0 deletions resources/static-folder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ impl<'a> ResourceBuilder<PathBuf> for StaticFolder<'a> {

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

if output_dir.join(self.folder) == input_dir {
return Ok(output_dir.join(self.folder));
}

let copy_options = CopyOptions::new().overwrite(true);
match copy(&input_dir, &output_dir, &copy_options) {
Ok(_) => Ok(output_dir.join(self.folder)),
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub use logger::Logger;
pub use next::{AxumWasm, NextArgs};
pub use provisioner_factory::ProvisionerFactory;
pub use shuttle_common::storage_manager::StorageManager;
pub use shuttle_service::{main, CustomError, Error, ResourceBuilder, Service};
pub use shuttle_service::{main, CustomError, Error, Factory, ResourceBuilder, Service};

// Dependencies required by the codegen
pub use anyhow::Context;
Expand Down