From 1b03ef21f34fc4acf890b01cfca3d07158ef5c46 Mon Sep 17 00:00:00 2001 From: Brahm Lower Date: Sun, 20 Nov 2022 09:27:48 -0800 Subject: [PATCH 1/4] fix(config): unrecognized config properties don't cause config error (#4547) * Fix #4481, config does not error when unrecognized properties are present * cleanup: use stuct update syntax to improve readability from review feedback Co-authored-by: David Knaack * cleanup: renamed ValueDeserializer func w/ better name * cleanup: added test to cover unknown key retry condition Co-authored-by: David Knaack --- src/config.rs | 30 +++++++++++++++++++++++++++++- src/serde_utils.rs | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 35cb86563..e0aa016ac 100644 --- a/src/config.rs +++ b/src/config.rs @@ -48,7 +48,17 @@ impl<'a, T: Deserialize<'a> + Default> ModuleConfig<'a, ValueError> for T { /// Create `ValueDeserializer` wrapper and use it to call `Deserialize::deserialize` on it. fn from_config(config: &'a Value) -> Result { let deserializer = ValueDeserializer::new(config); - T::deserialize(deserializer) + T::deserialize(deserializer).or_else(|err| { + // If the error is an unrecognized key, print a warning and run + // deserialize ignoring that error. Otherwise, just return the error + if err.to_string().contains("Unknown key") { + log::warn!("{}", err); + let deserializer2 = ValueDeserializer::new(config).with_allow_unknown_keys(); + T::deserialize(deserializer2) + } else { + Err(err) + } + }) } } @@ -582,6 +592,24 @@ mod tests { assert_eq!(rust_config.switch_c, Switch::Off); } + #[test] + fn test_load_unknown_key_config() { + #[derive(Clone, Default, Deserialize)] + #[serde(default)] + struct TestConfig<'a> { + pub foo: &'a str, + } + + let config = toml::toml! { + foo = "test" + bar = "ignore me" + }; + let rust_config = TestConfig::from_config(&config); + + assert!(rust_config.is_ok()); + assert_eq!(rust_config.unwrap().foo, "test"); + } + #[test] fn test_from_string() { let config = Value::String(String::from("S")); diff --git a/src/serde_utils.rs b/src/serde_utils.rs index c1c383848..b47c02cd5 100644 --- a/src/serde_utils.rs +++ b/src/serde_utils.rs @@ -13,6 +13,7 @@ pub struct ValueDeserializer<'de> { value: &'de Value, info: Option, current_key: Option<&'de str>, + error_on_ignored: bool, } /// When deserializing a struct, this struct stores information about the struct. @@ -28,14 +29,28 @@ impl<'de> ValueDeserializer<'de> { value, info: None, current_key: None, + error_on_ignored: true, } } - fn with_info(value: &'de Value, info: Option, current_key: &'de str) -> Self { + fn with_info( + value: &'de Value, + info: Option, + current_key: &'de str, + ignored: bool, + ) -> Self { ValueDeserializer { value, info, current_key: Some(current_key), + error_on_ignored: ignored, + } + } + + pub fn with_allow_unknown_keys(self) -> Self { + ValueDeserializer { + error_on_ignored: false, + ..self } } } @@ -83,7 +98,12 @@ impl<'de> Deserializer<'de> for ValueDeserializer<'de> { let map = MapDeserializer::new(t.iter().map(|(k, v)| { ( k.as_str(), - ValueDeserializer::with_info(v, self.info, k.as_str()), + ValueDeserializer::with_info( + v, + self.info, + k.as_str(), + self.error_on_ignored, + ), ) })); map.deserialize_map(visitor) @@ -131,6 +151,10 @@ impl<'de> Deserializer<'de> for ValueDeserializer<'de> { return visitor.visit_none(); } + if !self.error_on_ignored { + return visitor.visit_none(); + } + let did_you_mean = match (self.current_key, self.info) { (Some(key), Some(StructInfo { fields, .. })) => fields .iter() @@ -322,13 +346,17 @@ mod test { let value = toml::toml! { unknown_key = "foo" }; - let deserializer = ValueDeserializer::new(&value); + let deserializer = ValueDeserializer::new(&value); let result = StarshipRootConfig::deserialize(deserializer).unwrap_err(); assert_eq!( format!("{result}"), "Error in 'StarshipRoot' at 'unknown_key': Unknown key" ); + + let deserializer2 = ValueDeserializer::new(&value).with_allow_unknown_keys(); + let result = StarshipRootConfig::deserialize(deserializer2); + assert!(result.is_ok()); } #[test] From a9eb65ef35de948880cbf340ffbfe6af126e5e44 Mon Sep 17 00:00:00 2001 From: Scott Palmer Date: Sun, 20 Nov 2022 12:28:39 -0500 Subject: [PATCH 2/4] fix(java): Improved regex for Java version (starship#4610) (#4616) * fix(java): Improved regex for Java version (starship#4610) * fix(java): Fixed tests and tweaked regex to accomodate Eclipse OpenJ9 (starship#4610) --- src/modules/java.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/modules/java.rs b/src/modules/java.rs index da96bfd09..573c9ff8f 100644 --- a/src/modules/java.rs +++ b/src/modules/java.rs @@ -5,7 +5,7 @@ use crate::utils::get_command_string_output; use std::path::PathBuf; use regex::Regex; -const JAVA_VERSION_PATTERN: &str = "(?P[\\d\\.]+)[^\\s]*\\s(?:built|from)"; +const JAVA_VERSION_PATTERN: &str = "(?:JRE.*\\(|OpenJ9 )(?P[\\d]+(?:\\.\\d+){0,2})"; /// Creates a module with the current Java version pub fn module<'a>(context: &'a Context) -> Option> { @@ -120,15 +120,17 @@ mod tests { fn test_parse_java_version_zulu() { let java_8 = "OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (Zulu 8.40.0.25-CA-linux64) (1.8.0_222-b10), built on Jul 11 2019 11:36:39 by \"zulu_re\" with gcc 4.4.7 20120313 (Red Hat 4.4.7-3)"; let java_11 = "OpenJDK 64-Bit Server VM (11.0.4+11-LTS) for linux-amd64 JRE (Zulu11.33+15-CA) (11.0.4+11-LTS), built on Jul 11 2019 21:37:17 by \"zulu_re\" with gcc 4.9.2 20150212 (Red Hat 4.9.2-6)"; + let java_17 = "OpenJDK 64-Bit Server VM (17.0.5+8-LTS) for bsd-amd64 JRE (17.0.5+8-LTS) (Zulu17.38+21-CA), built on Oct 7 2022 06:03:12 by \"zulu_re\" with clang 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.17)"; assert_eq!(parse_java_version(java_8), Some("1.8.0".to_string())); assert_eq!(parse_java_version(java_11), Some("11.0.4".to_string())); + assert_eq!(parse_java_version(java_17), Some("17.0.5".to_string())); } #[test] fn test_parse_java_version_eclipse_openj9() { let java_8 = "Eclipse OpenJ9 OpenJDK 64-bit Server VM (1.8.0_222-b10) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 8.0.222.0, built on Jul 17 2019 21:29:18 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)"; let java_11 = "Eclipse OpenJ9 OpenJDK 64-bit Server VM (11.0.4+11) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 11.0.4.0, built on Jul 17 2019 21:51:37 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)"; - assert_eq!(parse_java_version(java_8), Some("1.8.0".to_string())); + assert_eq!(parse_java_version(java_8), Some("8.0.222".to_string())); assert_eq!(parse_java_version(java_11), Some("11.0.4".to_string())); } From 0e1dd6f7074c17d03385f17b511a414c5f3eaa93 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Mon, 21 Nov 2022 11:02:27 +0000 Subject: [PATCH 3/4] build(deps): update rust crate open to 3.2.0 --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e6131af92..8f3c4581c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1917,12 +1917,12 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "open" -version = "3.0.3" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4a3100141f1733ea40b53381b0ae3117330735ef22309a190ac57b9576ea716" +checksum = "2078c0039e6a54a0c42c28faa984e115fb4c2d5bf2208f77d1961002df8576f8" dependencies = [ "pathdiff", - "windows-sys 0.36.1", + "windows-sys 0.42.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index dea890ced..d60a4e5ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,7 +59,7 @@ log = { version = "0.4.17", features = ["std"] } notify-rust = { version = "4.5.10", optional = true } nu-ansi-term = "0.46.0" once_cell = "1.16.0" -open = "3.0.3" +open = "3.2.0" # update os module config and tests when upgrading os_info os_info = "3.5.1" path-slash = "0.2.1" From e4dbff0fc7e88f792b90703f03f83e31d401b90e Mon Sep 17 00:00:00 2001 From: Marco Ieni <11428655+MarcoIeni@users.noreply.github.com> Date: Mon, 21 Nov 2022 17:54:59 +0100 Subject: [PATCH 4/4] fix(ci): cache after selecting the toolchain (#4619) --- .github/workflows/workflow.yml | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 191d71be4..2d965a765 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -50,9 +50,6 @@ jobs: - name: Setup | Checkout uses: actions/checkout@v3 - - name: Setup | Cache - uses: Swatinem/rust-cache@v2 - - name: Setup | Rust uses: actions-rs/toolchain@v1.0.7 with: @@ -61,6 +58,9 @@ jobs: profile: minimal components: clippy + - name: Setup | Cache + uses: Swatinem/rust-cache@v2 + - name: Build | Lint uses: actions-rs/cargo@v1.0.3 with: @@ -75,9 +75,6 @@ jobs: - name: Setup | Checkout uses: actions/checkout@v3 - - name: Setup | Cache - uses: Swatinem/rust-cache@v2 - - name: Setup | Rust uses: actions-rs/toolchain@v1.0.7 with: @@ -85,6 +82,9 @@ jobs: profile: minimal override: true + - name: Setup | Cache + uses: Swatinem/rust-cache@v2 + - name: Build | Check run: cargo check --workspace --locked @@ -97,9 +97,6 @@ jobs: - name: Setup | Checkout uses: actions/checkout@v3 - - name: Setup | Cache - uses: Swatinem/rust-cache@v2 - - name: Setup | Rust uses: actions-rs/toolchain@v1.0.7 with: @@ -107,6 +104,9 @@ jobs: profile: minimal override: true + - name: Setup | Cache + uses: Swatinem/rust-cache@v2 + - name: Build | Check run: cargo check --workspace --locked --no-default-features @@ -119,9 +119,6 @@ jobs: - name: Setup | Checkout uses: actions/checkout@v3 - - name: Setup | Cache - uses: Swatinem/rust-cache@v2 - - name: Setup | Rust uses: actions-rs/toolchain@v1.0.7 with: @@ -129,6 +126,9 @@ jobs: profile: minimal override: true + - name: Setup | Cache + uses: Swatinem/rust-cache@v2 + - name: Build | Check run: cargo check --workspace --locked --all-features @@ -141,9 +141,6 @@ jobs: - name: Setup | Checkout uses: actions/checkout@v3 - - name: Setup | Cache - uses: Swatinem/rust-cache@v2 - - name: Setup | Rust uses: actions-rs/toolchain@v1.0.7 with: @@ -151,6 +148,9 @@ jobs: profile: minimal override: true + - name: Setup | Cache + uses: Swatinem/rust-cache@v2 + - name: Run | Generate Schema run: cargo run --locked --features config-schema -- config-schema > .github/config-schema.json @@ -177,9 +177,6 @@ jobs: - name: Setup | Checkout uses: actions/checkout@v3 - - name: Setup | Cache - uses: Swatinem/rust-cache@v2 - # Install all the required dependencies for testing - name: Setup | Rust uses: actions-rs/toolchain@v1.0.7 @@ -189,6 +186,9 @@ jobs: profile: minimal override: true + - name: Setup | Cache + uses: Swatinem/rust-cache@v2 + - name: Install cargo-llvm-cov uses: taiki-e/install-action@cargo-llvm-cov