2019-11-25 02:42:45 +00:00
|
|
|
//! Tests for config settings.
|
|
|
|
|
2021-08-14 21:45:25 +00:00
|
|
|
use cargo::core::{PackageIdSpec, Shell};
|
2023-06-02 14:10:10 +00:00
|
|
|
use cargo::util::config::{self, Config, Definition, JobsConfig, SslVersionConfig, StringList};
|
2019-12-27 03:59:19 +00:00
|
|
|
use cargo::CargoResult;
|
2021-06-16 01:04:50 +00:00
|
|
|
use cargo_test_support::compare;
|
2021-06-16 01:11:02 +00:00
|
|
|
use cargo_test_support::{panic_error, paths, project, symlink_supported, t};
|
2023-12-15 18:51:40 +00:00
|
|
|
use cargo_util_schemas::manifest::TomlTrimPaths;
|
|
|
|
use cargo_util_schemas::manifest::TomlTrimPathsValue;
|
|
|
|
use cargo_util_schemas::manifest::{self as cargo_toml, TomlDebugInfo, VecStringOrBool as VSOB};
|
2019-12-27 03:59:19 +00:00
|
|
|
use serde::Deserialize;
|
2018-12-11 01:55:13 +00:00
|
|
|
use std::borrow::Borrow;
|
2019-11-28 20:47:22 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2018-12-11 01:55:13 +00:00
|
|
|
use std::fs;
|
2019-08-29 07:39:15 +00:00
|
|
|
use std::io;
|
|
|
|
use std::os;
|
2019-11-30 21:55:52 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-12-11 01:55:13 +00:00
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
/// Helper for constructing a `Config` object.
|
|
|
|
pub struct ConfigBuilder {
|
|
|
|
env: HashMap<String, String>,
|
|
|
|
unstable: Vec<String>,
|
2019-11-30 21:55:52 +00:00
|
|
|
config_args: Vec<String>,
|
|
|
|
cwd: Option<PathBuf>,
|
2023-09-07 02:36:08 +00:00
|
|
|
root: Option<PathBuf>,
|
2021-02-18 18:46:31 +00:00
|
|
|
enable_nightly_features: bool,
|
2019-11-28 20:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigBuilder {
|
|
|
|
pub fn new() -> ConfigBuilder {
|
|
|
|
ConfigBuilder {
|
|
|
|
env: HashMap::new(),
|
|
|
|
unstable: Vec::new(),
|
2019-11-30 21:55:52 +00:00
|
|
|
config_args: Vec::new(),
|
2023-09-07 02:36:08 +00:00
|
|
|
root: None,
|
2019-11-30 21:55:52 +00:00
|
|
|
cwd: None,
|
2021-02-18 18:46:31 +00:00
|
|
|
enable_nightly_features: false,
|
2019-11-28 20:47:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Passes a `-Z` flag.
|
|
|
|
pub fn unstable_flag(&mut self, s: impl Into<String>) -> &mut Self {
|
|
|
|
self.unstable.push(s.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets an environment variable.
|
|
|
|
pub fn env(&mut self, key: impl Into<String>, val: impl Into<String>) -> &mut Self {
|
|
|
|
self.env.insert(key.into(), val.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-05-07 17:07:28 +00:00
|
|
|
/// Unconditionally enable nightly features, even on stable channels.
|
2021-02-24 19:56:14 +00:00
|
|
|
pub fn nightly_features_allowed(&mut self, allowed: bool) -> &mut Self {
|
|
|
|
self.enable_nightly_features = allowed;
|
2021-02-18 18:46:31 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-11-30 21:55:52 +00:00
|
|
|
/// Passes a `--config` flag.
|
|
|
|
pub fn config_arg(&mut self, arg: impl Into<String>) -> &mut Self {
|
|
|
|
self.config_args.push(arg.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the current working directory where config files will be loaded.
|
2023-09-07 02:36:08 +00:00
|
|
|
///
|
|
|
|
/// Default is the root from [`ConfigBuilder::root`] or [`paths::root`].
|
2019-12-01 18:19:02 +00:00
|
|
|
pub fn cwd(&mut self, path: impl AsRef<Path>) -> &mut Self {
|
2023-09-07 02:36:08 +00:00
|
|
|
let path = path.as_ref();
|
|
|
|
let cwd = self
|
|
|
|
.root
|
|
|
|
.as_ref()
|
|
|
|
.map_or_else(|| paths::root().join(path), |r| r.join(path));
|
|
|
|
self.cwd = Some(cwd);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the test root directory.
|
|
|
|
///
|
|
|
|
/// This generally should not be necessary. It is only useful if you want
|
|
|
|
/// to create a `Config` from within a thread. Since Cargo's testsuite
|
|
|
|
/// uses thread-local storage, this can be used to avoid accessing that
|
|
|
|
/// thread-local storage.
|
|
|
|
///
|
|
|
|
/// Default is [`paths::root`].
|
|
|
|
pub fn root(&mut self, path: impl Into<PathBuf>) -> &mut Self {
|
|
|
|
self.root = Some(path.into());
|
2019-11-30 21:55:52 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
/// Creates the `Config`.
|
|
|
|
pub fn build(&self) -> Config {
|
|
|
|
self.build_err().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates the `Config`, returning a Result.
|
|
|
|
pub fn build_err(&self) -> CargoResult<Config> {
|
2023-09-07 02:36:08 +00:00
|
|
|
let root = self.root.clone().unwrap_or_else(|| paths::root());
|
|
|
|
let output = Box::new(fs::File::create(root.join("shell.out")).unwrap());
|
2019-11-28 20:47:22 +00:00
|
|
|
let shell = Shell::from_write(output);
|
2023-09-07 02:36:08 +00:00
|
|
|
let cwd = self.cwd.clone().unwrap_or_else(|| root.clone());
|
|
|
|
let homedir = root.join("home").join(".cargo");
|
2019-11-28 20:47:22 +00:00
|
|
|
let mut config = Config::new(shell, cwd, homedir);
|
2021-02-24 19:56:14 +00:00
|
|
|
config.nightly_features_allowed = self.enable_nightly_features || !self.unstable.is_empty();
|
2019-11-28 20:47:22 +00:00
|
|
|
config.set_env(self.env.clone());
|
2023-09-07 02:36:08 +00:00
|
|
|
config.set_search_stop_path(&root);
|
2019-11-30 21:55:52 +00:00
|
|
|
config.configure(
|
|
|
|
0,
|
2020-01-26 23:02:37 +00:00
|
|
|
false,
|
2019-11-30 21:55:52 +00:00
|
|
|
None,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
&None,
|
|
|
|
&self.unstable,
|
2020-01-26 23:02:37 +00:00
|
|
|
&self.config_args,
|
2019-11-30 21:55:52 +00:00
|
|
|
)?;
|
2019-11-28 20:47:22 +00:00
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_config() -> Config {
|
|
|
|
ConfigBuilder::new().build()
|
|
|
|
}
|
|
|
|
|
2019-12-01 18:19:02 +00:00
|
|
|
/// Read the output from Config.
|
|
|
|
pub fn read_output(config: Config) -> String {
|
|
|
|
drop(config); // Paranoid about flushing the file.
|
|
|
|
let path = paths::root().join("shell.out");
|
|
|
|
fs::read_to_string(path).unwrap()
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
fn read_env_vars_for_config() {
|
2018-07-20 11:47:47 +00:00
|
|
|
let p = project()
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2020-09-27 00:59:58 +00:00
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
authors = []
|
|
|
|
version = "0.0.0"
|
|
|
|
build = "build.rs"
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.file("src/lib.rs", "")
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"build.rs",
|
|
|
|
r#"
|
2020-09-27 00:59:58 +00:00
|
|
|
use std::env;
|
|
|
|
fn main() {
|
|
|
|
assert_eq!(env::var("NUM_JOBS").unwrap(), "100");
|
|
|
|
}
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.build();
|
2016-02-19 08:07:22 +00:00
|
|
|
|
2023-02-15 22:23:30 +00:00
|
|
|
p.cargo("check").env("CARGO_BUILD_JOBS", "100").run();
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2018-05-15 04:57:47 +00:00
|
|
|
|
2024-01-26 19:40:46 +00:00
|
|
|
pub fn write_config_extless(config: &str) {
|
2019-12-01 18:19:02 +00:00
|
|
|
write_config_at(paths::root().join(".cargo/config"), config);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_config_at(path: impl AsRef<Path>, contents: &str) {
|
|
|
|
let path = paths::root().join(path.as_ref());
|
2018-05-15 04:57:47 +00:00
|
|
|
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
2019-12-01 18:19:02 +00:00
|
|
|
fs::write(path, contents).unwrap();
|
2018-05-15 04:57:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 04:24:08 +00:00
|
|
|
pub fn write_config_toml(config: &str) {
|
2019-12-01 18:19:02 +00:00
|
|
|
write_config_at(paths::root().join(".cargo/config.toml"), config);
|
2019-08-24 19:43:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 07:39:15 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn symlink_file(target: &Path, link: &Path) -> io::Result<()> {
|
|
|
|
os::unix::fs::symlink(target, link)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn symlink_file(target: &Path, link: &Path) -> io::Result<()> {
|
|
|
|
os::windows::fs::symlink_file(target, link)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn symlink_config_to_config_toml() {
|
|
|
|
let toml_path = paths::root().join(".cargo/config.toml");
|
|
|
|
let symlink_path = paths::root().join(".cargo/config");
|
|
|
|
t!(symlink_file(&toml_path, &symlink_path));
|
|
|
|
}
|
|
|
|
|
2021-03-03 17:16:44 +00:00
|
|
|
#[track_caller]
|
2020-01-07 22:30:15 +00:00
|
|
|
pub fn assert_error<E: Borrow<anyhow::Error>>(error: E, msgs: &str) {
|
2018-05-15 04:57:47 +00:00
|
|
|
let causes = error
|
2018-08-08 22:57:20 +00:00
|
|
|
.borrow()
|
2020-01-07 22:30:15 +00:00
|
|
|
.chain()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, e)| {
|
|
|
|
if i == 0 {
|
|
|
|
e.to_string()
|
|
|
|
} else {
|
|
|
|
format!("Caused by:\n {}", e)
|
|
|
|
}
|
|
|
|
})
|
2018-05-15 04:57:47 +00:00
|
|
|
.collect::<Vec<_>>()
|
2020-01-07 22:30:15 +00:00
|
|
|
.join("\n\n");
|
2019-12-01 18:19:02 +00:00
|
|
|
assert_match(msgs, &causes);
|
|
|
|
}
|
|
|
|
|
2021-03-03 17:16:44 +00:00
|
|
|
#[track_caller]
|
2019-12-01 18:19:02 +00:00
|
|
|
pub fn assert_match(expected: &str, actual: &str) {
|
2021-06-16 01:04:50 +00:00
|
|
|
if let Err(e) = compare::match_exact(expected, actual, "output", "", None) {
|
|
|
|
panic_error("", e);
|
2018-05-15 04:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn get_config() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
[S]
|
|
|
|
f1 = 123
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = new_config();
|
2018-05-15 04:57:47 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Eq, PartialEq)]
|
|
|
|
struct S {
|
|
|
|
f1: Option<i64>,
|
|
|
|
}
|
|
|
|
let s: S = config.get("S").unwrap();
|
|
|
|
assert_eq!(s, S { f1: Some(123) });
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new().env("CARGO_S_F1", "456").build();
|
2018-05-15 04:57:47 +00:00
|
|
|
let s: S = config.get("S").unwrap();
|
|
|
|
assert_eq!(s, S { f1: Some(456) });
|
|
|
|
}
|
|
|
|
|
2023-03-09 18:31:04 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
#[cargo_test]
|
|
|
|
fn environment_variable_casing() {
|
|
|
|
// Issue #11814: Environment variable names are case-insensitive on Windows.
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("Path", "abc")
|
|
|
|
.env("Two-Words", "abc")
|
|
|
|
.env("two_words", "def")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let var = config.get_env("PATH").unwrap();
|
|
|
|
assert_eq!(var, String::from("abc"));
|
|
|
|
|
|
|
|
let var = config.get_env("path").unwrap();
|
|
|
|
assert_eq!(var, String::from("abc"));
|
|
|
|
|
|
|
|
let var = config.get_env("TWO-WORDS").unwrap();
|
|
|
|
assert_eq!(var, String::from("abc"));
|
|
|
|
|
|
|
|
// Make sure that we can still distinguish between dashes and underscores
|
|
|
|
// in variable names.
|
|
|
|
let var = config.get_env("Two_Words").unwrap();
|
|
|
|
assert_eq!(var, String::from("def"));
|
|
|
|
}
|
|
|
|
|
2019-08-24 19:43:15 +00:00
|
|
|
#[cargo_test]
|
2024-01-26 19:40:46 +00:00
|
|
|
fn config_works_without_extension() {
|
|
|
|
write_config_extless(
|
2019-08-24 19:43:15 +00:00
|
|
|
"\
|
|
|
|
[foo]
|
|
|
|
f1 = 1
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = new_config();
|
2019-08-24 19:43:15 +00:00
|
|
|
|
|
|
|
assert_eq!(config.get::<Option<i32>>("foo.f1").unwrap(), Some(1));
|
2024-01-26 21:08:48 +00:00
|
|
|
|
|
|
|
// It should NOT have warned for the symlink.
|
|
|
|
let output = read_output(config);
|
2024-01-26 17:48:46 +00:00
|
|
|
let expected = "\
|
|
|
|
warning: `[ROOT]/.cargo/config` is deprecated in favor of `config.toml`
|
|
|
|
note: If you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`";
|
|
|
|
assert_match(expected, &output);
|
2019-08-24 19:43:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 07:39:15 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn config_ambiguous_filename_symlink_doesnt_warn() {
|
|
|
|
// Windows requires special permissions to create symlinks.
|
|
|
|
// If we don't have permission, just skip this test.
|
2021-06-16 01:11:02 +00:00
|
|
|
if !symlink_supported() {
|
2019-08-29 07:39:15 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
write_config_toml(
|
|
|
|
"\
|
|
|
|
[foo]
|
|
|
|
f1 = 1
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
symlink_config_to_config_toml();
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = new_config();
|
2019-08-29 07:39:15 +00:00
|
|
|
|
|
|
|
assert_eq!(config.get::<Option<i32>>("foo.f1").unwrap(), Some(1));
|
|
|
|
|
|
|
|
// It should NOT have warned for the symlink.
|
2019-12-01 18:19:02 +00:00
|
|
|
let output = read_output(config);
|
2024-01-26 21:16:05 +00:00
|
|
|
assert_match("", &output);
|
2019-08-29 07:39:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 19:43:15 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn config_ambiguous_filename() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_extless(
|
2019-08-24 19:43:15 +00:00
|
|
|
"\
|
|
|
|
[foo]
|
|
|
|
f1 = 1
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
write_config_toml(
|
|
|
|
"\
|
|
|
|
[foo]
|
|
|
|
f1 = 2
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = new_config();
|
2019-08-24 19:43:15 +00:00
|
|
|
|
|
|
|
// It should use the value from the one without the extension for
|
|
|
|
// backwards compatibility.
|
|
|
|
assert_eq!(config.get::<Option<i32>>("foo.f1").unwrap(), Some(1));
|
|
|
|
|
|
|
|
// But it also should have warned.
|
2019-12-01 18:19:02 +00:00
|
|
|
let output = read_output(config);
|
2019-08-24 19:43:15 +00:00
|
|
|
let expected = "\
|
|
|
|
warning: Both `[..]/.cargo/config` and `[..]/.cargo/config.toml` exist. Using `[..]/.cargo/config`
|
|
|
|
";
|
2019-12-01 18:19:02 +00:00
|
|
|
assert_match(expected, &output);
|
2019-08-24 19:43:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn config_unused_fields() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
[S]
|
|
|
|
unused = 456
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_S_UNUSED2", "1")
|
|
|
|
.env("CARGO_S2_UNUSED", "2")
|
|
|
|
.build();
|
2018-05-15 04:57:47 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Eq, PartialEq)]
|
|
|
|
struct S {
|
|
|
|
f1: Option<i64>,
|
|
|
|
}
|
2018-05-21 19:42:43 +00:00
|
|
|
// This prints a warning (verified below).
|
2018-05-15 04:57:47 +00:00
|
|
|
let s: S = config.get("S").unwrap();
|
|
|
|
assert_eq!(s, S { f1: None });
|
|
|
|
// This does not print anything, we cannot easily/reliably warn for
|
|
|
|
// environment variables.
|
|
|
|
let s: S = config.get("S2").unwrap();
|
|
|
|
assert_eq!(s, S { f1: None });
|
2018-05-21 19:42:43 +00:00
|
|
|
|
|
|
|
// Verify the warnings.
|
2019-12-01 18:19:02 +00:00
|
|
|
let output = read_output(config);
|
2018-05-21 19:42:43 +00:00
|
|
|
let expected = "\
|
2024-01-26 19:40:46 +00:00
|
|
|
warning: unused config key `S.unused` in `[..]/.cargo/config.toml`
|
2018-05-21 19:42:43 +00:00
|
|
|
";
|
2019-12-01 18:19:02 +00:00
|
|
|
assert_match(expected, &output);
|
2018-05-15 04:57:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn config_load_toml_profile() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
[profile.dev]
|
|
|
|
opt-level = 's'
|
|
|
|
lto = true
|
|
|
|
codegen-units=4
|
|
|
|
debug = true
|
|
|
|
debug-assertions = true
|
|
|
|
rpath = true
|
|
|
|
panic = 'abort'
|
|
|
|
overflow-checks = true
|
|
|
|
incremental = true
|
|
|
|
|
|
|
|
[profile.dev.build-override]
|
|
|
|
opt-level = 1
|
|
|
|
|
2019-10-10 21:39:30 +00:00
|
|
|
[profile.dev.package.bar]
|
2018-05-15 04:57:47 +00:00
|
|
|
codegen-units = 9
|
2019-06-01 10:43:58 +00:00
|
|
|
|
|
|
|
[profile.no-lto]
|
|
|
|
inherits = 'dev'
|
|
|
|
dir-name = 'without-lto'
|
|
|
|
lto = false
|
2018-05-15 04:57:47 +00:00
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.unstable_flag("advanced-env")
|
|
|
|
.env("CARGO_PROFILE_DEV_CODEGEN_UNITS", "5")
|
|
|
|
.env("CARGO_PROFILE_DEV_BUILD_OVERRIDE_CODEGEN_UNITS", "11")
|
|
|
|
.env("CARGO_PROFILE_DEV_PACKAGE_env_CODEGEN_UNITS", "13")
|
|
|
|
.env("CARGO_PROFILE_DEV_PACKAGE_bar_OPT_LEVEL", "2")
|
|
|
|
.build();
|
2018-05-15 04:57:47 +00:00
|
|
|
|
2019-02-03 04:01:23 +00:00
|
|
|
// TODO: don't use actual `tomlprofile`.
|
2023-01-19 21:26:28 +00:00
|
|
|
let p: cargo_toml::TomlProfile = config.get("profile.dev").unwrap();
|
2019-11-28 20:47:22 +00:00
|
|
|
let mut packages = BTreeMap::new();
|
2023-01-19 21:26:28 +00:00
|
|
|
let key =
|
|
|
|
cargo_toml::ProfilePackageSpec::Spec(::cargo::core::PackageIdSpec::parse("bar").unwrap());
|
|
|
|
let o_profile = cargo_toml::TomlProfile {
|
|
|
|
opt_level: Some(cargo_toml::TomlOptLevel("2".to_string())),
|
2018-05-15 04:57:47 +00:00
|
|
|
codegen_units: Some(9),
|
2019-06-01 10:43:58 +00:00
|
|
|
..Default::default()
|
2018-05-15 04:57:47 +00:00
|
|
|
};
|
2019-10-10 21:39:30 +00:00
|
|
|
packages.insert(key, o_profile);
|
2023-01-19 21:26:28 +00:00
|
|
|
let key =
|
|
|
|
cargo_toml::ProfilePackageSpec::Spec(::cargo::core::PackageIdSpec::parse("env").unwrap());
|
|
|
|
let o_profile = cargo_toml::TomlProfile {
|
2018-05-15 04:57:47 +00:00
|
|
|
codegen_units: Some(13),
|
2019-06-01 10:43:58 +00:00
|
|
|
..Default::default()
|
2018-05-15 04:57:47 +00:00
|
|
|
};
|
2019-10-10 21:39:30 +00:00
|
|
|
packages.insert(key, o_profile);
|
2018-05-15 04:57:47 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
p,
|
2023-01-19 21:26:28 +00:00
|
|
|
cargo_toml::TomlProfile {
|
|
|
|
opt_level: Some(cargo_toml::TomlOptLevel("s".to_string())),
|
|
|
|
lto: Some(cargo_toml::StringOrBool::Bool(true)),
|
2018-05-15 04:57:47 +00:00
|
|
|
codegen_units: Some(5),
|
2023-04-11 04:59:08 +00:00
|
|
|
debug: Some(cargo_toml::TomlDebugInfo::Full),
|
2018-05-15 04:57:47 +00:00
|
|
|
debug_assertions: Some(true),
|
|
|
|
rpath: Some(true),
|
|
|
|
panic: Some("abort".to_string()),
|
|
|
|
overflow_checks: Some(true),
|
|
|
|
incremental: Some(true),
|
2019-10-10 21:39:30 +00:00
|
|
|
package: Some(packages),
|
2023-01-19 21:26:28 +00:00
|
|
|
build_override: Some(Box::new(cargo_toml::TomlProfile {
|
|
|
|
opt_level: Some(cargo_toml::TomlOptLevel("1".to_string())),
|
2018-05-15 04:57:47 +00:00
|
|
|
codegen_units: Some(11),
|
2019-06-01 10:43:58 +00:00
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-01-19 21:26:28 +00:00
|
|
|
let p: cargo_toml::TomlProfile = config.get("profile.no-lto").unwrap();
|
2019-06-01 10:43:58 +00:00
|
|
|
assert_eq!(
|
|
|
|
p,
|
2023-01-19 21:26:28 +00:00
|
|
|
cargo_toml::TomlProfile {
|
|
|
|
lto: Some(cargo_toml::StringOrBool::Bool(false)),
|
refactor(toml): Decouple parsing from interning system
To have a separate manifest API (#12801), we can't rely on interning
because it might be used in longer-lifetime applications.
I consulted https://github.com/rosetta-rs/string-rosetta-rs when
deciding on what string type to use for performance.
Originally, I hoped to entirely replacing string interning. For that, I
was looking at `arcstr` as it had a fast equality operator. However,
that is only helpful so long as the two strings we are comparing came
from the original source. Unsure how likely that is to happen (and
daunted by converting all of the `Copy`s into `Clone`s), I decided to
scale back.
Concerned about all of the small allocations when parsing a manifest, I
assumed I'd need a string type with small-string optimizations, like
`hipstr`, `compact_str`, `flexstr`, and `ecow`.
The first three give us more wiggle room and `hipstr` was the fastest of
them, so I went with that.
I then double checked macro benchmarks, and realized `hipstr` made no
difference and switched to `String` to keep things simple / with lower
dependencies.
When doing this, I had created a type alias (`TomlStr`) for the string
type so I could more easily swap it out if needed
(and not have to always write out a lifetime).
With just using `String`, I went ahead and dropped that.
I had problems getting the cargo benchmarks running, so I did a quick
and dirty benchmark that is end-to-end, covering fresh builds, clean
builds, and resolution. I ran these against a fresh clone of cargo's
code base.
```console
$ ../dump/cargo-12801-bench.rs run
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `target/debug/cargo -Zscript -Zmsrv-policy ../dump/cargo-12801-bench.rs run`
warning: `package.edition` is unspecified, defaulting to `2021`
Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `/home/epage/.cargo/target/0a/7f4c1ab500f045/debug/cargo-12801-bench run`
$ hyperfine "../cargo-old check" "../cargo-new check"
Benchmark 1: ../cargo-old check
Time (mean ± σ): 119.3 ms ± 3.2 ms [User: 98.6 ms, System: 20.3 ms]
Range (min … max): 115.6 ms … 124.3 ms 24 runs
Benchmark 2: ../cargo-new check
Time (mean ± σ): 119.4 ms ± 2.4 ms [User: 98.0 ms, System: 21.1 ms]
Range (min … max): 115.7 ms … 123.6 ms 24 runs
Summary
../cargo-old check ran
1.00 ± 0.03 times faster than ../cargo-new check
$ hyperfine --prepare "cargo clean" "../cargo-old check" "../cargo-new check"
Benchmark 1: ../cargo-old check
Time (mean ± σ): 20.249 s ± 0.392 s [User: 157.719 s, System: 22.771 s]
Range (min … max): 19.605 s … 21.123 s 10 runs
Benchmark 2: ../cargo-new check
Time (mean ± σ): 20.123 s ± 0.212 s [User: 156.156 s, System: 22.325 s]
Range (min … max): 19.764 s … 20.420 s 10 runs
Summary
../cargo-new check ran
1.01 ± 0.02 times faster than ../cargo-old check
$ hyperfine --prepare "cargo clean && rm -f Cargo.lock" "../cargo-old check" "../cargo-new check"
Benchmark 1: ../cargo-old check
Time (mean ± σ): 21.105 s ± 0.465 s [User: 156.482 s, System: 22.799 s]
Range (min … max): 20.156 s … 22.010 s 10 runs
Benchmark 2: ../cargo-new check
Time (mean ± σ): 21.358 s ± 0.538 s [User: 156.187 s, System: 22.979 s]
Range (min … max): 20.703 s … 22.462 s 10 runs
Summary
../cargo-old check ran
1.01 ± 0.03 times faster than ../cargo-new check
```
2023-10-18 20:28:02 +00:00
|
|
|
dir_name: Some(String::from("without-lto")),
|
|
|
|
inherits: Some(String::from("dev")),
|
2019-06-01 10:43:58 +00:00
|
|
|
..Default::default()
|
2018-05-15 04:57:47 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-24 02:13:50 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn profile_env_var_prefix() {
|
|
|
|
// Check for a bug with collision on DEBUG vs DEBUG_ASSERTIONS.
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_PROFILE_DEV_DEBUG_ASSERTIONS", "false")
|
|
|
|
.build();
|
2023-01-19 21:26:28 +00:00
|
|
|
let p: cargo_toml::TomlProfile = config.get("profile.dev").unwrap();
|
2019-12-24 02:13:50 +00:00
|
|
|
assert_eq!(p.debug_assertions, Some(false));
|
|
|
|
assert_eq!(p.debug, None);
|
|
|
|
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_PROFILE_DEV_DEBUG", "1")
|
|
|
|
.build();
|
2023-01-19 21:26:28 +00:00
|
|
|
let p: cargo_toml::TomlProfile = config.get("profile.dev").unwrap();
|
2019-12-24 02:13:50 +00:00
|
|
|
assert_eq!(p.debug_assertions, None);
|
2023-04-11 04:59:08 +00:00
|
|
|
assert_eq!(p.debug, Some(cargo_toml::TomlDebugInfo::Limited));
|
2019-12-24 02:13:50 +00:00
|
|
|
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_PROFILE_DEV_DEBUG_ASSERTIONS", "false")
|
|
|
|
.env("CARGO_PROFILE_DEV_DEBUG", "1")
|
|
|
|
.build();
|
2023-01-19 21:26:28 +00:00
|
|
|
let p: cargo_toml::TomlProfile = config.get("profile.dev").unwrap();
|
2019-12-24 02:13:50 +00:00
|
|
|
assert_eq!(p.debug_assertions, Some(false));
|
2023-04-11 04:59:08 +00:00
|
|
|
assert_eq!(p.debug, Some(cargo_toml::TomlDebugInfo::Limited));
|
2019-12-24 02:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn config_deserialize_any() {
|
|
|
|
// Some tests to exercise deserialize_any for deserializers that need to
|
|
|
|
// be told the format.
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
a = true
|
|
|
|
b = ['b']
|
|
|
|
c = ['c']
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-30 21:55:52 +00:00
|
|
|
// advanced-env
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.unstable_flag("advanced-env")
|
|
|
|
.env("CARGO_ENVB", "false")
|
|
|
|
.env("CARGO_C", "['d']")
|
|
|
|
.env("CARGO_ENVL", "['a', 'b']")
|
|
|
|
.build();
|
2019-11-30 21:55:52 +00:00
|
|
|
assert_eq!(config.get::<VSOB>("a").unwrap(), VSOB::Bool(true));
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<VSOB>("b").unwrap(),
|
|
|
|
VSOB::VecString(vec!["b".to_string()])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<VSOB>("c").unwrap(),
|
|
|
|
VSOB::VecString(vec!["c".to_string(), "d".to_string()])
|
|
|
|
);
|
|
|
|
assert_eq!(config.get::<VSOB>("envb").unwrap(), VSOB::Bool(false));
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<VSOB>("envl").unwrap(),
|
|
|
|
VSOB::VecString(vec!["a".to_string(), "b".to_string()])
|
|
|
|
);
|
2018-05-15 04:57:47 +00:00
|
|
|
|
2019-11-30 21:55:52 +00:00
|
|
|
// Demonstrate where merging logic isn't very smart. This could be improved.
|
|
|
|
let config = ConfigBuilder::new().env("CARGO_A", "x y").build();
|
|
|
|
assert_error(
|
|
|
|
config.get::<VSOB>("a").unwrap_err(),
|
2020-01-08 22:51:49 +00:00
|
|
|
"\
|
|
|
|
error in environment variable `CARGO_A`: could not load config key `a`
|
|
|
|
|
|
|
|
Caused by:
|
|
|
|
invalid type: string \"x y\", expected a boolean or vector of strings",
|
2019-11-30 21:55:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Normal env.
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.unstable_flag("advanced-env")
|
|
|
|
.env("CARGO_B", "d e")
|
|
|
|
.env("CARGO_C", "f g")
|
|
|
|
.build();
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<VSOB>("b").unwrap(),
|
|
|
|
VSOB::VecString(vec!["b".to_string(), "d".to_string(), "e".to_string()])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<VSOB>("c").unwrap(),
|
|
|
|
VSOB::VecString(vec!["c".to_string(), "f".to_string(), "g".to_string()])
|
|
|
|
);
|
|
|
|
|
|
|
|
// config-cli
|
|
|
|
// This test demonstrates that ConfigValue::merge isn't very smart.
|
|
|
|
// It would be nice if it was smarter.
|
|
|
|
let config = ConfigBuilder::new().config_arg("a = ['a']").build_err();
|
|
|
|
assert_error(
|
|
|
|
config.unwrap_err(),
|
2019-12-01 18:19:02 +00:00
|
|
|
"\
|
2024-01-26 19:40:46 +00:00
|
|
|
failed to merge --config key `a` into `[..]/.cargo/config.toml`
|
2020-01-07 22:30:15 +00:00
|
|
|
|
|
|
|
Caused by:
|
2024-01-26 19:40:46 +00:00
|
|
|
failed to merge config value from `--config cli option` into `[..]/.cargo/config.toml`: \
|
2019-12-01 18:19:02 +00:00
|
|
|
expected boolean, but found array",
|
2019-11-30 21:55:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// config-cli and advanced-env
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.unstable_flag("advanced-env")
|
|
|
|
.config_arg("b=['clib']")
|
|
|
|
.config_arg("c=['clic']")
|
|
|
|
.env("CARGO_B", "env1 env2")
|
|
|
|
.env("CARGO_C", "['e1', 'e2']")
|
|
|
|
.build();
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<VSOB>("b").unwrap(),
|
|
|
|
VSOB::VecString(vec![
|
|
|
|
"b".to_string(),
|
|
|
|
"env1".to_string(),
|
2023-08-16 04:08:39 +00:00
|
|
|
"env2".to_string(),
|
|
|
|
"clib".to_string(),
|
2019-11-30 21:55:52 +00:00
|
|
|
])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<VSOB>("c").unwrap(),
|
|
|
|
VSOB::VecString(vec![
|
|
|
|
"c".to_string(),
|
|
|
|
"e1".to_string(),
|
2023-08-16 04:08:39 +00:00
|
|
|
"e2".to_string(),
|
|
|
|
"clic".to_string(),
|
2019-11-30 21:55:52 +00:00
|
|
|
])
|
|
|
|
);
|
2018-05-15 04:57:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn config_toml_errors() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
[profile.dev]
|
|
|
|
opt-level = 'foo'
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = new_config();
|
2018-05-15 04:57:47 +00:00
|
|
|
|
|
|
|
assert_error(
|
2023-01-19 21:26:28 +00:00
|
|
|
config
|
|
|
|
.get::<cargo_toml::TomlProfile>("profile.dev")
|
|
|
|
.unwrap_err(),
|
2020-01-08 22:51:49 +00:00
|
|
|
"\
|
2024-01-26 19:40:46 +00:00
|
|
|
error in [..]/.cargo/config.toml: could not load config key `profile.dev.opt-level`
|
2020-01-08 22:51:49 +00:00
|
|
|
|
|
|
|
Caused by:
|
2021-05-10 15:28:19 +00:00
|
|
|
must be `0`, `1`, `2`, `3`, `s` or `z`, but found the string: \"foo\"",
|
2018-05-15 04:57:47 +00:00
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_PROFILE_DEV_OPT_LEVEL", "asdf")
|
|
|
|
.build();
|
2018-05-15 04:57:47 +00:00
|
|
|
|
|
|
|
assert_error(
|
2023-01-19 21:26:28 +00:00
|
|
|
config.get::<cargo_toml::TomlProfile>("profile.dev").unwrap_err(),
|
2020-01-08 22:51:49 +00:00
|
|
|
"\
|
|
|
|
error in environment variable `CARGO_PROFILE_DEV_OPT_LEVEL`: could not load config key `profile.dev.opt-level`
|
|
|
|
|
|
|
|
Caused by:
|
2021-05-10 15:28:19 +00:00
|
|
|
must be `0`, `1`, `2`, `3`, `s` or `z`, but found the string: \"asdf\"",
|
2018-05-15 04:57:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn load_nested() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
[nest.foo]
|
|
|
|
f1 = 1
|
|
|
|
f2 = 2
|
|
|
|
[nest.bar]
|
|
|
|
asdf = 3
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.unstable_flag("advanced-env")
|
|
|
|
.env("CARGO_NEST_foo_f2", "3")
|
|
|
|
.env("CARGO_NESTE_foo_f1", "1")
|
|
|
|
.env("CARGO_NESTE_foo_f2", "3")
|
|
|
|
.env("CARGO_NESTE_bar_asdf", "3")
|
|
|
|
.build();
|
2018-05-15 04:57:47 +00:00
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
type Nested = HashMap<String, HashMap<String, u8>>;
|
2018-05-15 04:57:47 +00:00
|
|
|
|
|
|
|
let n: Nested = config.get("nest").unwrap();
|
2019-11-28 20:47:22 +00:00
|
|
|
let mut expected = HashMap::new();
|
|
|
|
let mut foo = HashMap::new();
|
2018-05-15 04:57:47 +00:00
|
|
|
foo.insert("f1".to_string(), 1);
|
|
|
|
foo.insert("f2".to_string(), 3);
|
|
|
|
expected.insert("foo".to_string(), foo);
|
2019-11-28 20:47:22 +00:00
|
|
|
let mut bar = HashMap::new();
|
2018-05-15 04:57:47 +00:00
|
|
|
bar.insert("asdf".to_string(), 3);
|
|
|
|
expected.insert("bar".to_string(), bar);
|
|
|
|
assert_eq!(n, expected);
|
|
|
|
|
|
|
|
let n: Nested = config.get("neste").unwrap();
|
|
|
|
assert_eq!(n, expected);
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn get_errors() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
[S]
|
|
|
|
f1 = 123
|
|
|
|
f2 = 'asdf'
|
|
|
|
big = 123456789
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_E_S", "asdf")
|
|
|
|
.env("CARGO_E_BIG", "123456789")
|
|
|
|
.build();
|
2018-05-15 04:57:47 +00:00
|
|
|
assert_error(
|
|
|
|
config.get::<i64>("foo").unwrap_err(),
|
|
|
|
"missing config key `foo`",
|
|
|
|
);
|
|
|
|
assert_error(
|
|
|
|
config.get::<i64>("foo.bar").unwrap_err(),
|
|
|
|
"missing config key `foo.bar`",
|
|
|
|
);
|
|
|
|
assert_error(
|
|
|
|
config.get::<i64>("S.f2").unwrap_err(),
|
2024-01-26 19:40:46 +00:00
|
|
|
"error in [..]/.cargo/config.toml: `S.f2` expected an integer, but found a string",
|
2018-05-15 04:57:47 +00:00
|
|
|
);
|
|
|
|
assert_error(
|
|
|
|
config.get::<u8>("S.big").unwrap_err(),
|
2020-01-08 22:51:49 +00:00
|
|
|
"\
|
2024-01-26 19:40:46 +00:00
|
|
|
error in [..].cargo/config.toml: could not load config key `S.big`
|
2020-01-08 22:51:49 +00:00
|
|
|
|
|
|
|
Caused by:
|
|
|
|
invalid value: integer `123456789`, expected u8",
|
2018-05-15 04:57:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Environment variable type errors.
|
|
|
|
assert_error(
|
|
|
|
config.get::<i64>("e.s").unwrap_err(),
|
|
|
|
"error in environment variable `CARGO_E_S`: invalid digit found in string",
|
|
|
|
);
|
|
|
|
assert_error(
|
|
|
|
config.get::<i8>("e.big").unwrap_err(),
|
2020-01-08 22:51:49 +00:00
|
|
|
"\
|
|
|
|
error in environment variable `CARGO_E_BIG`: could not load config key `e.big`
|
|
|
|
|
|
|
|
Caused by:
|
|
|
|
invalid value: integer `123456789`, expected i8",
|
2018-05-15 04:57:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
2021-09-13 17:21:06 +00:00
|
|
|
#[allow(dead_code)]
|
2018-05-15 04:57:47 +00:00
|
|
|
struct S {
|
|
|
|
f1: i64,
|
|
|
|
f2: String,
|
|
|
|
f3: i64,
|
|
|
|
big: i64,
|
|
|
|
}
|
2020-07-09 03:22:44 +00:00
|
|
|
assert_error(config.get::<S>("S").unwrap_err(), "missing field `f3`");
|
2018-05-15 04:57:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn config_get_option() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
[foo]
|
|
|
|
f1 = 1
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new().env("CARGO_BAR_ASDF", "3").build();
|
2018-05-15 04:57:47 +00:00
|
|
|
|
|
|
|
assert_eq!(config.get::<Option<i32>>("a").unwrap(), None);
|
|
|
|
assert_eq!(config.get::<Option<i32>>("a.b").unwrap(), None);
|
|
|
|
assert_eq!(config.get::<Option<i32>>("foo.f1").unwrap(), Some(1));
|
|
|
|
assert_eq!(config.get::<Option<i32>>("bar.asdf").unwrap(), Some(3));
|
|
|
|
assert_eq!(config.get::<Option<i32>>("bar.zzzz").unwrap(), None);
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn config_bad_toml() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml("asdf");
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = new_config();
|
2018-05-15 04:57:47 +00:00
|
|
|
assert_error(
|
|
|
|
config.get::<i32>("foo").unwrap_err(),
|
|
|
|
"\
|
|
|
|
could not load Cargo configuration
|
2019-09-27 21:19:48 +00:00
|
|
|
|
2018-05-15 04:57:47 +00:00
|
|
|
Caused by:
|
2024-01-26 19:40:46 +00:00
|
|
|
could not parse TOML configuration in `[..]/.cargo/config.toml`
|
2019-09-27 21:19:48 +00:00
|
|
|
|
2018-05-15 04:57:47 +00:00
|
|
|
Caused by:
|
2021-11-02 00:18:32 +00:00
|
|
|
TOML parse error at line 1, column 5
|
|
|
|
|
|
|
|
|
1 | asdf
|
|
|
|
| ^
|
2023-01-19 21:26:28 +00:00
|
|
|
expected `.`, `=`",
|
2018-05-15 04:57:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn config_get_list() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
l1 = []
|
|
|
|
l2 = ['one', 'two']
|
|
|
|
l3 = 123
|
|
|
|
l4 = ['one', 'two']
|
|
|
|
|
|
|
|
[nested]
|
|
|
|
l = ['x']
|
|
|
|
|
|
|
|
[nested2]
|
|
|
|
l = ['y']
|
|
|
|
|
|
|
|
[nested-empty]
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
type L = Vec<String>;
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.unstable_flag("advanced-env")
|
|
|
|
.env("CARGO_L4", "['three', 'four']")
|
|
|
|
.env("CARGO_L5", "['a']")
|
|
|
|
.env("CARGO_ENV_EMPTY", "[]")
|
|
|
|
.env("CARGO_ENV_BLANK", "")
|
|
|
|
.env("CARGO_ENV_NUM", "1")
|
|
|
|
.env("CARGO_ENV_NUM_LIST", "[1]")
|
|
|
|
.env("CARGO_ENV_TEXT", "asdf")
|
|
|
|
.env("CARGO_LEPAIR", "['a', 'b']")
|
|
|
|
.env("CARGO_NESTED2_L", "['z']")
|
|
|
|
.env("CARGO_NESTEDE_L", "['env']")
|
|
|
|
.env("CARGO_BAD_ENV", "[zzz]")
|
|
|
|
.build();
|
2018-05-15 04:57:47 +00:00
|
|
|
|
|
|
|
assert_eq!(config.get::<L>("unset").unwrap(), vec![] as Vec<String>);
|
|
|
|
assert_eq!(config.get::<L>("l1").unwrap(), vec![] as Vec<String>);
|
|
|
|
assert_eq!(config.get::<L>("l2").unwrap(), vec!["one", "two"]);
|
|
|
|
assert_error(
|
|
|
|
config.get::<L>("l3").unwrap_err(),
|
|
|
|
"\
|
|
|
|
invalid configuration for key `l3`
|
2024-01-26 19:40:46 +00:00
|
|
|
expected a list, but found a integer for `l3` in [..]/.cargo/config.toml",
|
2018-05-15 04:57:47 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<L>("l4").unwrap(),
|
|
|
|
vec!["one", "two", "three", "four"]
|
|
|
|
);
|
|
|
|
assert_eq!(config.get::<L>("l5").unwrap(), vec!["a"]);
|
|
|
|
assert_eq!(config.get::<L>("env-empty").unwrap(), vec![] as Vec<String>);
|
2019-11-30 21:55:52 +00:00
|
|
|
assert_eq!(config.get::<L>("env-blank").unwrap(), vec![] as Vec<String>);
|
|
|
|
assert_eq!(config.get::<L>("env-num").unwrap(), vec!["1".to_string()]);
|
2018-05-15 04:57:47 +00:00
|
|
|
assert_error(
|
|
|
|
config.get::<L>("env-num-list").unwrap_err(),
|
|
|
|
"error in environment variable `CARGO_ENV_NUM_LIST`: \
|
|
|
|
expected string, found integer",
|
|
|
|
);
|
2019-11-30 21:55:52 +00:00
|
|
|
assert_eq!(
|
|
|
|
config.get::<L>("env-text").unwrap(),
|
|
|
|
vec!["asdf".to_string()]
|
2018-05-15 04:57:47 +00:00
|
|
|
);
|
|
|
|
// "invalid number" here isn't the best error, but I think it's just toml.rs.
|
|
|
|
assert_error(
|
|
|
|
config.get::<L>("bad-env").unwrap_err(),
|
2021-11-02 00:18:32 +00:00
|
|
|
"\
|
2023-01-19 21:26:28 +00:00
|
|
|
error in environment variable `CARGO_BAD_ENV`: could not parse TOML list: TOML parse error at line 1, column 2
|
2021-11-02 00:18:32 +00:00
|
|
|
|
|
2023-01-19 21:26:28 +00:00
|
|
|
1 | [zzz]
|
|
|
|
| ^
|
|
|
|
invalid array
|
|
|
|
expected `]`
|
2021-11-02 00:18:32 +00:00
|
|
|
",
|
2018-05-15 04:57:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Try some other sequence-like types.
|
|
|
|
assert_eq!(
|
|
|
|
config
|
|
|
|
.get::<(String, String, String, String)>("l4")
|
|
|
|
.unwrap(),
|
|
|
|
(
|
|
|
|
"one".to_string(),
|
|
|
|
"two".to_string(),
|
|
|
|
"three".to_string(),
|
|
|
|
"four".to_string()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(config.get::<(String,)>("l5").unwrap(), ("a".to_string(),));
|
|
|
|
|
|
|
|
// Tuple struct
|
|
|
|
#[derive(Debug, Deserialize, Eq, PartialEq)]
|
|
|
|
struct TupS(String, String);
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<TupS>("lepair").unwrap(),
|
|
|
|
TupS("a".to_string(), "b".to_string())
|
|
|
|
);
|
|
|
|
|
|
|
|
// Nested with an option.
|
|
|
|
#[derive(Debug, Deserialize, Eq, PartialEq)]
|
|
|
|
struct S {
|
|
|
|
l: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
assert_eq!(config.get::<S>("nested-empty").unwrap(), S { l: None });
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<S>("nested").unwrap(),
|
|
|
|
S {
|
|
|
|
l: Some(vec!["x".to_string()]),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<S>("nested2").unwrap(),
|
|
|
|
S {
|
|
|
|
l: Some(vec!["y".to_string(), "z".to_string()]),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<S>("nestede").unwrap(),
|
|
|
|
S {
|
|
|
|
l: Some(vec!["env".to_string()]),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn config_get_other_types() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
ns = 123
|
|
|
|
ns2 = 456
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_NSE", "987")
|
|
|
|
.env("CARGO_NS2", "654")
|
|
|
|
.build();
|
2018-05-15 04:57:47 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Eq, PartialEq)]
|
2019-09-27 19:29:01 +00:00
|
|
|
#[serde(transparent)]
|
2018-05-15 04:57:47 +00:00
|
|
|
struct NewS(i32);
|
|
|
|
assert_eq!(config.get::<NewS>("ns").unwrap(), NewS(123));
|
|
|
|
assert_eq!(config.get::<NewS>("ns2").unwrap(), NewS(654));
|
|
|
|
assert_eq!(config.get::<NewS>("nse").unwrap(), NewS(987));
|
|
|
|
assert_error(
|
|
|
|
config.get::<NewS>("unset").unwrap_err(),
|
|
|
|
"missing config key `unset`",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-15 04:57:47 +00:00
|
|
|
fn config_relative_path() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(&format!(
|
2018-05-15 04:57:47 +00:00
|
|
|
"\
|
|
|
|
p1 = 'foo/bar'
|
|
|
|
p2 = '../abc'
|
|
|
|
p3 = 'b/c'
|
|
|
|
abs = '{}'
|
|
|
|
",
|
|
|
|
paths::home().display(),
|
|
|
|
));
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_EPATH", "a/b")
|
|
|
|
.env("CARGO_P3", "d/e")
|
|
|
|
.build();
|
2018-05-15 04:57:47 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
config
|
|
|
|
.get::<config::ConfigRelativePath>("p1")
|
|
|
|
.unwrap()
|
2019-09-27 21:19:48 +00:00
|
|
|
.resolve_path(&config),
|
2018-05-15 04:57:47 +00:00
|
|
|
paths::root().join("foo/bar")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config
|
|
|
|
.get::<config::ConfigRelativePath>("p2")
|
|
|
|
.unwrap()
|
2019-09-27 21:19:48 +00:00
|
|
|
.resolve_path(&config),
|
2018-05-15 04:57:47 +00:00
|
|
|
paths::root().join("../abc")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config
|
|
|
|
.get::<config::ConfigRelativePath>("p3")
|
|
|
|
.unwrap()
|
2019-09-27 21:19:48 +00:00
|
|
|
.resolve_path(&config),
|
2018-05-15 04:57:47 +00:00
|
|
|
paths::root().join("d/e")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config
|
|
|
|
.get::<config::ConfigRelativePath>("abs")
|
|
|
|
.unwrap()
|
2019-09-27 21:19:48 +00:00
|
|
|
.resolve_path(&config),
|
2018-05-15 04:57:47 +00:00
|
|
|
paths::home()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config
|
|
|
|
.get::<config::ConfigRelativePath>("epath")
|
|
|
|
.unwrap()
|
2019-09-27 21:19:48 +00:00
|
|
|
.resolve_path(&config),
|
2018-05-15 04:57:47 +00:00
|
|
|
paths::root().join("a/b")
|
|
|
|
);
|
|
|
|
}
|
2018-05-22 22:06:06 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-05-22 22:06:06 +00:00
|
|
|
fn config_get_integers() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2018-05-22 22:06:06 +00:00
|
|
|
"\
|
|
|
|
npos = 123456789
|
|
|
|
nneg = -123456789
|
|
|
|
i64max = 9223372036854775807
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_EPOS", "123456789")
|
|
|
|
.env("CARGO_ENEG", "-1")
|
|
|
|
.env("CARGO_EI64MAX", "9223372036854775807")
|
|
|
|
.build();
|
2018-05-22 22:06:06 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
config.get::<u64>("i64max").unwrap(),
|
|
|
|
9_223_372_036_854_775_807
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<i64>("i64max").unwrap(),
|
|
|
|
9_223_372_036_854_775_807
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<u64>("ei64max").unwrap(),
|
|
|
|
9_223_372_036_854_775_807
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
config.get::<i64>("ei64max").unwrap(),
|
|
|
|
9_223_372_036_854_775_807
|
|
|
|
);
|
2018-05-22 22:06:06 +00:00
|
|
|
|
|
|
|
assert_error(
|
|
|
|
config.get::<u32>("nneg").unwrap_err(),
|
2020-01-08 22:51:49 +00:00
|
|
|
"\
|
2024-01-26 19:40:46 +00:00
|
|
|
error in [..].cargo/config.toml: could not load config key `nneg`
|
2020-01-08 22:51:49 +00:00
|
|
|
|
|
|
|
Caused by:
|
|
|
|
invalid value: integer `-123456789`, expected u32",
|
2018-05-22 22:06:06 +00:00
|
|
|
);
|
|
|
|
assert_error(
|
|
|
|
config.get::<u32>("eneg").unwrap_err(),
|
2020-01-08 22:51:49 +00:00
|
|
|
"\
|
|
|
|
error in environment variable `CARGO_ENEG`: could not load config key `eneg`
|
|
|
|
|
|
|
|
Caused by:
|
|
|
|
invalid value: integer `-1`, expected u32",
|
2018-05-22 22:06:06 +00:00
|
|
|
);
|
|
|
|
assert_error(
|
|
|
|
config.get::<i8>("npos").unwrap_err(),
|
2020-01-08 22:51:49 +00:00
|
|
|
"\
|
2024-01-26 19:40:46 +00:00
|
|
|
error in [..].cargo/config.toml: could not load config key `npos`
|
2020-01-08 22:51:49 +00:00
|
|
|
|
|
|
|
Caused by:
|
|
|
|
invalid value: integer `123456789`, expected i8",
|
2018-05-22 22:06:06 +00:00
|
|
|
);
|
|
|
|
assert_error(
|
|
|
|
config.get::<i8>("epos").unwrap_err(),
|
2020-01-08 22:51:49 +00:00
|
|
|
"\
|
|
|
|
error in environment variable `CARGO_EPOS`: could not load config key `epos`
|
|
|
|
|
|
|
|
Caused by:
|
|
|
|
invalid value: integer `123456789`, expected i8",
|
2018-05-22 22:06:06 +00:00
|
|
|
);
|
|
|
|
}
|
2019-09-29 16:29:15 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn config_get_ssl_version_missing() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2019-09-29 16:29:15 +00:00
|
|
|
"\
|
|
|
|
[http]
|
|
|
|
hello = 'world'
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = new_config();
|
2019-09-29 16:29:15 +00:00
|
|
|
|
2019-09-30 15:48:08 +00:00
|
|
|
assert!(config
|
|
|
|
.get::<Option<SslVersionConfig>>("http.ssl-version")
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
2019-09-29 16:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
2019-09-29 16:32:57 +00:00
|
|
|
fn config_get_ssl_version_single() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2019-09-29 16:29:15 +00:00
|
|
|
"\
|
|
|
|
[http]
|
|
|
|
ssl-version = 'tlsv1.2'
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = new_config();
|
2019-09-29 16:29:15 +00:00
|
|
|
|
2019-09-30 15:48:08 +00:00
|
|
|
let a = config
|
|
|
|
.get::<Option<SslVersionConfig>>("http.ssl-version")
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2019-09-29 16:29:15 +00:00
|
|
|
match a {
|
2019-09-29 16:32:57 +00:00
|
|
|
SslVersionConfig::Single(v) => assert_eq!(&v, "tlsv1.2"),
|
2019-09-29 16:29:15 +00:00
|
|
|
SslVersionConfig::Range(_) => panic!("Did not expect ssl version min/max."),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn config_get_ssl_version_min_max() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2019-09-29 16:29:15 +00:00
|
|
|
"\
|
|
|
|
[http]
|
|
|
|
ssl-version.min = 'tlsv1.2'
|
|
|
|
ssl-version.max = 'tlsv1.3'
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = new_config();
|
2019-09-29 16:29:15 +00:00
|
|
|
|
2019-09-30 15:48:08 +00:00
|
|
|
let a = config
|
|
|
|
.get::<Option<SslVersionConfig>>("http.ssl-version")
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2019-09-29 16:29:15 +00:00
|
|
|
match a {
|
2019-09-29 16:32:57 +00:00
|
|
|
SslVersionConfig::Single(_) => panic!("Did not expect exact ssl version."),
|
2019-09-29 16:29:15 +00:00
|
|
|
SslVersionConfig::Range(range) => {
|
|
|
|
assert_eq!(range.min, Some(String::from("tlsv1.2")));
|
|
|
|
assert_eq!(range.max, Some(String::from("tlsv1.3")));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn config_get_ssl_version_both_forms_configured() {
|
|
|
|
// this is not allowed
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2019-09-29 16:29:15 +00:00
|
|
|
"\
|
|
|
|
[http]
|
|
|
|
ssl-version = 'tlsv1.1'
|
|
|
|
ssl-version.min = 'tlsv1.2'
|
|
|
|
ssl-version.max = 'tlsv1.3'
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
let config = new_config();
|
|
|
|
|
|
|
|
assert_error(
|
|
|
|
config
|
|
|
|
.get::<SslVersionConfig>("http.ssl-version")
|
|
|
|
.unwrap_err(),
|
|
|
|
"\
|
|
|
|
could not load Cargo configuration
|
|
|
|
|
|
|
|
Caused by:
|
2024-01-26 19:40:46 +00:00
|
|
|
could not parse TOML configuration in `[..]/.cargo/config.toml`
|
2019-09-29 16:29:15 +00:00
|
|
|
|
2019-11-28 20:47:22 +00:00
|
|
|
Caused by:
|
2021-11-02 00:18:32 +00:00
|
|
|
TOML parse error at line 3, column 1
|
|
|
|
|
|
|
|
|
3 | ssl-version.min = 'tlsv1.2'
|
|
|
|
| ^
|
2023-01-19 21:26:28 +00:00
|
|
|
dotted key `ssl-version` attempted to extend non-table type (string)
|
2021-11-02 00:18:32 +00:00
|
|
|
",
|
2019-11-28 20:47:22 +00:00
|
|
|
);
|
2019-09-29 16:29:15 +00:00
|
|
|
}
|
2019-12-01 18:19:02 +00:00
|
|
|
|
2020-06-21 20:19:37 +00:00
|
|
|
#[cargo_test]
|
|
|
|
/// Assert that unstable options can be configured with the `unstable` table in
|
|
|
|
/// cargo config files
|
2020-06-30 02:13:50 +00:00
|
|
|
fn unstable_table_notation() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2020-06-21 20:19:37 +00:00
|
|
|
"\
|
|
|
|
[unstable]
|
2020-06-30 02:13:50 +00:00
|
|
|
print-im-a-teapot = true
|
2020-06-21 20:19:37 +00:00
|
|
|
",
|
|
|
|
);
|
2021-02-24 19:56:14 +00:00
|
|
|
let config = ConfigBuilder::new().nightly_features_allowed(true).build();
|
2020-06-21 20:19:37 +00:00
|
|
|
assert_eq!(config.cli_unstable().print_im_a_teapot, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
/// Assert that dotted notation works for configuring unstable options
|
2020-06-30 02:13:50 +00:00
|
|
|
fn unstable_dotted_notation() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2020-06-21 20:19:37 +00:00
|
|
|
"\
|
2020-06-30 02:13:50 +00:00
|
|
|
unstable.print-im-a-teapot = true
|
2020-06-21 20:19:37 +00:00
|
|
|
",
|
|
|
|
);
|
2021-02-24 19:56:14 +00:00
|
|
|
let config = ConfigBuilder::new().nightly_features_allowed(true).build();
|
2020-06-21 20:19:37 +00:00
|
|
|
assert_eq!(config.cli_unstable().print_im_a_teapot, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
/// Assert that Zflags on the CLI take precedence over those from config
|
2020-06-30 02:13:50 +00:00
|
|
|
fn unstable_cli_precedence() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2020-06-21 20:19:37 +00:00
|
|
|
"\
|
2020-06-30 02:13:50 +00:00
|
|
|
unstable.print-im-a-teapot = true
|
2020-06-21 20:19:37 +00:00
|
|
|
",
|
|
|
|
);
|
2021-02-24 19:56:14 +00:00
|
|
|
let config = ConfigBuilder::new().nightly_features_allowed(true).build();
|
2020-06-21 20:19:37 +00:00
|
|
|
assert_eq!(config.cli_unstable().print_im_a_teapot, true);
|
|
|
|
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.unstable_flag("print-im-a-teapot=no")
|
|
|
|
.build();
|
|
|
|
assert_eq!(config.cli_unstable().print_im_a_teapot, false);
|
|
|
|
}
|
|
|
|
|
2020-06-30 02:13:50 +00:00
|
|
|
#[cargo_test]
|
2022-05-07 17:07:28 +00:00
|
|
|
/// Assert that attempting to set an unstable flag that doesn't exist via config
|
2020-06-30 02:13:50 +00:00
|
|
|
/// is ignored on stable
|
|
|
|
fn unstable_invalid_flag_ignored_on_stable() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2020-06-30 02:13:50 +00:00
|
|
|
"\
|
|
|
|
unstable.an-invalid-flag = 'yes'
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert!(ConfigBuilder::new().build_err().is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
/// Assert that unstable options can be configured with the `unstable` table in
|
|
|
|
/// cargo config files
|
|
|
|
fn unstable_flags_ignored_on_stable() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2020-06-30 02:13:50 +00:00
|
|
|
"\
|
|
|
|
[unstable]
|
|
|
|
print-im-a-teapot = true
|
|
|
|
",
|
|
|
|
);
|
2021-02-24 19:56:14 +00:00
|
|
|
// Enforce stable channel even when testing on nightly.
|
|
|
|
let config = ConfigBuilder::new().nightly_features_allowed(false).build();
|
2020-06-30 02:13:50 +00:00
|
|
|
assert_eq!(config.cli_unstable().print_im_a_teapot, false);
|
|
|
|
}
|
|
|
|
|
2019-12-01 18:19:02 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn table_merge_failure() {
|
|
|
|
// Config::merge fails to merge entries in two tables.
|
|
|
|
write_config_at(
|
2024-01-26 19:40:46 +00:00
|
|
|
"foo/.cargo/config.toml",
|
2019-12-01 18:19:02 +00:00
|
|
|
"
|
|
|
|
[table]
|
|
|
|
key = ['foo']
|
|
|
|
",
|
|
|
|
);
|
|
|
|
write_config_at(
|
2024-01-26 19:40:46 +00:00
|
|
|
".cargo/config.toml",
|
2019-12-01 18:19:02 +00:00
|
|
|
"
|
|
|
|
[table]
|
|
|
|
key = 'bar'
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
2021-09-13 17:21:06 +00:00
|
|
|
#[allow(dead_code)]
|
2019-12-01 18:19:02 +00:00
|
|
|
struct Table {
|
|
|
|
key: StringList,
|
|
|
|
}
|
|
|
|
let config = ConfigBuilder::new().cwd("foo").build();
|
|
|
|
assert_error(
|
|
|
|
config.get::<Table>("table").unwrap_err(),
|
|
|
|
"\
|
|
|
|
could not load Cargo configuration
|
|
|
|
|
|
|
|
Caused by:
|
2024-01-26 19:40:46 +00:00
|
|
|
failed to merge configuration at `[..]/.cargo/config.toml`
|
2019-12-01 18:19:02 +00:00
|
|
|
|
|
|
|
Caused by:
|
2024-01-26 19:40:46 +00:00
|
|
|
failed to merge key `table` between [..]/foo/.cargo/config.toml and [..]/.cargo/config.toml
|
2019-12-01 18:19:02 +00:00
|
|
|
|
|
|
|
Caused by:
|
2024-01-26 19:40:46 +00:00
|
|
|
failed to merge key `key` between [..]/foo/.cargo/config.toml and [..]/.cargo/config.toml
|
2019-12-01 18:19:02 +00:00
|
|
|
|
|
|
|
Caused by:
|
2024-01-26 19:40:46 +00:00
|
|
|
failed to merge config value from `[..]/.cargo/config.toml` into `[..]/foo/.cargo/config.toml`: \
|
2019-12-01 18:19:02 +00:00
|
|
|
expected array, but found string",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn non_string_in_array() {
|
|
|
|
// Currently only strings are supported.
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml("foo = [1, 2, 3]");
|
2019-12-01 18:19:02 +00:00
|
|
|
let config = new_config();
|
|
|
|
assert_error(
|
|
|
|
config.get::<Vec<i32>>("foo").unwrap_err(),
|
|
|
|
"\
|
|
|
|
could not load Cargo configuration
|
|
|
|
|
|
|
|
Caused by:
|
2024-01-26 19:40:46 +00:00
|
|
|
failed to load TOML configuration from `[..]/.cargo/config.toml`
|
2019-12-01 18:19:02 +00:00
|
|
|
|
|
|
|
Caused by:
|
|
|
|
failed to parse key `foo`
|
|
|
|
|
|
|
|
Caused by:
|
|
|
|
expected string but found integer in list",
|
|
|
|
);
|
|
|
|
}
|
2019-12-24 02:13:50 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn struct_with_opt_inner_struct() {
|
|
|
|
// Struct with a key that is Option of another struct.
|
|
|
|
// Check that can be defined with environment variable.
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Inner {
|
|
|
|
value: Option<i32>,
|
|
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Foo {
|
|
|
|
inner: Option<Inner>,
|
|
|
|
}
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_FOO_INNER_VALUE", "12")
|
|
|
|
.build();
|
|
|
|
let f: Foo = config.get("foo").unwrap();
|
|
|
|
assert_eq!(f.inner.unwrap().value.unwrap(), 12);
|
|
|
|
}
|
|
|
|
|
2020-07-09 18:53:42 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn struct_with_default_inner_struct() {
|
|
|
|
// Struct with serde defaults.
|
|
|
|
// Check that can be defined with environment variable.
|
|
|
|
#[derive(Deserialize, Default)]
|
|
|
|
#[serde(default)]
|
|
|
|
struct Inner {
|
|
|
|
value: i32,
|
|
|
|
}
|
|
|
|
#[derive(Deserialize, Default)]
|
|
|
|
#[serde(default)]
|
|
|
|
struct Foo {
|
|
|
|
inner: Inner,
|
|
|
|
}
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_FOO_INNER_VALUE", "12")
|
|
|
|
.build();
|
|
|
|
let f: Foo = config.get("foo").unwrap();
|
|
|
|
assert_eq!(f.inner.value, 12);
|
|
|
|
}
|
|
|
|
|
2019-12-24 02:13:50 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn overlapping_env_config() {
|
|
|
|
// Issue where one key is a prefix of another.
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
struct Ambig {
|
|
|
|
debug: Option<u32>,
|
|
|
|
debug_assertions: Option<bool>,
|
|
|
|
}
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_AMBIG_DEBUG_ASSERTIONS", "true")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let s: Ambig = config.get("ambig").unwrap();
|
|
|
|
assert_eq!(s.debug_assertions, Some(true));
|
|
|
|
assert_eq!(s.debug, None);
|
|
|
|
|
|
|
|
let config = ConfigBuilder::new().env("CARGO_AMBIG_DEBUG", "0").build();
|
|
|
|
let s: Ambig = config.get("ambig").unwrap();
|
|
|
|
assert_eq!(s.debug_assertions, None);
|
|
|
|
assert_eq!(s.debug, Some(0));
|
|
|
|
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_AMBIG_DEBUG", "1")
|
|
|
|
.env("CARGO_AMBIG_DEBUG_ASSERTIONS", "true")
|
|
|
|
.build();
|
|
|
|
let s: Ambig = config.get("ambig").unwrap();
|
|
|
|
assert_eq!(s.debug_assertions, Some(true));
|
|
|
|
assert_eq!(s.debug, Some(1));
|
|
|
|
}
|
2020-02-16 23:29:59 +00:00
|
|
|
|
2020-07-09 18:53:42 +00:00
|
|
|
#[cargo_test]
|
2020-07-09 19:12:34 +00:00
|
|
|
fn overlapping_env_with_defaults_errors_out() {
|
2020-07-09 18:53:42 +00:00
|
|
|
// Issue where one key is a prefix of another.
|
2020-07-09 19:12:34 +00:00
|
|
|
// This is a limitation of mapping environment variables on to a hierarchy.
|
|
|
|
// Check that we error out when we hit ambiguity in this way, rather than
|
|
|
|
// the more-surprising defaulting through.
|
|
|
|
// If, in the future, we can handle this more correctly, feel free to delete
|
|
|
|
// this test.
|
2020-07-09 18:53:42 +00:00
|
|
|
#[derive(Deserialize, Default)]
|
|
|
|
#[serde(default, rename_all = "kebab-case")]
|
|
|
|
struct Ambig {
|
|
|
|
debug: u32,
|
|
|
|
debug_assertions: bool,
|
|
|
|
}
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_AMBIG_DEBUG_ASSERTIONS", "true")
|
|
|
|
.build();
|
2020-07-09 19:12:34 +00:00
|
|
|
let err = config.get::<Ambig>("ambig").err().unwrap();
|
|
|
|
assert!(format!("{}", err).contains("missing config key `ambig.debug`"));
|
2020-07-09 18:53:42 +00:00
|
|
|
|
|
|
|
let config = ConfigBuilder::new().env("CARGO_AMBIG_DEBUG", "5").build();
|
|
|
|
let s: Ambig = config.get("ambig").unwrap();
|
|
|
|
assert_eq!(s.debug_assertions, bool::default());
|
|
|
|
assert_eq!(s.debug, 5);
|
|
|
|
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_AMBIG_DEBUG", "1")
|
|
|
|
.env("CARGO_AMBIG_DEBUG_ASSERTIONS", "true")
|
|
|
|
.build();
|
|
|
|
let s: Ambig = config.get("ambig").unwrap();
|
|
|
|
assert_eq!(s.debug_assertions, true);
|
|
|
|
assert_eq!(s.debug, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn struct_with_overlapping_inner_struct_and_defaults() {
|
|
|
|
// Struct with serde defaults.
|
|
|
|
// Check that can be defined with environment variable.
|
|
|
|
#[derive(Deserialize, Default)]
|
|
|
|
#[serde(default)]
|
|
|
|
struct Inner {
|
|
|
|
value: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Containing struct with a prefix of inner
|
2020-07-09 19:12:34 +00:00
|
|
|
//
|
|
|
|
// This is a limitation of mapping environment variables on to a hierarchy.
|
|
|
|
// Check that we error out when we hit ambiguity in this way, rather than
|
|
|
|
// the more-surprising defaulting through.
|
|
|
|
// If, in the future, we can handle this more correctly, feel free to delete
|
|
|
|
// this case.
|
2020-07-09 18:53:42 +00:00
|
|
|
#[derive(Deserialize, Default)]
|
|
|
|
#[serde(default)]
|
|
|
|
struct PrefixContainer {
|
|
|
|
inn: bool,
|
|
|
|
inner: Inner,
|
|
|
|
}
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_PREFIXCONTAINER_INNER_VALUE", "12")
|
|
|
|
.build();
|
2020-07-09 19:12:34 +00:00
|
|
|
let err = config
|
|
|
|
.get::<PrefixContainer>("prefixcontainer")
|
|
|
|
.err()
|
|
|
|
.unwrap();
|
|
|
|
assert!(format!("{}", err).contains("missing config key `prefixcontainer.inn`"));
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_PREFIXCONTAINER_INNER_VALUE", "12")
|
|
|
|
.env("CARGO_PREFIXCONTAINER_INN", "true")
|
|
|
|
.build();
|
2020-07-09 18:53:42 +00:00
|
|
|
let f: PrefixContainer = config.get("prefixcontainer").unwrap();
|
|
|
|
assert_eq!(f.inner.value, 12);
|
2020-07-09 19:12:34 +00:00
|
|
|
assert_eq!(f.inn, true);
|
2020-07-09 18:53:42 +00:00
|
|
|
|
|
|
|
// Containing struct where the inner value's field is a prefix of another
|
2020-07-09 19:12:34 +00:00
|
|
|
//
|
|
|
|
// This is a limitation of mapping environment variables on to a hierarchy.
|
|
|
|
// Check that we error out when we hit ambiguity in this way, rather than
|
|
|
|
// the more-surprising defaulting through.
|
|
|
|
// If, in the future, we can handle this more correctly, feel free to delete
|
|
|
|
// this case.
|
2020-07-09 18:53:42 +00:00
|
|
|
#[derive(Deserialize, Default)]
|
|
|
|
#[serde(default)]
|
|
|
|
struct InversePrefixContainer {
|
|
|
|
inner_field: bool,
|
|
|
|
inner: Inner,
|
|
|
|
}
|
|
|
|
let config = ConfigBuilder::new()
|
2020-07-09 19:12:34 +00:00
|
|
|
.env("CARGO_INVERSEPREFIXCONTAINER_INNER_VALUE", "12")
|
2020-07-09 18:53:42 +00:00
|
|
|
.build();
|
|
|
|
let f: InversePrefixContainer = config.get("inverseprefixcontainer").unwrap();
|
|
|
|
assert_eq!(f.inner_field, bool::default());
|
2020-07-09 19:12:34 +00:00
|
|
|
assert_eq!(f.inner.value, 12);
|
2020-07-09 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 23:29:59 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn string_list_tricky_env() {
|
|
|
|
// Make sure StringList handles typed env values.
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_KEY1", "123")
|
|
|
|
.env("CARGO_KEY2", "true")
|
|
|
|
.env("CARGO_KEY3", "1 2")
|
|
|
|
.build();
|
|
|
|
let x = config.get::<StringList>("key1").unwrap();
|
|
|
|
assert_eq!(x.as_slice(), &["123".to_string()]);
|
|
|
|
let x = config.get::<StringList>("key2").unwrap();
|
|
|
|
assert_eq!(x.as_slice(), &["true".to_string()]);
|
|
|
|
let x = config.get::<StringList>("key3").unwrap();
|
|
|
|
assert_eq!(x.as_slice(), &["1".to_string(), "2".to_string()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn string_list_wrong_type() {
|
|
|
|
// What happens if StringList is given then wrong type.
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml("some_list = 123");
|
2020-02-16 23:29:59 +00:00
|
|
|
let config = ConfigBuilder::new().build();
|
|
|
|
assert_error(
|
|
|
|
config.get::<StringList>("some_list").unwrap_err(),
|
|
|
|
"\
|
|
|
|
invalid configuration for key `some_list`
|
2024-01-26 19:40:46 +00:00
|
|
|
expected a string or array of strings, but found a integer for `some_list` in [..]/.cargo/config.toml",
|
2020-02-16 23:29:59 +00:00
|
|
|
);
|
|
|
|
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml("some_list = \"1 2\"");
|
2020-02-16 23:29:59 +00:00
|
|
|
let config = ConfigBuilder::new().build();
|
|
|
|
let x = config.get::<StringList>("some_list").unwrap();
|
|
|
|
assert_eq!(x.as_slice(), &["1".to_string(), "2".to_string()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn string_list_advanced_env() {
|
|
|
|
// StringList with advanced env.
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.unstable_flag("advanced-env")
|
|
|
|
.env("CARGO_KEY1", "[]")
|
|
|
|
.env("CARGO_KEY2", "['1 2', '3']")
|
|
|
|
.env("CARGO_KEY3", "[123]")
|
|
|
|
.build();
|
|
|
|
let x = config.get::<StringList>("key1").unwrap();
|
|
|
|
assert_eq!(x.as_slice(), &[] as &[String]);
|
|
|
|
let x = config.get::<StringList>("key2").unwrap();
|
|
|
|
assert_eq!(x.as_slice(), &["1 2".to_string(), "3".to_string()]);
|
|
|
|
assert_error(
|
|
|
|
config.get::<StringList>("key3").unwrap_err(),
|
|
|
|
"error in environment variable `CARGO_KEY3`: expected string, found integer",
|
|
|
|
);
|
|
|
|
}
|
2020-07-05 06:59:56 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
2021-02-10 00:19:10 +00:00
|
|
|
fn parse_strip_with_string() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2020-07-05 06:59:56 +00:00
|
|
|
"\
|
|
|
|
[profile.release]
|
|
|
|
strip = 'debuginfo'
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
let config = new_config();
|
|
|
|
|
2023-01-19 21:26:28 +00:00
|
|
|
let p: cargo_toml::TomlProfile = config.get("profile.release").unwrap();
|
2020-07-05 06:59:56 +00:00
|
|
|
let strip = p.strip.unwrap();
|
2023-01-19 21:26:28 +00:00
|
|
|
assert_eq!(
|
|
|
|
strip,
|
|
|
|
cargo_toml::StringOrBool::String("debuginfo".to_string())
|
|
|
|
);
|
2020-07-05 07:00:08 +00:00
|
|
|
}
|
2021-01-23 07:18:28 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn cargo_target_empty_cfg() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2021-01-23 07:18:28 +00:00
|
|
|
"\
|
|
|
|
[build]
|
|
|
|
target-dir = ''
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
let config = new_config();
|
|
|
|
|
|
|
|
assert_error(
|
|
|
|
config.target_dir().unwrap_err(),
|
2024-01-26 19:40:46 +00:00
|
|
|
"the target directory is set to an empty string in [..]/.cargo/config.toml",
|
2021-01-23 07:18:28 +00:00
|
|
|
);
|
|
|
|
}
|
2021-02-24 06:56:20 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn cargo_target_empty_env() {
|
|
|
|
let project = project().build();
|
|
|
|
|
2023-02-15 22:23:30 +00:00
|
|
|
project.cargo("check")
|
2021-02-24 06:56:20 +00:00
|
|
|
.env("CARGO_TARGET_DIR", "")
|
2021-02-25 07:48:05 +00:00
|
|
|
.with_stderr("error: the target directory is set to an empty string in the `CARGO_TARGET_DIR` environment variable")
|
2021-02-24 06:56:20 +00:00
|
|
|
.with_status(101)
|
|
|
|
.run()
|
|
|
|
}
|
2021-08-14 21:45:25 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn all_profile_options() {
|
|
|
|
// Check that all profile options can be serialized/deserialized.
|
2023-01-19 21:26:28 +00:00
|
|
|
let base_settings = cargo_toml::TomlProfile {
|
|
|
|
opt_level: Some(cargo_toml::TomlOptLevel("0".to_string())),
|
|
|
|
lto: Some(cargo_toml::StringOrBool::String("thin".to_string())),
|
refactor(toml): Decouple parsing from interning system
To have a separate manifest API (#12801), we can't rely on interning
because it might be used in longer-lifetime applications.
I consulted https://github.com/rosetta-rs/string-rosetta-rs when
deciding on what string type to use for performance.
Originally, I hoped to entirely replacing string interning. For that, I
was looking at `arcstr` as it had a fast equality operator. However,
that is only helpful so long as the two strings we are comparing came
from the original source. Unsure how likely that is to happen (and
daunted by converting all of the `Copy`s into `Clone`s), I decided to
scale back.
Concerned about all of the small allocations when parsing a manifest, I
assumed I'd need a string type with small-string optimizations, like
`hipstr`, `compact_str`, `flexstr`, and `ecow`.
The first three give us more wiggle room and `hipstr` was the fastest of
them, so I went with that.
I then double checked macro benchmarks, and realized `hipstr` made no
difference and switched to `String` to keep things simple / with lower
dependencies.
When doing this, I had created a type alias (`TomlStr`) for the string
type so I could more easily swap it out if needed
(and not have to always write out a lifetime).
With just using `String`, I went ahead and dropped that.
I had problems getting the cargo benchmarks running, so I did a quick
and dirty benchmark that is end-to-end, covering fresh builds, clean
builds, and resolution. I ran these against a fresh clone of cargo's
code base.
```console
$ ../dump/cargo-12801-bench.rs run
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `target/debug/cargo -Zscript -Zmsrv-policy ../dump/cargo-12801-bench.rs run`
warning: `package.edition` is unspecified, defaulting to `2021`
Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `/home/epage/.cargo/target/0a/7f4c1ab500f045/debug/cargo-12801-bench run`
$ hyperfine "../cargo-old check" "../cargo-new check"
Benchmark 1: ../cargo-old check
Time (mean ± σ): 119.3 ms ± 3.2 ms [User: 98.6 ms, System: 20.3 ms]
Range (min … max): 115.6 ms … 124.3 ms 24 runs
Benchmark 2: ../cargo-new check
Time (mean ± σ): 119.4 ms ± 2.4 ms [User: 98.0 ms, System: 21.1 ms]
Range (min … max): 115.7 ms … 123.6 ms 24 runs
Summary
../cargo-old check ran
1.00 ± 0.03 times faster than ../cargo-new check
$ hyperfine --prepare "cargo clean" "../cargo-old check" "../cargo-new check"
Benchmark 1: ../cargo-old check
Time (mean ± σ): 20.249 s ± 0.392 s [User: 157.719 s, System: 22.771 s]
Range (min … max): 19.605 s … 21.123 s 10 runs
Benchmark 2: ../cargo-new check
Time (mean ± σ): 20.123 s ± 0.212 s [User: 156.156 s, System: 22.325 s]
Range (min … max): 19.764 s … 20.420 s 10 runs
Summary
../cargo-new check ran
1.01 ± 0.02 times faster than ../cargo-old check
$ hyperfine --prepare "cargo clean && rm -f Cargo.lock" "../cargo-old check" "../cargo-new check"
Benchmark 1: ../cargo-old check
Time (mean ± σ): 21.105 s ± 0.465 s [User: 156.482 s, System: 22.799 s]
Range (min … max): 20.156 s … 22.010 s 10 runs
Benchmark 2: ../cargo-new check
Time (mean ± σ): 21.358 s ± 0.538 s [User: 156.187 s, System: 22.979 s]
Range (min … max): 20.703 s … 22.462 s 10 runs
Summary
../cargo-old check ran
1.01 ± 0.03 times faster than ../cargo-new check
```
2023-10-18 20:28:02 +00:00
|
|
|
codegen_backend: Some(String::from("example")),
|
2021-08-14 21:45:25 +00:00
|
|
|
codegen_units: Some(123),
|
2023-04-11 04:59:08 +00:00
|
|
|
debug: Some(cargo_toml::TomlDebugInfo::Limited),
|
2021-08-14 21:45:25 +00:00
|
|
|
split_debuginfo: Some("packed".to_string()),
|
|
|
|
debug_assertions: Some(true),
|
|
|
|
rpath: Some(true),
|
|
|
|
panic: Some("abort".to_string()),
|
|
|
|
overflow_checks: Some(true),
|
|
|
|
incremental: Some(true),
|
refactor(toml): Decouple parsing from interning system
To have a separate manifest API (#12801), we can't rely on interning
because it might be used in longer-lifetime applications.
I consulted https://github.com/rosetta-rs/string-rosetta-rs when
deciding on what string type to use for performance.
Originally, I hoped to entirely replacing string interning. For that, I
was looking at `arcstr` as it had a fast equality operator. However,
that is only helpful so long as the two strings we are comparing came
from the original source. Unsure how likely that is to happen (and
daunted by converting all of the `Copy`s into `Clone`s), I decided to
scale back.
Concerned about all of the small allocations when parsing a manifest, I
assumed I'd need a string type with small-string optimizations, like
`hipstr`, `compact_str`, `flexstr`, and `ecow`.
The first three give us more wiggle room and `hipstr` was the fastest of
them, so I went with that.
I then double checked macro benchmarks, and realized `hipstr` made no
difference and switched to `String` to keep things simple / with lower
dependencies.
When doing this, I had created a type alias (`TomlStr`) for the string
type so I could more easily swap it out if needed
(and not have to always write out a lifetime).
With just using `String`, I went ahead and dropped that.
I had problems getting the cargo benchmarks running, so I did a quick
and dirty benchmark that is end-to-end, covering fresh builds, clean
builds, and resolution. I ran these against a fresh clone of cargo's
code base.
```console
$ ../dump/cargo-12801-bench.rs run
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `target/debug/cargo -Zscript -Zmsrv-policy ../dump/cargo-12801-bench.rs run`
warning: `package.edition` is unspecified, defaulting to `2021`
Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `/home/epage/.cargo/target/0a/7f4c1ab500f045/debug/cargo-12801-bench run`
$ hyperfine "../cargo-old check" "../cargo-new check"
Benchmark 1: ../cargo-old check
Time (mean ± σ): 119.3 ms ± 3.2 ms [User: 98.6 ms, System: 20.3 ms]
Range (min … max): 115.6 ms … 124.3 ms 24 runs
Benchmark 2: ../cargo-new check
Time (mean ± σ): 119.4 ms ± 2.4 ms [User: 98.0 ms, System: 21.1 ms]
Range (min … max): 115.7 ms … 123.6 ms 24 runs
Summary
../cargo-old check ran
1.00 ± 0.03 times faster than ../cargo-new check
$ hyperfine --prepare "cargo clean" "../cargo-old check" "../cargo-new check"
Benchmark 1: ../cargo-old check
Time (mean ± σ): 20.249 s ± 0.392 s [User: 157.719 s, System: 22.771 s]
Range (min … max): 19.605 s … 21.123 s 10 runs
Benchmark 2: ../cargo-new check
Time (mean ± σ): 20.123 s ± 0.212 s [User: 156.156 s, System: 22.325 s]
Range (min … max): 19.764 s … 20.420 s 10 runs
Summary
../cargo-new check ran
1.01 ± 0.02 times faster than ../cargo-old check
$ hyperfine --prepare "cargo clean && rm -f Cargo.lock" "../cargo-old check" "../cargo-new check"
Benchmark 1: ../cargo-old check
Time (mean ± σ): 21.105 s ± 0.465 s [User: 156.482 s, System: 22.799 s]
Range (min … max): 20.156 s … 22.010 s 10 runs
Benchmark 2: ../cargo-new check
Time (mean ± σ): 21.358 s ± 0.538 s [User: 156.187 s, System: 22.979 s]
Range (min … max): 20.703 s … 22.462 s 10 runs
Summary
../cargo-old check ran
1.01 ± 0.03 times faster than ../cargo-new check
```
2023-10-18 20:28:02 +00:00
|
|
|
dir_name: Some(String::from("dir_name")),
|
|
|
|
inherits: Some(String::from("debug")),
|
2023-01-19 21:26:28 +00:00
|
|
|
strip: Some(cargo_toml::StringOrBool::String("symbols".to_string())),
|
2021-08-14 21:45:25 +00:00
|
|
|
package: None,
|
|
|
|
build_override: None,
|
2021-12-19 03:31:32 +00:00
|
|
|
rustflags: None,
|
2023-09-01 16:13:18 +00:00
|
|
|
trim_paths: None,
|
2021-08-14 21:45:25 +00:00
|
|
|
};
|
|
|
|
let mut overrides = BTreeMap::new();
|
2023-01-19 21:26:28 +00:00
|
|
|
let key = cargo_toml::ProfilePackageSpec::Spec(PackageIdSpec::parse("foo").unwrap());
|
2021-08-14 21:45:25 +00:00
|
|
|
overrides.insert(key, base_settings.clone());
|
2023-01-19 21:26:28 +00:00
|
|
|
let profile = cargo_toml::TomlProfile {
|
2021-08-14 21:45:25 +00:00
|
|
|
build_override: Some(Box::new(base_settings.clone())),
|
|
|
|
package: Some(overrides),
|
2021-11-21 03:40:31 +00:00
|
|
|
..base_settings
|
2021-08-14 21:45:25 +00:00
|
|
|
};
|
2023-01-19 21:26:28 +00:00
|
|
|
let profile_toml = toml::to_string(&profile).unwrap();
|
|
|
|
let roundtrip: cargo_toml::TomlProfile = toml::from_str(&profile_toml).unwrap();
|
|
|
|
let roundtrip_toml = toml::to_string(&roundtrip).unwrap();
|
2021-08-14 21:45:25 +00:00
|
|
|
compare::assert_match_exact(&profile_toml, &roundtrip_toml);
|
|
|
|
}
|
2022-12-15 03:01:40 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn value_in_array() {
|
|
|
|
// Value<String> in an array should work
|
|
|
|
let root_path = paths::root().join(".cargo/config.toml");
|
|
|
|
write_config_at(
|
|
|
|
&root_path,
|
|
|
|
"\
|
|
|
|
[net.ssh]
|
|
|
|
known-hosts = [
|
|
|
|
\"example.com ...\",
|
|
|
|
\"example.net ...\",
|
|
|
|
]
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
let foo_path = paths::root().join("foo/.cargo/config.toml");
|
|
|
|
write_config_at(
|
|
|
|
&foo_path,
|
|
|
|
"\
|
|
|
|
[net.ssh]
|
|
|
|
known-hosts = [
|
|
|
|
\"example.org ...\",
|
|
|
|
]
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.cwd("foo")
|
|
|
|
// environment variables don't actually work for known-hosts due to
|
|
|
|
// space splitting, but this is included here just to validate that
|
|
|
|
// they work (particularly if other Vec<Value> config vars are added
|
|
|
|
// in the future).
|
|
|
|
.env("CARGO_NET_SSH_KNOWN_HOSTS", "env-example")
|
|
|
|
.build();
|
|
|
|
let net_config = config.net_config().unwrap();
|
|
|
|
let kh = net_config
|
|
|
|
.ssh
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.known_hosts
|
|
|
|
.as_ref()
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(kh.len(), 4);
|
2023-08-16 04:08:39 +00:00
|
|
|
assert_eq!(kh[0].val, "example.com ...");
|
|
|
|
assert_eq!(kh[0].definition, Definition::Path(root_path.clone()));
|
|
|
|
assert_eq!(kh[1].val, "example.net ...");
|
2022-12-15 03:01:40 +00:00
|
|
|
assert_eq!(kh[1].definition, Definition::Path(root_path.clone()));
|
2023-08-16 04:08:39 +00:00
|
|
|
assert_eq!(kh[2].val, "example.org ...");
|
|
|
|
assert_eq!(kh[2].definition, Definition::Path(foo_path.clone()));
|
2022-12-15 03:01:40 +00:00
|
|
|
assert_eq!(kh[3].val, "env-example");
|
|
|
|
assert_eq!(
|
|
|
|
kh[3].definition,
|
|
|
|
Definition::Environment("CARGO_NET_SSH_KNOWN_HOSTS".to_string())
|
|
|
|
);
|
|
|
|
}
|
2023-04-20 00:31:22 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn debuginfo_parsing() {
|
|
|
|
let config = ConfigBuilder::new().build();
|
|
|
|
let p: cargo_toml::TomlProfile = config.get("profile.dev").unwrap();
|
|
|
|
assert_eq!(p.debug, None);
|
|
|
|
|
|
|
|
let env_test_cases = [
|
|
|
|
(TomlDebugInfo::None, ["false", "0", "none"].as_slice()),
|
|
|
|
(TomlDebugInfo::LineDirectivesOnly, &["line-directives-only"]),
|
|
|
|
(TomlDebugInfo::LineTablesOnly, &["line-tables-only"]),
|
|
|
|
(TomlDebugInfo::Limited, &["1", "limited"]),
|
|
|
|
(TomlDebugInfo::Full, &["true", "2", "full"]),
|
|
|
|
];
|
|
|
|
for (expected, config_strs) in env_test_cases {
|
|
|
|
for &val in config_strs {
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_PROFILE_DEV_DEBUG", val)
|
|
|
|
.build();
|
|
|
|
let debug: TomlDebugInfo = config.get("profile.dev.debug").unwrap();
|
|
|
|
assert_eq!(debug, expected, "failed to parse {val}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let toml_test_cases = [
|
|
|
|
(TomlDebugInfo::None, ["false", "0", "\"none\""].as_slice()),
|
|
|
|
(
|
|
|
|
TomlDebugInfo::LineDirectivesOnly,
|
|
|
|
&["\"line-directives-only\""],
|
|
|
|
),
|
|
|
|
(TomlDebugInfo::LineTablesOnly, &["\"line-tables-only\""]),
|
|
|
|
(TomlDebugInfo::Limited, &["1", "\"limited\""]),
|
|
|
|
(TomlDebugInfo::Full, &["true", "2", "\"full\""]),
|
|
|
|
];
|
|
|
|
for (expected, config_strs) in toml_test_cases {
|
|
|
|
for &val in config_strs {
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.config_arg(format!("profile.dev.debug={val}"))
|
|
|
|
.build();
|
|
|
|
let debug: TomlDebugInfo = config.get("profile.dev.debug").unwrap();
|
|
|
|
assert_eq!(debug, expected, "failed to parse {val}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let toml_err_cases = ["\"\"", "\"unrecognized\"", "3"];
|
|
|
|
for err_val in toml_err_cases {
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.config_arg(format!("profile.dev.debug={err_val}"))
|
|
|
|
.build();
|
|
|
|
let err = config
|
|
|
|
.get::<TomlDebugInfo>("profile.dev.debug")
|
|
|
|
.unwrap_err();
|
|
|
|
assert!(err
|
|
|
|
.to_string()
|
|
|
|
.ends_with("could not load config key `profile.dev.debug`"));
|
|
|
|
}
|
|
|
|
}
|
2023-06-02 14:10:10 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn build_jobs_missing() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2023-06-02 14:10:10 +00:00
|
|
|
"\
|
|
|
|
[build]
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
let config = new_config();
|
|
|
|
|
|
|
|
assert!(config
|
|
|
|
.get::<Option<JobsConfig>>("build.jobs")
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn build_jobs_default() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2023-06-02 14:10:10 +00:00
|
|
|
"\
|
|
|
|
[build]
|
|
|
|
jobs = \"default\"
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
let config = new_config();
|
|
|
|
|
|
|
|
let a = config
|
|
|
|
.get::<Option<JobsConfig>>("build.jobs")
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
match a {
|
|
|
|
JobsConfig::String(v) => assert_eq!(&v, "default"),
|
|
|
|
JobsConfig::Integer(_) => panic!("Did not except an integer."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn build_jobs_integer() {
|
2024-01-26 19:40:46 +00:00
|
|
|
write_config_toml(
|
2023-06-02 14:10:10 +00:00
|
|
|
"\
|
|
|
|
[build]
|
|
|
|
jobs = 2
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
let config = new_config();
|
|
|
|
|
|
|
|
let a = config
|
|
|
|
.get::<Option<JobsConfig>>("build.jobs")
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
match a {
|
|
|
|
JobsConfig::String(_) => panic!("Did not except an integer."),
|
|
|
|
JobsConfig::Integer(v) => assert_eq!(v, 2),
|
|
|
|
}
|
|
|
|
}
|
2023-09-01 16:14:25 +00:00
|
|
|
|
|
|
|
#[cargo_test]
|
|
|
|
fn trim_paths_parsing() {
|
|
|
|
let config = ConfigBuilder::new().build();
|
|
|
|
let p: cargo_toml::TomlProfile = config.get("profile.dev").unwrap();
|
|
|
|
assert_eq!(p.trim_paths, None);
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
(TomlTrimPathsValue::Diagnostics.into(), "diagnostics"),
|
|
|
|
(TomlTrimPathsValue::Macro.into(), "macro"),
|
|
|
|
(TomlTrimPathsValue::Object.into(), "object"),
|
|
|
|
];
|
|
|
|
for (expected, val) in test_cases {
|
|
|
|
// env
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_PROFILE_DEV_TRIM_PATHS", val)
|
|
|
|
.build();
|
|
|
|
let trim_paths: TomlTrimPaths = config.get("profile.dev.trim-paths").unwrap();
|
|
|
|
assert_eq!(trim_paths, expected, "failed to parse {val}");
|
|
|
|
|
|
|
|
// config.toml
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.config_arg(format!("profile.dev.trim-paths='{val}'"))
|
|
|
|
.build();
|
|
|
|
let trim_paths: TomlTrimPaths = config.get("profile.dev.trim-paths").unwrap();
|
|
|
|
assert_eq!(trim_paths, expected, "failed to parse {val}");
|
|
|
|
}
|
|
|
|
|
|
|
|
let test_cases = [(TomlTrimPaths::none(), false), (TomlTrimPaths::All, true)];
|
|
|
|
|
|
|
|
for (expected, val) in test_cases {
|
|
|
|
// env
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.env("CARGO_PROFILE_DEV_TRIM_PATHS", format!("{val}"))
|
|
|
|
.build();
|
|
|
|
let trim_paths: TomlTrimPaths = config.get("profile.dev.trim-paths").unwrap();
|
|
|
|
assert_eq!(trim_paths, expected, "failed to parse {val}");
|
|
|
|
|
|
|
|
// config.toml
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.config_arg(format!("profile.dev.trim-paths={val}"))
|
|
|
|
.build();
|
|
|
|
let trim_paths: TomlTrimPaths = config.get("profile.dev.trim-paths").unwrap();
|
|
|
|
assert_eq!(trim_paths, expected, "failed to parse {val}");
|
|
|
|
}
|
|
|
|
|
|
|
|
let expected = vec![
|
|
|
|
TomlTrimPathsValue::Diagnostics,
|
|
|
|
TomlTrimPathsValue::Macro,
|
|
|
|
TomlTrimPathsValue::Object,
|
|
|
|
]
|
|
|
|
.into();
|
|
|
|
let val = r#"["diagnostics", "macro", "object"]"#;
|
|
|
|
// config.toml
|
|
|
|
let config = ConfigBuilder::new()
|
|
|
|
.config_arg(format!("profile.dev.trim-paths={val}"))
|
|
|
|
.build();
|
|
|
|
let trim_paths: TomlTrimPaths = config.get("profile.dev.trim-paths").unwrap();
|
|
|
|
assert_eq!(trim_paths, expected, "failed to parse {val}");
|
|
|
|
}
|