Fix region selection when it's in a package configuration. · cargo-lambda/cargo-lambda@92462c2 · GitHub
Skip to content

Commit 92462c2

Browse files
committed
Fix region selection when it's in a package configuration.
1 parent bc645a5 commit 92462c2

7 files changed

Lines changed: 142 additions & 25 deletions

File tree

‎crates/cargo-lambda-cli/tests/cargo_lambda.rs‎

Lines changed: 50 additions & 0 deletions

‎crates/cargo-lambda-metadata/src/cargo/deploy.rs‎

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const DEFAULT_RUNTIME: &str = "provided.al2023";
2727
)]
2828
pub struct Deploy {
2929
#[command(flatten)]
30-
#[serde(default, skip_serializing_if = "Option::is_none")]
30+
#[serde(default, flatten, skip_serializing_if = "Option::is_none")]
3131
pub remote_config: Option<RemoteConfig>,
3232

3333
#[command(flatten)]
@@ -222,7 +222,7 @@ impl Serialize for Deploy {
222222
+ self.include.is_some() as usize
223223
+ self.dry as usize
224224
+ self.name.is_some() as usize
225-
+ self.remote_config.is_some() as usize
225+
+ self.remote_config.as_ref().map_or(0, |r| r.count_fields())
226226
+ self.function_config.count_fields();
227227

228228
let mut state = serializer.serialize_struct("Deploy", len)?;
@@ -270,7 +270,7 @@ impl Serialize for Deploy {
270270
state.serialize_field("name", name)?;
271271
}
272272
if let Some(ref remote_config) = self.remote_config {
273-
state.serialize_field("remote_config", remote_config)?;
273+
remote_config.serialize_fields::<S>(&mut state)?;
274274
}
275275
self.function_config.serialize_fields::<S>(&mut state)?;
276276

@@ -665,4 +665,66 @@ mod tests {
665665

666666
assert_eq!(config.deploy.function_config.log_retention, Some(14));
667667
}
668+
669+
#[test]
670+
fn test_load_region_from_package_metadata() {
671+
let metadata = load_metadata(fixture_metadata("single-binary-package")).unwrap();
672+
673+
let options = ConfigOptions {
674+
names: FunctionNames::from_package("basic-lambda"),
675+
..Default::default()
676+
};
677+
678+
let config = load_config_without_cli_flags(&metadata, &options).unwrap();
679+
680+
// Region and profile should be loaded from package.metadata.lambda.deploy
681+
let remote_config = config
682+
.deploy
683+
.remote_config
684+
.expect("remote_config should be Some");
685+
assert_eq!(
686+
remote_config.region,
687+
Some("eu-central-1".to_string()),
688+
"region should be loaded from Cargo.toml"
689+
);
690+
assert_eq!(
691+
remote_config.profile,
692+
Some("test-profile".to_string()),
693+
"profile should be loaded from Cargo.toml"
694+
);
695+
}
696+
697+
#[test]
698+
fn test_deploy_serialization_with_remote_config() {
699+
use cargo_lambda_remote::RemoteConfig;
700+
701+
let deploy = Deploy {
702+
remote_config: Some(RemoteConfig {
703+
region: Some("us-west-2".to_string()),
704+
profile: Some("production".to_string()),
705+
..Default::default()
706+
}),
707+
..Default::default()
708+
};
709+
710+
// Serialize to JSON
711+
let serialized = serde_json::to_value(&deploy).unwrap();
712+
713+
// Should serialize as flat fields at top level
714+
assert_eq!(serialized["region"], "us-west-2");
715+
assert_eq!(serialized["profile"], "production");
716+
assert!(
717+
!serialized
718+
.as_object()
719+
.unwrap()
720+
.contains_key("remote_config"),
721+
"remote_config should be flattened, not nested"
722+
);
723+
724+
// Deserialize back
725+
let deserialized: Deploy = serde_json::from_value(serialized).unwrap();
726+
let rc = deserialized.remote_config.unwrap();
727+
assert_eq!(rc.region, Some("us-west-2".to_string()));
728+
assert_eq!(rc.profile, Some("production".to_string()));
729+
}
668730
}

‎crates/cargo-lambda-new/src/lib.rs‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -433,16 +433,16 @@ fn should_render_file(
433433
return false;
434434
};
435435

436-
if let Some(condition_value) = &condition.r#match {
437-
if condition_value.to_value() == *variable {
438-
return true;
439-
}
436+
if let Some(condition_value) = &condition.r#match
437+
&& condition_value.to_value() == *variable
438+
{
439+
return true;
440440
}
441441

442-
if let Some(condition_value) = &condition.not_match {
443-
if condition_value.to_value() != *variable {
444-
return true;
445-
}
442+
if let Some(condition_value) = &condition.not_match
443+
&& condition_value.to_value() != *variable
444+
{
445+
return true;
446446
}
447447
}
448448

@@ -481,16 +481,16 @@ fn should_ignore_file(
481481
return false;
482482
};
483483

484-
if let Some(condition_value) = &condition.r#match {
485-
if condition_value.to_value() == *variable {
486-
return true;
487-
}
484+
if let Some(condition_value) = &condition.r#match
485+
&& condition_value.to_value() == *variable
486+
{
487+
return true;
488488
}
489489

490-
if let Some(condition_value) = &condition.not_match {
491-
if condition_value.to_value() != *variable {
492-
return true;
493-
}
490+
if let Some(condition_value) = &condition.not_match
491+
&& condition_value.to_value() != *variable
492+
{
493+
return true;
494494
}
495495
}
496496

‎crates/cargo-lambda-new/src/template.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,12 @@ fn is_remote_zip_file(path: &str) -> bool {
268268

269269
fn find_local_zip_file(value: &str) -> Option<PathBuf> {
270270
// ignore error to fallback to other template options.
271-
if let Ok(path) = dunce::realpath(value) {
272-
if path.exists() && path.is_file() && path.extension().unwrap_or_default() == "zip" {
273-
return Some(path);
274-
}
271+
if let Ok(path) = dunce::realpath(value)
272+
&& path.exists()
273+
&& path.is_file()
274+
&& path.extension().unwrap_or_default() == "zip"
275+
{
276+
return Some(path);
275277
}
276278

277279
None

‎crates/cargo-lambda-new/src/template/config.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl TemplatePrompt {
152152
}
153153
}
154154

155-
fn text_prompt(&self) -> Text {
155+
fn text_prompt(&self) -> Text<'_> {
156156
let mut prompt = Text::new(&self.message);
157157

158158
if let Some(choices) = &self.choices {

‎crates/cargo-lambda-watch/src/watcher/ignore.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
collections::HashSet,
33
path::{Path, PathBuf},
4+
slice,
45
sync::Arc,
56
};
67

@@ -142,7 +143,7 @@ pub(crate) async fn create_filter(
142143
.applies_in
143144
.clone()
144145
.unwrap_or_else(|| base.to_path_buf());
145-
let filter = IgnoreFilter::new(&base, &[file.clone()])
146+
let filter = IgnoreFilter::new(&base, slice::from_ref(file))
146147
.await
147148
.map_err(ServerError::InvalidIgnoreFiles)?;
148149
filters.push(filter);

‎tests/fixtures/single-binary-package/Cargo.toml‎

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)