Skip to content

Commit 4eabaa2

Browse files
committed
test(config): support array of any types
Actually it is not supported yet. Will do in the next few commits.
1 parent bf0ec84 commit 4eabaa2

File tree

1 file changed

+217
-22
lines changed

1 file changed

+217
-22
lines changed

tests/testsuite/config.rs

Lines changed: 217 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use cargo::CargoResult;
1212
use cargo::core::features::{GitFeatures, GitoxideFeatures};
1313
use cargo::core::{PackageIdSpec, Shell};
1414
use cargo::util::auth::RegistryConfig;
15+
use cargo::util::context::Value;
1516
use cargo::util::context::{
1617
self, Definition, GlobalContext, JobsConfig, SslVersionConfig, StringList,
1718
};
@@ -1351,28 +1352,6 @@ Caused by:
13511352
);
13521353
}
13531354

1354-
#[cargo_test]
1355-
fn non_string_in_array() {
1356-
// Currently only strings are supported.
1357-
write_config_toml("foo = [1, 2, 3]");
1358-
let gctx = new_gctx();
1359-
assert_error(
1360-
gctx.get::<Vec<i32>>("foo").unwrap_err(),
1361-
str![[r#"
1362-
could not load Cargo configuration
1363-
1364-
Caused by:
1365-
failed to load TOML configuration from `[ROOT]/.cargo/config.toml`
1366-
1367-
Caused by:
1368-
failed to parse config at `foo[0]`
1369-
1370-
Caused by:
1371-
expected string but found integer at index 0
1372-
"#]],
1373-
);
1374-
}
1375-
13761355
#[cargo_test]
13771356
fn struct_with_opt_inner_struct() {
13781357
// Struct with a key that is Option of another struct.
@@ -2309,3 +2288,219 @@ fn build_std() {
23092288
]
23102289
);
23112290
}
2291+
2292+
#[cargo_test]
2293+
fn array_of_any_types() {
2294+
write_config_toml(
2295+
r#"
2296+
ints = [1, 2, 3]
2297+
2298+
bools = [true, false, true]
2299+
2300+
strings = ["hello", "world", "test"]
2301+
2302+
[[tables]]
2303+
name = "first"
2304+
value = 1
2305+
[[tables]]
2306+
name = "second"
2307+
value = 2
2308+
"#,
2309+
);
2310+
2311+
let gctx = new_gctx();
2312+
2313+
// Test integer array
2314+
assert_error(
2315+
gctx.get::<Vec<i32>>("ints").unwrap_err(),
2316+
str![[r#"
2317+
could not load Cargo configuration
2318+
2319+
Caused by:
2320+
failed to load TOML configuration from `[ROOT]/.cargo/config.toml`
2321+
2322+
Caused by:
2323+
failed to parse config at `ints[0]`
2324+
2325+
Caused by:
2326+
expected string but found integer at index 0
2327+
"#]],
2328+
);
2329+
2330+
assert_error(
2331+
gctx.get::<Vec<bool>>("bools").unwrap_err(),
2332+
str![[r#"
2333+
could not load Cargo configuration
2334+
2335+
Caused by:
2336+
failed to load TOML configuration from `[ROOT]/.cargo/config.toml`
2337+
2338+
Caused by:
2339+
failed to parse config at `ints[0]`
2340+
2341+
Caused by:
2342+
expected string but found integer at index 0
2343+
"#]],
2344+
);
2345+
2346+
#[derive(Deserialize, Debug, PartialEq)]
2347+
struct T {
2348+
name: String,
2349+
value: i32,
2350+
}
2351+
assert_error(
2352+
gctx.get::<Vec<T>>("tables").unwrap_err(),
2353+
str![[r#"
2354+
could not load Cargo configuration
2355+
2356+
Caused by:
2357+
failed to load TOML configuration from `[ROOT]/.cargo/config.toml`
2358+
2359+
Caused by:
2360+
failed to parse config at `ints[0]`
2361+
2362+
Caused by:
2363+
expected string but found integer at index 0
2364+
"#]],
2365+
);
2366+
}
2367+
2368+
#[cargo_test]
2369+
fn array_env() {
2370+
// for environment, only strings are supported.
2371+
let gctx = GlobalContextBuilder::new()
2372+
.env("CARGO_INTS", "3 4 5")
2373+
.env("CARGO_BOOLS", "false true false")
2374+
.env("CARGO_STRINGS", "env1 env2 env3")
2375+
.build();
2376+
2377+
assert_error(
2378+
gctx.get::<Vec<i32>>("ints").unwrap_err(),
2379+
str![[r#"invalid type: string "3", expected i32"#]],
2380+
);
2381+
2382+
assert_error(
2383+
gctx.get::<Vec<bool>>("bools").unwrap_err(),
2384+
str![[r#"invalid type: string "false", expected a boolean"#]],
2385+
);
2386+
2387+
assert_eq!(
2388+
gctx.get::<Vec<String>>("strings").unwrap(),
2389+
vec!["env1".to_string(), "env2".to_string(), "env3".to_string()],
2390+
);
2391+
}
2392+
2393+
#[cargo_test]
2394+
fn nested_array() {
2395+
let root_path = paths::root().join(".cargo/config.toml");
2396+
write_config_at(
2397+
&root_path,
2398+
r#"
2399+
nested_ints = [[1, 2], [3, 4]]
2400+
nested_bools = [[true], [false, true]]
2401+
nested_strings = [["a", "b"], ["3", "4"]]
2402+
nested_tables = [
2403+
[
2404+
{ x = "a" },
2405+
{ x = "b" },
2406+
],
2407+
[
2408+
{ x = "c" },
2409+
{ x = "d" },
2410+
],
2411+
]
2412+
deeply_nested = [[
2413+
{ x = [[[ { x = [], y = 2 } ]]], y = 1 },
2414+
]]
2415+
"#,
2416+
);
2417+
2418+
let gctx = GlobalContextBuilder::new().build();
2419+
2420+
assert_error(
2421+
gctx.get::<Vec<Vec<i32>>>("nested_ints").unwrap_err(),
2422+
str![[r#"
2423+
could not load Cargo configuration
2424+
2425+
Caused by:
2426+
failed to load TOML configuration from `[ROOT]/.cargo/config.toml`
2427+
2428+
Caused by:
2429+
failed to parse config at `nested_ints[0]`
2430+
2431+
Caused by:
2432+
expected string but found array at index 0
2433+
"#]],
2434+
);
2435+
2436+
// exercising Value and Definition
2437+
assert_error(
2438+
gctx.get::<Vec<Value<Vec<Value<i32>>>>>("nested_ints")
2439+
.unwrap_err(),
2440+
str![[r#"
2441+
could not load Cargo configuration
2442+
2443+
Caused by:
2444+
failed to load TOML configuration from `[ROOT]/.cargo/config.toml`
2445+
2446+
Caused by:
2447+
failed to parse config at `nested_ints[0]`
2448+
2449+
Caused by:
2450+
expected string but found array at index 0
2451+
"#]],
2452+
);
2453+
2454+
assert_error(
2455+
gctx.get::<Vec<Vec<bool>>>("nested_bools").unwrap_err(),
2456+
str![[r#"
2457+
could not load Cargo configuration
2458+
2459+
Caused by:
2460+
failed to load TOML configuration from `[ROOT]/.cargo/config.toml`
2461+
2462+
Caused by:
2463+
failed to parse config at `nested_ints[0]`
2464+
2465+
Caused by:
2466+
expected string but found array at index 0
2467+
"#]],
2468+
);
2469+
2470+
assert_error(
2471+
gctx.get::<Vec<Vec<String>>>("nested_strings").unwrap_err(),
2472+
str![[r#"
2473+
could not load Cargo configuration
2474+
2475+
Caused by:
2476+
failed to load TOML configuration from `[ROOT]/.cargo/config.toml`
2477+
2478+
Caused by:
2479+
failed to parse config at `nested_ints[0]`
2480+
2481+
Caused by:
2482+
expected string but found array at index 0
2483+
"#]],
2484+
);
2485+
2486+
#[derive(Deserialize, Debug, PartialEq)]
2487+
struct S {
2488+
x: Vec<Vec<Vec<S>>>,
2489+
y: i32,
2490+
}
2491+
assert_error(
2492+
gctx.get::<Vec<Vec<S>>>("deeply_nested").unwrap_err(),
2493+
str![[r#"
2494+
could not load Cargo configuration
2495+
2496+
Caused by:
2497+
failed to load TOML configuration from `[ROOT]/.cargo/config.toml`
2498+
2499+
Caused by:
2500+
failed to parse config at `nested_ints[0]`
2501+
2502+
Caused by:
2503+
expected string but found array at index 0
2504+
"#]],
2505+
);
2506+
}

0 commit comments

Comments
 (0)