Skip to content

Commit 55eba14

Browse files
committed
Update coverage for error and config packages
Currently, coverage rate has been upgraded for nativelink-error and nativelink-config packages.
1 parent cb9d441 commit 55eba14

File tree

4 files changed

+651
-17
lines changed

4 files changed

+651
-17
lines changed

nativelink-config/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
[dependencies]
77
byte-unit = { version = "5.1.4", default-features = false, features = ["byte"] }
88
humantime = "2.1.0"
9-
serde = { version = "1.0.210", default-features = false }
9+
serde = { version = "1.0.210", default-features = false, features = ["derive"] }
1010
serde_json5 = "0.1.0"
1111
shellexpand = { version = "3.1.0", default-features = false, features = ["base-0"] }
1212

nativelink-config/tests/deserialization_test.rs

+196-12
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,26 @@
1414

1515
use nativelink_config::serde_utils::{
1616
convert_data_size_with_shellexpand, convert_duration_with_shellexpand,
17+
convert_numeric_with_shellexpand, convert_optional_numeric_with_shellexpand,
18+
convert_optional_string_with_shellexpand, convert_string_with_shellexpand,
19+
convert_vec_string_with_shellexpand,
1720
};
1821
use pretty_assertions::assert_eq;
1922
use serde::Deserialize;
2023

24+
const INVALID_ENV_VAR_ERROR: &str =
25+
"error looking key 'INVALID_ENV_VAR' up: environment variable not found";
26+
const MAP_TYPE_ERROR: &str = "invalid type: map, expected a string containing json data";
27+
28+
const DURATION_HUMAN_READABLE: &str = r#"{"duration": "1m 10s"}"#;
29+
const DURATION_USIZE: &str = r#"{"duration": 10}"#;
30+
const DATA_SIZE_KIB: &str = r#"{"data_size": "1KiB"}"#;
31+
const DATA_SIZE_USIZE: &str = r#"{"data_size": 10}"#;
32+
const STRING_ENV_VAR: &str = r#"{"expanded_string": "$INVALID_ENV_VAR"}"#;
33+
const EMPTY_STRING_ENV_VAR: &str = r#"{"expanded_string": ""}"#;
34+
const NULL_STRING_ENV_VAR: &str = r#"{"expanded_string": null}"#;
35+
const VEC_STRING_EMPTY: &str = r#"{"expanded_strings": []}"#;
36+
2137
#[derive(Deserialize)]
2238
struct DurationEntity {
2339
#[serde(default, deserialize_with = "convert_duration_with_shellexpand")]
@@ -30,38 +46,206 @@ struct DataSizeEntity {
3046
data_size: usize,
3147
}
3248

49+
#[derive(Deserialize)]
50+
struct NumericEntity {
51+
#[serde(default, deserialize_with = "convert_numeric_with_shellexpand")]
52+
value: usize,
53+
}
54+
55+
#[derive(Deserialize)]
56+
struct OptionalNumericEntity {
57+
#[serde(
58+
default,
59+
deserialize_with = "convert_optional_numeric_with_shellexpand"
60+
)]
61+
optional_value: Option<usize>,
62+
}
63+
64+
#[derive(Deserialize)]
65+
struct VecStringEntity {
66+
#[serde(deserialize_with = "convert_vec_string_with_shellexpand")]
67+
expanded_strings: Vec<String>,
68+
}
69+
70+
#[derive(Deserialize)]
71+
struct OptionalStringEntity {
72+
#[serde(deserialize_with = "convert_optional_string_with_shellexpand")]
73+
expanded_string: Option<String>,
74+
}
75+
76+
#[derive(Deserialize)]
77+
struct StringEntity {
78+
#[serde(deserialize_with = "convert_string_with_shellexpand")]
79+
expanded_string: String,
80+
}
81+
3382
#[test]
3483
fn test_duration_human_readable_deserialize() {
35-
let example = r#"
36-
{"duration": "1m 10s"}
37-
"#;
84+
let example = DURATION_HUMAN_READABLE;
3885
let deserialized: DurationEntity = serde_json5::from_str(example).unwrap();
3986
assert_eq!(deserialized.duration, 70);
4087
}
4188

4289
#[test]
4390
fn test_duration_usize_deserialize() {
44-
let example = r#"
45-
{"duration": 10}
46-
"#;
91+
let example = DURATION_USIZE;
4792
let deserialized: DurationEntity = serde_json5::from_str(example).unwrap();
4893
assert_eq!(deserialized.duration, 10);
4994
}
5095

96+
#[test]
97+
fn test_duration_invalid_string() {
98+
let example = r#"{"duration": {size:10, in:8}}"#;
99+
let result: Result<DurationEntity, _> = serde_json5::from_str(example);
100+
assert!(result.is_err());
101+
if let Err(e) = result {
102+
assert_eq!(e.to_string(), MAP_TYPE_ERROR);
103+
}
104+
}
105+
51106
#[test]
52107
fn test_data_size_unit_deserialize() {
53-
let example = r#"
54-
{"data_size": "1KiB"}
55-
"#;
108+
let example = DATA_SIZE_KIB;
56109
let deserialized: DataSizeEntity = serde_json5::from_str(example).unwrap();
57110
assert_eq!(deserialized.data_size, 1024);
58111
}
59112

60113
#[test]
61114
fn test_data_size_usize_deserialize() {
62-
let example = r#"
63-
{"data_size": 10}
64-
"#;
115+
let example = DATA_SIZE_USIZE;
65116
let deserialized: DataSizeEntity = serde_json5::from_str(example).unwrap();
66117
assert_eq!(deserialized.data_size, 10);
67118
}
119+
120+
#[test]
121+
fn test_data_size_invalid_string() {
122+
let example = r#"{"data_size": {size:10, in:8}}"#;
123+
let result: Result<DataSizeEntity, _> = serde_json5::from_str(example);
124+
assert!(result.is_err());
125+
if let Err(e) = result {
126+
assert_eq!(e.to_string(), MAP_TYPE_ERROR);
127+
}
128+
}
129+
130+
#[test]
131+
fn test_numeric_with_shellexpand_integer() {
132+
let example = r#"{ "value": 42 }"#;
133+
let deserialized: NumericEntity = serde_json5::from_str(example).unwrap();
134+
assert_eq!(deserialized.value, 42);
135+
}
136+
137+
#[test]
138+
fn test_numeric_with_shellexpand_string() {
139+
let example = r#"{ "value": "100" }"#;
140+
let deserialized: NumericEntity = serde_json5::from_str(example).unwrap();
141+
assert_eq!(deserialized.value, 100);
142+
}
143+
144+
#[test]
145+
fn test_numeric_with_shellexpand_invalid_string() {
146+
let example = r#"{ "value": {size:10, in:8} }"#;
147+
let result: Result<NumericEntity, _> = serde_json5::from_str(example);
148+
assert!(result.is_err());
149+
if let Err(e) = result {
150+
assert_eq!(e.to_string(), MAP_TYPE_ERROR);
151+
}
152+
}
153+
154+
#[test]
155+
fn test_optional_numeric_with_shellexpand_integer() {
156+
let example = r#"{ "optional_value": 42 }"#;
157+
let deserialized: OptionalNumericEntity = serde_json5::from_str(example).unwrap();
158+
assert_eq!(deserialized.optional_value, Some(42));
159+
}
160+
161+
#[test]
162+
fn test_optional_numeric_with_shellexpand_string() {
163+
let example = r#"{ "optional_value": "100" }"#;
164+
let deserialized: OptionalNumericEntity = serde_json5::from_str(example).unwrap();
165+
assert_eq!(deserialized.optional_value, Some(100));
166+
}
167+
168+
#[test]
169+
fn test_optional_numeric_with_shellexpand_empty_string() {
170+
let example = r#"{"optional_value": ""}"#;
171+
let deserialized: OptionalNumericEntity = serde_json5::from_str(example).unwrap();
172+
assert_eq!(deserialized.optional_value, None);
173+
}
174+
175+
#[test]
176+
fn test_optional_numeric_with_shellexpand_invalid_string() {
177+
let example = r#"{ "optional_value": {size:10, in:8} }"#;
178+
let result: Result<OptionalNumericEntity, _> = serde_json5::from_str(example);
179+
assert!(result.is_err());
180+
if let Err(e) = result {
181+
assert_eq!(e.to_string(), MAP_TYPE_ERROR);
182+
}
183+
}
184+
185+
#[test]
186+
fn test_convert_string_with_shellexpand_invalid() {
187+
let example = STRING_ENV_VAR;
188+
let result: Result<StringEntity, _> = serde_json5::from_str(example);
189+
assert!(result.is_err());
190+
if let Err(e) = result {
191+
assert_eq!(e.to_string(), INVALID_ENV_VAR_ERROR);
192+
}
193+
}
194+
195+
#[test]
196+
fn test_convert_string_with_shellexpand_empty() {
197+
let example = EMPTY_STRING_ENV_VAR;
198+
let deserialized: StringEntity = serde_json5::from_str(example).unwrap();
199+
assert_eq!(deserialized.expanded_string, "");
200+
}
201+
202+
#[test]
203+
fn test_convert_vec_string_with_shellexpand_empty() {
204+
let example = VEC_STRING_EMPTY;
205+
let deserialized: VecStringEntity = serde_json5::from_str(example).unwrap();
206+
assert_eq!(deserialized.expanded_strings, Vec::<String>::new());
207+
}
208+
209+
#[test]
210+
fn test_convert_vec_string_with_shellexpand_invalid() {
211+
let example = r#"{"expanded_strings": ["$INVALID_ENV_VAR", "$ANOTHER_MISSING_VAR"]}"#;
212+
let result: Result<VecStringEntity, _> = serde_json5::from_str(example);
213+
assert!(result.is_err());
214+
if let Err(e) = result {
215+
assert_eq!(e.to_string(), INVALID_ENV_VAR_ERROR);
216+
}
217+
}
218+
219+
#[test]
220+
fn test_convert_vec_string_with_shellexpand_invalid_alternate() {
221+
let example = r#"{"expanded_strings": ["config.json", "$INVALID_ENV_VAR"]}"#;
222+
let result: Result<VecStringEntity, _> = serde_json5::from_str(example);
223+
assert!(result.is_err());
224+
if let Err(e) = result {
225+
assert_eq!(e.to_string(), INVALID_ENV_VAR_ERROR);
226+
}
227+
}
228+
229+
#[test]
230+
fn test_convert_optional_string_with_shellexpand_none() {
231+
let example = NULL_STRING_ENV_VAR;
232+
let deserialized: OptionalStringEntity = serde_json5::from_str(example).unwrap();
233+
assert_eq!(deserialized.expanded_string, None);
234+
}
235+
236+
#[test]
237+
fn test_convert_optional_string_with_shellexpand_empty() {
238+
let example = EMPTY_STRING_ENV_VAR;
239+
let deserialized: OptionalStringEntity = serde_json5::from_str(example).unwrap();
240+
assert_eq!(deserialized.expanded_string, Some(String::new()));
241+
}
242+
243+
#[test]
244+
fn test_convert_optional_string_with_shellexpand_invalid() {
245+
let example = STRING_ENV_VAR;
246+
let result: Result<OptionalStringEntity, _> = serde_json5::from_str(example);
247+
assert!(result.is_err());
248+
if let Err(e) = result {
249+
assert_eq!(e.to_string(), INVALID_ENV_VAR_ERROR);
250+
}
251+
}

nativelink-error/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.5.3"
44
edition = "2021"
55
autobins = false
66
autoexamples = false
7-
autotests = false
7+
autotests = true
88
autobenches = false
99

1010
[dependencies]
@@ -14,8 +14,8 @@ fred = { version = "9.2.1", default-features = false, features = [
1414
"enable-rustls-ring",
1515
] }
1616
hex = { version = "0.4.3", default-features = false }
17-
prost = { version = "0.13.3", default-features = false }
17+
prost = { version = "0.13.3", features = ["std"], default-features = false }
1818
prost-types = { version = "0.13.3", default-features = false }
19-
serde = { version = "1.0.210", default-features = false }
19+
serde = { version = "1.0.210", features = ["derive"], default-features = false }
2020
tokio = { version = "1.40.0", features = ["fs", "rt-multi-thread", "signal", "io-util"], default-features = false }
21-
tonic = { version = "0.12.3", features = ["transport", "tls"], default-features = false }
21+
tonic = { version = "0.12.3", features = ["transport"], default-features = false }

0 commit comments

Comments
 (0)