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

Update coverage for nativelink-config and nativelink-error #1445

Closed
wants to merge 8 commits into from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ nixos.bazelrc
rust-project.json
darwin.bazelrc
nativelink.bazelrc
coverage_report/
.gitignore
2 changes: 1 addition & 1 deletion nativelink-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
byte-unit = { version = "5.1.4", default-features = false, features = ["byte"] }
humantime = "2.1.0"
serde = { version = "1.0.210", default-features = false }
serde = { version = "1.0.210", default-features = false, features = ["derive"] }
serde_json5 = "0.1.0"
shellexpand = { version = "3.1.0", default-features = false, features = ["base-0"] }

Expand Down
202 changes: 187 additions & 15 deletions nativelink-config/tests/deserialization_test.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
// Copyright 2024 The NativeLink Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use nativelink_config::serde_utils::{
convert_data_size_with_shellexpand, convert_duration_with_shellexpand,
convert_data_size_with_shellexpand,
convert_duration_with_shellexpand,
convert_numeric_with_shellexpand,
convert_optional_numeric_with_shellexpand,
convert_string_with_shellexpand,
convert_vec_string_with_shellexpand,
convert_optional_string_with_shellexpand,
};
use pretty_assertions::assert_eq;
use serde::Deserialize;
Expand All @@ -30,6 +22,36 @@ struct DataSizeEntity {
data_size: usize,
}

#[derive(Deserialize)]
struct NumericEntity {
#[serde(default, deserialize_with = "convert_numeric_with_shellexpand")]
value: usize,
}

#[derive(Deserialize)]
struct OptionalNumericEntity {
#[serde(default, deserialize_with = "convert_optional_numeric_with_shellexpand")]
optional_value: Option<usize>,
}

#[derive(Deserialize)]
struct VecStringEntity {
#[serde(deserialize_with = "convert_vec_string_with_shellexpand")]
expanded_strings: Vec<String>,
}

#[derive(Deserialize)]
struct OptionalStringEntity {
#[serde(deserialize_with = "convert_optional_string_with_shellexpand")]
expanded_string: Option<String>,
}

#[derive(Deserialize)]
struct StringEntity {
#[serde(deserialize_with = "convert_string_with_shellexpand")]
expanded_string: String,
}

#[test]
fn test_duration_human_readable_deserialize() {
let example = r#"
Expand All @@ -48,6 +70,15 @@ fn test_duration_usize_deserialize() {
assert_eq!(deserialized.duration, 10);
}

#[test]
fn test_duration_invalid_string() {
let example = r#"
{"duration": {size:10, in:8}}
"#;
let result: Result<DurationEntity, _> = serde_json5::from_str(example);
assert!(result.is_err());
}

#[test]
fn test_data_size_unit_deserialize() {
let example = r#"
Expand All @@ -65,3 +96,144 @@ fn test_data_size_usize_deserialize() {
let deserialized: DataSizeEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.data_size, 10);
}

#[test]
fn test_data_size_invalid_string() {
let example = r#"
{"data_size": {size:10, in:8}}
"#;
let result: Result<DataSizeEntity, _> = serde_json5::from_str(example);
assert!(result.is_err());
}

#[test]
fn test_numeric_with_shellexpand_integer() {
let example = r#"{ "value": 42 }"#;
let deserialized: NumericEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.value, 42);
}

#[test]
fn test_numeric_with_shellexpand_string() {
let example = r#"{ "value": "100" }"#;
let deserialized: NumericEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.value, 100);
}

#[test]
fn test_numeric_with_shellexpand_invalid_string() {
let example = r#"{ "value": {size:10, in:8} }"#;
let result: Result<NumericEntity, _> = serde_json5::from_str(example);
assert!(result.is_err());
}

#[test]
fn test_optional_numeric_with_shellexpand_integer() {
let example = r#"{ "optional_value": 42 }"#;
let deserialized: OptionalNumericEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.optional_value, Some(42));
}

#[test]
fn test_optional_numeric_with_shellexpand_string() {
let example = r#"{ "optional_value": "100" }"#;
let deserialized: OptionalNumericEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.optional_value, Some(100));
}

#[test]
fn test_optional_numeric_with_shellexpand_empty_string() {
let example = r#"
{"optional_value": ""}
"#;
let deserialized: OptionalNumericEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.optional_value, None);
}

#[test]
fn test_optional_numeric_with_shellexpand_invalid_string() {
let example = r#"{ "optional_value": {size:10, in:8} }"#;
let result: Result<OptionalNumericEntity, _> = serde_json5::from_str(example);
assert!(result.is_err());
}

#[test]
fn test_convert_string_with_shellexpand_literal_string() {
let example = r#"
{"expanded_string": "Hello, World!"}
"#;
let deserialized: StringEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.expanded_string, "Hello, World!");
}

#[test]
fn test_convert_string_with_shellexpand_invalid() {
let example = r#"
{"expanded_string": "$INVALID_ENV_VAR"}
"#;
let result: Result<StringEntity, _> = serde_json5::from_str(example);
assert!(result.is_err());
}

#[test]
fn test_convert_string_with_shellexpand_empty() {
let example = r#"
{"expanded_string": ""}
"#;
let deserialized: StringEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.expanded_string, "");
}

#[test]
fn test_convert_vec_string_with_shellexpand_empty() {
let example = r#"
{"expanded_strings": []}
"#;
let deserialized: VecStringEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.expanded_strings, Vec::<String>::new());
}

#[test]
fn test_convert_vec_string_with_shellexpand_invalid() {
let example = r#"
{"expanded_strings": ["$HOME", "$INVALID_ENV_VAR"]}
"#;
let result: Result<VecStringEntity, _> = serde_json5::from_str(example);
assert!(result.is_err());
}

#[test]
fn test_convert_vec_string_with_shellexpand_mixed() {
let example = r#"
{"expanded_strings": ["$HOME", "config.json", "$INVALID_ENV_VAR"]}
"#;
let result: Result<VecStringEntity, _> = serde_json5::from_str(example);
assert!(result.is_err());
}

#[test]
fn test_convert_optional_string_with_shellexpand_none() {
let example = r#"
{"expanded_string": null}
"#;
let deserialized: OptionalStringEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.expanded_string, None);
}

#[test]
fn test_convert_optional_string_with_shellexpand_empty() {
let example = r#"
{"expanded_string": ""}
"#;
let deserialized: OptionalStringEntity = serde_json5::from_str(example).unwrap();
assert_eq!(deserialized.expanded_string, Some("".to_string()));
}

#[test]
fn test_convert_optional_string_with_shellexpand_invalid() {
let example = r#"
{"expanded_string": "$INVALID_ENV_VAR"}
"#;
let result: Result<OptionalStringEntity, _> = serde_json5::from_str(example);
assert!(result.is_err());
}
9 changes: 5 additions & 4 deletions nativelink-error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.5.3"
edition = "2021"
autobins = false
autoexamples = false
autotests = false
autotests = true
autobenches = false

[dependencies]
Expand All @@ -14,8 +14,9 @@ fred = { version = "9.2.1", default-features = false, features = [
"enable-rustls-ring",
] }
hex = { version = "0.4.3", default-features = false }
prost = { version = "0.13.3", default-features = false }
prost = { version = "0.13.3", features = ["std"], default-features = false }
prost-types = { version = "0.13.3", default-features = false }
serde = { version = "1.0.210", default-features = false }
serde = { version = "1.0", features = ["derive"], default-features = false }
tokio = { version = "1.40.0", features = ["fs", "rt-multi-thread", "signal", "io-util"], default-features = false }
tonic = { version = "0.12.3", features = ["transport", "tls"], default-features = false }
tonic = { version = "0.12.3", features = ["transport", "tls", "codegen", "prost"], default-features = false }

5 changes: 3 additions & 2 deletions nativelink-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
// limitations under the License.

use std::convert::Into;
use serde::{Deserialize, Serialize};

use nativelink_metric::{
MetricFieldData, MetricKind, MetricPublishKnownKindData, MetricsComponent,
};
use prost_types::TimestampError;
use serde::{Deserialize, Serialize};

#[macro_export]
macro_rules! make_err {
Expand Down Expand Up @@ -151,10 +151,11 @@ impl std::fmt::Display for Error {

impl From<prost::DecodeError> for Error {
fn from(err: prost::DecodeError) -> Self {
make_err!(Code::Internal, "{}", err.to_string())
Error::new(Code::Internal, err.to_string())
}
}


impl From<prost::EncodeError> for Error {
fn from(err: prost::EncodeError) -> Self {
make_err!(Code::Internal, "{}", err.to_string())
Expand Down
Loading
Loading